forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRequestComplexity.spec.js
More file actions
455 lines (403 loc) · 14.1 KB
/
Copy pathRequestComplexity.spec.js
File metadata and controls
455 lines (403 loc) · 14.1 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
'use strict';
const Config = require('../lib/Config');
const auth = require('../lib/Auth');
const rest = require('../lib/rest');
describe('request complexity', () => {
function buildNestedInQuery(depth, className = '_User') {
let where = {};
for (let i = 0; i < depth; i++) {
where = { username: { $inQuery: { className, where } } };
}
return where;
}
function buildNestedNotInQuery(depth, className = '_User') {
let where = {};
for (let i = 0; i < depth; i++) {
where = { username: { $notInQuery: { className, where } } };
}
return where;
}
function buildNestedSelect(depth, className = '_User') {
let where = {};
for (let i = 0; i < depth; i++) {
where = { username: { $select: { query: { className, where }, key: 'username' } } };
}
return where;
}
function buildNestedDontSelect(depth, className = '_User') {
let where = {};
for (let i = 0; i < depth; i++) {
where = { username: { $dontSelect: { query: { className, where }, key: 'username' } } };
}
return where;
}
function buildNestedOrQuery(depth) {
let where = { username: 'test' };
for (let i = 0; i < depth; i++) {
where = { $or: [where, { username: 'test' }] };
}
return where;
}
function buildNestedAndQuery(depth) {
let where = { username: 'test' };
for (let i = 0; i < depth; i++) {
where = { $and: [where, { username: 'test' }] };
}
return where;
}
function buildNestedNorQuery(depth) {
let where = { username: 'test' };
for (let i = 0; i < depth; i++) {
where = { $nor: [where, { username: 'test' }] };
}
return where;
}
describe('config validation', () => {
it('should accept valid requestComplexity config', async () => {
await expectAsync(
reconfigureServer({
requestComplexity: {
includeDepth: 10,
includeCount: 100,
subqueryDepth: 5,
queryDepth: 10,
graphQLDepth: 15,
graphQLFields: 300,
},
})
).toBeResolved();
});
it('should accept -1 to disable a specific limit', async () => {
await expectAsync(
reconfigureServer({
requestComplexity: {
includeDepth: -1,
includeCount: -1,
subqueryDepth: -1,
queryDepth: -1,
graphQLDepth: -1,
graphQLFields: -1,
},
})
).toBeResolved();
});
it('should reject value of 0', async () => {
await expectAsync(
reconfigureServer({
requestComplexity: { includeDepth: 0 },
})
).toBeRejectedWith(
new Error('requestComplexity.includeDepth must be a positive integer or -1 to disable.')
);
});
it('should reject non-integer values', async () => {
await expectAsync(
reconfigureServer({
requestComplexity: { includeDepth: 3.5 },
})
).toBeRejectedWith(
new Error('requestComplexity.includeDepth must be a positive integer or -1 to disable.')
);
});
it('should reject unknown properties', async () => {
await expectAsync(
reconfigureServer({
requestComplexity: { unknownProp: 5 },
})
).toBeRejectedWith(
new Error("requestComplexity contains unknown property 'unknownProp'.")
);
});
it('should reject non-object values', async () => {
await expectAsync(
reconfigureServer({
requestComplexity: 'invalid',
})
).toBeRejectedWith(new Error('requestComplexity must be an object.'));
});
it('should apply defaults for missing properties', async () => {
await reconfigureServer({
requestComplexity: { includeDepth: 3 },
});
const config = Config.get('test');
expect(config.requestComplexity.includeDepth).toBe(3);
expect(config.requestComplexity.includeCount).toBe(50);
expect(config.requestComplexity.subqueryDepth).toBe(5);
expect(config.requestComplexity.queryDepth).toBe(-1);
expect(config.requestComplexity.graphQLDepth).toBe(50);
expect(config.requestComplexity.graphQLFields).toBe(200);
});
it('should apply full defaults when not configured', async () => {
await reconfigureServer({});
const config = Config.get('test');
expect(config.requestComplexity).toEqual({
includeDepth: 5,
includeCount: 50,
subqueryDepth: 5,
queryDepth: -1,
graphQLDepth: 50,
graphQLFields: 200,
});
});
});
describe('subquery depth', () => {
let config;
beforeEach(async () => {
await reconfigureServer({
requestComplexity: { subqueryDepth: 3 },
});
config = Config.get('test');
});
it('should allow $inQuery within depth limit', async () => {
const where = buildNestedInQuery(3);
await expectAsync(
rest.find(config, auth.nobody(config), '_User', where)
).toBeResolved();
});
it('should reject $inQuery exceeding depth limit', async () => {
const where = buildNestedInQuery(4);
await expectAsync(
rest.find(config, auth.nobody(config), '_User', where)
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringMatching(/Subquery nesting depth exceeds maximum allowed depth of 3/),
})
);
});
it('should reject $notInQuery exceeding depth limit', async () => {
const where = buildNestedNotInQuery(4);
await expectAsync(
rest.find(config, auth.nobody(config), '_User', where)
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringMatching(/Subquery nesting depth exceeds maximum allowed depth of 3/),
})
);
});
it('should reject $select exceeding depth limit', async () => {
const where = buildNestedSelect(4);
await expectAsync(
rest.find(config, auth.nobody(config), '_User', where)
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringMatching(/Subquery nesting depth exceeds maximum allowed depth of 3/),
})
);
});
it('should reject $dontSelect exceeding depth limit', async () => {
const where = buildNestedDontSelect(4);
await expectAsync(
rest.find(config, auth.nobody(config), '_User', where)
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringMatching(/Subquery nesting depth exceeds maximum allowed depth of 3/),
})
);
});
it('should allow subqueries with master key even when exceeding limit', async () => {
const where = buildNestedInQuery(4);
await expectAsync(
rest.find(config, auth.master(config), '_User', where)
).toBeResolved();
});
it('should allow subqueries with maintenance key even when exceeding limit', async () => {
const where = buildNestedInQuery(4);
await expectAsync(
rest.find(config, auth.maintenance(config), '_User', where)
).toBeResolved();
});
it('should allow unlimited subqueries when subqueryDepth is -1', async () => {
await reconfigureServer({
requestComplexity: { subqueryDepth: -1 },
});
config = Config.get('test');
const where = buildNestedInQuery(15);
await expectAsync(
rest.find(config, auth.nobody(config), '_User', where)
).toBeResolved();
});
});
describe('query depth', () => {
let config;
beforeEach(async () => {
await reconfigureServer({
requestComplexity: { queryDepth: 3 },
});
config = Config.get('test');
});
it('should allow $or within depth limit', async () => {
const where = buildNestedOrQuery(3);
await expectAsync(
rest.find(config, auth.nobody(config), '_User', where)
).toBeResolved();
});
it('should reject $or exceeding depth limit', async () => {
const where = buildNestedOrQuery(4);
await expectAsync(
rest.find(config, auth.nobody(config), '_User', where)
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringMatching(/Query condition nesting depth exceeds maximum allowed depth of 3/),
})
);
});
it('should reject $and exceeding depth limit', async () => {
const where = buildNestedAndQuery(4);
await expectAsync(
rest.find(config, auth.nobody(config), '_User', where)
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringMatching(/Query condition nesting depth exceeds maximum allowed depth of 3/),
})
);
});
it('should reject $nor exceeding depth limit', async () => {
const where = buildNestedNorQuery(4);
await expectAsync(
rest.find(config, auth.nobody(config), '_User', where)
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringMatching(/Query condition nesting depth exceeds maximum allowed depth of 3/),
})
);
});
it('should reject mixed nested operators exceeding depth limit', async () => {
// $or > $and > $nor > $or = depth 4
const where = {
$or: [
{
$and: [
{
$nor: [
{ $or: [{ username: 'a' }, { username: 'b' }] },
],
},
],
},
],
};
await expectAsync(
rest.find(config, auth.nobody(config), '_User', where)
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringMatching(/Query condition nesting depth exceeds maximum allowed depth of 3/),
})
);
});
it('should allow with master key even when exceeding limit', async () => {
const where = buildNestedOrQuery(4);
await expectAsync(
rest.find(config, auth.master(config), '_User', where)
).toBeResolved();
});
it('should allow with maintenance key even when exceeding limit', async () => {
const where = buildNestedOrQuery(4);
await expectAsync(
rest.find(config, auth.maintenance(config), '_User', where)
).toBeResolved();
});
it('should allow unlimited when queryDepth is -1', async () => {
await reconfigureServer({
requestComplexity: { queryDepth: -1 },
});
config = Config.get('test');
const where = buildNestedOrQuery(15);
await expectAsync(
rest.find(config, auth.nobody(config), '_User', where)
).toBeResolved();
});
});
describe('include limits', () => {
let config;
beforeEach(async () => {
await reconfigureServer({
requestComplexity: { includeDepth: 3, includeCount: 5 },
});
config = Config.get('test');
});
it('should allow include within depth limit', async () => {
await expectAsync(
rest.find(config, auth.nobody(config), '_User', {}, { include: 'a.b.c' })
).toBeResolved();
});
it('should reject include exceeding depth limit', async () => {
await expectAsync(
rest.find(config, auth.nobody(config), '_User', {}, { include: 'a.b.c.d' })
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringMatching(/Include depth of 4 exceeds maximum allowed depth of 3/),
})
);
});
it('should allow include count within limit', async () => {
await expectAsync(
rest.find(config, auth.nobody(config), '_User', {}, { include: 'a,b,c,d,e' })
).toBeResolved();
});
it('should reject include count exceeding limit', async () => {
await expectAsync(
rest.find(config, auth.nobody(config), '_User', {}, { include: 'a,b,c,d,e,f' })
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringMatching(/Number of include fields \(\d+\) exceeds maximum allowed \(5\)/),
})
);
});
it('should allow includeAll when within count limit', async () => {
const schema = new Parse.Schema('IncludeTestClass');
schema.addPointer('ptr1', '_User');
schema.addPointer('ptr2', '_User');
schema.addPointer('ptr3', '_User');
await schema.save();
const obj = new Parse.Object('IncludeTestClass');
await obj.save();
await expectAsync(
rest.find(config, auth.nobody(config), 'IncludeTestClass', {}, { includeAll: true })
).toBeResolved();
});
it('should reject includeAll when exceeding count limit', async () => {
await reconfigureServer({
requestComplexity: { includeDepth: 3, includeCount: 2 },
});
config = Config.get('test');
const schema = new Parse.Schema('IncludeTestClass2');
schema.addPointer('ptr1', '_User');
schema.addPointer('ptr2', '_User');
schema.addPointer('ptr3', '_User');
await schema.save();
const obj = new Parse.Object('IncludeTestClass2');
await obj.save();
await expectAsync(
rest.find(config, auth.nobody(config), 'IncludeTestClass2', {}, { includeAll: true })
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringMatching(/Number of include fields .* exceeds maximum allowed/),
})
);
});
it('should allow includes with master key even when exceeding limits', async () => {
await expectAsync(
rest.find(config, auth.master(config), '_User', {}, { include: 'a.b.c.d' })
).toBeResolved();
});
it('should allow unlimited depth when includeDepth is -1', async () => {
await reconfigureServer({
requestComplexity: { includeDepth: -1 },
});
config = Config.get('test');
await expectAsync(
rest.find(config, auth.nobody(config), '_User', {}, { include: 'a.b.c.d.e.f.g' })
).toBeResolved();
});
it('should allow unlimited count when includeCount is -1', async () => {
await reconfigureServer({
requestComplexity: { includeCount: -1 },
});
config = Config.get('test');
const includes = Array.from({ length: 100 }, (_, i) => `field${i}`).join(',');
await expectAsync(
rest.find(config, auth.nobody(config), '_User', {}, { include: includes })
).toBeResolved();
});
});
});