Skip to content

Commit e66a68c

Browse files
committed
feature #47422 [Process] Support using Process::findExecutable() independently of open_basedir (BlackbitDevs)
This PR was merged into the 6.4 branch. Discussion ---------- [Process] Support using `Process::findExecutable()` independently of `open_basedir` | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #41006 | License | MIT With this PR `Console::findExecutable()` will find executables also if the their path is not allowed in `open_basedir` config similar to https://github.com/symfony/symfony/blob/ddaedd28bd2e4fc22b64567753cf934fe9d68c4c/src/Symfony/Component/Process/PhpExecutableFinder.php#L36-L41 `Console::findExecutable()`'s responsibility is to find an executable which can be called with a Symfony Process or by PHP's functions like `exec`, `system` etc. The goal of PHP's `open_basedir` config is to restrict reading / writing files within PHP processes. Imho this is completely independent of finding an executable. If PHP's intention was to restrict executing applications which are not present in `open_basedir`'s paths, it would have been implemented there. Commits ------- 5af7d53 [Process] Support finding executables independently of open_basedir
2 parents 268a359 + 5af7d53 commit e66a68c

File tree

4 files changed

+21
-38
lines changed

4 files changed

+21
-38
lines changed

src/Symfony/Component/Process/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

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

910
5.2.0
1011
-----

src/Symfony/Component/Process/ExecutableFinder.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,10 @@ public function addSuffix(string $suffix)
5050
*/
5151
public function find(string $name, string $default = null, array $extraDirs = []): ?string
5252
{
53-
if (\ini_get('open_basedir')) {
54-
$searchPath = array_merge(explode(\PATH_SEPARATOR, \ini_get('open_basedir')), $extraDirs);
55-
$dirs = [];
56-
foreach ($searchPath as $path) {
57-
// Silencing against https://bugs.php.net/69240
58-
if (@is_dir($path)) {
59-
$dirs[] = $path;
60-
} else {
61-
if (basename($path) == $name && @is_executable($path)) {
62-
return $path;
63-
}
64-
}
65-
}
66-
} else {
67-
$dirs = array_merge(
68-
explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
69-
$extraDirs
70-
);
71-
}
53+
$dirs = array_merge(
54+
explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
55+
$extraDirs
56+
);
7257

7358
$suffixes = [''];
7459
if ('\\' === \DIRECTORY_SEPARATOR) {
@@ -80,9 +65,18 @@ public function find(string $name, string $default = null, array $extraDirs = []
8065
if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) {
8166
return $file;
8267
}
68+
69+
if (!@is_dir($dir) && basename($dir) === $name.$suffix && @is_executable($dir)) {
70+
return $dir;
71+
}
8372
}
8473
}
8574

75+
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v';
76+
if (\function_exists('exec') && ($executablePath = strtok(@exec($command.' '.escapeshellarg($name)), \PHP_EOL)) && is_executable($executablePath)) {
77+
return $executablePath;
78+
}
79+
8680
return $default;
8781
}
8882
}

src/Symfony/Component/Process/PhpExecutableFinder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function find(bool $includeArgs = true): string|false
3434
if ($php = getenv('PHP_BINARY')) {
3535
if (!is_executable($php)) {
3636
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v';
37-
if ($php = strtok(exec($command.' '.escapeshellarg($php)), \PHP_EOL)) {
37+
if (\function_exists('exec') && $php = strtok(exec($command.' '.escapeshellarg($php)), \PHP_EOL)) {
3838
if (!is_executable($php)) {
3939
return false;
4040
}

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,9 @@
1919
*/
2020
class ExecutableFinderTest extends TestCase
2121
{
22-
private string|false $path = false;
23-
2422
protected function tearDown(): void
2523
{
26-
if ($this->path) {
27-
// Restore path if it was changed.
28-
putenv('PATH='.$this->path);
29-
}
30-
}
31-
32-
private function setPath($path)
33-
{
34-
$this->path = getenv('PATH');
35-
putenv('PATH='.$path);
24+
putenv('PATH='.($_SERVER['PATH'] ?? $_SERVER['Path']));
3625
}
3726

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

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

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

5847
$expected = 'defaultValue';
5948

60-
$this->setPath('');
49+
putenv('PATH=');
6150

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

74-
$this->setPath('');
63+
putenv('PATH=');
7564

7665
$finder = new ExecutableFinder();
7766

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

89-
$this->setPath('');
78+
putenv('PATH=');
9079

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

@@ -129,7 +118,6 @@ public function testFindProcessInOpenBasedir()
129118
$this->markTestSkipped('Cannot run test on windows');
130119
}
131120

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

135123
$finder = new ExecutableFinder();
@@ -154,7 +142,7 @@ public function testFindBatchExecutableOnWindows()
154142

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

157-
$this->setPath(sys_get_temp_dir());
145+
putenv('PATH='.sys_get_temp_dir());
158146

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy