Skip to content

Commit a784ef1

Browse files
committed
add disable introspection
1 parent 5977b16 commit a784ef1

File tree

9 files changed

+74
-292
lines changed

9 files changed

+74
-292
lines changed

docs/execution/validators.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Example
2020
Here is how you would implement depth-limiting on your schema.
2121

2222
.. code:: python
23-
from graphene.validators import depth_limit_validator
23+
from graphene.validation import depth_limit_validator
2424
2525
# The following schema doesn't execute queries
2626
# which have a depth more than 20.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def is_introspection_key(key):
2+
# from: https://spec.graphql.org/June2018/#sec-Schema
3+
# > All types and directives defined within a schema must not have a name which
4+
# > begins with "__" (two underscores), as this is used exclusively
5+
# > by GraphQL’s introspection system.
6+
return str(node.name.value).startswith("__")

graphene/validation/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from .depth_limit import depth_limit_validator
2+
from .disable_introspection import disable_introspection
3+
4+
5+
__all__ = [
6+
"depth_limit_validator",
7+
"disable_introspection"
8+
]

graphene/validators/depth_limit_validator.py renamed to graphene/validation/depth_limit.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from typing import Callable, Dict, List, Optional, Union
3030

3131
from graphql import GraphQLError
32+
from graphql.validation import ValidationContext, ValidationRule
3233
from graphql.language import (
3334
DefinitionNode,
3435
FieldNode,
@@ -38,7 +39,8 @@
3839
Node,
3940
OperationDefinitionNode,
4041
)
41-
from graphql.validation import ValidationContext, ValidationRule
42+
43+
from ..utils.is_introspection_key import is_introspection_key
4244

4345

4446
IgnoreType = Union[Callable[[str], bool], re.Pattern, str]
@@ -121,11 +123,7 @@ def determine_depth(
121123
return depth_so_far
122124

123125
if isinstance(node, FieldNode):
124-
# from: https://spec.graphql.org/June2018/#sec-Schema
125-
# > All types and directives defined within a schema must not have a name which
126-
# > begins with "__" (two underscores), as this is used exclusively
127-
# > by GraphQL’s introspection system.
128-
should_ignore = str(node.name.value).startswith("__") or is_ignored(
126+
should_ignore = is_introspection_key(node.name.value) or is_ignored(
129127
node, ignore
130128
)
131129

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from graphql import GraphQLError
2+
from graphql.language import FieldNode
3+
from graphql.validation import ValidationRule
4+
5+
from ..utils.is_introspection_key import is_introspection_key
6+
7+
8+
def disable_introspection():
9+
class DisableIntrospection(ValidationRule):
10+
def enter_field(self, node: FieldNode, *_args):
11+
field_name = node.name.value
12+
if not is_introspection_key(field_name):
13+
return
14+
15+
self.report_error(
16+
GraphQLError(
17+
f"Cannot query '{field_name}': introspection is disabled.",
18+
node,
19+
)
20+
)
21+
22+
return DisableIntrospection
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from graphql import parse, validate
2+
3+
from ...types import Schema, ObjectType, String
4+
from ..disable_introspection import disable_introspection
5+
6+
7+
class Query(ObjectType):
8+
name = String(
9+
required=True
10+
)
11+
12+
13+
schema = Schema(query=Query)
14+
15+
16+
def run_query(query: str):
17+
document = parse(query)
18+
19+
result = None
20+
21+
def callback(query_depths):
22+
nonlocal result
23+
result = query_depths
24+
25+
errors = validate(
26+
schema.graphql_schema,
27+
document,
28+
rules=(
29+
disable_introspection(),
30+
),
31+
)
32+
33+
return errors, result

graphene/validators/__init__.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

graphene/validators/tests/test_depth_limit_validator.py

Lines changed: 0 additions & 279 deletions
This file was deleted.

0 commit comments

Comments
 (0)