diff --git a/src/Validator/Rules/QueryComplexity.php b/src/Validator/Rules/QueryComplexity.php index 62dbe31a6..fd37719bc 100644 --- a/src/Validator/Rules/QueryComplexity.php +++ b/src/Validator/Rules/QueryComplexity.php @@ -52,6 +52,9 @@ class QueryComplexity extends QuerySecurityRule */ private bool $variableCoercionFailed = false; + /** @var array Complexity of each fragment by name; reset per document */ + private array $fragmentComplexities = []; + /** @throws \InvalidArgumentException */ public function __construct(int $maxQueryComplexity) { @@ -66,6 +69,7 @@ public function getVisitor(QueryValidationContext $context): array $this->fieldNodeAndDefs = new \ArrayObject(); $this->coercedVariableValues = null; $this->variableCoercionFailed = false; + $this->fragmentComplexities = []; return $this->invokeIfNeeded( $context, @@ -184,7 +188,8 @@ protected function nodeComplexity(SelectionNode $node): int $fragment = $this->getFragment($node); if ($fragment !== null) { - return $this->fieldComplexity($fragment->selectionSet); + // Score each fragment once, so spreading it repeatedly does not re-walk it + return $this->fragmentComplexities[$fragment->name->value] ??= $this->fieldComplexity($fragment->selectionSet); } } diff --git a/src/Validator/Rules/QueryDepth.php b/src/Validator/Rules/QueryDepth.php index 2ea3f17c5..7f9119d2a 100644 --- a/src/Validator/Rules/QueryDepth.php +++ b/src/Validator/Rules/QueryDepth.php @@ -18,6 +18,12 @@ class QueryDepth extends QuerySecurityRule /** @var array Fragment names which are already calculated in recursion */ protected array $calculatedFragments = []; + /** @var array Depth of each fragment relative to where it is spread, -1 when it has no nested selections; reset per document */ + private array $fragmentDepths = []; + + /** @var array Fragment names which contain a cycle; reset per document */ + private array $cyclicFragments = []; + protected int $maxQueryDepth; /** @throws \InvalidArgumentException */ @@ -28,6 +34,9 @@ public function __construct(int $maxQueryDepth) public function getVisitor(QueryValidationContext $context): array { + $this->fragmentDepths = []; + $this->cyclicFragments = []; + return $this->invokeIfNeeded( $context, [ @@ -86,13 +95,26 @@ protected function nodeDepth(Node $node, int $depth = 0, int $maxDepth = 0): int if ($fragment !== null) { $name = $fragment->name->value; - if (isset($this->calculatedFragments[$name])) { + if (isset($this->calculatedFragments[$name]) || isset($this->cyclicFragments[$name])) { + $this->cyclicFragments += $this->calculatedFragments; + return $this->maxQueryDepth + 1; } - $this->calculatedFragments[$name] = true; - $maxDepth = $this->fieldDepth($fragment, $depth, $maxDepth); - unset($this->calculatedFragments[$name]); + // Measure each fragment once, so spreading it repeatedly does not re-walk it + if (! isset($this->fragmentDepths[$name])) { + $this->calculatedFragments[$name] = true; + $fragmentDepth = $this->fieldDepth($fragment, 0, -1); + unset($this->calculatedFragments[$name]); + + if (isset($this->cyclicFragments[$name])) { + return $this->maxQueryDepth + 1; + } + + $this->fragmentDepths[$name] = $fragmentDepth; + } + + $maxDepth = max($maxDepth, $depth + $this->fragmentDepths[$name]); } break; diff --git a/tests/Validator/QueryComplexityTest.php b/tests/Validator/QueryComplexityTest.php index 659e8bd7a..82b15b3ab 100644 --- a/tests/Validator/QueryComplexityTest.php +++ b/tests/Validator/QueryComplexityTest.php @@ -74,6 +74,42 @@ public function fragmentQueriesOnRootProvider(): iterable yield ['query { ...humanFragment } fragment humanFragment on QueryRoot { human { dogs { name } } }']; } + /** @dataProvider repeatedFragmentSpreadsProvider */ + public function testRepeatedFragmentSpreads(int $levels): void + { + $query = "query MyQuery { human { ...F{$levels} } } fragment F0 on Human { firstName }"; + + foreach (range(1, $levels) as $level) { + $previous = $level - 1; + $query .= " fragment F{$level} on Human { dogs { master { ...F{$previous} ...F{$previous} } } }"; + } + + // human (1) + F{n}, where F0 = 1 and F{n} = dogs (10) + master (1) + 2 * F{n-1} + $complexity = 12 * 2 ** $levels - 10; + + $this->assertDocumentValidator($query, $complexity); + $this->assertDocumentValidator($query, $complexity - 1, [self::createFormattedError($complexity - 1, $complexity)]); + } + + /** @return iterable */ + public static function repeatedFragmentSpreadsProvider(): iterable + { + yield [1]; + yield [4]; + yield [50]; // Would never finish if each spread re-walked its fragment + } + + public function testRepeatedFragmentSpreadsWithDirectives(): void + { + $query = 'query MyQuery($withDogs: Boolean!) { human { ...F1 } jeremy: human(name: "Jeremy") { ...F1 ...F1 } } fragment F1 on Human { firstName dogs(name: "Root") @include(if: $withDogs) { name } }'; + + $this->getRule()->setRawVariableValues(['withDogs' => true]); + $this->assertDocumentValidators($query, 11, 12); + + $this->getRule()->setRawVariableValues(['withDogs' => false]); + $this->assertDocumentValidators($query, 5, 6); + } + public function testAliasesQueries(): void { $query = 'query MyQuery { thomas: human(name: "Thomas") { firstName } jeremy: human(name: "Jeremy") { firstName } }'; diff --git a/tests/Validator/QueryDepthTest.php b/tests/Validator/QueryDepthTest.php index 33c8ff183..39f49d5ee 100644 --- a/tests/Validator/QueryDepthTest.php +++ b/tests/Validator/QueryDepthTest.php @@ -99,6 +99,57 @@ public function testInfiniteRecursion(): void $this->assertDocumentValidator($query, 7, [self::createFormattedError(7, 8)]); } + public function testFragmentSpreadAtDifferentDepths(): void + { + $query = 'query MyQuery { human { ...F1 dogs { master { ...F1 } } } } fragment F1 on Human { dogs { name } }'; + + $this->assertDocumentValidator($query, 3); + $this->assertDocumentValidator($query, 2, [self::createFormattedError(2, 3)]); + } + + /** @dataProvider repeatedFragmentSpreadsProvider */ + public function testRepeatedFragmentSpreads(int $levels): void + { + $query = $this->buildRepeatedFragmentSpreadsQuery($levels); + $depth = 2 * $levels; + + $this->assertDocumentValidator($query, $depth); + $this->assertDocumentValidator($query, $depth - 1, [self::createFormattedError($depth - 1, $depth)]); + } + + /** @return iterable */ + public static function repeatedFragmentSpreadsProvider(): iterable + { + yield [1]; + yield [4]; + yield [50]; // Would never finish if each spread re-walked its fragment + } + + /** Each fragment spreads the one below it twice, so the expanded query doubles in size per level. */ + private function buildRepeatedFragmentSpreadsQuery(int $levels): string + { + $query = "query MyQuery { human { ...F{$levels} } } fragment F0 on Human { firstName }"; + + foreach (range(1, $levels) as $level) { + $previous = $level - 1; + $query .= " fragment F{$level} on Human { dogs { master { ...F{$previous} ...F{$previous} } } }"; + } + + return $query; + } + + public function testInfiniteRecursionWithRepeatedFragmentSpreads(): void + { + $query = 'query MyQuery { human { ...F0 } } fragment F50 on Human { ...F0 }'; + + foreach (range(0, 49) as $level) { + $next = $level + 1; + $query .= " fragment F{$level} on Human { ...F{$next} ...F{$next} }"; + } + + $this->assertDocumentValidator($query, 7, [self::createFormattedError(7, 8)]); + } + /** @return iterable>}> */ public static function queryDataProvider(): iterable {