-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[JsonPath] Improve escape sequence validation in name selector #60802
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
base: 7.3
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Component\JsonPath; | ||
|
||
use Symfony\Component\JsonPath\Exception\InvalidArgumentException; | ||
use Symfony\Component\JsonPath\Exception\JsonCrawlerException; | ||
use Symfony\Component\JsonPath\Tokenizer\JsonPathToken; | ||
use Symfony\Component\JsonPath\Tokenizer\TokenType; | ||
use Symfony\Component\JsonStreamer\Read\Splitter; | ||
|
@@ -86,8 +87,13 @@ public static function findSmallestDeserializableStringAndPath(array $tokens, mi | |
]; | ||
} | ||
|
||
/** | ||
* @throws JsonCrawlerException When an invalid Unicode escape sequence occurs | ||
*/ | ||
public static function unescapeString(string $str, string $quoteChar): string | ||
{ | ||
self::validateEscapeSequences($str, $quoteChar); | ||
|
||
if ('"' === $quoteChar) { | ||
// try JSON decoding first for unicode sequences | ||
$jsonStr = '"'.$str.'"'; | ||
|
@@ -108,7 +114,7 @@ public static function unescapeString(string $str, string $quoteChar): string | |
"'" => "'", | ||
'\\' => '\\', | ||
'/' => '/', | ||
'b' => "\b", | ||
'b' => "\x08", | ||
'f' => "\f", | ||
'n' => "\n", | ||
'r' => "\r", | ||
|
@@ -227,4 +233,24 @@ public static function parseCommaSeparatedValues(string $expr): array | |
|
||
return $parts; | ||
} | ||
|
||
private static function validateEscapeSequences(string $str, string $quoteChar): void | ||
{ | ||
$i = -1; | ||
while (null !== $char = $str[++$i] ?? null) { | ||
if ('\\' !== $char || !isset($str[$i + 1])) { | ||
continue; | ||
} | ||
|
||
if (!\in_array($next = $str[$i + 1], [$quoteChar, '\\', '/', 'b', 'f', 'n', 'r', 't', 'u'], true)) { | ||
throw new JsonCrawlerException('', \sprintf('Invalid escape sequence "\\%s" in %s-quoted string', $next, "'" === $quoteChar ? 'single' : 'double')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm quite sure this case could be detected during the unescaping loop, as this exception corresponds to the |
||
} | ||
|
||
if ('u' === $next && (!isset($str[$i + 5]) || !ctype_xdigit(substr($str, $i + 2, 4)))) { | ||
throw new JsonCrawlerException('', 'Invalid unicode escape sequence'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
++$i; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it make sense to merge the new loop with the existing one? can we do everything in one pass?