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\Language\AST\Field; |
15
|
|
|
use GraphQL\Language\AST\FragmentDefinition; |
16
|
|
|
use GraphQL\Language\AST\FragmentSpread; |
17
|
|
|
use GraphQL\Language\AST\InlineFragment; |
18
|
|
|
use GraphQL\Language\AST\Node; |
19
|
|
|
use GraphQL\Language\AST\SelectionSet; |
20
|
|
|
use GraphQL\Type\Definition\Type; |
21
|
|
|
use GraphQL\Type\Introspection; |
22
|
|
|
use GraphQL\Utils\TypeInfo; |
23
|
|
|
use GraphQL\Validator\ValidationContext; |
24
|
|
|
|
25
|
|
|
abstract class AbstractQuerySecurity |
26
|
|
|
{ |
27
|
|
|
const DISABLED = 0; |
28
|
|
|
|
29
|
|
|
/** @var FragmentDefinition[] */ |
30
|
|
|
private $fragments = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return \GraphQL\Language\AST\FragmentDefinition[] |
34
|
|
|
*/ |
35
|
13 |
|
protected function getFragments() |
36
|
|
|
{ |
37
|
13 |
|
return $this->fragments; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* check if equal to 0 no check is done. Must be greater or equal to 0. |
42
|
|
|
* |
43
|
|
|
* @param $value |
44
|
|
|
*/ |
45
|
84 |
|
protected static function checkIfGreaterOrEqualToZero($name, $value) |
46
|
|
|
{ |
47
|
84 |
|
if ($value < 0) { |
48
|
2 |
|
throw new \InvalidArgumentException(sprintf('$%s argument must be greater or equal to 0.', $name)); |
49
|
|
|
} |
50
|
82 |
|
} |
51
|
|
|
|
52
|
82 |
|
protected function gatherFragmentDefinition(ValidationContext $context) |
53
|
|
|
{ |
54
|
|
|
// Gather all the fragment definition. |
55
|
|
|
// Importantly this does not include inline fragments. |
56
|
82 |
|
$definitions = $context->getDocument()->definitions; |
57
|
82 |
|
foreach ($definitions as $node) { |
58
|
82 |
|
if ($node instanceof FragmentDefinition) { |
59
|
15 |
|
$this->fragments[$node->name->value] = $node; |
60
|
15 |
|
} |
61
|
82 |
|
} |
62
|
82 |
|
} |
63
|
|
|
|
64
|
13 |
|
protected function getFragment(FragmentSpread $fragmentSpread) |
65
|
|
|
{ |
66
|
13 |
|
$spreadName = $fragmentSpread->name->value; |
67
|
13 |
|
$fragments = $this->getFragments(); |
68
|
|
|
|
69
|
13 |
|
return isset($fragments[$spreadName]) ? $fragments[$spreadName] : null; |
70
|
|
|
} |
71
|
|
|
|
72
|
82 |
|
protected function invokeIfNeeded(ValidationContext $context, array $validators) |
73
|
|
|
{ |
74
|
82 |
|
$this->gatherFragmentDefinition($context); |
75
|
|
|
|
76
|
|
|
// is disabled? |
77
|
82 |
|
if (!$this->isEnabled()) { |
78
|
46 |
|
return []; |
79
|
|
|
} |
80
|
|
|
|
81
|
45 |
|
return $validators; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Given a selectionSet, adds all of the fields in that selection to |
86
|
|
|
* the passed in map of fields, and returns it at the end. |
87
|
|
|
* |
88
|
|
|
* Note: This is not the same as execution's collectFields because at static |
89
|
|
|
* time we do not know what object type will be used, so we unconditionally |
90
|
|
|
* spread in all fragments. |
91
|
|
|
* |
92
|
|
|
* @see GraphQL\Validator\Rules\OverlappingFieldsCanBeMerged |
93
|
|
|
* |
94
|
|
|
* @param ValidationContext $context |
95
|
|
|
* @param Type|null $parentType |
96
|
|
|
* @param SelectionSet $selectionSet |
97
|
|
|
* @param \ArrayObject $visitedFragmentNames |
98
|
|
|
* @param \ArrayObject $astAndDefs |
99
|
|
|
* |
100
|
|
|
* @return \ArrayObject |
101
|
|
|
*/ |
102
|
12 |
|
protected function collectFieldASTsAndDefs(ValidationContext $context, $parentType, SelectionSet $selectionSet, \ArrayObject $visitedFragmentNames = null, \ArrayObject $astAndDefs = null) |
103
|
|
|
{ |
104
|
12 |
|
$_visitedFragmentNames = $visitedFragmentNames ?: new \ArrayObject(); |
105
|
12 |
|
$_astAndDefs = $astAndDefs ?: new \ArrayObject(); |
106
|
|
|
|
107
|
12 |
|
foreach ($selectionSet->selections as $selection) { |
108
|
12 |
|
switch ($selection->kind) { |
|
|
|
|
109
|
12 |
|
case Node::FIELD: |
110
|
12 |
|
$fieldName = $selection->name->value; |
|
|
|
|
111
|
12 |
|
$fieldDef = null; |
112
|
12 |
|
if ($parentType && method_exists($parentType, 'getFields')) { |
113
|
12 |
|
$tmp = $parentType->getFields(); |
|
|
|
|
114
|
12 |
|
$schemaMetaFieldDef = Introspection::schemaMetaFieldDef(); |
115
|
12 |
|
$typeMetaFieldDef = Introspection::typeMetaFieldDef(); |
116
|
12 |
|
$typeNameMetaFieldDef = Introspection::typeNameMetaFieldDef(); |
117
|
|
|
|
118
|
12 |
|
if ($fieldName === $schemaMetaFieldDef->name && $context->getSchema()->getQueryType() === $parentType) { |
119
|
1 |
|
$fieldDef = $schemaMetaFieldDef; |
120
|
12 |
|
} elseif ($fieldName === $typeMetaFieldDef->name && $context->getSchema()->getQueryType() === $parentType) { |
121
|
1 |
|
$fieldDef = $typeMetaFieldDef; |
122
|
12 |
|
} elseif ($fieldName === $typeNameMetaFieldDef->name) { |
123
|
1 |
|
$fieldDef = $typeNameMetaFieldDef; |
124
|
12 |
|
} elseif (isset($tmp[$fieldName])) { |
125
|
12 |
|
$fieldDef = $tmp[$fieldName]; |
126
|
12 |
|
} |
127
|
12 |
|
} |
128
|
12 |
|
$responseName = $this->getFieldName($selection); |
|
|
|
|
129
|
12 |
|
if (!isset($_astAndDefs[$responseName])) { |
130
|
12 |
|
$_astAndDefs[$responseName] = new \ArrayObject(); |
131
|
12 |
|
} |
132
|
|
|
// create field context |
133
|
12 |
|
$_astAndDefs[$responseName][] = [$selection, $fieldDef]; |
134
|
12 |
|
break; |
135
|
3 |
|
case Node::INLINE_FRAGMENT: |
136
|
|
|
/* @var InlineFragment $inlineFragment */ |
137
|
1 |
|
$_astAndDefs = $this->collectFieldASTsAndDefs( |
138
|
1 |
|
$context, |
139
|
1 |
|
TypeInfo::typeFromAST($context->getSchema(), $selection->typeCondition), |
|
|
|
|
140
|
1 |
|
$selection->selectionSet, |
|
|
|
|
141
|
1 |
|
$_visitedFragmentNames, |
142
|
|
|
$_astAndDefs |
143
|
1 |
|
); |
144
|
1 |
|
break; |
145
|
2 |
|
case Node::FRAGMENT_SPREAD: |
146
|
|
|
/* @var FragmentSpread $selection */ |
147
|
2 |
|
$fragName = $selection->name->value; |
148
|
|
|
|
149
|
2 |
|
if (empty($_visitedFragmentNames[$fragName])) { |
150
|
2 |
|
$_visitedFragmentNames[$fragName] = true; |
151
|
2 |
|
$fragment = $context->getFragment($fragName); |
152
|
|
|
|
153
|
2 |
|
if ($fragment) { |
154
|
2 |
|
$_astAndDefs = $this->collectFieldASTsAndDefs( |
155
|
2 |
|
$context, |
156
|
2 |
|
TypeInfo::typeFromAST($context->getSchema(), $fragment->typeCondition), |
|
|
|
|
157
|
2 |
|
$fragment->selectionSet, |
158
|
2 |
|
$_visitedFragmentNames, |
159
|
|
|
$_astAndDefs |
160
|
2 |
|
); |
161
|
2 |
|
} |
162
|
2 |
|
} |
163
|
2 |
|
break; |
164
|
12 |
|
} |
165
|
12 |
|
} |
166
|
|
|
|
167
|
12 |
|
return $_astAndDefs; |
168
|
|
|
} |
169
|
|
|
|
170
|
12 |
|
protected function getFieldName(Field $node) |
171
|
|
|
{ |
172
|
12 |
|
$fieldName = $node->name->value; |
173
|
12 |
|
$responseName = $node->alias ? $node->alias->value : $fieldName; |
174
|
|
|
|
175
|
12 |
|
return $responseName; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
abstract protected function isEnabled(); |
179
|
|
|
} |
180
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: