| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Shell; |
| 22 |
|
| 23 |
use Exception; |
| 24 |
use MediaWiki\ShellDisabledError; |
| 25 |
use Profiler; |
| 26 |
use Psr\Log\LoggerInterface; |
| 27 |
use Psr\Log\NullLogger; |
| 28 |
use Shellbox\Command\UnboxedCommand; |
| 29 |
use Shellbox\Command\UnboxedExecutor; |
| 30 |
use Shellbox\Command\UnboxedResult; |
| 31 |
use Stringable; |
| 32 |
use Wikimedia\ScopedCallback; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class Command extends UnboxedCommand implements Stringable { |
| 40 |
private bool $everExecuted = false; |
| 41 |
|
| 42 |
|
| 43 |
private $method; |
| 44 |
|
| 45 |
protected LoggerInterface $logger; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function __construct( UnboxedExecutor $executor ) { |
| 54 |
if ( Shell::isDisabled() ) { |
| 55 |
throw new ShellDisabledError(); |
| 56 |
} |
| 57 |
parent::__construct( $executor ); |
| 58 |
$this->setLogger( new NullLogger() ); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function __destruct() { |
| 65 |
if ( !$this->everExecuted ) { |
| 66 |
$context = [ 'command' => $this->getCommandString() ]; |
| 67 |
$message = __CLASS__ . " was instantiated, but execute() was never called."; |
| 68 |
if ( $this->method ) { |
| 69 |
$message .= ' Calling method: {method}.'; |
| 70 |
$context['method'] = $this->method; |
| 71 |
} |
| 72 |
$message .= ' Command: {command}'; |
| 73 |
$this->logger->warning( $message, $context ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function setLogger( LoggerInterface $logger ) { |
| 81 |
$this->logger = $logger; |
| 82 |
if ( $this->executor ) { |
| 83 |
$this->executor->setLogger( $logger ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function limits( array $limits ): Command { |
| 95 |
if ( !isset( $limits['walltime'] ) && isset( $limits['time'] ) ) { |
| 96 |
|
| 97 |
|
| 98 |
$limits['walltime'] = $limits['time']; |
| 99 |
} |
| 100 |
if ( isset( $limits['filesize'] ) ) { |
| 101 |
$this->fileSizeLimit( $limits['filesize'] * 1024 ); |
| 102 |
} |
| 103 |
if ( isset( $limits['memory'] ) ) { |
| 104 |
$this->memoryLimit( $limits['memory'] * 1024 ); |
| 105 |
} |
| 106 |
if ( isset( $limits['time'] ) ) { |
| 107 |
$this->cpuTimeLimit( $limits['time'] ); |
| 108 |
} |
| 109 |
if ( isset( $limits['walltime'] ) ) { |
| 110 |
$this->wallTimeLimit( $limits['walltime'] ); |
| 111 |
} |
| 112 |
|
| 113 |
return $this; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function profileMethod( string $method ): Command { |
| 123 |
$this->method = $method; |
| 124 |
|
| 125 |
return $this; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public function input( string $inputString ): Command { |
| 137 |
return $this->stdin( $inputString ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public function restrict( int $restrictions ): Command { |
| 165 |
$this->privateUserNamespace( (bool)( $restrictions & Shell::NO_ROOT ) ); |
| 166 |
$this->firejailDefaultSeccomp( (bool)( $restrictions & Shell::SECCOMP ) ); |
| 167 |
$this->noNewPrivs( (bool)( $restrictions & Shell::SECCOMP ) ); |
| 168 |
$this->privateDev( (bool)( $restrictions & Shell::PRIVATE_DEV ) ); |
| 169 |
$this->disableNetwork( (bool)( $restrictions & Shell::NO_NETWORK ) ); |
| 170 |
if ( $restrictions & Shell::NO_EXECVE ) { |
| 171 |
$this->disabledSyscalls( [ 'execve' ] ); |
| 172 |
} else { |
| 173 |
$this->disabledSyscalls( [] ); |
| 174 |
} |
| 175 |
if ( $restrictions & Shell::NO_LOCALSETTINGS ) { |
| 176 |
$this->disallowedPaths( [ realpath( MW_CONFIG_FILE ) ] ); |
| 177 |
} else { |
| 178 |
$this->disallowedPaths( [] ); |
| 179 |
} |
| 180 |
if ( $restrictions === 0 ) { |
| 181 |
$this->disableSandbox(); |
| 182 |
} |
| 183 |
|
| 184 |
return $this; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public function whitelistPaths( array $paths ): Command { |
| 199 |
wfDeprecated( __METHOD__, '1.36' ); |
| 200 |
$this->allowedPaths( array_merge( $this->getAllowedPaths(), $paths ) ); |
| 201 |
return $this; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public function execute(): UnboxedResult { |
| 212 |
$this->everExecuted = true; |
| 213 |
$profileMethod = $this->method ?: wfGetCaller(); |
| 214 |
$scoped = Profiler::instance()->scopedProfileIn( __FUNCTION__ . '-' . $profileMethod ); |
| 215 |
$result = parent::execute(); |
| 216 |
ScopedCallback::consume( $scoped ); |
| 217 |
return $result; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public function __toString(): string { |
| 228 |
return '#Command: ' . $this->getCommandString(); |
| 229 |
} |
| 230 |
} |