Debian Codegraph @

vendor/wikimedia/shellbox/src/Command/Command.php

raw
1 <?php
2
3 namespace Shellbox\Command;
4
5 use Shellbox\Shellbox;
6 use Shellbox\ShellParser\ShellParser;
7 use Shellbox\ShellParser\ShellSyntaxError;
8 use Shellbox\ShellParser\SyntaxInfo;
9
10 /**
11 * The abstract base class for commands.
12 */
13 abstract class Command {
14 /** @var string */
15 private $command = '';
16 /** @var int|float|null */
17 private $cpuTimeLimit;
18 /** @var int|float|null */
19 private $wallTimeLimit;
20 /** @var int|float|null */
21 private $memoryLimit;
22 /** @var int|float|null */
23 private $fileSizeLimit;
24 /** @var string[] */
25 private $environment = [];
26 /** @var string */
27 private $stdin = '';
28 /** @var bool */
29 private $passStdin;
30 /** @var bool */
31 private $includeStderr;
32 /** @var bool */
33 private $logStderr = false;
34 /** @var bool */
35 private $forwardStderr = false;
36 /** @var bool */
37 private $useLogPipe = false;
38 /** @var string|null */
39 private $workingDirectory;
40 /** @var array */
41 private $procOpenOptions = [];
42 /** @var bool */
43 private $disableNetwork = false;
44 /** @var string[] */
45 private $disabledSyscalls = [];
46 /** @var bool */
47 private $firejailDefaultSeccomp = false;
48 /** @var bool */
49 private $noNewPrivs = false;
50 /** @var bool */
51 private $privateUserNamespace = false;
52 /** @var bool */
53 private $privateDev = false;
54 /** @var string[] */
55 private $allowedPaths = [];
56 /** @var string[] */
57 private $disallowedPaths = [];
58 /** @var bool */
59 private $disableSandbox = false;
60 /** @var SyntaxInfo|null */
61 private $syntaxInfo;
62
63 /**
64 * Adds parameters to the command. All parameters are escaped via Shellbox::escape().
65 * Null values are ignored.
66 *
67 * @param mixed|mixed[] ...$args
68 * @return $this
69 */
70 public function params( ...$args ) {
71 if ( count( $args ) === 1 && is_array( $args[0] ) ) {
72 $args = $args[0];
73 }
74 $command = Shellbox::escape( $args );
75 if ( $this->command === '' ) {
76 $this->command = $command;
77 } else {
78 $this->command .= ' ' . $command;
79 }
80 $this->syntaxInfo = null;
81 return $this;
82 }
83
84 /**
85 * Adds unsafe parameters to the command. These parameters are NOT sanitized in any way.
86 * Null values are ignored.
87 *
88 * @param string|string[]|null ...$args
89 * @return $this
90 */
91 public function unsafeParams( ...$args ) {
92 if ( count( $args ) === 1 && is_array( $args[0] ) ) {
93 $args = $args[0];
94 }
95 foreach ( $args as $arg ) {
96 if ( $arg !== null ) {
97 if ( $this->command !== '' ) {
98 $this->command .= ' ';
99 }
100 $this->command .= $arg;
101 }
102 }
103 $this->syntaxInfo = null;
104 return $this;
105 }
106
107 /**
108 * Replace the whole command with the given set of arguments.
109 *
110 * @param string|string[] ...$args
111 * @return $this
112 */
113 public function replaceParams( ...$args ) {
114 $this->command = '';
115 $this->params( ...$args );
116 return $this;
117 }
118
119 /**
120 * Replace the whole command string with something else. The command is not
121 * escaped or sanitized.
122 *
123 * @param string $command
124 * @return $this
125 */
126 public function unsafeCommand( string $command ) {
127 $this->command = $command;
128 $this->syntaxInfo = null;
129 return $this;
130 }
131
132 /**
133 * Set the CPU time limit, that is, the amount of time the process spends
134 * in the running state.
135 *
136 * Whether this limit can be respected depends on the executor
137 * configuration.
138 *
139 * @param int|float $limit The limit in seconds
140 * @return $this
141 */
142 public function cpuTimeLimit( $limit ) {
143 $this->cpuTimeLimit = $limit;
144 return $this;
145 }
146
147 /**
148 * Set the wall clock time limit, that is, the amount of real time the
149 * process may run for.
150 *
151 * Whether this limit can be respected depends on the executor
152 * configuration.
153 *
154 * @param int|float $limit The limit in seconds
155 * @return $this
156 */
157 public function wallTimeLimit( $limit ) {
158 $this->wallTimeLimit = $limit;
159 return $this;
160 }
161
162 /**
163 * Set the memory limit in bytes.
164 *
165 * Whether this limit can be respected depends on the executor
166 * configuration.
167 *
168 * @param int|float $limit The limit in bytes
169 * @return $this
170 */
171 public function memoryLimit( $limit ) {
172 $this->memoryLimit = $limit;
173 return $this;
174 }
175
176 /**
177 * Set the maximum file size that the command may create
178 *
179 * Whether this limit can be respected depends on the executor
180 * configuration.
181 *
182 * @param int|float $limit The limit in bytes
183 * @return $this
184 */
185 public function fileSizeLimit( $limit ) {
186 $this->fileSizeLimit = $limit;
187 return $this;
188 }
189
190 /**
191 * Sets environment variables which should be added to the executed command
192 * environment. In CLI mode, the environment of the parent process will
193 * also be inherited.
194 *
195 * @param string[] $environment array of variable name => value
196 * @return $this
197 */
198 public function environment( array $environment ) {
199 $this->environment = $environment;
200 return $this;
201 }
202
203 /**
204 * Sends the provided input to the command. Defaults to an empty string.
205 * If you want to pass stdin through to the command instead, use
206 * passStdin().
207 *
208 * @param string $stdin
209 * @return $this
210 */
211 public function stdin( string $stdin ) {
212 $this->stdin = $stdin;
213 return $this;
214 }
215
216 /**
217 * Controls whether stdin is passed through to the command, so that the
218 * user can interact with the command when it is run in CLI mode. If this
219 * is enabled:
220 * - The wall clock timeout will be disabled to avoid stopping the
221 * process with SIGTTIN/SIGTTOU (T206957).
222 * - The string specified with input() will be ignored.
223 *
224 * @param bool $yesno
225 * @return $this
226 */
227 public function passStdin( bool $yesno = true ) {
228 $this->passStdin = $yesno;
229 return $this;
230 }
231
232 /**
233 * Controls whether stderr should be included in stdout, including errors
234 * from wrappers. Default: don't include.
235 *
236 * @param bool $includeStderr
237 * @return $this
238 */
239 public function includeStderr( bool $includeStderr = true ) {
240 $this->includeStderr = $includeStderr;
241 return $this;
242 }
243
244 /**
245 * If this is set to true, text written to stderr by the command will be
246 * passed through to PHP's stderr. To avoid SIGTTIN/SIGTTOU, and to support
247 * Result::getStderr(), the file descriptor is not passed through, we just
248 * copy the data to stderr as we receive it.
249 *
250 * @param bool $yesno
251 * @return $this
252 */
253 public function forwardStderr( bool $yesno = true ) {
254 $this->forwardStderr = $yesno;
255 return $this;
256 }
257
258 /**
259 * When enabled, text sent to stderr will be logged with a level of 'error'.
260 *
261 * @param bool $yesno
262 * @return $this
263 */
264 public function logStderr( bool $yesno = true ) {
265 $this->logStderr = $yesno;
266 return $this;
267 }
268
269 /**
270 * Open FD 3 as a pipe and pass the write side to the command. Lines
271 * written to this pipe will be logged. This is used by some wrappers to
272 * provide log messages.
273 *
274 * @internal For Wrapper subclasses only
275 * @param bool $yesno
276 * @return $this
277 */
278 public function useLogPipe( bool $yesno = true ) {
279 $this->useLogPipe = $yesno;
280 return $this;
281 }
282
283 /**
284 * Set the working directory under which the command will be run.
285 *
286 * @param string $path
287 * @return $this
288 */
289 public function workingDirectory( string $path ) {
290 $this->workingDirectory = $path;
291 return $this;
292 }
293
294 /**
295 * Set special options to proc_open().
296 *
297 * @internal For Wrapper subclasses only
298 * @param array $options
299 * @return $this
300 */
301 public function procOpenOptions( array $options ) {
302 $this->procOpenOptions = $options;
303 return $this;
304 }
305
306 /**
307 * Disable networking, if possible.
308 *
309 * @param bool $yesno
310 * @return $this
311 */
312 public function disableNetwork( bool $yesno = true ) {
313 $this->disableNetwork = $yesno;
314 return $this;
315 }
316
317 /**
318 * Specify the set of disabled syscalls. If the sandbox configuration
319 * permits, a seccomp filter will be set up to disallow them.
320 *
321 * @param string[] $syscalls
322 * @return $this
323 */
324 public function disabledSyscalls( array $syscalls ) {
325 $this->disabledSyscalls = $syscalls;
326 return $this;
327 }
328
329 /**
330 * Enable/disable the default Firejail seccomp filter. This only works if
331 * Firejail is enabled. Firejail will also enable no_new_privs when this is
332 * enabled.
333 *
334 * @param bool $yesno
335 * @return $this
336 */
337 public function firejailDefaultSeccomp( bool $yesno = true ) {
338 $this->firejailDefaultSeccomp = $yesno;
339 return $this;
340 }
341
342 /**
343 * Enable the no_new_privs attribute to prevent privilege escalation via
344 * setuid executables and similar.
345 *
346 * @param bool $yesno
347 * @return $this
348 */
349 public function noNewPrivs( bool $yesno = true ) {
350 $this->noNewPrivs = $yesno;
351 return $this;
352 }
353
354 /**
355 * Use a private user namespace.
356 *
357 * @param bool $yesno
358 * @return $this
359 */
360 public function privateUserNamespace( bool $yesno = true ) {
361 $this->privateUserNamespace = $yesno;
362 return $this;
363 }
364
365 /**
366 * Create a private /dev mount
367 *
368 * @param bool $yesno
369 * @return $this
370 */
371 public function privateDev( bool $yesno = true ) {
372 $this->privateDev = $yesno;
373 return $this;
374 }
375
376 /**
377 * If called, the files/directories that are allowed will certainly be
378 * available to the shell command.
379 *
380 * Whether this can be respected depends on the configuration of the
381 * executor.
382 *
383 * @param string ...$paths
384 *
385 * @return $this
386 */
387 public function allowPath( ...$paths ) {
388 $this->allowedPaths = array_merge( $this->allowedPaths, $paths );
389 return $this;
390 }
391
392 /**
393 * Replace the list of allowed paths.
394 *
395 * @param string[] $paths
396 * @return $this
397 */
398 public function allowedPaths( array $paths ) {
399 $this->allowedPaths = $paths;
400 return $this;
401 }
402
403 /**
404 * Disallow the specified paths so that the command cannot access them.
405 *
406 * Whether this can be respected depends on the configuration of the
407 * executor.
408 *
409 * @param string ...$paths
410 * @return $this
411 */
412 public function disallowPath( ...$paths ) {
413 $this->disallowedPaths = array_merge( $this->disallowedPaths, $paths );
414 return $this;
415 }
416
417 /**
418 * Replace the list of disallowed paths
419 *
420 * @param string[] $paths
421 * @return $this
422 */
423 public function disallowedPaths( array $paths ) {
424 $this->disallowedPaths = $paths;
425 return $this;
426 }
427
428 /**
429 * Disable firejail and similar sandboxes
430 *
431 * @param bool $yesno
432 * @return $this
433 */
434 public function disableSandbox( bool $yesno = true ) {
435 $this->disableSandbox = $yesno;
436 return $this;
437 }
438
439 /**
440 * Get command parameters for JSON serialization by the client.
441 *
442 * @internal
443 * @return array
444 */
445 public function getClientData() {
446 return [
447 'command' => $this->command,
448 'cpuLimit' => $this->cpuTimeLimit,
449 'wallTimeLimit' => $this->wallTimeLimit,
450 'memoryLimit' => $this->memoryLimit,
451 'fileSizeLimit' => $this->fileSizeLimit,
452 'environment' => $this->environment,
453 'includeStderr' => $this->includeStderr,
454 'logStderr' => $this->logStderr
455 ];
456 }
457
458 /**
459 * Set command parameters using a data array created by getClientData()
460 *
461 * @internal
462 * @param array $data
463 */
464 public function setClientData( $data ) {
465 foreach ( $data as $name => $value ) {
466 switch ( $name ) {
467 case 'command':
468 $this->command = $value;
469 $this->syntaxInfo = null;
470 break;
471
472 case 'cpuLimit':
473 $this->cpuTimeLimit = $value;
474 break;
475
476 case 'wallTimeLimit':
477 $this->wallTimeLimit = $value;
478 break;
479
480 case 'memoryLimit':
481 $this->memoryLimit = $value;
482 break;
483
484 case 'fileSizeLimit':
485 $this->fileSizeLimit = $value;
486 break;
487
488 case 'environment':
489 $this->environment = $value;
490 break;
491
492 case 'includeStderr':
493 $this->includeStderr = $value;
494 break;
495
496 case 'logStderr':
497 $this->logStderr = $value;
498 break;
499 }
500 }
501 }
502
503 /**
504 * Get the current command string
505 *
506 * @return string
507 */
508 public function getCommandString() {
509 return $this->command;
510 }
511
512 /**
513 * Get the CPU limit
514 *
515 * @return int|float|null
516 */
517 public function getCpuTimeLimit() {
518 return $this->cpuTimeLimit;
519 }
520
521 /**
522 * Get the wall clock time limit
523 *
524 * @return int|float|null
525 */
526 public function getWallTimeLimit() {
527 return $this->wallTimeLimit;
528 }
529
530 /**
531 * Get the memory limit
532 *
533 * @return int|float|null
534 */
535 public function getMemoryLimit() {
536 return $this->memoryLimit;
537 }
538
539 /**
540 * Get the file size limit
541 *
542 * @return int|float|null
543 */
544 public function getFileSizeLimit() {
545 return $this->fileSizeLimit;
546 }
547
548 /**
549 * Get the environment
550 *
551 * @return string[]
552 */
553 public function getEnvironment() {
554 return $this->environment;
555 }
556
557 /**
558 * Get the text to be passed to stdin
559 *
560 * @return string
561 */
562 public function getStdin() {
563 return $this->stdin;
564 }
565
566 /**
567 * Get whether to pass through stdin
568 *
569 * @return bool
570 */
571 public function getPassStdin() {
572 return $this->passStdin;
573 }
574
575 /**
576 * Get whether to duplicate stderr to stdout
577 *
578 * @return bool
579 */
580 public function getIncludeStderr() {
581 return $this->includeStderr;
582 }
583
584 /**
585 * Get whether to log text seen on stderr
586 *
587 * @return bool
588 */
589 public function getLogStderr() {
590 return $this->logStderr;
591 }
592
593 /**
594 * Get whether to forward the command's stderr to the parent's stderr
595 *
596 * @return bool
597 */
598 public function getForwardStderr() {
599 return $this->forwardStderr;
600 }
601
602 /**
603 * Get whether to enable the log pipe
604 *
605 * @return bool
606 */
607 public function getUseLogPipe() {
608 return $this->useLogPipe;
609 }
610
611 /**
612 * @return string|null
613 */
614 public function getWorkingDirectory() {
615 return $this->workingDirectory;
616 }
617
618 /**
619 * Get the additional proc_open() options
620 *
621 * @return array
622 */
623 public function getProcOpenOptions() {
624 return $this->procOpenOptions;
625 }
626
627 /**
628 * Get whether to disable external networking
629 *
630 * @return bool
631 */
632 public function getDisableNetwork() {
633 return $this->disableNetwork;
634 }
635
636 /**
637 * Get the list of disabled syscalls
638 *
639 * @return string[]
640 */
641 public function getDisabledSyscalls() {
642 return $this->disabledSyscalls;
643 }
644
645 /**
646 * Get whether to use firejail's default seccomp filter
647 *
648 * @return bool
649 */
650 public function getFirejailDefaultSeccomp() {
651 return $this->firejailDefaultSeccomp;
652 }
653
654 /**
655 * Get whether to enable the no_new_privs process attribute
656 *
657 * @return bool
658 */
659 public function getNoNewPrivs() {
660 return $this->noNewPrivs;
661 }
662
663 /**
664 * Get whether to use a private user namespace
665 *
666 * @return bool
667 */
668 public function getPrivateUserNamespace() {
669 return $this->privateUserNamespace;
670 }
671
672 /**
673 * Get whether to mount a private /dev filesystem
674 *
675 * @return bool
676 */
677 public function getPrivateDev() {
678 return $this->privateDev;
679 }
680
681 /**
682 * Get the allowed paths
683 *
684 * @return string[]
685 */
686 public function getAllowedPaths() {
687 return $this->allowedPaths;
688 }
689
690 /**
691 * Get the disallowed paths
692 *
693 * @return string[]
694 */
695 public function getDisallowedPaths() {
696 return $this->disallowedPaths;
697 }
698
699 /**
700 * Get whether to disable firejail and similar sandboxes
701 *
702 * @return bool
703 */
704 public function getDisableSandbox() {
705 return $this->disableSandbox;
706 }
707
708 /**
709 * Parse the current command string
710 *
711 * @return SyntaxInfo
712 * @throws ShellSyntaxError
713 */
714 public function getSyntaxInfo() {
715 if ( $this->syntaxInfo === null ) {
716 $parser = new ShellParser;
717 $tree = $parser->parse( $this->getCommandString() );
718 $this->syntaxInfo = $tree->getInfo();
719 }
720 return $this->syntaxInfo;
721 }
722 }