Skip to content
Snippets Groups Projects

Strengthen RegEx for tokenization of argument strings

Merged mondrake requested to merge issue/imagemagick-3414601:3414601-II into 8.x-3.x
Files
6
@@ -156,8 +156,20 @@ class ImagemagickEventSubscriber implements EventSubscriberInterface {
@@ -156,8 +156,20 @@ class ImagemagickEventSubscriber implements EventSubscriberInterface {
protected function prependArguments(ImagemagickExecArguments $arguments) {
protected function prependArguments(ImagemagickExecArguments $arguments) {
// Add prepended arguments if needed.
// Add prepended arguments if needed.
if ($prepend = $this->imagemagickSettings->get('prepend')) {
if ($prepend = $this->imagemagickSettings->get('prepend')) {
preg_match_all('/[^\s]+/m', $prepend, $tokens);
// Split the prepend string in multiple space-separated tokens. Quotes,
$arguments->add($tokens[0], ArgumentMode::PreSource, 0);
// both " and ', can delimit tokens with spaces inside. Such tokens can
 
// contain escaped quotes too.
 
// @see https://stackoverflow.com/questions/366202/regex-for-splitting-a-string-using-space-when-not-surrounded-by-single-or-double
 
// @see https://stackoverflow.com/questions/6525556/regular-expression-to-match-escaped-characters-quotes
 
$re = '/[^\s"\']+|"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'/m';
 
preg_match_all($re, $prepend, $tokens, PREG_SET_ORDER);
 
$args = [];
 
foreach ($tokens as $token) {
 
// The escape character needs to be removed, Symfony Process will
 
// escape the quote character again.
 
$args[] = str_replace("\\", "", end($token));
 
}
 
$arguments->add($args, ArgumentMode::PreSource, 0);
}
}
}
}
Loading