-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
GoodFaithIntrospectionTest.groovy
231 lines (195 loc) · 7.88 KB
/
GoodFaithIntrospectionTest.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package graphql.introspection
import graphql.ExecutionInput
import graphql.ExecutionResult
import graphql.TestUtil
import graphql.execution.CoercedVariables
import graphql.language.Document
import graphql.normalized.ExecutableNormalizedOperationFactory
import spock.lang.Specification
class GoodFaithIntrospectionTest extends Specification {
def graphql = TestUtil.graphQL("type Query { normalField : String }").build()
def setup() {
GoodFaithIntrospection.enabledJvmWide(true)
}
def cleanup() {
GoodFaithIntrospection.enabledJvmWide(true)
}
def "standard introspection query is inside limits just in general"() {
when:
Document document = TestUtil.toDocument(IntrospectionQuery.INTROSPECTION_QUERY)
def eno = ExecutableNormalizedOperationFactory.createExecutableNormalizedOperation(graphql.getGraphQLSchema(), document,
"IntrospectionQuery", CoercedVariables.emptyVariables())
then:
eno.getOperationFieldCount() < GoodFaithIntrospection.GOOD_FAITH_MAX_FIELDS_COUNT // currently 189
eno.getOperationDepth() < GoodFaithIntrospection.GOOD_FAITH_MAX_DEPTH_COUNT // currently 13
}
def "test asking for introspection in good faith"() {
when:
ExecutionResult er = graphql.execute(IntrospectionQuery.INTROSPECTION_QUERY)
then:
er.errors.isEmpty()
}
def "test asking for introspection in bad faith"() {
when:
ExecutionResult er = graphql.execute(query)
then:
!er.errors.isEmpty()
er.errors[0] instanceof GoodFaithIntrospection.BadFaithIntrospectionError
where:
query | _
// long attack
"""
query badActor{__schema{types{fields{type{fields{type{fields{type{fields{type{name}}}}}}}}}}}
""" | _
// a case for __Type interfaces
""" query badActor {
__schema { types { interfaces { fields { type { interfaces { name } } } } } }
}
""" | _
// a case for __Type inputFields
""" query badActor {
__schema { types { inputFields { type { inputFields { name }}}}}
}
""" | _
// a case for __Type possibleTypes
""" query badActor {
__schema { types { inputFields { type { inputFields { name }}}}}
}
""" | _
// a case leading from __InputValue
""" query badActor {
__schema { types { fields { args { type { name fields { name }}}}}}
}
""" | _
// a case leading from __Field
""" query badActor {
__schema { types { fields { type { name fields { name }}}}}
}
""" | _
// a case for __type
""" query badActor {
__type(name : "t") { name }
alias1 : __type(name : "t1") { name }
}
""" | _
// a case for __type with aliases
""" query badActor {
a1: __type(name : "t") { name }
a2 : __type(name : "t1") { name }
}
""" | _
// a case for schema repeated - dont ask twice
""" query badActor {
__schema { types { name} }
alias1 : __schema { types { name} }
}
""" | _
// a case for used aliases
""" query badActor {
a1: __schema { types { name} }
a2 : __schema { types { name} }
}
""" | _
}
def "mixed general queries and introspections will be stopped anyway"() {
def query = """
query goodAndBad {
normalField
__schema{types{fields{type{fields{type{fields{type{fields{type{name}}}}}}}}}}
}
"""
when:
ExecutionResult er = graphql.execute(query)
then:
!er.errors.isEmpty()
er.errors[0] instanceof GoodFaithIntrospection.BadFaithIntrospectionError
er.data == null // it stopped hard - it did not continue to normal business
}
def "can be disabled"() {
when:
def currentState = GoodFaithIntrospection.isEnabledJvmWide()
then:
currentState
when:
def prevState = GoodFaithIntrospection.enabledJvmWide(false)
then:
prevState
when:
ExecutionResult er = graphql.execute("query badActor{__schema{types{fields{type{fields{type{fields{type{fields{type{name}}}}}}}}}}}")
then:
er.errors.isEmpty()
}
def "can be disabled per request"() {
when:
def context = [(GoodFaithIntrospection.GOOD_FAITH_INTROSPECTION_DISABLED): true]
ExecutionInput executionInput = ExecutionInput.newExecutionInput("query badActor{__schema{types{fields{type{fields{type{fields{type{fields{type{name}}}}}}}}}}}")
.graphQLContext(context).build()
ExecutionResult er = graphql.execute(executionInput)
then:
er.errors.isEmpty()
when:
context = [(GoodFaithIntrospection.GOOD_FAITH_INTROSPECTION_DISABLED): false]
executionInput = ExecutionInput.newExecutionInput("query badActor{__schema{types{fields{type{fields{type{fields{type{fields{type{name}}}}}}}}}}}")
.graphQLContext(context).build()
er = graphql.execute(executionInput)
then:
!er.errors.isEmpty()
er.errors[0] instanceof GoodFaithIntrospection.BadFaithIntrospectionError
}
def "can stop deep queries"() {
when:
def query = createDeepQuery(depth)
def then = System.currentTimeMillis()
ExecutionResult er = graphql.execute(query)
def ms = System.currentTimeMillis() - then
then:
!er.errors.isEmpty()
er.errors[0].class == targetError
er.data == null // it stopped hard - it did not continue to normal business
println "Took " + ms + "ms"
where:
depth | targetError
2 | GoodFaithIntrospection.BadFaithIntrospectionError.class
10 | GoodFaithIntrospection.BadFaithIntrospectionError.class
15 | GoodFaithIntrospection.BadFaithIntrospectionError.class
20 | GoodFaithIntrospection.BadFaithIntrospectionError.class
25 | GoodFaithIntrospection.BadFaithIntrospectionError.class
50 | GoodFaithIntrospection.BadFaithIntrospectionError.class
100 | GoodFaithIntrospection.BadFaithIntrospectionError.class
}
String createDeepQuery(int depth = 25) {
def result = """
query test {
__schema {
types {
...F1
}
}
}
"""
for (int i = 1; i < depth; i++) {
result += """
fragment F$i on __Type {
fields {
type {
...F${i + 1}
}
}
ofType {
...F${i + 1}
}
}
"""
}
result += """
fragment F$depth on __Type {
fields {
type {
name
}
}
}
"""
return result
}
}