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' )
34 * ->environment( [ 'ENVIRONMENT_VARIABLE' => 'VALUE' ] )
35 * ->limits( [ 'time' => 300 ] )
38 * ... = $result->getExitCode();
39 * ... = $result->getStdout();
40 * ... = $result->getStderr();
45 * Apply a default set of restrictions for improved
46 * security out of the box.
48 * Equal to NO_ROOT | SECCOMP | PRIVATE_DEV
50 * @note This value will change over time to provide increased security
51 * by default, and is not guaranteed to be backwards-compatible.
54 const RESTRICT_DEFAULT
= 7;
57 * Disallow any root access. Any setuid binaries
58 * will be run without elevated access.
65 * Use seccomp to block dangerous syscalls
66 * @see <https://en.wikipedia.org/wiki/seccomp>
73 * Create a private /dev
77 const PRIVATE_DEV
= 4;
80 * Restrict the request to have no
88 * Deny execve syscall with seccomp
89 * @see <https://en.wikipedia.org/wiki/exec_(system_call)>
96 * Returns a new instance of Command class
98 * @param string|string[] $command String or array of strings representing the command to
99 * be executed, each value will be escaped.
100 * Example: [ 'convert', '-font', 'font name' ] would produce "'convert' '-font' 'font name'"
103 public static function command( $command ) {
104 $args = func_get_args();
105 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
106 // If only one argument has been passed, and that argument is an array,
107 // treat it as a list of arguments
108 $args = reset( $args );
110 $command = MediaWikiServices
::getInstance()
111 ->getShellCommandFactory()
114 return $command->params( $args );
118 * Check if this class is effectively disabled via php.ini config
122 public static function isDisabled() {
123 static $disabled = null;
125 if ( is_null( $disabled ) ) {
126 if ( !function_exists( 'proc_open' ) ) {
127 wfDebug( "proc_open() is disabled\n" );
138 * Version of escapeshellarg() that works better on Windows.
140 * Originally, this fixed the incorrect use of single quotes on Windows
141 * (https://bugs.php.net/bug.php?id=26285) and the locale problems on Linux in
142 * PHP 5.2.6+ (bug backported to earlier distro releases of PHP).
144 * @param string $args,... strings to escape and glue together, or a single array of
148 public static function escape( /* ... */ ) {
149 $args = func_get_args();
150 if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
151 // If only one argument has been passed, and that argument is an array,
152 // treat it as a list of arguments
153 $args = reset( $args );
158 foreach ( $args as $arg ) {
165 if ( wfIsWindows() ) {
166 // Escaping for an MSVC-style command line parser and CMD.EXE
168 // * https://web.archive.org/web/20020708081031/http://mailman.lyra.org/pipermail/scite-interest/2002-March/000436.html
169 // * https://technet.microsoft.com/en-us/library/cc723564.aspx
172 // Double the backslashes before any double quotes. Escape the double quotes.
173 $tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE
);
176 foreach ( $tokens as $token ) {
177 if ( $iteration %
2 == 1 ) {
178 // Delimiter, a double quote preceded by zero or more slashes
179 $arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
180 } elseif ( $iteration %
4 == 2 ) {
181 // ^ in $token will be outside quotes, need to be escaped
182 $arg .= str_replace( '^', '^^', $token );
183 } else { // $iteration % 4 == 0
184 // ^ in $token will appear inside double quotes, so leave as is
189 // Double the backslashes before the end of the string, because
190 // we will soon add a quote
192 if ( preg_match( '/^(.*?)(\\\\+)$/', $arg, $m ) ) {
193 $arg = $m[1] . str_replace( '\\', '\\\\', $m[2] );
196 // Add surrounding quotes
197 $retVal .= '"' . $arg . '"';
199 $retVal .= escapeshellarg( $arg );