Skip to content

Commit 7277210

Browse files
committed
Use JSON.generate for GraphQL Introspection queries
1 parent 4075e1a commit 7277210

File tree

1 file changed

+62
-72
lines changed

1 file changed

+62
-72
lines changed

modules/auxiliary/scanner/http/graphql_introspection_scanner.rb

Lines changed: 62 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,18 @@ def responded_with_introspected_data?(response)
5454
response&.body.to_s == "{\"data\":{\"__schema\":{\"queryType\":{\"name\":\"Query\"}}}}\n"
5555
end
5656

57-
# Process a query before sending it off in a web request.
58-
# @param query The string query to process.
59-
# @return [String] The processed query, with spaces and new-lines (\r and \n) removed.
60-
def process_query(query)
61-
query.gsub(/ +/, ' ').gsub(/\r?\n/, '')
62-
end
63-
6457
# Create a small query, used to test if introspection is enabledo n the GraphQL endpoint.
6558
# @return [String] The processed introspection probe query.
6659
def introspection_probe_query
67-
raw_query = '{"query": "
60+
<<~EOF
6861
query {
6962
__schema {
7063
queryType {
7164
name
7265
}
7366
}
74-
}"
75-
}'
76-
process_query(raw_query)
67+
}
68+
EOF
7769
end
7870

7971
# Create a unique query that will try to dump the GraphQL schema.
@@ -88,79 +80,75 @@ def schema_dump_query
8880
type_reference: Rex::Text.rand_text_alpha(8)
8981
}
9082

91-
# Remove extra spaces, and new lines.
92-
# Remember, fragments need to be present at the end, outside the curly braces, but as part
93-
# of the quoted 'query' param.
94-
raw_query = "{\"query\": \"query {
95-
__schema {
96-
queryType {
97-
name
98-
}
99-
mutationType {
100-
name
101-
}
102-
subscriptionType {
103-
name
83+
# Fragments need to be present at the end, outside the curly braces of the 'query'
84+
<<~EOF
85+
query {
86+
__schema {
87+
queryType {
88+
name
89+
}
90+
mutationType {
91+
name
92+
}
93+
subscriptionType {
94+
name
95+
}
96+
types {
97+
...#{vars_map[:type_fragment]}
98+
}
99+
directives {
100+
name
101+
description
102+
args {
103+
...#{vars_map[:input_fragment]}
104+
}
105+
}
104106
}
105-
types {
106-
...#{vars_map[:type_fragment]}
107+
}
108+
fragment #{vars_map[:type_fragment]} on __Type {
109+
kind
110+
name
111+
description
112+
inputFields {
113+
...#{vars_map[:input_fragment]}
107114
}
108-
directives {
115+
fields(includeDeprecated: true) {
109116
name
110117
description
118+
isDeprecated
119+
deprecationReason
111120
args {
112121
...#{vars_map[:input_fragment]}
113122
}
123+
type {
124+
...#{vars_map[:type_reference]}
125+
}
114126
}
115-
}
116-
}
117-
fragment #{vars_map[:type_fragment]} on __Type {
118-
kind
119-
name
120-
description
121-
inputFields {
122-
...#{vars_map[:input_fragment]}
123-
}
124-
fields(includeDeprecated: true) {
125-
name
126-
description
127-
isDeprecated
128-
deprecationReason
129-
args {
127+
inputFields {
130128
...#{vars_map[:input_fragment]}
131129
}
132-
type {
130+
interfaces {
131+
...#{vars_map[:type_reference]}
132+
}
133+
enumValues(includeDeprecated: true) {
134+
name
135+
description
136+
isDeprecated
137+
deprecationReason
138+
}
139+
possibleTypes {
133140
...#{vars_map[:type_reference]}
134141
}
135142
}
136-
inputFields {
137-
...#{vars_map[:input_fragment]}
138-
}
139-
interfaces {
140-
...#{vars_map[:type_reference]}
141-
}
142-
enumValues(includeDeprecated: true) {
143+
fragment #{vars_map[:input_fragment]} on __InputValue {
143144
name
144145
description
145-
isDeprecated
146-
deprecationReason
147-
}
148-
possibleTypes {
149-
...#{vars_map[:type_reference]}
150-
}
151-
}
152-
fragment #{vars_map[:input_fragment]} on __InputValue {
153-
name
154-
description
155-
defaultValue
156-
type {
157-
...#{vars_map[:type_reference]}
146+
defaultValue
147+
type {
148+
...#{vars_map[:type_reference]}
149+
}
158150
}
159-
}
160-
fragment #{vars_map[:type_reference]} on __Type {
161-
kind
162-
name
163-
ofType {
151+
fragment #{vars_map[:type_reference]} on __Type {
164152
kind
165153
name
166154
ofType {
@@ -169,12 +157,14 @@ def schema_dump_query
169157
ofType {
170158
kind
171159
name
160+
ofType {
161+
kind
162+
name
163+
}
172164
}
173165
}
174166
}
175-
}
176-
\"}"
177-
process_query(raw_query)
167+
EOF
178168
end
179169

180170
# Report a GraphQL instance on the current host and port.
@@ -239,7 +229,7 @@ def send_graphql_request(query)
239229
'headers' => {
240230
'Accept' => 'application/json'
241231
},
242-
'data' => query
232+
'data' => JSON.generate({ query: query })
243233
)
244234
end
245235

0 commit comments

Comments
 (0)