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