3 * Class used for executing shell commands
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Shell
;
25 use MediaWiki\MediaWikiServices
;
28 * Executes shell commands
32 * Use call chaining with this class for expressiveness:
33 * $result = Shell::command( 'some command' )
35 * ->environment( [ 'ENVIRONMENT_VARIABLE' => 'VALUE' ] )
36 * ->limits( [ 'time' => 300 ] )
39 * ... = $result->getExitCode();
40 * ... = $result->getStdout();
41 * ... = $result->getStderr();
46 * Apply a default set of restrictions for improved
47 * security out of the box.
49 * Equal to NO_ROOT | SECCOMP | PRIVATE_DEV | NO_LOCALSETTINGS
51 * @note This value will change over time to provide increased security
52 * by default, and is not guaranteed to be backwards-compatible.
55 const RESTRICT_DEFAULT
= 39;
58 * Disallow any root access. Any setuid binaries
59 * will be run without elevated access.
66 * Use seccomp to block dangerous syscalls
67 * @see <https://en.wikipedia.org/wiki/seccomp>
74 * Create a private /dev
78 const PRIVATE_DEV
= 4;
81 * Restrict the request to have no
89 * Deny execve syscall with seccomp
90 * @see <https://en.wikipedia.org/wiki/exec_(system_call)>
97 * Deny access to LocalSettings.php (MW_CONFIG_FILE)
101 const NO_LOCALSETTINGS
= 32;
104 * Returns a new instance of Command class
106 * @param string|string[] $command String or array of strings representing the command to
107 * be executed, each value will be escaped.
108 * Example: [ 'convert', '-font', 'font name' ] would produce "'convert' '-font' 'font name'"
111 public static function command( $command ) {
112 $args = func_get_args();
113 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
114 // If only one argument has been passed, and that argument is an array,
115 // treat it as a list of arguments
116 $args = reset( $args );
118 $command = MediaWikiServices
::getInstance()
119 ->getShellCommandFactory()
122 return $command->params( $args );
126 * Check if this class is effectively disabled via php.ini config
130 public static function isDisabled() {
131 static $disabled = null;
133 if ( is_null( $disabled ) ) {
134 if ( !function_exists( 'proc_open' ) ) {
135 wfDebug( "proc_open() is disabled\n" );
146 * Version of escapeshellarg() that works better on Windows.
148 * Originally, this fixed the incorrect use of single quotes on Windows
149 * (https://bugs.php.net/bug.php?id=26285) and the locale problems on Linux in
150 * PHP 5.2.6+ (bug backported to earlier distro releases of PHP).
152 * @param string $args,... strings to escape and glue together, or a single array of
153 * strings parameter. Null values are ignored.
156 public static function escape( /* ... */ ) {
157 $args = func_get_args();
158 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
159 // If only one argument has been passed, and that argument is an array,
160 // treat it as a list of arguments
161 $args = reset( $args );
166 foreach ( $args as $arg ) {
167 if ( $arg === null ) {
176 if ( wfIsWindows() ) {
177 // Escaping for an MSVC-style command line parser and CMD.EXE
179 // * https://web.archive.org/web/20020708081031/http://mailman.lyra.org/pipermail/scite-interest/2002-March/000436.html
180 // * https://technet.microsoft.com/en-us/library/cc723564.aspx
183 // Double the backslashes before any double quotes. Escape the double quotes.
184 $tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE
);
187 foreach ( $tokens as $token ) {
188 if ( $iteration %
2 == 1 ) {
189 // Delimiter, a double quote preceded by zero or more slashes
190 $arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
191 } elseif ( $iteration %
4 == 2 ) {
192 // ^ in $token will be outside quotes, need to be escaped
193 $arg .= str_replace( '^', '^^', $token );
194 } else { // $iteration % 4 == 0
195 // ^ in $token will appear inside double quotes, so leave as is
200 // Double the backslashes before the end of the string, because
201 // we will soon add a quote
203 if ( preg_match( '/^(.*?)(\\\\+)$/', $arg, $m ) ) {
204 $arg = $m[1] . str_replace( '\\', '\\\\', $m[2] );
207 // Add surrounding quotes
208 $retVal .= '"' . $arg . '"';
210 $retVal .= escapeshellarg( $arg );