forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraphQLQueryComplexity.spec.js
More file actions
209 lines (191 loc) · 7.13 KB
/
GraphQLQueryComplexity.spec.js
File metadata and controls
209 lines (191 loc) · 7.13 KB
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
'use strict';
const http = require('http');
const express = require('express');
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
require('./helper');
const { ParseGraphQLServer } = require('../lib/GraphQL/ParseGraphQLServer');
describe('graphql query complexity', () => {
let httpServer;
let graphQLServer;
const headers = {
'X-Parse-Application-Id': 'test',
'X-Parse-Javascript-Key': 'test',
'Content-Type': 'application/json',
};
async function setupGraphQL(serverOptions = {}) {
if (httpServer) {
await new Promise(resolve => httpServer.close(resolve));
}
const server = await reconfigureServer(serverOptions);
const expressApp = express();
httpServer = http.createServer(expressApp);
expressApp.use('/parse', server.app);
graphQLServer = new ParseGraphQLServer(server, {
graphQLPath: '/graphql',
});
graphQLServer.applyGraphQL(expressApp);
await new Promise(resolve => httpServer.listen({ port: 13378 }, resolve));
}
async function graphqlRequest(query, requestHeaders = headers) {
const response = await fetch('http://localhost:13378/graphql', {
method: 'POST',
headers: requestHeaders,
body: JSON.stringify({ query }),
});
return response.json();
}
// Returns a query with depth 4: users(1) > edges(2) > node(3) > objectId(4)
function buildDeepQuery() {
return '{ users { edges { node { objectId } } } }';
}
function buildWideQuery(fieldCount) {
const fields = Array.from({ length: fieldCount }, (_, i) => `field${i}: objectId`).join('\n ');
return `{ users { edges { node { ${fields} } } } }`;
}
afterEach(async () => {
if (httpServer) {
await new Promise(resolve => httpServer.close(resolve));
httpServer = null;
}
});
describe('depth limit', () => {
it('should reject query exceeding depth limit', async () => {
await setupGraphQL({
requestComplexity: { graphQLDepth: 3 },
});
const result = await graphqlRequest(buildDeepQuery());
expect(result.errors).toBeDefined();
expect(result.errors[0].message).toMatch(
/GraphQL query depth of \d+ exceeds maximum allowed depth of 3/
);
});
it('should allow query within depth limit', async () => {
await setupGraphQL({
requestComplexity: { graphQLDepth: 10 },
});
const result = await graphqlRequest(buildDeepQuery());
expect(result.errors).toBeUndefined();
});
it('should allow deep query with master key', async () => {
await setupGraphQL({
requestComplexity: { graphQLDepth: 3 },
});
const result = await graphqlRequest(buildDeepQuery(), {
...headers,
'X-Parse-Master-Key': 'test',
});
expect(result.errors).toBeUndefined();
});
it('should allow unlimited depth when graphQLDepth is -1', async () => {
await setupGraphQL({
requestComplexity: { graphQLDepth: -1 },
});
const result = await graphqlRequest(buildDeepQuery());
expect(result.errors).toBeUndefined();
});
});
describe('fields limit', () => {
it('should reject query exceeding fields limit', async () => {
await setupGraphQL({
requestComplexity: { graphQLFields: 5 },
});
const result = await graphqlRequest(buildWideQuery(10));
expect(result.errors).toBeDefined();
expect(result.errors[0].message).toMatch(
/Number of GraphQL fields \(\d+\) exceeds maximum allowed \(5\)/
);
});
it('should allow query within fields limit', async () => {
await setupGraphQL({
requestComplexity: { graphQLFields: 200 },
});
const result = await graphqlRequest(buildDeepQuery());
expect(result.errors).toBeUndefined();
});
it('should allow wide query with master key', async () => {
await setupGraphQL({
requestComplexity: { graphQLFields: 5 },
});
const result = await graphqlRequest(buildWideQuery(10), {
...headers,
'X-Parse-Master-Key': 'test',
});
expect(result.errors).toBeUndefined();
});
it('should count fragment fields at each spread location', async () => {
// With correct counting: 2 aliases (2) + 2×edges (2) + 2×node (2) + 2×objectId from fragment (2) = 8
// With incorrect counting (fragment once): 2 + 2 + 2 + 1 = 7
// Set limit to 7 so incorrect counting passes but correct counting rejects
await setupGraphQL({
requestComplexity: { graphQLFields: 7 },
});
const result = await graphqlRequest(`
fragment UserFields on User { objectId }
{
a1: users { edges { node { ...UserFields } } }
a2: users { edges { node { ...UserFields } } }
}
`);
expect(result.errors).toBeDefined();
expect(result.errors[0].message).toMatch(
/Number of GraphQL fields \(\d+\) exceeds maximum allowed \(7\)/
);
});
it('should count inline fragment fields toward depth and field limits', async () => {
await setupGraphQL({
requestComplexity: { graphQLFields: 3 },
});
// Inline fragment adds fields without increasing depth:
// users(1) > edges(2) > ... on UserConnection { edges(3) > node(4) }
const result = await graphqlRequest(`{
users {
edges {
... on UserEdge {
node {
objectId
}
}
}
}
}`);
expect(result.errors).toBeDefined();
expect(result.errors[0].message).toMatch(
/Number of GraphQL fields \(\d+\) exceeds maximum allowed \(3\)/
);
});
it('should allow unlimited fields when graphQLFields is -1', async () => {
await setupGraphQL({
requestComplexity: { graphQLFields: -1 },
});
const result = await graphqlRequest(buildWideQuery(50));
expect(result.errors).toBeUndefined();
});
});
describe('fragment fan-out', () => {
it('should reject query with exponential fragment fan-out efficiently', async () => {
await setupGraphQL({
requestComplexity: { graphQLFields: 100 },
});
// Binary fan-out: each fragment spreads the next one twice.
// Without fix: 2^(levels-1) field visits = 2^25 ≈ 33M (hangs event loop).
// With fix (memoization): O(levels) traversal, same field count, instant rejection.
const levels = 26;
let query = 'query Q { ...F0 }\n';
for (let i = 0; i < levels; i++) {
if (i === levels - 1) {
query += `fragment F${i} on Query { __typename }\n`;
} else {
query += `fragment F${i} on Query { ...F${i + 1} ...F${i + 1} }\n`;
}
}
const start = Date.now();
const result = await graphqlRequest(query);
const elapsed = Date.now() - start;
// Must complete in under 5 seconds (without fix it would take seconds or hang)
expect(elapsed).toBeLessThan(5000);
// Field count is 2^(levels-1) = 16777216, which exceeds the limit of 100
expect(result.errors).toBeDefined();
expect(result.errors[0].message).toMatch(/Number of GraphQL fields .* exceeds maximum allowed/);
});
});
});