Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30df6fc

Browse files
committed
do not search in $PATH entries not allowed by open_basedir
1 parent 5d0fa8e commit 30df6fc

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/Symfony/Component/Process/ExecutableFinder.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,23 @@ public function addSuffix(string $suffix)
4848
*/
4949
public function find(string $name, ?string $default = null, array $extraDirs = [])
5050
{
51+
$dirs = array_merge(
52+
explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
53+
$extraDirs
54+
);
55+
5156
if (\ini_get('open_basedir')) {
5257
$searchPath = array_merge(explode(\PATH_SEPARATOR, \ini_get('open_basedir')), $extraDirs);
53-
$dirs = [];
58+
foreach ($dirs as $index => $dir) {
59+
foreach ($searchPath as $path) {
60+
if (str_starts_with($dir, $path)) {
61+
continue 2;
62+
}
63+
}
64+
65+
unset($dirs[$index]);
66+
}
67+
5468
foreach ($searchPath as $path) {
5569
// Silencing against https://bugs.php.net/69240
5670
if (@is_dir($path)) {
@@ -61,11 +75,6 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
6175
}
6276
}
6377
}
64-
} else {
65-
$dirs = array_merge(
66-
explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
67-
$extraDirs
68-
);
6978
}
7079

7180
$suffixes = [''];

src/Symfony/Component/Process/Tests/ExecutableFinderTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,38 @@ public function testFindWithOpenBaseDir()
109109
$this->markTestSkipped('Cannot test when open_basedir is set');
110110
}
111111

112-
$initialOpenBaseDir = ini_set('open_basedir', \dirname(\PHP_BINARY).\PATH_SEPARATOR.'/');
112+
$initialOpenBaseDir = ini_set('open_basedir', \dirname(\PHP_BINARY).\PATH_SEPARATOR.sys_get_temp_dir().\PATH_SEPARATOR.getcwd());
113113

114114
try {
115115
$finder = new ExecutableFinder();
116116
$result = $finder->find($this->getPhpBinaryName());
117-
118117
$this->assertSamePath(\PHP_BINARY, $result);
119118
} finally {
120119
ini_set('open_basedir', $initialOpenBaseDir);
121120
}
122121
}
123122

123+
/**
124+
* @runInSeparateProcess
125+
*/
126+
public function testFindWithSubdirectoryOfOpenBaseDir()
127+
{
128+
if ('\\' === \DIRECTORY_SEPARATOR) {
129+
$this->markTestSkipped('Cannot run test on windows');
130+
}
131+
132+
if (\ini_get('open_basedir')) {
133+
$this->markTestSkipped('Cannot test when open_basedir is set');
134+
}
135+
136+
ini_set('open_basedir', \dirname(\dirname(\PHP_BINARY)).\PATH_SEPARATOR.sys_get_temp_dir().\PATH_SEPARATOR.getcwd());
137+
138+
$finder = new ExecutableFinder();
139+
$result = $finder->find($this->getPhpBinaryName());
140+
141+
$this->assertSamePath(\PHP_BINARY, $result);
142+
}
143+
124144
/**
125145
* @runInSeparateProcess
126146
*/

0 commit comments

Comments
 (0)