Skip to content

Commit fe91a2c

Browse files
committed
bug symfony#10448 [2.3][Process] Fix quoted arguments escaping (romainneutron)
This PR was merged into the 2.3 branch. Discussion ---------- [2.3][Process] Fix quoted arguments escaping | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT This PR replaces symfony#8972 Commits ------- de681cb [Process] Add tests on ProcessUtils::escapeArgument 85fb495 [Process] Fix: Arguments including space and quote are not correctly escaped (win)
2 parents 79e92f9 + de681cb commit fe91a2c

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Symfony/Component/Process/ProcessUtils.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public static function escapeArgument($argument)
4646
}
4747

4848
$escapedArgument = '';
49+
$quote = false;
4950
foreach (preg_split('/([%"])/i', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
5051
if ('"' === $part) {
5152
$escapedArgument .= '\\"';
@@ -55,9 +56,17 @@ public static function escapeArgument($argument)
5556
if ('\\' === substr($part, -1)) {
5657
$part .= '\\';
5758
}
58-
$escapedArgument .= escapeshellarg($part);
59+
$part = escapeshellarg($part);
60+
if ('"' === $part[0] && '"' === $part[strlen($part) - 1]) {
61+
$part = substr($part, 1, -1);
62+
$quote = true;
63+
}
64+
$escapedArgument .= $part;
5965
}
6066
}
67+
if ($quote) {
68+
$escapedArgument = '"'.$escapedArgument.'"';
69+
}
6170

6271
return $escapedArgument;
6372
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function dataArguments()
2727
{
2828
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
2929
return array(
30+
array('"\"php\" \"-v\""', '"php" "-v"'),
3031
array('"foo bar"', 'foo bar'),
3132
array('^%"path"^%', '%path%'),
3233
array('"<|>"\\"" "\\""\'f"', '<|>" "\'f'),
@@ -36,6 +37,7 @@ public function dataArguments()
3637
}
3738

3839
return array(
40+
array("'\"php\" \"-v\"'", '"php" "-v"'),
3941
array("'foo bar'", 'foo bar'),
4042
array("'%path%'", '%path%'),
4143
array("'<|>\" \"'\\''f'", '<|>" "\'f'),

0 commit comments

Comments
 (0)