Skip to content

Commit

Permalink
[Process] Don't return executable directories in PhpExecutableFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyweb committed Mar 8, 2022
1 parent bea4df7 commit 947152b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
10 changes: 7 additions & 3 deletions src/Symfony/Component/Process/PhpExecutableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public function find($includeArgs = true)
}
}

if (@is_dir($php)) {
return false;
}

return $php;
}

Expand All @@ -59,20 +63,20 @@ public function find($includeArgs = true)
}

if ($php = getenv('PHP_PATH')) {
if (!@is_executable($php)) {
if (!@is_executable($php) || @is_dir($php)) {
return false;
}

return $php;
}

if ($php = getenv('PHP_PEAR_PHP_BIN')) {
if (@is_executable($php)) {
if (@is_executable($php) && !@is_dir($php)) {
return $php;
}
}

if (@is_executable($php = \PHP_BINDIR.('\\' === \DIRECTORY_SEPARATOR ? '\\php.exe' : '/php'))) {
if (@is_executable($php = \PHP_BINDIR.('\\' === \DIRECTORY_SEPARATOR ? '\\php.exe' : '/php')) && !@is_dir($php)) {
return $php;
}

Expand Down
30 changes: 25 additions & 5 deletions src/Symfony/Component/Process/Tests/PhpExecutableFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,32 @@ public function testFindArguments()
public function testNotExitsBinaryFile()
{
$f = new PhpExecutableFinder();
$phpBinaryEnv = \PHP_BINARY;
putenv('PHP_BINARY=/usr/local/php/bin/php-invalid');

$this->assertFalse($f->find(), '::find() returns false because of not exist file');
$this->assertFalse($f->find(false), '::find(false) returns false because of not exist file');
$originalPhpBinary = getenv('PHP_BINARY');

putenv('PHP_BINARY='.$phpBinaryEnv);
try {
putenv('PHP_BINARY=/usr/local/php/bin/php-invalid');

$this->assertFalse($f->find(), '::find() returns false because of not exist file');
$this->assertFalse($f->find(false), '::find(false) returns false because of not exist file');
} finally {
putenv('PHP_BINARY='.$originalPhpBinary);
}
}

public function testFindWithExecutableDirectory()
{
$originalPhpBinary = getenv('PHP_BINARY');

try {
$executableDirectoryPath = sys_get_temp_dir().'/PhpExecutableFinderTest_testFindWithExecutableDirectory';
@mkdir($executableDirectoryPath);
$this->assertTrue(is_executable($executableDirectoryPath));
putenv('PHP_BINARY='.$executableDirectoryPath);

$this->assertFalse((new PhpExecutableFinder())->find());
} finally {
putenv('PHP_BINARY='.$originalPhpBinary);
}
}
}

0 comments on commit 947152b

Please sign in to comment.