Skip to content

Commit ccd09b4

Browse files
committed
Work around open_basedir redirections when overwriting inherited FDs
1 parent 08416df commit ccd09b4

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/Factory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,12 @@ private function openProcessIo($filename, $flags = null)
254254
\defined('STDERR') ? \STDERR : \fopen('php://stderr', 'w')
255255
);
256256

257-
// do not inherit open FDs by explicitly overwriting existing FDs with dummy files
257+
// do not inherit open FDs by explicitly overwriting existing FDs with dummy files.
258+
// Accessing /dev/null with null spec requires PHP 7.4+, older PHP versions may be restricted due to open_basedir, so let's reuse STDERR here.
258259
// additionally, close all dummy files in the child process again
259260
foreach ($fds as $fd) {
260261
if ($fd > 2) {
261-
$pipes[$fd] = array('file', '/dev/null', 'r');
262+
$pipes[$fd] = \PHP_VERSION_ID >= 70400 ? ['null'] : $pipes[2];
262263
$command .= ' ' . $fd . '>&-';
263264
}
264265
}

tests/FunctionalExampleTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,20 @@ public function testQueryExampleExecutedWithCgiReturnsDefaultValueAfterContentTy
3131
$this->assertStringEndsWith("\r\n\r\n" . 'value' . PHP_EOL . '42' . PHP_EOL, $output);
3232
}
3333

34-
public function testQueryExampleWithOpenBasedirRestrictedRunsDefaultPhpAndReturnsDefaultValue()
34+
public function testQueryExampleWithOpenBasedirRestrictedReturnsDefaultValue()
3535
{
36-
if (!$this->canExecute('php --version')) {
37-
$this->markTestSkipped('Unable to execute "php"');
36+
$output = $this->execExample(escapeshellarg(PHP_BINARY) . ' -dopen_basedir=' . escapeshellarg(dirname(__DIR__)) . ' query.php');
37+
38+
$this->assertEquals('value' . PHP_EOL . '42' . PHP_EOL, $output);
39+
}
40+
41+
public function testQueryExampleWithOpenBasedirRestrictedAndAdditionalFileDescriptorReturnsDefaultValue()
42+
{
43+
if (DIRECTORY_SEPARATOR === '\\') {
44+
$this->markTestSkipped('Not supported on Windows');
3845
}
3946

40-
$output = $this->execExample(escapeshellarg(PHP_BINARY) . ' -dopen_basedir=' . escapeshellarg(dirname(__DIR__)) . ' query.php');
47+
$output = $this->execExample(escapeshellarg(PHP_BINARY) . ' -dopen_basedir=' . escapeshellarg(dirname(__DIR__)) . ' query.php 3</dev/null');
4148

4249
$this->assertEquals('value' . PHP_EOL . '42' . PHP_EOL, $output);
4350
}

0 commit comments

Comments
 (0)