Skip to content

Commit e579464

Browse files
[Process] Ignore invalid env var names
1 parent 8541b73 commit e579464

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

Process.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public function start(?callable $callback = null, array $env = [])
331331

332332
$envPairs = [];
333333
foreach ($env as $k => $v) {
334-
if (false !== $v && false === \in_array($k, ['argc', 'argv', 'ARGC', 'ARGV'], true)) {
334+
if (false !== $v && !\in_array($k = (string) $k, ['', 'argc', 'argv', 'ARGC', 'ARGV'], true) && !str_contains($k, '=') && !str_contains($k, "\0")) {
335335
$envPairs[] = $k.'='.$v;
336336
}
337337
}

Tests/ProcessTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,46 @@ public function testEnvArgument()
15681568
$this->assertSame($env, $p->getEnv());
15691569
}
15701570

1571+
public function testEnvVarNamesCastToString()
1572+
{
1573+
$process = $this->getProcess('echo hello');
1574+
$process->setEnv([123 => 'value']);
1575+
1576+
$process->run();
1577+
1578+
$this->assertSame('hello'.\PHP_EOL, $process->getOutput());
1579+
}
1580+
1581+
public function testEnvVarNamesWithEqualsSigns()
1582+
{
1583+
$process = $this->getProcess('echo hello');
1584+
$process->setEnv(['VAR=NAME' => 'value']);
1585+
1586+
$process->run();
1587+
1588+
$this->assertSame('hello'.\PHP_EOL, $process->getOutput());
1589+
}
1590+
1591+
public function testEnvVarNamesWithNullBytes()
1592+
{
1593+
$process = $this->getProcess('echo hello');
1594+
$process->setEnv(["VAR\0NAME" => 'value']);
1595+
1596+
$process->run();
1597+
1598+
$this->assertSame('hello'.\PHP_EOL, $process->getOutput());
1599+
}
1600+
1601+
public function testEnvVarNamesEmpty()
1602+
{
1603+
$process = $this->getProcess('echo hello');
1604+
$process->setEnv(['' => 'value']);
1605+
1606+
$process->run();
1607+
1608+
$this->assertSame('hello'.\PHP_EOL, $process->getOutput());
1609+
}
1610+
15711611
public function testWaitStoppedDeadProcess()
15721612
{
15731613
$process = $this->getProcess(self::$phpBin.' '.__DIR__.'/ErrorProcessInitiator.php -e '.self::$phpBin);

0 commit comments

Comments
 (0)