|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 4 | +import { ObjectQL } from './engine'; |
| 5 | +import { SchemaRegistry } from './registry'; |
| 6 | + |
| 7 | +/** |
| 8 | + * #2737 — count() and aggregate() must honor middleware-injected read filters. |
| 9 | + * |
| 10 | + * The security/sharing middlewares inject RLS / OWD-scope filters into |
| 11 | + * `opCtx.ast.where`. find() carried its AST on the opCtx, so records were |
| 12 | + * scoped — but count() and aggregate() built a LOCAL ast inside the executor |
| 13 | + * from the caller's raw `where`, discarding every injected filter. Result: |
| 14 | + * `GET /data/:object` returned scoped `records` with an UNSCOPED `total` |
| 15 | + * (a row-count oracle over invisible records, and broken pagination). |
| 16 | + * |
| 17 | + * These tests assert on what the DRIVER receives: the middleware's filter |
| 18 | + * must be present in the ast that reaches driver.count / driver.aggregate. |
| 19 | + */ |
| 20 | +vi.mock('./registry', () => { |
| 21 | + const instance: any = { |
| 22 | + getObject: vi.fn(), |
| 23 | + resolveObject: vi.fn((n: string) => instance.getObject(n)), |
| 24 | + registerObject: vi.fn(), |
| 25 | + getObjectOwner: vi.fn(), |
| 26 | + registerNamespace: vi.fn(), |
| 27 | + registerKind: vi.fn(), |
| 28 | + registerItem: vi.fn(), |
| 29 | + registerApp: vi.fn(), |
| 30 | + installPackage: vi.fn(), |
| 31 | + reset: vi.fn(), |
| 32 | + metadata: { get: vi.fn(() => new Map()) }, |
| 33 | + }; |
| 34 | + function SchemaRegistry() { |
| 35 | + return instance; |
| 36 | + } |
| 37 | + Object.assign(SchemaRegistry, instance); |
| 38 | + return { |
| 39 | + SchemaRegistry, |
| 40 | + computeFQN: (_ns: string | undefined, name: string) => name, |
| 41 | + parseFQN: (fqn: string) => ({ namespace: undefined, shortName: fqn }), |
| 42 | + RESERVED_NAMESPACES: new Set(['base', 'system']), |
| 43 | + }; |
| 44 | +}); |
| 45 | + |
| 46 | +const NOTE_SCHEMA = { |
| 47 | + name: 'note', |
| 48 | + fields: { |
| 49 | + title: { type: 'text' }, |
| 50 | + owner: { type: 'text' }, |
| 51 | + }, |
| 52 | +}; |
| 53 | + |
| 54 | +function makeDriver() { |
| 55 | + const seen: { countAst?: any; aggregateAst?: any; findAst?: any } = {}; |
| 56 | + const driver: any = { |
| 57 | + name: 'memory', |
| 58 | + supports: {}, |
| 59 | + connect: vi.fn().mockResolvedValue(undefined), |
| 60 | + disconnect: vi.fn().mockResolvedValue(undefined), |
| 61 | + find: vi.fn(async (_o: string, ast: any) => { |
| 62 | + seen.findAst = ast; |
| 63 | + return []; |
| 64 | + }), |
| 65 | + count: vi.fn(async (_o: string, ast: any) => { |
| 66 | + seen.countAst = ast; |
| 67 | + return 0; |
| 68 | + }), |
| 69 | + aggregate: vi.fn(async (_o: string, ast: any) => { |
| 70 | + seen.aggregateAst = ast; |
| 71 | + return []; |
| 72 | + }), |
| 73 | + create: vi.fn(), |
| 74 | + update: vi.fn(), |
| 75 | + delete: vi.fn(), |
| 76 | + }; |
| 77 | + return { driver, seen }; |
| 78 | +} |
| 79 | + |
| 80 | +/** A read-filter middleware shaped like the security/sharing ones. */ |
| 81 | +function injectOwnerFilter(ql: ObjectQL) { |
| 82 | + ql.registerMiddleware(async (ctx: any, next: () => Promise<void>) => { |
| 83 | + if (['find', 'findOne', 'count', 'aggregate'].includes(ctx.operation)) { |
| 84 | + const scoped = { owner: 'me' }; |
| 85 | + const ast: any = ctx.ast ?? { object: ctx.object }; |
| 86 | + ast.where = ast.where ? { $and: [ast.where, scoped] } : scoped; |
| 87 | + ctx.ast = ast; |
| 88 | + } |
| 89 | + await next(); |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +async function makeEngine(driver: any) { |
| 94 | + vi.mocked((SchemaRegistry as any).getObject).mockImplementation((name: string) => |
| 95 | + name === 'note' ? NOTE_SCHEMA : undefined, |
| 96 | + ); |
| 97 | + const ql = new ObjectQL(); |
| 98 | + ql.registerDriver(driver, true); |
| 99 | + await ql.init(); |
| 100 | + return ql; |
| 101 | +} |
| 102 | + |
| 103 | +describe('engine read scoping — count/aggregate honor injected filters (#2737)', () => { |
| 104 | + beforeEach(() => { |
| 105 | + vi.clearAllMocks(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('count(): the middleware filter reaches driver.count', async () => { |
| 109 | + const { driver, seen } = makeDriver(); |
| 110 | + const ql = await makeEngine(driver); |
| 111 | + injectOwnerFilter(ql); |
| 112 | + |
| 113 | + await ql.count('note', { where: { title: 'x' } }); |
| 114 | + |
| 115 | + expect(seen.countAst?.where).toEqual({ $and: [{ title: 'x' }, { owner: 'me' }] }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('count() with no caller where: filter still applies', async () => { |
| 119 | + const { driver, seen } = makeDriver(); |
| 120 | + const ql = await makeEngine(driver); |
| 121 | + injectOwnerFilter(ql); |
| 122 | + |
| 123 | + await ql.count('note'); |
| 124 | + |
| 125 | + expect(seen.countAst?.where).toEqual({ owner: 'me' }); |
| 126 | + }); |
| 127 | + |
| 128 | + it('aggregate(): the middleware filter reaches driver.aggregate', async () => { |
| 129 | + const { driver, seen } = makeDriver(); |
| 130 | + const ql = await makeEngine(driver); |
| 131 | + injectOwnerFilter(ql); |
| 132 | + |
| 133 | + await ql.aggregate('note', { |
| 134 | + where: { title: 'x' }, |
| 135 | + groupBy: ['owner'], |
| 136 | + aggregations: [{ func: 'count', field: 'id', alias: 'n' }], |
| 137 | + } as any); |
| 138 | + |
| 139 | + expect(seen.aggregateAst?.where).toEqual({ $and: [{ title: 'x' }, { owner: 'me' }] }); |
| 140 | + // groupBy/aggregations survive on the same ast. |
| 141 | + expect(seen.aggregateAst?.groupBy).toEqual(['owner']); |
| 142 | + }); |
| 143 | + |
| 144 | + it('count() and find() see the SAME scoped where (total matches records)', async () => { |
| 145 | + const { driver, seen } = makeDriver(); |
| 146 | + const ql = await makeEngine(driver); |
| 147 | + injectOwnerFilter(ql); |
| 148 | + |
| 149 | + await ql.find('note', { where: { title: 'x' } }); |
| 150 | + await ql.count('note', { where: { title: 'x' } }); |
| 151 | + |
| 152 | + expect(seen.countAst?.where).toEqual(seen.findAst?.where); |
| 153 | + }); |
| 154 | +}); |
0 commit comments