bug #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 #8972

Commits
-------

de681cb [Process] Add tests on ProcessUtils::escapeArgument
85fb495 [Process] Fix: Arguments including space and quote are not correctly escaped (win)
This commit is contained in:
Fabien Potencier 2014-03-14 12:05:38 +01:00
commit fe91a2ca62
2 changed files with 12 additions and 1 deletions

View File

@ -46,6 +46,7 @@ class ProcessUtils
}
$escapedArgument = '';
$quote = false;
foreach (preg_split('/([%"])/i', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
if ('"' === $part) {
$escapedArgument .= '\\"';
@ -55,9 +56,17 @@ class ProcessUtils
if ('\\' === substr($part, -1)) {
$part .= '\\';
}
$escapedArgument .= escapeshellarg($part);
$part = escapeshellarg($part);
if ('"' === $part[0] && '"' === $part[strlen($part) - 1]) {
$part = substr($part, 1, -1);
$quote = true;
}
$escapedArgument .= $part;
}
}
if ($quote) {
$escapedArgument = '"'.$escapedArgument.'"';
}
return $escapedArgument;
}

View File

@ -27,6 +27,7 @@ class ProcessUtilsTest extends \PHPUnit_Framework_TestCase
{
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
return array(
array('"\"php\" \"-v\""', '"php" "-v"'),
array('"foo bar"', 'foo bar'),
array('^%"path"^%', '%path%'),
array('"<|>"\\"" "\\""\'f"', '<|>" "\'f'),
@ -36,6 +37,7 @@ class ProcessUtilsTest extends \PHPUnit_Framework_TestCase
}
return array(
array("'\"php\" \"-v\"'", '"php" "-v"'),
array("'foo bar'", 'foo bar'),
array("'%path%'", '%path%'),
array("'<|>\" \"'\\''f'", '<|>" "\'f'),