We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
1 | <?php |
||
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() |
|
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 | 45 | protected function gatherFragmentDefinition(ValidationContext $context) |
|
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 | // is disabled? |
||
75 | 82 | if (!$this->isEnabled()) { |
|
76 | 46 | return []; |
|
77 | } |
||
78 | |||
79 | 45 | $this->gatherFragmentDefinition($context); |
|
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 | /* @var Field $selection */ |
||
111 | 12 | $fieldName = $selection->name->value; |
|
112 | 12 | $fieldDef = null; |
|
113 | 12 | if ($parentType && method_exists($parentType, 'getFields')) { |
|
114 | 12 | $tmp = $parentType->getFields(); |
|
115 | 12 | $schemaMetaFieldDef = Introspection::schemaMetaFieldDef(); |
|
116 | 12 | $typeMetaFieldDef = Introspection::typeMetaFieldDef(); |
|
117 | 12 | $typeNameMetaFieldDef = Introspection::typeNameMetaFieldDef(); |
|
118 | |||
119 | 12 | if ($fieldName === $schemaMetaFieldDef->name && $context->getSchema()->getQueryType() === $parentType) { |
|
120 | 1 | $fieldDef = $schemaMetaFieldDef; |
|
121 | 12 | } elseif ($fieldName === $typeMetaFieldDef->name && $context->getSchema()->getQueryType() === $parentType) { |
|
122 | 1 | $fieldDef = $typeMetaFieldDef; |
|
123 | 12 | } elseif ($fieldName === $typeNameMetaFieldDef->name) { |
|
124 | 1 | $fieldDef = $typeNameMetaFieldDef; |
|
125 | 12 | } elseif (isset($tmp[$fieldName])) { |
|
126 | 12 | $fieldDef = $tmp[$fieldName]; |
|
127 | 12 | } |
|
128 | 12 | } |
|
129 | 12 | $responseName = $this->getFieldName($selection); |
|
130 | 12 | if (!isset($_astAndDefs[$responseName])) { |
|
131 | 12 | $_astAndDefs[$responseName] = new \ArrayObject(); |
|
132 | 12 | } |
|
133 | // create field context |
||
134 | 12 | $_astAndDefs[$responseName][] = [$selection, $fieldDef]; |
|
135 | 12 | break; |
|
136 | 3 | case Node::INLINE_FRAGMENT: |
|
137 | /* @var InlineFragment $selection */ |
||
138 | 1 | $_astAndDefs = $this->collectFieldASTsAndDefs( |
|
139 | 1 | $context, |
|
140 | 1 | TypeInfo::typeFromAST($context->getSchema(), $selection->typeCondition), |
|
141 | 1 | $selection->selectionSet, |
|
142 | 1 | $_visitedFragmentNames, |
|
143 | $_astAndDefs |
||
144 | 1 | ); |
|
145 | 1 | break; |
|
146 | 2 | case Node::FRAGMENT_SPREAD: |
|
147 | /* @var FragmentSpread $selection */ |
||
148 | 2 | $fragName = $selection->name->value; |
|
149 | |||
150 | 2 | if (empty($_visitedFragmentNames[$fragName])) { |
|
151 | 2 | $_visitedFragmentNames[$fragName] = true; |
|
152 | 2 | $fragment = $context->getFragment($fragName); |
|
153 | |||
154 | 2 | if ($fragment) { |
|
155 | 2 | $_astAndDefs = $this->collectFieldASTsAndDefs( |
|
156 | 2 | $context, |
|
157 | 2 | TypeInfo::typeFromAST($context->getSchema(), $fragment->typeCondition), |
|
158 | 2 | $fragment->selectionSet, |
|
159 | 2 | $_visitedFragmentNames, |
|
160 | $_astAndDefs |
||
161 | 2 | ); |
|
162 | 2 | } |
|
163 | 2 | } |
|
164 | 2 | break; |
|
165 | 12 | } |
|
166 | 12 | } |
|
167 | |||
168 | 12 | return $_astAndDefs; |
|
169 | } |
||
170 | |||
171 | 12 | protected function getFieldName(Field $node) |
|
172 | { |
||
173 | 12 | $fieldName = $node->name->value; |
|
174 | 12 | $responseName = $node->alias ? $node->alias->value : $fieldName; |
|
175 | |||
176 | 12 | return $responseName; |
|
177 | } |
||
178 | |||
179 | abstract protected function isEnabled(); |
||
180 | } |
||
181 |
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: