24use Psr\Log\LoggerAwareTrait;
25use Psr\Log\NullLogger;
26use Shellbox\Command\BoxedCommand;
27use Shellbox\Command\RemoteBoxedExecutor;
45 private $doLogStderr =
false;
50 private $restrictionMethod;
61 private $shellboxClientFactory;
70 array $limits, $cgroup, $restrictionMethod
72 $this->shellboxClientFactory = $shellboxClientFactory;
73 $this->limits = $limits;
74 $this->cgroup = $cgroup;
75 if ( $restrictionMethod ===
'autodetect' ) {
78 $this->restrictionMethod =
'firejail';
80 $this->restrictionMethod =
false;
83 $this->restrictionMethod = $restrictionMethod;
85 $this->setLogger(
new NullLogger() );
92 if ( $this->firejail ===
null ) {
93 $this->firejail = ExecutableFinder::findInDefaultPaths(
'firejail' );
96 return $this->firejail;
106 $this->doLogStderr = $yesno;
116 private function getLocalShellboxOptions() {
119 'useBashWrapper' => file_exists(
'/bin/bash' ),
120 'cgroup' => $this->cgroup
122 if ( $this->restrictionMethod ===
'firejail' ) {
123 $firejailPath = $this->findFirejail();
124 if ( !$firejailPath ) {
125 throw new \RuntimeException(
'firejail is enabled, but cannot be found' );
127 $options[
'useFirejail'] =
true;
128 $options[
'firejailPath'] = $firejailPath;
129 $options[
'firejailProfile'] = __DIR__ .
'/firejail.profile';
141 if ( $this->restrictionMethod ===
'firejail' ) {
142 if ( $this->useAllUsers ===
null ) {
146 $realIP = realpath(
$IP );
147 $currentUser = posix_getpwuid( posix_geteuid() );
148 $this->useAllUsers = str_starts_with( $realIP,
'/home/' )
149 && !str_starts_with( $realIP, $currentUser[
'dir'] );
150 if ( $this->useAllUsers ) {
151 $this->logger->warning(
'firejail: MediaWiki is located ' .
152 'in a home directory that does not belong to the ' .
153 'current user, so allowing access to all home ' .
154 'directories (--allusers)' );
157 $allUsers = $this->useAllUsers;
159 $executor = Shellbox::createUnboxedExecutor(
160 $this->getLocalShellboxOptions(), $this->logger );
162 $command =
new Command( $executor );
163 $command->setLogger( $this->logger );
165 $command->allowPath(
'/home' );
168 ->limits( $this->limits )
169 ->logStderr( $this->doLogStderr );
182 public function createBoxed( ?
string $service =
null, $wallTimeLimit =
null ): BoxedCommand {
183 $wallTimeLimit ??= $this->limits[
'walltime'];
184 if ( $this->shellboxClientFactory->isEnabled( $service ) ) {
185 $client = $this->shellboxClientFactory->getClient( [
186 'timeout' => $wallTimeLimit + 1,
187 'service' => $service,
189 $executor =
new RemoteBoxedExecutor( $client );
190 $executor->setLogger( $this->logger );
192 $executor = Shellbox::createBoxedExecutor(
193 $this->getLocalShellboxOptions(),
196 return $executor->createCommand()
197 ->cpuTimeLimit( $this->limits[
'time'] )
198 ->wallTimeLimit( $wallTimeLimit )
199 ->memoryLimit( $this->limits[
'memory'] * 1024 )
200 ->fileSizeLimit( $this->limits[
'filesize'] * 1024 )
201 ->logStderr( $this->doLogStderr );
wfTempDir()
Tries to get the system directory for temporary files.
if(!defined( 'MEDIAWIKI')) if(ini_get('mbstring.func_overload')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Utility class to find executables in likely places.