Skip to content

[Process] Support using Process::findExecutable() independently of open_basedir #47422

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
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Process/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `RunProcessMessage` and `RunProcessMessageHandler`
* Support using `Process::findExecutable()` independently of `open_basedir`

5.2.0
-----
Expand Down
32 changes: 13 additions & 19 deletions src/Symfony/Component/Process/ExecutableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,10 @@ public function addSuffix(string $suffix)
*/
public function find(string $name, string $default = null, array $extraDirs = []): ?string
{
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 @@ -80,9 +65,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 @@ -34,7 +34,7 @@ public function find(bool $includeArgs = true): string|false
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
24 changes: 6 additions & 18 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 string|false $path = false;

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 Down Expand Up @@ -129,7 +118,6 @@ public function testFindProcessInOpenBasedir()
$this->markTestSkipped('Cannot run test on windows');
}

$this->setPath('');
$this->iniSet('open_basedir', \PHP_BINARY.\PATH_SEPARATOR.'/');

$finder = new ExecutableFinder();
Expand All @@ -154,7 +142,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