Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 406bb09

Browse filesBrowse files
[Process] Fix ignoring of bad env var names
1 parent 346eacd commit 406bb09
Copy full SHA for 406bb09

File tree

2 files changed

+27
-15
lines changed
Filter options

2 files changed

+27
-15
lines changed

‎src/Symfony/Component/Process/Process.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Process/Process.php
+15-15Lines changed: 15 additions & 15 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
}
@@ -1104,10 +1107,7 @@ public function setEnv(array $env)
11041107
return !is_array($value);
11051108
});
11061109

1107-
$this->env = array();
1108-
foreach ($env as $key => $value) {
1109-
$this->env[$key] = (string) $value;
1110-
}
1110+
$this->env = $env;
11111111

11121112
return $this;
11131113
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Process/Tests/ProcessTest.php
+12Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.