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

Skip to content

Commit 3e1be89

Browse files
[Process] Fix ignoring of bad env var names
1 parent 346eacd commit 3e1be89

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,24 +266,25 @@ public function start(callable $callback = null)
266266
$this->callback = $this->buildCallback($callback);
267267
$this->hasCallback = null !== $callback;
268268
$descriptors = $this->getDescriptors();
269+
$inheritEnv = $this->inheritEnv;
269270

270271
$commandline = $this->commandline;
271-
$envline = '';
272272

273-
if (null !== $this->env && $this->inheritEnv) {
273+
$env = $this->env;
274+
$envBackup = array();
275+
if (null !== $env && $inheritEnv) {
274276
if ('\\' === DIRECTORY_SEPARATOR && !empty($this->options['bypass_shell']) && !$this->enhanceWindowsCompatibility) {
275277
throw new LogicException('The "bypass_shell" option must be false to inherit environment variables while enhanced Windows compatibility is off');
276278
}
277-
$env = '\\' === DIRECTORY_SEPARATOR ? '(SET %s)&&' : 'export %s;';
278-
foreach ($this->env as $k => $v) {
279-
$envline .= sprintf($env, ProcessUtils::escapeArgument("$k=$v"));
279+
280+
foreach ($env as $k => $v) {
281+
$envBackup[$k] = getenv($v);
282+
putenv(false === $v || null === $v ? $k : "$k=$v");
280283
}
281284
$env = null;
282-
} else {
283-
$env = $this->env;
284285
}
285286
if ('\\' === DIRECTORY_SEPARATOR && $this->enhanceWindowsCompatibility) {
286-
$commandline = 'cmd /V:ON /E:ON /D /C "('.$envline.$commandline.')';
287+
$commandline = 'cmd /V:ON /E:ON /D /C "('.$commandline.')';
287288
foreach ($this->processPipes->getFiles() as $offset => $filename) {
288289
$commandline .= ' '.$offset.'>'.ProcessUtils::escapeArgument($filename);
289290
}
@@ -297,18 +298,20 @@ public function start(callable $callback = null)
297298
$descriptors[3] = array('pipe', 'w');
298299

299300
// See https://unix.stackexchange.com/questions/71205/background-process-pipe-input
300-
$commandline = $envline.'{ ('.$this->commandline.') <&3 3<&- 3>/dev/null & } 3<&0;';
301+
$commandline = '{ ('.$this->commandline.') <&3 3<&- 3>/dev/null & } 3<&0;';
301302
$commandline .= 'pid=$!; echo $pid >&3; wait $pid; code=$?; echo $code >&3; exit $code';
302303

303304
// Workaround for the bug, when PTS functionality is enabled.
304305
// @see : https://bugs.php.net/69442
305306
$ptsWorkaround = fopen(__FILE__, 'r');
306-
} elseif ('' !== $envline) {
307-
$commandline = $envline.$commandline;
308307
}
309308

310309
$this->process = proc_open($commandline, $descriptors, $this->processPipes->pipes, $this->cwd, $env, $this->options);
311310

311+
foreach ($envBackup as $k => $v) {
312+
putenv(false === $v ? $k : "$k=$v");
313+
}
314+
312315
if (!is_resource($this->process)) {
313316
throw new RuntimeException('Unable to launch a new process.');
314317
}
@@ -1108,6 +1111,7 @@ public function setEnv(array $env)
11081111
foreach ($env as $key => $value) {
11091112
$this->env[$key] = (string) $value;
11101113
}
1114+
$this->env = $env;
11111115

11121116
return $this;
11131117
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,18 @@ public function testChainedProcesses()
13911391
$this->assertSame('456', $p2->getOutput());
13921392
}
13931393

1394+
public function testSetBadEnv()
1395+
{
1396+
$process = $this->getProcess('echo hello');
1397+
$process->setEnv(array('bad%%' => '123'));
1398+
$process->inheritEnvironmentVariables(true);
1399+
1400+
$process->run();
1401+
1402+
$this->assertSame('hello'.PHP_EOL, $process->getOutput());
1403+
$this->assertSame('', $process->getErrorOutput());
1404+
}
1405+
13941406
public function testInheritEnvEnabled()
13951407
{
13961408
$process = $this->getProcess(self::$phpBin.' -r '.escapeshellarg('echo serialize($_SERVER);'), null, array('BAR' => 'BAZ'));

0 commit comments

Comments
 (0)