Skip to content

Commit f2f5bb9

Browse files
Merge branch '7.1' into 7.2
* 7.1: 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 4b3cae7 + 66716d3 commit f2f5bb9

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

ExecutableFinder.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ class ExecutableFinder
2929

3030
private array $suffixes = [];
3131

32-
public function __construct()
33-
{
34-
// Set common extensions on Windows.
35-
if ('\\' === \DIRECTORY_SEPARATOR) {
36-
$this->suffixes = ['.exe', '.bat', '.cmd', '.com'];
37-
}
38-
}
39-
4032
/**
4133
* Replaces default suffixes of executable.
4234
*/
@@ -75,11 +67,12 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
7567
$extraDirs
7668
);
7769

78-
$suffixes = [''];
79-
if ('\\' === \DIRECTORY_SEPARATOR && $pathExt = getenv('PATHEXT')) {
80-
$suffixes = array_merge(explode(\PATH_SEPARATOR, $pathExt), $suffixes);
70+
$suffixes = $this->suffixes;
71+
if ('\\' === \DIRECTORY_SEPARATOR) {
72+
$pathExt = getenv('PATHEXT');
73+
$suffixes = array_merge($suffixes, $pathExt ? explode(\PATH_SEPARATOR, $pathExt) : ['.exe', '.bat', '.cmd', '.com']);
8174
}
82-
$suffixes = array_merge($suffixes, $this->suffixes);
75+
$suffixes = '' !== pathinfo($name, PATHINFO_EXTENSION) ? array_merge([''], $suffixes) : array_merge($suffixes, ['']);
8376
foreach ($suffixes as $suffix) {
8477
foreach ($dirs as $dir) {
8578
if ('' === $dir) {
@@ -95,12 +88,11 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
9588
}
9689
}
9790

98-
if (!\function_exists('exec') || \strlen($name) !== strcspn($name, '/'.\DIRECTORY_SEPARATOR)) {
91+
if ('\\' === \DIRECTORY_SEPARATOR || !\function_exists('exec') || \strlen($name) !== strcspn($name, '/'.\DIRECTORY_SEPARATOR)) {
9992
return $default;
10093
}
10194

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

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

PhpExecutableFinder.php

Lines changed: 2 additions & 13 deletions
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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,14 @@ function ($m) use (&$env, $uid) {
15921592
$cmd
15931593
);
15941594

1595-
$cmd = 'cmd /V:ON /E:ON /D /C ('.str_replace("\n", ' ', $cmd).')';
1595+
static $comSpec;
1596+
1597+
if (!$comSpec && $comSpec = (new ExecutableFinder())->find('cmd.exe')) {
1598+
// Escape according to CommandLineToArgvW rules
1599+
$comSpec = '"'.preg_replace('{(\\\\*+)"}', '$1$1\"', $comSpec) .'"';
1600+
}
1601+
1602+
$cmd = ($comSpec ?? 'cmd').' /V:ON /E:ON /D /C ('.str_replace("\n", ' ', $cmd).')';
15961603
foreach ($this->processPipes->getFiles() as $offset => $filename) {
15971604
$cmd .= ' '.$offset.'>"'.$filename.'"';
15981605
}

0 commit comments

Comments
 (0)