diff --git a/src/Symfony/Component/Process/ExecutableFinder.php b/src/Symfony/Component/Process/ExecutableFinder.php index f392c962e3130..04c54d9d8e295 100644 --- a/src/Symfony/Component/Process/ExecutableFinder.php +++ b/src/Symfony/Component/Process/ExecutableFinder.php @@ -48,9 +48,14 @@ public function addSuffix(string $suffix) */ public function find(string $name, ?string $default = null, array $extraDirs = []) { + $dirs = array_merge( + explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')), + $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)) { @@ -61,11 +66,6 @@ public function find(string $name, ?string $default = null, array $extraDirs = [ } } } - } else { - $dirs = array_merge( - explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')), - $extraDirs - ); } $suffixes = ['']; diff --git a/src/Symfony/Component/Process/Tests/ExecutableFinderTest.php b/src/Symfony/Component/Process/Tests/ExecutableFinderTest.php index 6d089def27ad1..a90d22f985a6b 100644 --- a/src/Symfony/Component/Process/Tests/ExecutableFinderTest.php +++ b/src/Symfony/Component/Process/Tests/ExecutableFinderTest.php @@ -109,7 +109,13 @@ public function testFindWithOpenBaseDir() $this->markTestSkipped('Cannot test when open_basedir is set'); } - $initialOpenBaseDir = ini_set('open_basedir', \dirname(\PHP_BINARY).\PATH_SEPARATOR.'/'); + $openBaseDir = \dirname(\PHP_BINARY).\PATH_SEPARATOR.sys_get_temp_dir().\PATH_SEPARATOR.getcwd(); + + if ($_SERVER['SYMFONY_PHPUNIT_DIR'] ?? null) { + $openBaseDir .= \PATH_SEPARATOR.$_SERVER['SYMFONY_PHPUNIT_DIR']; + } + + $initialOpenBaseDir = ini_set('open_basedir', $openBaseDir); try { $finder = new ExecutableFinder(); @@ -121,6 +127,38 @@ public function testFindWithOpenBaseDir() } } + /** + * @runInSeparateProcess + */ + public function testFindWithSubdirectoryOfOpenBaseDir() + { + if (\ini_get('open_basedir')) { + $this->markTestSkipped('Cannot test when open_basedir is set'); + } + + $paths = explode(\PATH_SEPARATOR, getenv('PATH')); + $phpBinaryPath = \dirname(\PHP_BINARY); + + if (!in_array($phpBinaryPath, $paths, true)) { + $paths[] = $phpBinaryPath; + } + + $this->setPath(implode(\PATH_SEPARATOR, $paths)); + + $openBaseDir = \dirname(\dirname(\PHP_BINARY)).\PATH_SEPARATOR.sys_get_temp_dir().\PATH_SEPARATOR.getcwd(); + + if ($_SERVER['SYMFONY_PHPUNIT_DIR'] ?? null) { + $openBaseDir .= \PATH_SEPARATOR.$_SERVER['SYMFONY_PHPUNIT_DIR']; + } + + ini_set('open_basedir', $openBaseDir); + + $finder = new ExecutableFinder(); + $result = $finder->find($this->getPhpBinaryName()); + + $this->assertSamePath(\PHP_BINARY, $result); + } + /** * @runInSeparateProcess */