This commit is contained in:
1 parent
f7c532a648
commit
640bd67556
5801 files changed
+484532
-55857
No files matched your search
Vendored
+126
-51
@@ -51,6 +51,20 @@ class Process implements \IteratorAggregate
|
||||
public const ITER_SKIP_OUT = 4; // Use this flag to skip STDOUT while iterating
|
||||
public const ITER_SKIP_ERR = 8; // Use this flag to skip STDERR while iterating
|
||||
|
||||
/**
|
||||
* Maximum number of UTF-16 code units allowed in the Windows environment block.
|
||||
*
|
||||
* The Win32 CreateProcess API encodes env vars as KEY=VALUE\0 in UTF-16LE,
|
||||
* terminated by an extra \0. Exceeding this limit causes proc_open() to hang
|
||||
* silently rather than returning false.
|
||||
*
|
||||
* @see https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa
|
||||
*/
|
||||
private const WINDOWS_ENV_BLOCK_MAX_LENGTH = 32767;
|
||||
|
||||
/**
|
||||
* @var \Closure('out'|'err', string):bool|null
|
||||
*/
|
||||
private ?\Closure $callback = null;
|
||||
private array|string $commandline;
|
||||
private ?string $cwd;
|
||||
@@ -82,9 +96,9 @@ class Process implements \IteratorAggregate
|
||||
private WindowsPipes|UnixPipes $processPipes;
|
||||
|
||||
private ?int $latestSignal = null;
|
||||
private ?int $cachedExitCode = null;
|
||||
|
||||
private static ?bool $sigchild = null;
|
||||
private static array $executables = [];
|
||||
|
||||
/**
|
||||
* Exit codes translation table.
|
||||
@@ -197,12 +211,12 @@ class Process implements \IteratorAggregate
|
||||
return $process;
|
||||
}
|
||||
|
||||
public function __sleep(): array
|
||||
public function __serialize(): array
|
||||
{
|
||||
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
|
||||
}
|
||||
|
||||
public function __wakeup(): void
|
||||
public function __unserialize(array $data): void
|
||||
{
|
||||
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
|
||||
}
|
||||
@@ -231,8 +245,8 @@ class Process implements \IteratorAggregate
|
||||
* The STDOUT and STDERR are also available after the process is finished
|
||||
* via the getOutput() and getErrorOutput() methods.
|
||||
*
|
||||
* @param callable|null $callback A PHP callback to run whenever there is some
|
||||
* output available on STDOUT or STDERR
|
||||
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
|
||||
* output available on STDOUT or STDERR
|
||||
*
|
||||
* @return int The exit status code
|
||||
*
|
||||
@@ -257,9 +271,17 @@ class Process implements \IteratorAggregate
|
||||
* This is identical to run() except that an exception is thrown if the process
|
||||
* exits with a non-zero exit code.
|
||||
*
|
||||
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
|
||||
* output available on STDOUT or STDERR
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws ProcessFailedException if the process didn't terminate successfully
|
||||
* @throws ProcessFailedException When process didn't terminate successfully
|
||||
* @throws RuntimeException When process can't be launched
|
||||
* @throws RuntimeException When process is already running
|
||||
* @throws ProcessTimedOutException When process timed out
|
||||
* @throws ProcessSignaledException When process stopped after receiving signal
|
||||
* @throws LogicException In case a callback is provided and output has been disabled
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
@@ -284,8 +306,8 @@ class Process implements \IteratorAggregate
|
||||
* the output in real-time while writing the standard input to the process.
|
||||
* It allows to have feedback from the independent process during execution.
|
||||
*
|
||||
* @param callable|null $callback A PHP callback to run whenever there is some
|
||||
* output available on STDOUT or STDERR
|
||||
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
|
||||
* output available on STDOUT or STDERR
|
||||
*
|
||||
* @throws ProcessStartFailedException When process can't be launched
|
||||
* @throws RuntimeException When process is already running
|
||||
@@ -332,17 +354,25 @@ class Process implements \IteratorAggregate
|
||||
|
||||
$envPairs = [];
|
||||
foreach ($env as $k => $v) {
|
||||
if (false !== $v && false === \in_array($k, ['argc', 'argv', 'ARGC', 'ARGV'], true)) {
|
||||
if (!\is_scalar($v ?? '') && !$v instanceof \Stringable) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (false !== $v && !\in_array($k = (string) $k, ['', 'argc', 'argv', 'ARGC', 'ARGV'], true) && !str_contains($k, '=') && !str_contains($k, "\0")) {
|
||||
$envPairs[] = $k.'='.$v;
|
||||
}
|
||||
}
|
||||
|
||||
if ('\\' === \DIRECTORY_SEPARATOR) {
|
||||
$this->validateWindowsEnvBlockSize($envPairs);
|
||||
}
|
||||
|
||||
if (!is_dir($this->cwd)) {
|
||||
throw new RuntimeException(sprintf('The provided cwd "%s" does not exist.', $this->cwd));
|
||||
throw new RuntimeException(\sprintf('The provided cwd "%s" does not exist.', $this->cwd));
|
||||
}
|
||||
|
||||
$lastError = null;
|
||||
set_error_handler(function ($type, $msg) use (&$lastError) {
|
||||
set_error_handler(static function ($type, $msg) use (&$lastError) {
|
||||
$lastError = $msg;
|
||||
|
||||
return true;
|
||||
@@ -395,8 +425,8 @@ class Process implements \IteratorAggregate
|
||||
*
|
||||
* Be warned that the process is cloned before being started.
|
||||
*
|
||||
* @param callable|null $callback A PHP callback to run whenever there is some
|
||||
* output available on STDOUT or STDERR
|
||||
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
|
||||
* output available on STDOUT or STDERR
|
||||
*
|
||||
* @throws ProcessStartFailedException When process can't be launched
|
||||
* @throws RuntimeException When process is already running
|
||||
@@ -424,7 +454,8 @@ class Process implements \IteratorAggregate
|
||||
* from the output in real-time while writing the standard input to the process.
|
||||
* It allows to have feedback from the independent process during execution.
|
||||
*
|
||||
* @param callable|null $callback A valid PHP callback
|
||||
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
|
||||
* output available on STDOUT or STDERR
|
||||
*
|
||||
* @return int The exitcode of the process
|
||||
*
|
||||
@@ -471,6 +502,9 @@ class Process implements \IteratorAggregate
|
||||
* from the output in real-time while writing the standard input to the process.
|
||||
* It allows to have feedback from the independent process during execution.
|
||||
*
|
||||
* @param (callable('out'|'err', string):bool)|null $callback A PHP callback to run whenever there is some
|
||||
* output available on STDOUT or STDERR
|
||||
*
|
||||
* @throws RuntimeException When process timed out
|
||||
* @throws LogicException When process is not yet started
|
||||
* @throws ProcessTimedOutException In case the timeout was reached
|
||||
@@ -1220,7 +1254,7 @@ class Process implements \IteratorAggregate
|
||||
foreach ($options as $key => $value) {
|
||||
if (!\in_array($key, $existingOptions)) {
|
||||
$this->options = $defaultOptions;
|
||||
throw new LogicException(sprintf('Invalid option "%s" passed to "%s()". Supported options are "%s".', $key, __METHOD__, implode('", "', $existingOptions)));
|
||||
throw new LogicException(\sprintf('Invalid option "%s" passed to "%s()". Supported options are "%s".', $key, __METHOD__, implode('", "', $existingOptions)));
|
||||
}
|
||||
$this->options[$key] = $value;
|
||||
}
|
||||
@@ -1291,22 +1325,21 @@ class Process implements \IteratorAggregate
|
||||
* The callbacks adds all occurred output to the specific buffer and calls
|
||||
* the user callback (if present) with the received output.
|
||||
*
|
||||
* @param callable|null $callback The user defined PHP callback
|
||||
* @param callable('out'|'err', string)|null $callback
|
||||
*
|
||||
* @return \Closure('out'|'err', string):bool
|
||||
*/
|
||||
protected function buildCallback(?callable $callback = null): \Closure
|
||||
{
|
||||
if ($this->outputDisabled) {
|
||||
return fn ($type, $data): bool => null !== $callback && $callback($type, $data);
|
||||
return static fn ($type, $data): bool => null !== $callback && $callback($type, $data);
|
||||
}
|
||||
|
||||
$out = self::OUT;
|
||||
|
||||
return function ($type, $data) use ($callback, $out): bool {
|
||||
if ($out == $type) {
|
||||
$this->addOutput($data);
|
||||
} else {
|
||||
$this->addErrorOutput($data);
|
||||
}
|
||||
return function ($type, $data) use ($callback): bool {
|
||||
match ($type) {
|
||||
self::OUT => $this->addOutput($data),
|
||||
self::ERR => $this->addErrorOutput($data),
|
||||
};
|
||||
|
||||
return null !== $callback && $callback($type, $data);
|
||||
};
|
||||
@@ -1323,21 +1356,10 @@ class Process implements \IteratorAggregate
|
||||
return;
|
||||
}
|
||||
|
||||
$this->processInformation = proc_get_status($this->process);
|
||||
$running = $this->processInformation['running'];
|
||||
|
||||
// In PHP < 8.3, "proc_get_status" only returns the correct exit status on the first call.
|
||||
// Subsequent calls return -1 as the process is discarded. This workaround caches the first
|
||||
// retrieved exit status for consistent results in later calls, mimicking PHP 8.3 behavior.
|
||||
if (\PHP_VERSION_ID < 80300) {
|
||||
if (!isset($this->cachedExitCode) && !$running && -1 !== $this->processInformation['exitcode']) {
|
||||
$this->cachedExitCode = $this->processInformation['exitcode'];
|
||||
}
|
||||
|
||||
if (isset($this->cachedExitCode) && !$running && -1 === $this->processInformation['exitcode']) {
|
||||
$this->processInformation['exitcode'] = $this->cachedExitCode;
|
||||
}
|
||||
if ($this->processInformation['running'] ?? true) {
|
||||
$this->processInformation = proc_get_status($this->process);
|
||||
}
|
||||
$running = $this->processInformation['running'];
|
||||
|
||||
$this->readPipes($running && $blocking, '\\' !== \DIRECTORY_SEPARATOR || !$running);
|
||||
|
||||
@@ -1504,10 +1526,10 @@ class Process implements \IteratorAggregate
|
||||
}
|
||||
|
||||
if ('\\' === \DIRECTORY_SEPARATOR) {
|
||||
exec(sprintf('taskkill /F /T /PID %d 2>&1', $pid), $output, $exitCode);
|
||||
exec(\sprintf('taskkill /F /T /PID %d 2>&1', $pid), $output, $exitCode);
|
||||
if ($exitCode && $this->isRunning()) {
|
||||
if ($throwException) {
|
||||
throw new RuntimeException(sprintf('Unable to kill the process (%s).', implode(' ', $output)));
|
||||
throw new RuntimeException(\sprintf('Unable to kill the process (%s).', implode(' ', $output)));
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -1517,12 +1539,12 @@ class Process implements \IteratorAggregate
|
||||
$ok = @proc_terminate($this->process, $signal);
|
||||
} elseif (\function_exists('posix_kill')) {
|
||||
$ok = @posix_kill($pid, $signal);
|
||||
} elseif ($ok = proc_open(sprintf('kill -%d %d', $signal, $pid), [2 => ['pipe', 'w']], $pipes)) {
|
||||
} elseif ($ok = proc_open(\sprintf('kill -%d %d', $signal, $pid), [2 => ['pipe', 'w']], $pipes)) {
|
||||
$ok = false === fgets($pipes[2]);
|
||||
}
|
||||
if (!$ok) {
|
||||
if ($throwException) {
|
||||
throw new RuntimeException(sprintf('Error while sending signal "%s".', $signal));
|
||||
throw new RuntimeException(\sprintf('Error while sending signal "%s".', $signal));
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -1543,13 +1565,19 @@ class Process implements \IteratorAggregate
|
||||
return $commandline;
|
||||
}
|
||||
|
||||
if ('\\' === \DIRECTORY_SEPARATOR && isset($commandline[0][0]) && \strlen($commandline[0]) === strcspn($commandline[0], ':/\\')) {
|
||||
// On Windows, we don't rely on the OS to find the executable if possible to avoid lookups
|
||||
// in the current directory which could be untrusted. Instead we use the ExecutableFinder.
|
||||
$commandline[0] = (self::$executables[$commandline[0]] ??= (new ExecutableFinder())->find($commandline[0])) ?? $commandline[0];
|
||||
}
|
||||
|
||||
return implode(' ', array_map($this->escapeArgument(...), $commandline));
|
||||
}
|
||||
|
||||
private function prepareWindowsCommandLine(string|array $cmd, array &$env): string
|
||||
{
|
||||
$cmd = $this->buildShellCommandline($cmd);
|
||||
$uid = uniqid('', true);
|
||||
$uid = bin2hex(random_bytes(4));
|
||||
$cmd = preg_replace_callback(
|
||||
'/"(?:(
|
||||
[^"%!^]*+
|
||||
@@ -1558,7 +1586,7 @@ class Process implements \IteratorAggregate
|
||||
[^"%!^]*+
|
||||
)++
|
||||
) | [^"]*+ )"/x',
|
||||
function ($m) use (&$env, $uid) {
|
||||
static function ($m) use (&$env, $uid) {
|
||||
static $varCount = 0;
|
||||
static $varCache = [];
|
||||
if (!isset($m[1])) {
|
||||
@@ -1585,7 +1613,14 @@ class Process implements \IteratorAggregate
|
||||
$cmd
|
||||
);
|
||||
|
||||
$cmd = 'cmd /V:ON /E:ON /D /C ('.str_replace("\n", ' ', $cmd).')';
|
||||
static $comSpec;
|
||||
|
||||
if (!$comSpec && $comSpec = (new ExecutableFinder())->find('cmd.exe')) {
|
||||
// Escape according to CommandLineToArgvW rules
|
||||
$comSpec = '"'.preg_replace('{(\\\\*+)"}', '$1$1\"', $comSpec).'"';
|
||||
}
|
||||
|
||||
$cmd = ($comSpec ?? 'cmd').' /V:ON /E:ON /D /C ('.str_replace("\n", ' ', $cmd).')';
|
||||
foreach ($this->processPipes->getFiles() as $offset => $filename) {
|
||||
$cmd .= ' '.$offset.'>"'.$filename.'"';
|
||||
}
|
||||
@@ -1601,7 +1636,7 @@ class Process implements \IteratorAggregate
|
||||
private function requireProcessIsStarted(string $functionName): void
|
||||
{
|
||||
if (!$this->isStarted()) {
|
||||
throw new LogicException(sprintf('Process must be started before calling "%s()".', $functionName));
|
||||
throw new LogicException(\sprintf('Process must be started before calling "%s()".', $functionName));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1613,7 +1648,7 @@ class Process implements \IteratorAggregate
|
||||
private function requireProcessIsTerminated(string $functionName): void
|
||||
{
|
||||
if (!$this->isTerminated()) {
|
||||
throw new LogicException(sprintf('Process must be terminated before calling "%s()".', $functionName));
|
||||
throw new LogicException(\sprintf('Process must be terminated before calling "%s()".', $functionName));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1631,7 +1666,7 @@ class Process implements \IteratorAggregate
|
||||
if (str_contains($argument, "\0")) {
|
||||
$argument = str_replace("\0", '?', $argument);
|
||||
}
|
||||
if (!preg_match('/[\/()%!^"<>&|\s]/', $argument)) {
|
||||
if (!preg_match('/[()%!^"<>&|\s[\]=;*?\'$]/', $argument)) {
|
||||
return $argument;
|
||||
}
|
||||
$argument = preg_replace('/(\\\\+)$/', '$1$1', $argument);
|
||||
@@ -1643,7 +1678,7 @@ class Process implements \IteratorAggregate
|
||||
{
|
||||
return preg_replace_callback('/"\$\{:([_a-zA-Z]++[_a-zA-Z0-9]*+)\}"/', function ($matches) use ($commandline, $env) {
|
||||
if (!isset($env[$matches[1]]) || false === $env[$matches[1]]) {
|
||||
throw new InvalidArgumentException(sprintf('Command line is missing a value for parameter "%s": ', $matches[1]).$commandline);
|
||||
throw new InvalidArgumentException(\sprintf('Command line is missing a value for parameter "%s": ', $matches[1]).$commandline);
|
||||
}
|
||||
|
||||
return $this->escapeArgument($env[$matches[1]]);
|
||||
@@ -1654,7 +1689,47 @@ class Process implements \IteratorAggregate
|
||||
{
|
||||
$env = getenv();
|
||||
$env = ('\\' === \DIRECTORY_SEPARATOR ? array_intersect_ukey($env, $_SERVER, 'strcasecmp') : array_intersect_key($env, $_SERVER)) ?: $env;
|
||||
$env = $_ENV + ('\\' === \DIRECTORY_SEPARATOR ? array_diff_ukey($env, $_ENV, 'strcasecmp') : $env);
|
||||
|
||||
return $_ENV + ('\\' === \DIRECTORY_SEPARATOR ? array_diff_ukey($env, $_ENV, 'strcasecmp') : $env);
|
||||
if (\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
|
||||
return $env;
|
||||
}
|
||||
|
||||
// On non-CLI SAPIs (notably PHP-FPM and CGI), CGI/FastCGI request-context
|
||||
// vars are exposed through $_SERVER, $_ENV and getenv(), and must not
|
||||
// propagate to subprocesses.
|
||||
foreach ($env as $k => $v) {
|
||||
if (str_starts_with($k, 'HTTP_')
|
||||
|| str_starts_with($k, 'ORIG_')
|
||||
|| str_starts_with($k, 'REDIRECT_')
|
||||
|| \in_array($k, [
|
||||
'AUTH_TYPE', 'CONTENT_LENGTH', 'CONTENT_TYPE', 'DOCUMENT_ROOT',
|
||||
'DOCUMENT_URI', 'GATEWAY_INTERFACE', 'HTTPS', 'PATH_INFO',
|
||||
'PATH_TRANSLATED', 'PHP_AUTH_DIGEST', 'PHP_AUTH_PW', 'PHP_AUTH_USER',
|
||||
'PHP_SELF', 'QUERY_STRING', 'REMOTE_ADDR', 'REMOTE_HOST',
|
||||
'REMOTE_IDENT', 'REMOTE_PORT', 'REMOTE_USER', 'REQUEST_METHOD',
|
||||
'REQUEST_SCHEME', 'REQUEST_TIME', 'REQUEST_TIME_FLOAT', 'REQUEST_URI',
|
||||
'SCRIPT_FILENAME', 'SCRIPT_NAME', 'SCRIPT_URI', 'SCRIPT_URL',
|
||||
'SERVER_ADDR', 'SERVER_ADMIN', 'SERVER_NAME', 'SERVER_PORT',
|
||||
'SERVER_PROTOCOL', 'SERVER_SIGNATURE', 'SERVER_SOFTWARE',
|
||||
], true)
|
||||
) {
|
||||
unset($env[$k]);
|
||||
}
|
||||
}
|
||||
|
||||
return $env;
|
||||
}
|
||||
|
||||
private function validateWindowsEnvBlockSize(array $envPairs): void
|
||||
{
|
||||
$block = implode("\0", $envPairs)."\0";
|
||||
@preg_replace('/./u', '', $block, -1, $blockLength)
|
||||
?? preg_replace('/./', '', $block, -1, $blockLength);
|
||||
$blockLength += 1 + preg_match_all('/[\xF0-\xF4][\x80-\xBF]{3}/', $block);
|
||||
|
||||
if ($blockLength > self::WINDOWS_ENV_BLOCK_MAX_LENGTH) {
|
||||
throw new InvalidArgumentException(\sprintf('The environment block size (%d) exceeds the Windows limit of %d UTF-16 code units.', $blockLength, self::WINDOWS_ENV_BLOCK_MAX_LENGTH));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user