1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Request\Validator\Rule; |
13
|
|
|
|
14
|
|
|
use GraphQL\Error; |
15
|
|
|
use GraphQL\Language\AST\Field; |
16
|
|
|
use GraphQL\Language\AST\FragmentSpread; |
17
|
|
|
use GraphQL\Language\AST\InlineFragment; |
18
|
|
|
use GraphQL\Language\AST\Node; |
19
|
|
|
use GraphQL\Language\AST\OperationDefinition; |
20
|
|
|
use GraphQL\Language\AST\SelectionSet; |
21
|
|
|
use GraphQL\Validator\ValidationContext; |
22
|
|
|
|
23
|
|
|
class QueryDepth extends AbstractQuerySecurity |
24
|
|
|
{ |
25
|
|
|
const DEFAULT_QUERY_MAX_DEPTH = self::DISABLED; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
private static $maxQueryDepth; |
31
|
|
|
|
32
|
73 |
|
public function __construct($maxQueryDepth = self::DEFAULT_QUERY_MAX_DEPTH) |
33
|
|
|
{ |
34
|
73 |
|
$this->setMaxQueryDepth($maxQueryDepth); |
35
|
72 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set max query depth. If equal to 0 no check is done. Must be greater or equal to 0. |
39
|
|
|
* |
40
|
|
|
* @param $maxQueryDepth |
41
|
|
|
*/ |
42
|
73 |
|
public static function setMaxQueryDepth($maxQueryDepth) |
43
|
|
|
{ |
44
|
73 |
|
self::checkIfGreaterOrEqualToZero('maxQueryDepth', $maxQueryDepth); |
45
|
|
|
|
46
|
72 |
|
self::$maxQueryDepth = (int) $maxQueryDepth; |
47
|
72 |
|
} |
48
|
|
|
|
49
|
72 |
|
public static function getMaxQueryDepth() |
50
|
|
|
{ |
51
|
72 |
|
return self::$maxQueryDepth; |
52
|
|
|
} |
53
|
|
|
|
54
|
7 |
|
public static function maxQueryDepthErrorMessage($max, $count) |
55
|
|
|
{ |
56
|
7 |
|
return sprintf('Max query depth should be %d but got %d.', $max, $count); |
57
|
|
|
} |
58
|
|
|
|
59
|
72 |
|
public function __invoke(ValidationContext $context) |
60
|
|
|
{ |
61
|
72 |
|
return $this->invokeIfNeeded( |
62
|
72 |
|
$context, |
63
|
|
|
[ |
64
|
72 |
|
Node::OPERATION_DEFINITION => [ |
65
|
72 |
|
'leave' => function (OperationDefinition $operationDefinition) use ($context) { |
66
|
33 |
|
$maxDepth = $this->fieldDepth($operationDefinition); |
67
|
|
|
|
68
|
33 |
|
if ($maxDepth > $this->getMaxQueryDepth()) { |
69
|
7 |
|
return new Error($this->maxQueryDepthErrorMessage($this->getMaxQueryDepth(), $maxDepth)); |
70
|
|
|
} |
71
|
72 |
|
}, |
72
|
72 |
|
], |
73
|
|
|
] |
74
|
72 |
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
72 |
|
protected function isEnabled() |
78
|
|
|
{ |
79
|
72 |
|
return $this->getMaxQueryDepth() !== static::DISABLED; |
80
|
|
|
} |
81
|
|
|
|
82
|
33 |
View Code Duplication |
private function fieldDepth($node, $depth = 0, $maxDepth = 0) |
|
|
|
|
83
|
|
|
{ |
84
|
33 |
|
if (isset($node->selectionSet) && $node->selectionSet instanceof SelectionSet) { |
85
|
33 |
|
foreach ($node->selectionSet->selections as $childNode) { |
86
|
33 |
|
$maxDepth = $this->nodeDepth($childNode, $depth, $maxDepth); |
|
|
|
|
87
|
33 |
|
} |
88
|
33 |
|
} |
89
|
|
|
|
90
|
33 |
|
return $maxDepth; |
91
|
|
|
} |
92
|
|
|
|
93
|
33 |
|
private function nodeDepth(Node $node, $depth = 0, $maxDepth = 0) |
94
|
|
|
{ |
95
|
33 |
|
switch ($node->kind) { |
96
|
33 |
|
case Node::FIELD: |
97
|
|
|
/* @var Field $node */ |
98
|
|
|
// node has children? |
99
|
33 |
|
if (null !== $node->selectionSet) { |
|
|
|
|
100
|
|
|
// update maxDepth if needed |
101
|
33 |
|
if ($depth > $maxDepth) { |
102
|
31 |
|
$maxDepth = $depth; |
103
|
31 |
|
} |
104
|
33 |
|
$maxDepth = $this->fieldDepth($node, $depth + 1, $maxDepth); |
105
|
33 |
|
} |
106
|
33 |
|
break; |
107
|
|
|
|
108
|
21 |
|
case Node::INLINE_FRAGMENT: |
109
|
|
|
/* @var InlineFragment $node */ |
110
|
|
|
// node has children? |
111
|
10 |
|
if (null !== $node->selectionSet) { |
112
|
10 |
|
$maxDepth = $this->fieldDepth($node, $depth, $maxDepth); |
113
|
10 |
|
} |
114
|
10 |
|
break; |
115
|
|
|
|
116
|
11 |
|
case Node::FRAGMENT_SPREAD: |
117
|
|
|
/* @var FragmentSpread $node */ |
118
|
11 |
|
$fragment = $this->getFragment($node); |
119
|
|
|
|
120
|
11 |
|
if (null !== $fragment) { |
121
|
11 |
|
$maxDepth = $this->fieldDepth($fragment, $depth, $maxDepth); |
122
|
11 |
|
} |
123
|
11 |
|
break; |
124
|
33 |
|
} |
125
|
|
|
|
126
|
33 |
|
return $maxDepth; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.