Skip to content

Commit 047057c

Browse files
authored
fix: Query with very large conditions can block the event loop ([GHSA-q5g6-5ggq-6g4h](GHSA-q5g6-5ggq-6g4h)) (#10914)
1 parent 84aa456 commit 047057c

2 files changed

Lines changed: 97 additions & 7 deletions

File tree

‎spec/vulnerabilities.spec.js‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16456,4 +16456,86 @@ describe('Vulnerabilities', () => {
1645616456
});
1645716457
});
1645816458
});
16459+
16460+
describe('(GHSA-q5g6-5ggq-6g4h) Query with very large conditions', () => {
16461+
const auth = require('../lib/Auth');
16462+
const rest = require('../lib/rest');
16463+
// Error of a find in a class that clients cannot query; the request ends before the database
16464+
const findError = where => {
16465+
const config = Config.get(Parse.applicationId);
16466+
return rest.find(config, auth.nobody(config), 'Locked', where).catch(error => error);
16467+
};
16468+
// Conditions with the given number of objects or arrays nested in each other
16469+
const nested = {
16470+
objects: count => {
16471+
let where = {};
16472+
for (let i = 0; i < count; i++) {
16473+
where = { a: where };
16474+
}
16475+
return where;
16476+
},
16477+
arrays: count => {
16478+
let list = [];
16479+
for (let i = 1; i < count; i++) {
16480+
list = [list];
16481+
}
16482+
return { a: list };
16483+
},
16484+
};
16485+
const tooDeep = jasmine.objectContaining({
16486+
code: Parse.Error.INVALID_QUERY,
16487+
message: 'Query conditions are nested too deeply.',
16488+
});
16489+
const forbidden = jasmine.objectContaining({ code: Parse.Error.OPERATION_FORBIDDEN });
16490+
16491+
beforeEach(async () => {
16492+
const schema = new Parse.Schema('Locked');
16493+
schema.setCLP({ find: {} });
16494+
await schema.save();
16495+
});
16496+
16497+
it('does not keep a record of every condition that it searches', async () => {
16498+
const spies = [
16499+
[WeakSet, 'add'],
16500+
[WeakMap, 'set'],
16501+
[Set, 'add'],
16502+
[Map, 'set'],
16503+
].map(([type, method]) => spyOn(type.prototype, method).and.callThrough());
16504+
// Collection entries added while a query with the given number of conditions is processed
16505+
const records = async count => {
16506+
spies.forEach(spy => spy.calls.reset());
16507+
const conditions = Array.from({ length: count }, (_, i) => ({
16508+
group: { $in: [[{ value: i }]] },
16509+
}));
16510+
const error = await findError({ $or: conditions });
16511+
const entries = spies.reduce((sum, spy) => sum + spy.calls.count(), 0);
16512+
expect(error).toEqual(forbidden);
16513+
return entries;
16514+
};
16515+
const few = await records(10);
16516+
const many = await records(1010);
16517+
expect(many - few).toBeLessThan(1000);
16518+
});
16519+
16520+
it('does not count conditions next to each other as nesting', async () => {
16521+
const values = Array.from({ length: 20000 }, () => [{ a: [{ b: 1 }] }]);
16522+
expect(await findError({ tags: { $in: values } })).toEqual(forbidden);
16523+
});
16524+
16525+
for (const [kind, conditions] of Object.entries(nested)) {
16526+
it(`searches ${kind} nested 10,000 deep`, async () => {
16527+
expect(await findError(conditions(10000))).toEqual(forbidden);
16528+
});
16529+
16530+
it(`rejects ${kind} nested deeper than 10,000`, async () => {
16531+
expect(await findError(conditions(10001))).toEqual(tooDeep);
16532+
});
16533+
}
16534+
16535+
it('rejects conditions that contain themselves', async () => {
16536+
const where = { name: 'a' };
16537+
where.self = where;
16538+
expect(await findError(where)).toEqual(tooDeep);
16539+
});
16540+
});
1645916541
});

‎src/RestQuery.js‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,15 +1395,23 @@ function replacePointers(object, path, replace) {
13951395
return answer;
13961396
}
13971397

1398+
// Maximum nesting depth of the query conditions that are searched
1399+
const maxSearchDepth = 10000;
1400+
// Ends the subobjects of an object on the search stack
1401+
const endOfSubobjects = Symbol('endOfSubobjects');
1402+
const isObject = value => typeof value === 'object' && value !== null;
1403+
13981404
// Iterates over the subobjects that have the given key, depth first.
13991405
// A yielded object is searched after the caller has replaced the key.
14001406
function* findObjectsWithKey(root, key) {
1401-
const isObject = value => value !== null && typeof value === 'object';
14021407
const stack = [root].filter(isObject);
1403-
// Ends the search of circular references
1404-
const searched = new WeakSet();
1408+
let depth = 0;
14051409
while (stack.length > 0) {
14061410
const node = stack.pop();
1411+
if (node === endOfSubobjects) {
1412+
depth--;
1413+
continue;
1414+
}
14071415
const start = stack.length;
14081416
if (Array.isArray(node)) {
14091417
for (let i = 0; i < node.length; i++) {
@@ -1426,12 +1434,12 @@ function* findObjectsWithKey(root, key) {
14261434
if (stack.length === start) {
14271435
continue;
14281436
}
1429-
if (searched.has(node)) {
1430-
stack.length = start;
1431-
continue;
1437+
// Conditions that contain themselves end here
1438+
if (++depth > maxSearchDepth) {
1439+
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Query conditions are nested too deeply.');
14321440
}
1433-
searched.add(node);
14341441
// The stack returns the last subobject first
1442+
stack.push(endOfSubobjects);
14351443
for (let i = start, j = stack.length - 1; i < j; i++, j--) {
14361444
const last = stack[j];
14371445
stack[j] = stack[i];

0 commit comments

Comments
 (0)