8000 [Process] Decouple Process::findExecutable() from open_basedir by Luc45 · Pull Request #54151 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Process] Decouple Process::findExecutable() from open_basedir #54151

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Port PR #47422 to Symfony Process 5.4
  • Loading branch information
Luc45 committed Mar 4, 2024
commit f0e9c2cf83bfceeff3fd3d0d86729e2c398e2bb3
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
27 changes: 7 additions & 20 deletions src/Symfony/Component/Process/Tests/ExecutableFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,8 @@
*/
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);
protected function tearDown(): void {
putenv('PATH=' . ($_SERVER['PATH'] ?? $_SERVER['Path']));
}

public function testFind()
Expand All @@ -41,7 +29,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 +45,7 @@ public function testFindWithDefault()

$expected = 'defaultValue';

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

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

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

$finder = new ExecutableFinder();

Expand All @@ -86,7 +74,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 +117,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 +141,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
0