Skip to content

Commit 5701d66

Browse files
authored
fix: LiveQuery subscription with a large list constraint can block the event loop ([GHSA-f6wh-8wcf-xfv8](GHSA-f6wh-8wcf-xfv8)) (#10883)
1 parent 86bf1e7 commit 5701d66

2 files changed

Lines changed: 196 additions & 5 deletions

File tree

‎spec/vulnerabilities.spec.js‎

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14253,4 +14253,154 @@ describe('Vulnerabilities', () => {
1425314253
expect(response.data.objectId).toBe(victim.id);
1425414254
});
1425514255
});
14256+
14257+
describe('(GHSA-f6wh-8wcf-xfv8) LiveQuery matching of a large constraint against a large field value', () => {
14258+
const { matchesQuery } = require('../lib/LiveQuery/QueryTools');
14259+
const strings = (prefix, length) => Array.from({ length }, (_, i) => `${prefix}${i}`);
14260+
const pointer = objectId => ({ __type: 'Pointer', className: 'Item', objectId });
14261+
const timedMatch = (object, query) => {
14262+
const start = Date.now();
14263+
const matches = matchesQuery(object, query);
14264+
return { matches, elapsed: Date.now() - start };
14265+
};
14266+
14267+
it('matches a large $in constraint against a large array field in time', () => {
14268+
const object = { tags: strings('s', 50000) };
14269+
const matching = timedMatch(object, { tags: { $in: [...strings('k', 49999), 's49999'] } });
14270+
const other = timedMatch(object, { tags: { $in: strings('k', 50000) } });
14271+
expect(matching.matches).toBe(true);
14272+
expect(other.matches).toBe(false);
14273+
expect(matching.elapsed + other.elapsed).toBeLessThan(1000);
14274+
});
14275+
14276+
it('matches a large $nin constraint against a large array field in time', () => {
14277+
const object = { tags: strings('s', 50000) };
14278+
const matching = timedMatch(object, { tags: { $nin: strings('k', 50000) } });
14279+
const other = timedMatch(object, { tags: { $nin: [...strings('k', 49999), 's49999'] } });
14280+
expect(matching.matches).toBe(true);
14281+
expect(other.matches).toBe(false);
14282+
expect(matching.elapsed + other.elapsed).toBeLessThan(1000);
14283+
});
14284+
14285+
it('matches a large $all constraint against a large array field in time', () => {
14286+
const object = { tags: strings('s', 50000) };
14287+
const matching = timedMatch(object, { tags: { $all: Array(50000).fill('s49999') } });
14288+
const other = timedMatch(object, { tags: { $all: [...Array(49999).fill('s49999'), 'k0'] } });
14289+
expect(matching.matches).toBe(true);
14290+
expect(other.matches).toBe(false);
14291+
expect(matching.elapsed + other.elapsed).toBeLessThan(1000);
14292+
});
14293+
14294+
it('matches a large $containedBy constraint against a large array field in time', () => {
14295+
const object = { tags: strings('s', 40000) };
14296+
const matching = timedMatch(object, {
14297+
tags: { $containedBy: [...strings('k', 40000), ...strings('s', 40000)] },
14298+
});
14299+
const other = timedMatch(object, {
14300+
tags: { $containedBy: [...strings('k', 40000), ...strings('s', 39999)] },
14301+
});
14302+
expect(matching.matches).toBe(true);
14303+
expect(other.matches).toBe(false);
14304+
expect(matching.elapsed + other.elapsed).toBeLessThan(1000);
14305+
});
14306+
14307+
it('matches a large $in constraint of pointers against a large array field of pointers in time', () => {
14308+
const object = { items: strings('s', 15000).map(pointer) };
14309+
const matching = timedMatch(object, {
14310+
items: { $in: [...strings('k', 14999).map(pointer), pointer('s14999')] },
14311+
});
14312+
const other = timedMatch(object, { items: { $in: strings('k', 15000).map(pointer) } });
14313+
expect(matching.matches).toBe(true);
14314+
expect(other.matches).toBe(false);
14315+
expect(matching.elapsed + other.elapsed).toBeLessThan(1000);
14316+
});
14317+
14318+
it('throws for a long string $all constraint against a long string field in time', () => {
14319+
const object = { title: 'b'.repeat(399999) + 'a' };
14320+
const start = Date.now();
14321+
expect(() => matchesQuery(object, { title: { $all: 'a'.repeat(400000) } })).toThrow();
14322+
expect(Date.now() - start).toBeLessThan(1000);
14323+
});
14324+
14325+
for (const [operator, value] of [
14326+
['$in', ['b']],
14327+
['$nin', ['x']],
14328+
['$containedBy', ['a', 'b']],
14329+
]) {
14330+
it(`throws for a ${operator} constraint that is a string against an array field`, () => {
14331+
expect(() => matchesQuery({ name: value }, { name: { [operator]: 'abc' } })).toThrow();
14332+
});
14333+
}
14334+
14335+
it('throws for a $all constraint against a string field', () => {
14336+
expect(() =>
14337+
matchesQuery({ title: 'hello world' }, { title: { $all: ['hello'] } })
14338+
).toThrow();
14339+
});
14340+
14341+
describe('LiveQuery', () => {
14342+
beforeEach(async () => {
14343+
Parse.CoreManager.getLiveQueryController().setDefaultLiveQueryClient(null);
14344+
await reconfigureServer({
14345+
liveQuery: { classNames: ['Item'] },
14346+
startLiveQueryServer: true,
14347+
verbose: false,
14348+
silent: true,
14349+
});
14350+
});
14351+
14352+
afterEach(async () => {
14353+
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
14354+
if (client) {
14355+
await client.close();
14356+
}
14357+
});
14358+
14359+
async function subscribe(query) {
14360+
const subscription = await query.subscribe();
14361+
await new Promise(resolve => subscription.on('open', resolve));
14362+
return subscription;
14363+
}
14364+
14365+
it('delivers an event for a large $in constraint and a large array field in time', async () => {
14366+
const subscription = await subscribe(
14367+
new Parse.Query('Item').containedIn('tags', [...strings('k', 59999), 's59999'])
14368+
);
14369+
const created = new Promise(resolve => subscription.on('create', resolve));
14370+
const start = Date.now();
14371+
await new Parse.Object('Item').save({ tags: strings('s', 60000) });
14372+
const object = await created;
14373+
expect(Date.now() - start).toBeLessThan(2000);
14374+
expect(object.get('tags').length).toBe(60000);
14375+
});
14376+
14377+
it('does not deliver an event for a string $all constraint and delivers other events in time', async () => {
14378+
const stringConstraint = await subscribe(
14379+
new Parse.Query('Item').containsAll('title', 'a'.repeat(400000))
14380+
);
14381+
const other = await subscribe(new Parse.Query('Item').exists('title'));
14382+
const received = [];
14383+
stringConstraint.on('create', object => received.push(object));
14384+
const createdWithTitle = title =>
14385+
new Promise(resolve =>
14386+
other.on('create', object => {
14387+
if (object.get('title') === title) {
14388+
resolve();
14389+
}
14390+
})
14391+
);
14392+
const title = 'b'.repeat(399999) + 'a';
14393+
const created = createdWithTitle(title);
14394+
const start = Date.now();
14395+
await new Parse.Object('Item').save({ title });
14396+
await created;
14397+
expect(Date.now() - start).toBeLessThan(2000);
14398+
// Events of one client arrive in order, so the string constraint's event would come first
14399+
const sentinelCreated = createdWithTitle('sentinel');
14400+
await new Parse.Object('Item').save({ title: 'sentinel' });
14401+
await sentinelCreated;
14402+
expect(received.length).toBe(0);
14403+
});
14404+
});
14405+
});
1425614406
});

