Skip to content

Commit 1c62f55

Browse files
committed
Implements DisableIntrospection validation rule
1 parent 6d6d1ac commit 1c62f55

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

0 commit comments

Comments
 (0)