Skip to content

Commit 85fb495

Browse files
mishak87romainneutron
authored andcommitted
[Process] Fix: Arguments including space and quote are not correctly escaped (win)
Bad `"bin" "command" \""bin"\"" "\""another command"\"` Better `"bin" "command" "\"bin" \"another command\""`
1 parent 9213a82 commit 85fb495

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-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
}

0 commit comments

Comments
 (0)