Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[JsonPath] Better handling of unicode chars in expressions #60681

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Symfony/Component/JsonPath/JsonCrawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private function evaluateBracket(string $expr, mixed $value): array

// quoted strings for object keys
if (preg_match('/^([\'"])(.*)\1$/', $expr, $matches)) {
$key = stripslashes($matches[2]);
$key = JsonPathUtils::unescapeString($matches[2], $matches[1]);

return \array_key_exists($key, $value) ? [$value[$key]] : [];
}
Expand Down Expand Up @@ -335,7 +335,7 @@ private function evaluateScalar(string $expr, array $context): mixed

// string literals
if (preg_match('/^([\'"])(.*)\1$/', $expr, $matches)) {
return $matches[2];
return JsonPathUtils::unescapeString($matches[2], $matches[1]);
}

// current node references
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/JsonPath/JsonCrawlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface JsonCrawlerInterface
* @return list<array|string|float|int|bool|null>
*
* @throws InvalidArgumentException When the JSON string provided to the crawler cannot be decoded
* @throws JsonCrawlerException When a syntax error occurs in the provided JSON path
* @throws JsonCrawlerException When a syntax error occurs in the provided JSON path
*/
public function find(string|JsonPath $query): array;
}
6 changes: 3 additions & 3 deletions src/Symfony/Component/JsonPath/JsonPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ private function escapeKey(string $key): string
"\r" => '\\r',
"\t" => '\\t',
"\b" => '\\b',
"\f" => '\\f'
"\f" => '\\f',
]);

for ($i = 0; $i <= 31; $i++) {
for ($i = 0; $i <= 31; ++$i) {
if ($i < 8 || $i > 13) {
$key = str_replace(chr($i), sprintf('\\u%04x', $i), $key);
$key = str_replace(\chr($i), \sprintf('\\u%04x', $i), $key);
}
}

Expand Down
74 changes: 74 additions & 0 deletions src/Symfony/Component/JsonPath/JsonPathUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,78 @@
'tokens' => $remainingTokens,
];
}

public static function unescapeString(string $str, string $quoteChar): string
{
if ('"' === $quoteChar) {
// try JSON decoding first for unicode sequences
$jsonStr = '"'.$str.'"';
$decoded = json_decode($jsonStr, true);

if (null !== $decoded) {
return $decoded;
}
}

$result = '';
$length = \strlen($str);

for ($i = 0; $i < $length; ++$i) {
if ('\\' === $str[$i] && $i + 1 < $length) {
$result .= match ($str[$i + 1]) {
'"' => '"',
"'" => "'",
'\\' => '\\',
'/' => '/',
'b' => "\b",
'f' => "\f",
'n' => "\n",
'r' => "\r",
't' => "\t",
'u' => self::unescapeUnicodeSequence($str, $length, $i),
default => $str[$i].$str[$i + 1], // keep the backslash
};

++$i;
} else {
$result .= $str[$i];
}
}

return $result;
}

private static function unescapeUnicodeSequence(string $str, int $length, int &$i): string
{
if ($i + 5 >= $length) {
// not enough characters for Unicode escape, treat as literal
return $str[$i];
}

$hex = substr($str, $i + 2, 4);
if (!ctype_xdigit($hex)) {
// invalid hex, treat as literal
return $str[$i];
}

$codepoint = hexdec($hex);
// looks like a valid Unicode codepoint, string length is sufficient and it starts with \u
if (0xD800 <= $codepoint && $codepoint <= 0xDBFF && $i + 11 < $length && '\\' === $str[$i + 6] && 'u' === $str[$i + 7]) {
$lowHex = substr($str, $i + 8, 4);
if (ctype_xdigit($lowHex)) {
$lowSurrogate = hexdec($lowHex);
if (0xDC00 <= $lowSurrogate && $lowSurrogate <= 0xDFFF) {
$codepoint = 0x10000 + (($codepoint & 0x3FF) << 10) + ($lowSurrogate & 0x3FF);
$i += 10; // skip surrogate pair

return mb_chr($codepoint, 'UTF-8');

Check failure on line 152 in src/Symfony/Component/JsonPath/JsonPathUtils.php

View workflow job for this annotation

GitHub Actions / Psalm

FalsableReturnStatement

src/Symfony/Component/JsonPath/JsonPathUtils.php:152:28: FalsableReturnStatement: The declared return type 'string' for Symfony\Component\JsonPath\JsonPathUtils::unescapeUnicodeSequence does not allow false, but the function returns 'false|string' (see https://psalm.dev/137)

Check failure on line 152 in src/Symfony/Component/JsonPath/JsonPathUtils.php

View workflow job for this annotation

GitHub Actions / Psalm

FalsableReturnStatement

src/Symfony/Component/JsonPath/JsonPathUtils.php:152:28: FalsableReturnStatement: The declared return type 'string' for Symfony\Component\JsonPath\JsonPathUtils::unescapeUnicodeSequence does not allow false, but the function returns 'false|string' (see https://psalm.dev/137)
}
}
}

// single Unicode character or invalid surrogate, skip the sequence
$i += 4;

return mb_chr($codepoint, 'UTF-8');

Check failure on line 160 in src/Symfony/Component/JsonPath/JsonPathUtils.php

View workflow job for this annotation

GitHub Actions / Psalm

FalsableReturnStatement

src/Symfony/Component/JsonPath/JsonPathUtils.php:160:16: FalsableReturnStatement: The declared return type 'string' for Symfony\Component\JsonPath\JsonPathUtils::unescapeUnicodeSequence does not allow false, but the function returns 'false|string' (see https://psalm.dev/137)

Check failure on line 160 in src/Symfony/Component/JsonPath/JsonPathUtils.php

View workflow job for this annotation

GitHub Actions / Psalm

FalsableReturnStatement

src/Symfony/Component/JsonPath/JsonPathUtils.php:160:16: FalsableReturnStatement: The declared return type 'string' for Symfony\Component\JsonPath\JsonPathUtils::unescapeUnicodeSequence does not allow false, but the function returns 'false|string' (see https://psalm.dev/137)
}
}
Loading
Loading