‎src/LiveQuery/QueryTools.js‎

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,50 @@ function contains(haystack: Array, needle: any): boolean {
121121
return false;
122122
}
123123

124+
if (Array.isArray(needle) && needle.length > 0) {
125+
// Index the list once for all values of an array field
126+
return isIndexed(indexList(haystack), needle);
127+
}
128+
129+
return haystack.indexOf(needle) > -1;
130+
}
131+
132+
// Values and pointer object IDs of a list; throws for a list that is not an array
133+
function indexList(list: Array) {
134+
const index = { values: new Set(), pointers: new Map() };
135+
list.forEach(value => {
136+
index.values.add(value);
137+
if (value === null || value === undefined) {
138+
return;
139+
}
140+
let objectIds = index.pointers.get(value.className);
141+
if (!objectIds) {
142+
objectIds = new Set();
143+
index.pointers.set(value.className, objectIds);
144+
}
145+
objectIds.add(value.objectId);
146+
});
147+
return index;
148+
}
149+
150+
// Same as contains, with a list index
151+
function isIndexed(index: any, needle: any): boolean {
152+
if (needle && needle.__type && needle.__type === 'Pointer') {
153+
return (
154+
(typeof needle.objectId === 'string' && index.values.has(needle.objectId)) ||
155+
!!index.pointers.get(needle.className)?.has(needle.objectId)
156+
);
157+
}
158+
124159
if (Array.isArray(needle)) {
125160
for (const need of needle) {
126-
if (contains(haystack, need)) {
161+
if (isIndexed(index, need)) {
127162
return true;
128163
}
129164
}
130165
}
131166

132-
return haystack.indexOf(needle) > -1;
167+
return index.values.has(needle);
133168
}
134169
/**
135170
* matchesQuery -- Determines if an object would be returned by a Parse Query
@@ -290,16 +325,20 @@ function matchesKeyConstraints(object, key, constraints) {
290325
return false;
291326
}
292327
break;
293-
case '$all':
328+
case '$all': {
294329
if (!object[key]) {
295330
return false;
296331
}
332+
// Throws for a field that is not an array
333+
const values = new Set();
334+
object[key].forEach(value => values.add(value));
297335
for (i = 0; i < compareTo.length; i++) {
298-
if (object[key].indexOf(compareTo[i]) < 0) {
336+
if (!values.has(compareTo[i])) {
299337
return false;
300338
}
301339
}
302340
break;
341+
}
303342
case '$exists': {
304343
const propertyExists = typeof object[key] !== 'undefined';
305344
const existenceIsRequired = constraints['$exists'];
@@ -367,8 +406,10 @@ function matchesKeyConstraints(object, key, constraints) {
367406
object[key].longitude < northEast.longitude
368407
);
369408
case '$containedBy': {
409+
let index;
370410
for (const value of object[key]) {
371-
if (!contains(compareTo, value)) {
411+
index = index || indexList(compareTo);
412+
if (!isIndexed(index, value)) {
372413
return false;
373414
}
374415
}

0 commit comments

Comments
 (0)