@@ -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});
0 commit comments