Skip to content

Commit 18f50a7

Browse files
[Process] On Windows, don't rely on the OS to find executables
1 parent 2ad775b commit 18f50a7

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

Process.php

+7
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class Process implements \IteratorAggregate
8585
private ?int $cachedExitCode = null;
8686

8787
private static ?bool $sigchild = null;
88+
private static array $executables = [];
8889

8990
/**
9091
* Exit codes translation table.
@@ -1543,6 +1544,12 @@ private function buildShellCommandline(string|array $commandline): string
15431544
return $commandline;
15441545
}
15451546

1547+
if ('\\' === \DIRECTORY_SEPARATOR && isset($commandline[0][0]) && \strlen($commandline[0]) === strcspn($commandline[0], ':/\\')) {
1548+
// On Windows, we don't rely on the OS to find the executable if possible to avoid lookups
1549+
// in the current directory which could be untrusted. Instead we use the ExecutableFinder.
1550+
$commandline[0] = (self::$executables[$commandline[0]] ??= (new ExecutableFinder())->find($commandline[0])) ?? $commandline[0];
1551+
}
1552+
15461553
return implode(' ', array_map($this->escapeArgument(...), $commandline));
15471554
}
15481555

Tests/ProcessFailedExceptionTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public function testProcessFailedExceptionPopulatesInformationFromProcessOutput(
8080

8181
$exception = new ProcessFailedException($process);
8282

83-
$this->assertEquals(
84-
"The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}\n\nOutput:\n================\n{$output}\n\nError Output:\n================\n{$errorOutput}",
83+
$this->assertStringMatchesFormat(
84+
"The command \"%s\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}\n\nOutput:\n================\n{$output}\n\nError Output:\n================\n{$errorOutput}",
8585
str_replace("'php'", 'php', $exception->getMessage())
8686
);
8787
}
@@ -126,9 +126,9 @@ public function testDisabledOutputInFailedExceptionDoesNotPopulateOutput()
126126

127127
$exception = new ProcessFailedException($process);
128128

129-
$this->assertEquals(
130-
"The command \"$cmd\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}",
131-
str_replace("'php'", 'php', $exception->getMessage())
129+
$this->assertStringMatchesFormat(
130+
"The command \"%s\" failed.\n\nExit Code: $exitCode($exitText)\n\nWorking directory: {$workingDirectory}",
131+
$exception->getMessage()
132132
);
133133
}
134134
}

Tests/ProcessTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,17 @@ public function testNotIgnoringSignal()
16871687
$this->assertSame(\SIGTERM, $process->getTermSignal());
16881688
}
16891689

1690+
public function testPathResolutionOnWindows()
1691+
{
1692+
if ('\\' !== \DIRECTORY_SEPARATOR) {
1693+
$this->markTestSkipped('This test is for Windows platform only');
1694+
}
1695+
1696+
$process = $this->getProcess(['where']);
1697+
1698+
$this->assertSame('C:\\Windows\\system32\\where.EXE', $process->getCommandLine());
1699+
}
1700+
16901701
private function getProcess(string|array $commandline, ?string $cwd = null, ?array $env = null, mixed $input = null, ?int $timeout = 60): Process
16911702
{
16921703
if (\is_string($commandline)) {

0 commit comments

Comments
 (0)