Skip to content

[Process] Fix finding executables independently of open_basedir #58291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions src/Symfony/Component/Process/ExecutableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,10 @@ public function addSuffix(string $suffix)
*/
public function find(string $name, ?string $default = null, array $extraDirs = [])
{
if (\ini_get('open_basedir')) {
$searchPath = array_merge(explode(\PATH_SEPARATOR, \ini_get('open_basedir')), $extraDirs);
$dirs = [];
foreach ($searchPath as $path) {
// Silencing against https://bugs.php.net/69240
if (@is_dir($path)) {
$dirs[] = $path;
} else {
if (basename($path) == $name && @is_executable($path)) {
return $path;
}
}
}
} else {
$dirs = array_merge(
explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
$extraDirs
);
}
$dirs = array_merge(
explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
$extraDirs
);

$suffixes = [''];
if ('\\' === \DIRECTORY_SEPARATOR) {
Expand All @@ -78,9 +63,18 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) {
return $file;
}

if (!@is_dir($dir) && basename($dir) === $name.$suffix && @is_executable($dir)) {
return $dir;
}
}
}

$command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v --';
if (\function_exists('exec') && ($executablePath = strtok(@exec($command.' '.escapeshellarg($name)), \PHP_EOL)) && is_executable($executablePath)) {
return $executablePath;
}

return $default;
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Process/PhpExecutableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function find(bool $includeArgs = true)
if ($php = getenv('PHP_BINARY')) {
if (!is_executable($php)) {
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v --';
if ($php = strtok(exec($command.' '.escapeshellarg($php)), \PHP_EOL)) {
if (\function_exists('exec') && $php = strtok(exec($command.' '.escapeshellarg($php)), \PHP_EOL)) {
if (!is_executable($php)) {
return false;
}
Expand Down
50 changes: 7 additions & 43 deletions src/Symfony/Component/Process/Tests/ExecutableFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,9 @@
*/
class ExecutableFinderTest extends TestCase
{
private $path;

protected function tearDown(): void
{
if ($this->path) {
// Restore path if it was changed.
putenv('PATH='.$this->path);
}
}

private function setPath($path)
{
$this->path = getenv('PATH');
putenv('PATH='.$path);
putenv('PATH='.($_SERVER['PATH'] ?? $_SERVER['Path']));
}

public function testFind()
Expand All @@ -41,7 +30,7 @@ public function testFind()
$this->markTestSkipped('Cannot test when open_basedir is set');
}

$this->setPath(\dirname(\PHP_BINARY));
putenv('PATH='.\dirname(\PHP_BINARY));

$finder = new ExecutableFinder();
$result = $finder->find($this->getPhpBinaryName());
Expand All @@ -57,7 +46,7 @@ public function testFindWithDefault()

$expected = 'defaultValue';

$this->setPath('');
putenv('PATH=');

$finder = new ExecutableFinder();
$result = $finder->find('foo', $expected);
Expand All @@ -71,7 +60,7 @@ public function testFindWithNullAsDefault()
$this->markTestSkipped('Cannot test when open_basedir is set');
}

$this->setPath('');
putenv('PATH=');

$finder = new ExecutableFinder();

Expand All @@ -86,7 +75,7 @@ public function testFindWithExtraDirs()
$this->markTestSkipped('Cannot test when open_basedir is set');
}

$this->setPath('');
putenv('PATH=');

$extraDirs = [\dirname(\PHP_BINARY)];

Expand All @@ -109,6 +98,7 @@ public function testFindWithOpenBaseDir()
$this->markTestSkipped('Cannot test when open_basedir is set');
}

putenv('PATH='.\dirname(\PHP_BINARY));
$initialOpenBaseDir = ini_set('open_basedir', \dirname(\PHP_BINARY).\PATH_SEPARATOR.'/');

try {
Expand All @@ -121,32 +111,6 @@ public function testFindWithOpenBaseDir()
}
}

/**
* @runInSeparateProcess
*/
public function testFindProcessInOpenBasedir()
{
if (\ini_get('open_basedir')) {
$this->markTestSkipped('Cannot test when open_basedir is set');
}
if ('\\' === \DIRECTORY_SEPARATOR) {
$this->markTestSkipped('Cannot run test on windows');
}

$this->setPath('');

$initialOpenBaseDir = ini_set('open_basedir', \PHP_BINARY.\PATH_SEPARATOR.'/');

try {
$finder = new ExecutableFinder();
$result = $finder->find($this->getPhpBinaryName(), false);

$this->assertSamePath(\PHP_BINARY, $result);
} finally {
ini_set('open_basedir', $initialOpenBaseDir);
}
}

public function testFindBatchExecutableOnWindows()
{
if (\ini_get('open_basedir')) {
Expand All @@ -163,7 +127,7 @@ public function testFindBatchExecutableOnWindows()

$this->assertFalse(is_executable($target));

$this->setPath(sys_get_temp_dir());
putenv('PATH='.sys_get_temp_dir());

$finder = new ExecutableFinder();
$result = $finder->find(basename($target), false);
Expand Down