Skip to content

Commit 88638b9

Browse files
Merge branch '5.4' into 6.4
* 5.4: Do not read from argv on non-CLI SAPIs [Process] Use %PATH% before %CD% to load the shell on Windows [HttpFoundation] Reject URIs that contain invalid characters [HttpClient] Filter private IPs before connecting when Host == IP
2 parents 0776b99 + ee75984 commit 88638b9

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

ExecutableFinder.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020
class ExecutableFinder
2121
{
22-
private array $suffixes = ['.exe', '.bat', '.cmd', '.com'];
2322
private const CMD_BUILTINS = [
2423
'assoc', 'break', 'call', 'cd', 'chdir', 'cls', 'color', 'copy', 'date',
2524
'del', 'dir', 'echo', 'endlocal', 'erase', 'exit', 'for', 'ftype', 'goto',
@@ -28,6 +27,8 @@ class ExecutableFinder
2827
'setlocal', 'shift', 'start', 'time', 'title', 'type', 'ver', 'vol',
2928
];
3029

30+
private array $suffixes = [];
31+
3132
/**
3233
* Replaces default suffixes of executable.
3334
*
@@ -67,11 +68,13 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
6768
$extraDirs
6869
);
6970

70-
$suffixes = [''];
71+
$suffixes = [];
7172
if ('\\' === \DIRECTORY_SEPARATOR) {
7273
$pathExt = getenv('PATHEXT');
73-
$suffixes = array_merge($pathExt ? explode(\PATH_SEPARATOR, $pathExt) : $this->suffixes, $suffixes);
74+
$suffixes = $this->suffixes;
75+
$suffixes = array_merge($suffixes, $pathExt ? explode(\PATH_SEPARATOR, $pathExt) : ['.exe', '.bat', '.cmd', '.com']);
7476
}
77+
$suffixes = '' !== pathinfo($name, PATHINFO_EXTENSION) ? array_merge([''], $suffixes) : array_merge($suffixes, ['']);
7578
foreach ($suffixes as $suffix) {
7679
foreach ($dirs as $dir) {
7780
if ('' === $dir) {
@@ -87,12 +90,11 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
8790
}
8891
}
8992

90-
if (!\function_exists('exec') || \strlen($name) !== strcspn($name, '/'.\DIRECTORY_SEPARATOR)) {
93+
if ('\\' === \DIRECTORY_SEPARATOR || !\function_exists('exec') || \strlen($name) !== strcspn($name, '/'.\DIRECTORY_SEPARATOR)) {
9194
return $default;
9295
}
9396

94-
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where %s 2> NUL' : 'command -v -- %s';
95-
$execResult = exec(\sprintf($command, escapeshellarg($name)));
97+
$execResult = exec('command -v -- '.escapeshellarg($name));
9698

9799
if (($executablePath = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) && @is_executable($executablePath)) {
98100
return $executablePath;

PhpExecutableFinder.php

+2-13
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,8 @@ public function __construct()
3232
public function find(bool $includeArgs = true): string|false
3333
{
3434
if ($php = getenv('PHP_BINARY')) {
35-
if (!is_executable($php)) {
36-
if (!\function_exists('exec') || \strlen($php) !== strcspn($php, '/'.\DIRECTORY_SEPARATOR)) {
37-
return false;
38-
}
39-
40-
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where %s 2> NUL' : 'command -v -- %s';
41-
$execResult = exec(\sprintf($command, escapeshellarg($php)));
42-
if (!$php = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) {
43-
return false;
44-
}
45-
if (!is_executable($php)) {
46-
return false;
47-
}
35+
if (!is_executable($php) && !$php = $this->executableFinder->find($php)) {
36+
return false;
4837
}
4938

5039
if (@is_dir($php)) {

Process.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,14 @@ function ($m) use (&$env, $uid) {
15361536
$cmd
15371537
);
15381538

1539-
$cmd = 'cmd /V:ON /E:ON /D /C ('.str_replace("\n", ' ', $cmd).')';
1539+
static $comSpec;
1540+
1541+
if (!$comSpec && $comSpec = (new ExecutableFinder())->find('cmd.exe')) {
1542+
// Escape according to CommandLineToArgvW rules
1543+
$comSpec = '"'.preg_replace('{(\\\\*+)"}', '$1$1\"', $comSpec) .'"';
1544+
}
1545+
1546+
$cmd = ($comSpec ?? 'cmd').' /V:ON /E:ON /D /C ('.str_replace("\n", ' ', $cmd).')';
15401547
foreach ($this->processPipes->getFiles() as $offset => $filename) {
15411548
$cmd .= ' '.$offset.'>"'.$filename.'"';
15421549
}

0 commit comments

Comments
 (0)