File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ namespace GraphQL \Validator \Rules ;
3+
4+ use GraphQL \Error \Error ;
5+ use GraphQL \Language \AST \FieldNode ;
6+ use GraphQL \Language \AST \NodeKind ;
7+ use GraphQL \Validator \ValidationContext ;
8+
9+ class DisableIntrospection extends AbstractQuerySecurity
10+ {
11+ const ENABLED = 1 ;
12+ private $ isEnabled ;
13+
14+ public function __construct ($ enabled )
15+ {
16+ $ this ->setEnabled ($ enabled );
17+ }
18+
19+ public function setEnabled ($ enabled )
20+ {
21+ $ this ->isEnabled = $ enabled ;
22+ }
23+
24+ static function introspectionDisabledMessage ()
25+ {
26+ return 'GraphQL introspection is not allowed, but the query contained __schema or __type ' ;
27+ }
28+
29+ protected function isEnabled ()
30+ {
31+ return $ this ->isEnabled !== static ::DISABLED ;
32+ }
33+
34+ public function __invoke (ValidationContext $ context )
35+ {
36+ return $ this ->invokeIfNeeded (
37+ $ context ,
38+ [
39+ NodeKind::FIELD => function (FieldNode $ node ) use ($ context ) {
40+ if ($ node ->name ->value === '__type ' || $ node ->name ->value === '__schema ' ) {
41+ $ context ->reportError (new Error (
42+ static ::introspectionDisabledMessage (),
43+ [$ node ]
44+ ));
45+ }
46+ }
47+ ]
48+ );
49+ }
50+ }
You can’t perform that action at this time.
0 commit comments