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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / lib / packages / GraphQL / Validator / Rules / QuerySecurityRule.php
woocommerce / lib / packages / GraphQL / Validator / Rules Last commit date
CustomValidationRule.php 2 months ago DisableIntrospection.php 2 months ago ExecutableDefinitions.php 2 months ago FieldsOnCorrectType.php 2 months ago FragmentsOnCompositeTypes.php 2 months ago KnownArgumentNames.php 2 months ago KnownArgumentNamesOnDirectives.php 2 months ago KnownDirectives.php 2 months ago KnownFragmentNames.php 2 months ago KnownTypeNames.php 2 months ago LoneAnonymousOperation.php 2 months ago LoneSchemaDefinition.php 2 months ago NoFragmentCycles.php 2 months ago NoUndefinedVariables.php 2 months ago NoUnusedFragments.php 2 months ago NoUnusedVariables.php 2 months ago OneOfInputObjectsRule.php 2 months ago OverlappingFieldsCanBeMerged.php 2 months ago PossibleFragmentSpreads.php 2 months ago PossibleTypeExtensions.php 2 months ago ProvidedRequiredArguments.php 2 months ago ProvidedRequiredArgumentsOnDirectives.php 2 months ago QueryComplexity.php 2 months ago QueryDepth.php 2 months ago QuerySecurityRule.php 2 months ago ScalarLeafs.php 2 months ago SingleFieldSubscription.php 2 months ago UniqueArgumentDefinitionNames.php 2 months ago UniqueArgumentNames.php 2 months ago UniqueDirectiveNames.php 2 months ago UniqueDirectivesPerLocation.php 2 months ago UniqueEnumValueNames.php 2 months ago UniqueFieldDefinitionNames.php 2 months ago UniqueFragmentNames.php 2 months ago UniqueInputFieldNames.php 2 months ago UniqueOperationNames.php 2 months ago UniqueOperationTypes.php 2 months ago UniqueTypeNames.php 2 months ago UniqueVariableNames.php 2 months ago ValidationRule.php 2 months ago ValuesOfCorrectType.php 2 months ago VariablesAreInputTypes.php 2 months ago VariablesInAllowedPosition.php 2 months ago
QuerySecurityRule.php
185 lines
1 <?php declare(strict_types=1);
2
3 namespace Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules;
4
5 use Automattic\WooCommerce\Vendor\GraphQL\Error\InvariantViolation;
6 use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\FieldNode;
7 use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\FragmentDefinitionNode;
8 use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\FragmentSpreadNode;
9 use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\InlineFragmentNode;
10 use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\SelectionSetNode;
11 use Automattic\WooCommerce\Vendor\GraphQL\Language\Visitor;
12 use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\FieldDefinition;
13 use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\HasFieldsType;
14 use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\Type;
15 use Automattic\WooCommerce\Vendor\GraphQL\Type\Introspection;
16 use Automattic\WooCommerce\Vendor\GraphQL\Utils\AST;
17 use Automattic\WooCommerce\Vendor\GraphQL\Validator\QueryValidationContext;
18
19 /**
20 * @see Visitor, FieldDefinition
21 *
22 * @phpstan-import-type VisitorArray from Visitor
23 *
24 * @phpstan-type ASTAndDefs \ArrayObject<string, \ArrayObject<int, array{FieldNode, FieldDefinition|null}>>
25 */
26 abstract class QuerySecurityRule extends ValidationRule
27 {
28 public const DISABLED = 0;
29
30 /** @var array<string, FragmentDefinitionNode> */
31 protected array $fragments = [];
32
33 /** @throws \InvalidArgumentException */
34 protected function checkIfGreaterOrEqualToZero(string $name, int $value): void
35 {
36 if ($value < 0) {
37 throw new \InvalidArgumentException("\${$name} argument must be greater or equal to 0.");
38 }
39 }
40
41 protected function getFragment(FragmentSpreadNode $fragmentSpread): ?FragmentDefinitionNode
42 {
43 return $this->fragments[$fragmentSpread->name->value] ?? null;
44 }
45
46 /** @return array<string, FragmentDefinitionNode> */
47 protected function getFragments(): array
48 {
49 return $this->fragments;
50 }
51
52 /**
53 * @phpstan-param VisitorArray $validators
54 *
55 * @phpstan-return VisitorArray
56 */
57 protected function invokeIfNeeded(QueryValidationContext $context, array $validators): array
58 {
59 if (! $this->isEnabled()) {
60 return [];
61 }
62
63 $this->gatherFragmentDefinition($context);
64
65 return $validators;
66 }
67
68 abstract protected function isEnabled(): bool;
69
70 protected function gatherFragmentDefinition(QueryValidationContext $context): void
71 {
72 // Gather all the fragment definition.
73 // Importantly this does not include inline fragments.
74 $definitions = $context->getDocument()->definitions;
75 foreach ($definitions as $node) {
76 if ($node instanceof FragmentDefinitionNode) {
77 $this->fragments[$node->name->value] = $node;
78 }
79 }
80 }
81
82 /**
83 * Given a selectionSet, adds all fields in that selection to
84 * the passed in map of fields, and returns it at the end.
85 *
86 * Note: This is not the same as execution's collectFields because at static
87 * time we do not know what object type will be used, so we unconditionally
88 * spread in all fragments.
89 *
90 * @see \Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules\OverlappingFieldsCanBeMerged
91 *
92 * @param \ArrayObject<string, true>|null $visitedFragmentNames
93 *
94 * @phpstan-param ASTAndDefs|null $astAndDefs
95 *
96 * @throws \Exception
97 * @throws \ReflectionException
98 * @throws InvariantViolation
99 *
100 * @phpstan-return ASTAndDefs
101 */
102 protected function collectFieldASTsAndDefs(
103 QueryValidationContext $context,
104 ?Type $parentType,
105 SelectionSetNode $selectionSet,
106 ?\ArrayObject $visitedFragmentNames = null,
107 ?\ArrayObject $astAndDefs = null
108 ): \ArrayObject {
109 $visitedFragmentNames ??= new \ArrayObject();
110 $astAndDefs ??= new \ArrayObject();
111
112 foreach ($selectionSet->selections as $selection) {
113 if ($selection instanceof FieldNode) {
114 $fieldName = $selection->name->value;
115
116 $fieldDef = null;
117 if ($parentType instanceof HasFieldsType) {
118 $schemaMetaFieldDef = Introspection::schemaMetaFieldDef();
119 $typeMetaFieldDef = Introspection::typeMetaFieldDef();
120 $typeNameMetaFieldDef = Introspection::typeNameMetaFieldDef();
121
122 $queryType = $context->getSchema()->getQueryType();
123
124 if ($fieldName === $schemaMetaFieldDef->name && $queryType === $parentType) {
125 $fieldDef = $schemaMetaFieldDef;
126 } elseif ($fieldName === $typeMetaFieldDef->name && $queryType === $parentType) {
127 $fieldDef = $typeMetaFieldDef;
128 } elseif ($fieldName === $typeNameMetaFieldDef->name) {
129 $fieldDef = $typeNameMetaFieldDef;
130 } elseif ($parentType->hasField($fieldName)) {
131 $fieldDef = $parentType->getField($fieldName);
132 }
133 }
134
135 $responseName = $this->getFieldName($selection);
136 $responseContext = $astAndDefs[$responseName] ??= new \ArrayObject();
137 $responseContext[] = [$selection, $fieldDef];
138 } elseif ($selection instanceof InlineFragmentNode) {
139 $typeCondition = $selection->typeCondition;
140 $fragmentParentType = $typeCondition === null
141 ? $parentType
142 : AST::typeFromAST([$context->getSchema(), 'getType'], $typeCondition);
143 $astAndDefs = $this->collectFieldASTsAndDefs(
144 $context,
145 $fragmentParentType,
146 $selection->selectionSet,
147 $visitedFragmentNames,
148 $astAndDefs
149 );
150 } elseif ($selection instanceof FragmentSpreadNode) {
151 $fragName = $selection->name->value;
152
153 if (isset($visitedFragmentNames[$fragName])) {
154 continue;
155 }
156 $visitedFragmentNames[$fragName] = true;
157
158 $fragment = $context->getFragment($fragName);
159 if ($fragment === null) {
160 continue;
161 }
162
163 $astAndDefs = $this->collectFieldASTsAndDefs(
164 $context,
165 AST::typeFromAST([$context->getSchema(), 'getType'], $fragment->typeCondition),
166 $fragment->selectionSet,
167 $visitedFragmentNames,
168 $astAndDefs
169 );
170 }
171 }
172
173 return $astAndDefs;
174 }
175
176 protected function getFieldName(FieldNode $node): string
177 {
178 $fieldName = $node->name->value;
179
180 return $node->alias === null
181 ? $fieldName
182 : $node->alias->value;
183 }
184 }
185