Skip to content

Commit e1bb5dc

Browse files
bug #62691 [JsonPath] Update and fix the compliance test suite (alexandre-daubois)
This PR was merged into the 7.3 branch. Discussion ---------- [JsonPath] Update and fix the compliance test suite | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT New cases have been added the compliance test suite and this PR fixes the failures. Commits ------- 710bbd4 [JsonPath] Update and fix the compliance test suite
2 parents 5b76ddf + 710bbd4 commit e1bb5dc

File tree

3 files changed

+364
-3
lines changed

3 files changed

+364
-3
lines changed

src/Symfony/Component/JsonPath/JsonCrawler.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ final class JsonCrawler implements JsonCrawlerInterface
4242
'value' => true,
4343
];
4444

45+
private const SINGULAR_ARGUMENT_FUNCTIONS = ['length', 'match', 'search'];
46+
4547
/**
4648
* Comparison operators and their corresponding lengths.
4749
*/
@@ -780,6 +782,10 @@ private function evaluateFunction(string $name, string $args, mixed $context): m
780782
$value = $argList[0] ?? null;
781783
$nodelistSize = $nodelistSizes[0] ?? 0;
782784

785+
if ($nodelistSize > 1 && \in_array($name, self::SINGULAR_ARGUMENT_FUNCTIONS, true)) {
786+
throw new JsonCrawlerException($args, \sprintf('non-singular query is not allowed as argument to "%s" function', $name));
787+
}
788+
783789
return match ($name) {
784790
'length' => match (true) {
785791
\is_string($value) => mb_strlen($value),
@@ -833,8 +839,8 @@ private function compare(mixed $left, mixed $right, string $operator): bool
833839

834840
private function compareEquality(mixed $left, mixed $right): bool
835841
{
836-
$leftIsNothing = $left === Nothing::Nothing;
837-
$rightIsNothing = $right === Nothing::Nothing;
842+
$leftIsNothing = Nothing::Nothing === $left;
843+
$rightIsNothing = Nothing::Nothing === $right;
838844

839845
if (
840846
$leftIsNothing && $rightIsNothing
@@ -1033,11 +1039,31 @@ private function validateFilterExpression(string $expr): void
10331039
throw new JsonCrawlerException($left, 'non-singular query is not comparable');
10341040
}
10351041

1042+
$this->validateFunctionArguments($left);
1043+
$this->validateFunctionArguments($right);
1044+
10361045
return;
10371046
}
10381047
}
10391048
}
10401049

1050+
private function validateFunctionArguments(string $expr): void
1051+
{
1052+
// is there a function call?
1053+
if (!preg_match('/^(\w+)\((.*)\)$/', trim($expr), $matches)) {
1054+
return;
1055+
}
1056+
1057+
if (!\in_array($functionName = $matches[1], self::SINGULAR_ARGUMENT_FUNCTIONS, true)) {
1058+
return;
1059+
}
1060+
1061+
$arg = trim($matches[2]);
1062+
if (str_starts_with($arg, '@') && $this->isNonSingularRelativeQuery($arg)) {
1063+
throw new JsonCrawlerException($arg, \sprintf('non-singular query is not allowed as argument to "%s" function', $functionName));
1064+
}
1065+
}
1066+
10411067
/**
10421068
* Transforms JSONPath regex patterns to comply with RFC 9485.
10431069
*

src/Symfony/Component/JsonPath/Tests/Fixtures/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
override hash := 05f6cac786bf0cce95437e6f1adedc3186d54a71
1+
override hash := b9d7153e58711ad38bb8e35ece69c13f4b2f7d63
22

33
.PHONY: cts.json
44
cts.json:

0 commit comments

Comments
 (0)