Skip to content

[Process] Fixed escaping arguments on Windows when inheritEnvironmentVariables is set to false #22614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public function start(callable $callback = null/*, array $env = array()*/)
}
if ('\\' === DIRECTORY_SEPARATOR && $this->enhanceWindowsCompatibility) {
$this->options['bypass_shell'] = true;
$commandline = $this->prepareWindowsCommandLine($commandline, $envBackup);
$commandline = $this->prepareWindowsCommandLine($commandline, $envBackup, $env);
} elseif (!$this->useFileHandles && $this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
// last exit code is output on the fourth pipe and caught to work around --enable-sigchild
$descriptors[3] = array('pipe', 'w');
Expand Down Expand Up @@ -1627,7 +1627,7 @@ private function doSignal($signal, $throwException)
return true;
}

private function prepareWindowsCommandLine($cmd, array &$envBackup)
private function prepareWindowsCommandLine($cmd, array &$envBackup, array &$env = null)
{
$uid = uniqid('', true);
$varCount = 0;
Expand All @@ -1640,7 +1640,7 @@ private function prepareWindowsCommandLine($cmd, array &$envBackup)
[^"%!^]*+
)++
)"/x',
function ($m) use (&$envBackup, &$varCache, &$varCount, $uid) {
function ($m) use (&$envBackup, &$env, &$varCache, &$varCount, $uid) {
if (isset($varCache[$m[0]])) {
return $varCache[$m[0]];
}
Expand All @@ -1652,10 +1652,15 @@ function ($m) use (&$envBackup, &$varCache, &$varCount, $uid) {
}

$value = str_replace(array('!LF!', '"^!"', '"^%"', '"^^"', '""'), array("\n", '!', '%', '^', '"'), $value);
$value = preg_replace('/(\\\\*)"/', '$1$1\\"', $value);

$value = '"'.preg_replace('/(\\\\*)"/', '$1$1\\"', $value).'"';
$var = $uid.++$varCount;
putenv("$var=\"$value\"");

if (null === $env) {
putenv("$var=$value");
} else {
$env[$var] = $value;
}

$envBackup[$var] = false;

return $varCache[$m[0]] = '!'.$var.'!';
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Process/Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,19 @@ public function testEscapeArgument($arg)
$this->assertSame($arg, $p->getOutput());
}

/**
* @dataProvider provideEscapeArgument
* @group legacy
*/
public function testEscapeArgumentWhenInheritEnvDisabled($arg)
{
$p = new Process(array(self::$phpBin, '-r', 'echo $argv[1];', $arg), null, array('BAR' => 'BAZ'));
$p->inheritEnvironmentVariables(false);
$p->run();

$this->assertSame($arg, $p->getOutput());
}

public function provideEscapeArgument()
{
yield array('a"b%c%');
Expand Down