Skip to content

[JsonPath] Fix parsing invalid Unicode codepoints #61199

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 1 commit into from
Jul 26, 2025
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
5 changes: 3 additions & 2 deletions src/Symfony/Component/JsonPath/JsonCrawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\JsonPath;

use Symfony\Component\JsonPath\Exception\InvalidArgumentException;
use Symfony\Component\JsonPath\Exception\InvalidJsonPathException;
use Symfony\Component\JsonPath\Exception\InvalidJsonStringInputException;
use Symfony\Component\JsonPath\Exception\JsonCrawlerException;
use Symfony\Component\JsonPath\Tokenizer\JsonPathToken;
Expand Down Expand Up @@ -83,7 +84,7 @@ private function evaluate(JsonPath $query): array
return $this->evaluateTokensOnDecodedData($tokens, $data);
} catch (InvalidArgumentException $e) {
throw $e;
} catch (\Throwable $e) {
} catch (InvalidJsonPathException $e) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catching Throwable is a terrible practice that needs great care
here it's certainly not justified

throw new JsonCrawlerException($query, $e->getMessage(), previous: $e);
}
}
Expand Down Expand Up @@ -329,7 +330,7 @@ private function evaluateBracket(string $expr, mixed $value): array
return \array_key_exists($key, $value) ? [$value[$key]] : [];
}

throw new \LogicException(\sprintf('Unsupported bracket expression "%s".', $expr));
throw new InvalidJsonPathException(\sprintf('Unsupported bracket expression "%s".', $expr));
}

private function evaluateFilter(string $expr, mixed $value): array
Expand Down
39 changes: 21 additions & 18 deletions src/Symfony/Component/JsonPath/JsonPathUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static function unescapeString(string $str, string $quoteChar): string
't' => "\t",
'u' => self::unescapeUnicodeSequence($str, $i),
$quoteChar => $quoteChar,
default => throw new JsonCrawlerException('', \sprintf('Invalid escape sequence "\\%s" in %s-quoted string', $str[$i + 1], "'" === $quoteChar ? 'single' : 'double')),
default => throw new JsonCrawlerException('', \sprintf('Invalid escape sequence "\\%s" in %s-quoted string.', $str[$i + 1], "'" === $quoteChar ? 'single' : 'double')),
};

++$i;
Expand All @@ -132,30 +132,33 @@ public static function unescapeString(string $str, string $quoteChar): string
private static function unescapeUnicodeSequence(string $str, int &$i): string
{
if (!isset($str[$i + 5]) || !ctype_xdigit(substr($str, $i + 2, 4))) {
throw new JsonCrawlerException('', 'Invalid unicode escape sequence');
throw new JsonCrawlerException('', 'Invalid unicode escape sequence.');
}

$hex = substr($str, $i + 2, 4);
$codepoint = hexdec(substr($str, $i + 2, 4));

$codepoint = hexdec($hex);
// looks like a valid Unicode codepoint, string length is sufficient and it starts with \u
if (0xD800 <= $codepoint && $codepoint <= 0xDBFF && isset($str[$i + 11]) && '\\' === $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');
}
}
if (0xD800 <= $codepoint
&& $codepoint <= 0xDBFF
&& isset($str[$i + 11])
&& '\\' === $str[$i + 6]
&& 'u' === $str[$i + 7]
&& ctype_xdigit($lowSurrogate = substr($str, $i + 8, 4))
&& 0xDC00 <= ($lowSurrogate = hexdec($lowSurrogate))
&& $lowSurrogate <= 0xDFFF
) {
$codepoint = 0x10000 + (($codepoint & 0x3FF) << 10) + ($lowSurrogate & 0x3FF);
$i += 10; // skip surrogate pair
} else {
// single Unicode character or invalid surrogate, skip the sequence
$i += 4;
}

// single Unicode character or invalid surrogate, skip the sequence
$i += 4;
if (false === $chr = mb_chr($codepoint, 'UTF-8')) {
throw new JsonCrawlerException('', \sprintf('Invalid Unicode codepoint: U+%04X.', $codepoint));
}

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

/**
Expand Down