| 1 |
from functools import partial |
| 2 |
|
| 3 |
from graphql.utilities import build_schema |
| 4 |
from graphql.validation import NoSchemaIntrospectionCustomRule |
| 5 |
|
| 6 |
from .harness import assert_validation_errors |
| 7 |
|
| 8 |
schema = build_schema(""" |
| 9 |
type Query { |
| 10 |
someQuery: SomeType |
| 11 |
} |
| 12 |
|
| 13 |
type SomeType { |
| 14 |
someField: String |
| 15 |
introspectionField: __EnumValue |
| 16 |
} |
| 17 |
""") |
| 18 |
|
| 19 |
assert_errors = partial( |
| 20 |
assert_validation_errors, NoSchemaIntrospectionCustomRule, schema=schema |
| 21 |
) |
| 22 |
|
| 23 |
assert_valid = partial(assert_errors, errors=[]) |
| 24 |
|
| 25 |
|
| 26 |
def describe_validate_prohibit_introspection_queries(): |
| 27 |
def ignores_valid_fields_including_typename(): |
| 28 |
assert_valid(""" |
| 29 |
{ |
| 30 |
someQuery { |
| 31 |
__typename |
| 32 |
someField |
| 33 |
} |
| 34 |
} |
| 35 |
""") |
| 36 |
|
| 37 |
def ignores_fields_not_in_the_schema(): |
| 38 |
assert_valid(""" |
| 39 |
{ |
| 40 |
__introspect |
| 41 |
} |
| 42 |
""") |
| 43 |
|
| 44 |
def reports_error_when_a_field_with_an_introspection_type_is_requested(): |
| 45 |
assert_errors( |
| 46 |
""" |
| 47 |
{ |
| 48 |
__schema { |
| 49 |
queryType { |
| 50 |
name |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
""", |
| 55 |
[ |
| 56 |
{ |
| 57 |
"message": "GraphQL introspection has been disabled," |
| 58 |
" but the requested query contained the field '__schema'.", |
| 59 |
"locations": [(3, 15)], |
| 60 |
}, |
| 61 |
{ |
| 62 |
"message": "GraphQL introspection has been disabled," |
| 63 |
" but the requested query contained the field 'queryType'.", |
| 64 |
"locations": [(4, 17)], |
| 65 |
}, |
| 66 |
], |
| 67 |
) |
| 68 |
|
| 69 |
def reports_error_when_a_field_with_introspection_type_is_requested_and_aliased(): |
| 70 |
assert_errors( |
| 71 |
""" |
| 72 |
{ |
| 73 |
s: __schema { |
| 74 |
queryType { |
| 75 |
name |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
""", |
| 80 |
[ |
| 81 |
{ |
| 82 |
"message": "GraphQL introspection has been disabled," |
| 83 |
" but the requested query contained the field '__schema'.", |
| 84 |
"locations": [(3, 15)], |
| 85 |
}, |
| 86 |
{ |
| 87 |
"message": "GraphQL introspection has been disabled," |
| 88 |
" but the requested query contained the field 'queryType'.", |
| 89 |
"locations": [(4, 17)], |
| 90 |
}, |
| 91 |
], |
| 92 |
) |
| 93 |
|
| 94 |
def reports_error_when_using_a_fragment_with_a_field_with_an_introspection_type(): |
| 95 |
assert_errors( |
| 96 |
""" |
| 97 |
{ |
| 98 |
...QueryFragment |
| 99 |
} |
| 100 |
|
| 101 |
fragment QueryFragment on Query { |
| 102 |
__schema { |
| 103 |
queryType { |
| 104 |
name |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
""", |
| 109 |
[ |
| 110 |
{ |
| 111 |
"message": "GraphQL introspection has been disabled," |
| 112 |
" but the requested query contained the field '__schema'.", |
| 113 |
"locations": [(7, 15)], |
| 114 |
}, |
| 115 |
{ |
| 116 |
"message": "GraphQL introspection has been disabled," |
| 117 |
" but the requested query contained the field 'queryType'.", |
| 118 |
"locations": [(8, 17)], |
| 119 |
}, |
| 120 |
], |
| 121 |
) |
| 122 |
|
| 123 |
def reports_error_for_non_standard_introspection_fields(): |
| 124 |
assert_errors( |
| 125 |
""" |
| 126 |
{ |
| 127 |
someQuery { |
| 128 |
introspectionField |
| 129 |
} |
| 130 |
} |
| 131 |
""", |
| 132 |
[ |
| 133 |
{ |
| 134 |
"message": "GraphQL introspection has been disabled, but" |
| 135 |
" the requested query contained the field 'introspectionField'.", |
| 136 |
"locations": [(4, 17)], |
| 137 |
}, |
| 138 |
], |
| 139 |
) |