Skip to content

Commit 30f0f7e

Browse files
committed
test(objectql,rest,dogfood): pin the stored-row read gate, its parity with the /meta type gate, and event body withholding
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibwurKjbb54r3yD6yJg
1 parent 5679a8d commit 30f0f7e

4 files changed

Lines changed: 394 additions & 7 deletions

File tree

‎packages/objectql/src/engine-realtime-stored-metadata-body.test.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,18 @@ describe('[#21120] realtime data.record.* events redact a stored metadata body',
109109
expect(published).toHaveLength(1);
110110
const payload = published[0].payload as Record<string, any>;
111111
expect(JSON.stringify(payload)).not.toContain(CRED);
112-
// The event still describes the row — type survives, the body is still present but projected.
112+
// The event still describes the row — type survives. [#27884] A datasource
113+
// body is withheld whole (the connection configuration with it): the
114+
// type's documents are served only to holders of its read capability, and
115+
// an event's subscribers are unknown at publish.
113116
expect(payload.after.type).toBe('datasource');
114-
expect(payload.after.metadata).toBeDefined();
115-
expect(JSON.parse(payload.after.metadata).config.authToken).toBeUndefined();
116-
expect(JSON.parse(payload.after.metadata).config.url).toBe('libsql://db.turso.io');
117+
expect(payload.after).not.toHaveProperty('metadata');
118+
expect(JSON.stringify(payload)).not.toContain('libsql://db.turso.io');
117119
});
118120

119-
it('a credential-free sys_metadata body rides the event unchanged', async () => {
120-
const clean = JSON.stringify({ name: 'v', driver: 'postgres', config: { host: 'h', database: 'd' } });
121-
await engine.insert('sys_metadata', { id: 'm2', type: 'datasource', name: 'v', metadata: clean });
121+
it('a credential-free sys_metadata body of an ungated type rides the event unchanged', async () => {
122+
const clean = JSON.stringify({ name: 'v', label: 'V', type: 'grid' });
123+
await engine.insert('sys_metadata', { id: 'm2', type: 'view', name: 'v', metadata: clean });
122124
const payload = published[0].payload as Record<string, any>;
123125
expect(payload.after.metadata).toBe(clean);
124126
});
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#27884] The engine's stored-row read gate (`stored-metadata-type-read-gate.ts`):
5+
* a caller-context read of `sys_metadata` / `sys_metadata_history` serves a
6+
* capability-gated type's documents (a datasource's connection configuration
7+
* and secret handle) only to holders of the capability the `/meta` type gate
8+
* names for that type. Every pin here is a negative or a preservation pin
9+
* against a driver that really evaluates the composed predicate.
10+
*/
11+
12+
import { describe, it, expect, beforeEach, vi } from 'vitest';
13+
import type { IRealtimeService, RealtimeEventPayload } from '@objectstack/spec/contracts';
14+
import { STORED_METADATA_TYPE_READ_CAPABILITIES, storedMetadataTypesWithheldFrom } from '@objectstack/metadata-core';
15+
import { ObjectQL } from './engine.js';
16+
import { storedMetadataTypeReadFilter } from './stored-metadata-type-read-gate.js';
17+
18+
const CAPABILITY = 'manage_platform_settings';
19+
const REF = 'sys_secret:sec_gate_pin_0001';
20+
const HOST = 'db.gate-pin.internal';
21+
const datasourceBody = JSON.stringify({
22+
name: 'ds_pin',
23+
driver: 'postgres',
24+
config: { host: HOST, port: 5432, database: 'pin_db', username: 'pin_user' },
25+
external: { credentialsRef: REF },
26+
});
27+
const viewBody = JSON.stringify({ name: 'v_pin', label: 'V', type: 'grid' });
28+
29+
const tableOf = (name: string) => ({
30+
name,
31+
label: name,
32+
fields: {
33+
id: { name: 'id', type: 'text' as const, primaryKey: true },
34+
type: { name: 'type', type: 'text' as const },
35+
name: { name: 'name', type: 'text' as const },
36+
metadata: { name: 'metadata', type: 'textarea' as const },
37+
},
38+
});
39+
40+
/** A predicate evaluator for the shapes these reads compose: equality, `$in`, `$nin`, `$and`. */
41+
function matches(row: Record<string, unknown>, where: unknown): boolean {
42+
if (!where || typeof where !== 'object') return true;
43+
return Object.entries(where as Record<string, unknown>).every(([key, cond]) => {
44+
if (key === '$and') return (cond as unknown[]).every((c) => matches(row, c));
45+
const value = row[key] ?? null;
46+
if (cond && typeof cond === 'object' && !Array.isArray(cond)) {
47+
const c = cond as Record<string, unknown>;
48+
if ('$nin' in c) return !(c.$nin as unknown[]).includes(value);
49+
if ('$in' in c) return (c.$in as unknown[]).includes(value);
50+
if ('$eq' in c) return value === c.$eq;
51+
throw new Error(`pin driver: unsupported condition on ${key}: ${JSON.stringify(c)}`);
52+
}
53+
return value === (cond ?? null);
54+
});
55+
}
56+
57+
function makeDriver() {
58+
const stores = new Map<string, Map<string, Record<string, unknown>>>();
59+
const storeFor = (o: string) => {
60+
let s = stores.get(o);
61+
if (!s) { s = new Map(); stores.set(o, s); }
62+
return s;
63+
};
64+
const rows = (o: string, ast: any) => Array.from(storeFor(o).values()).filter((r) => matches(r, ast?.where));
65+
const driver: any = {
66+
name: 'memory', version: '0.0.0', supports: {},
67+
async connect() {}, async disconnect() {}, async checkHealth() { return true; }, async execute() { return null; },
68+
async find(o: string, ast: any) { return rows(o, ast); },
69+
findStream() { throw new Error('ns'); },
70+
async findOne(o: string, ast: any) { return rows(o, ast)[0] ?? null; },
71+
async count(o: string, ast: any) { return rows(o, ast).length; },
72+
async aggregate(o: string, ast: any) { return [{ n: rows(o, ast).length }]; },
73+
async create(o: string, data: Record<string, unknown>) {
74+
const row = { ...data };
75+
storeFor(o).set(String(data.id), row);
76+
return row;
77+
},
78+
async update(o: string, id: string, data: Record<string, unknown>) {
79+
const s = storeFor(o); const up = { ...s.get(id), ...data, id }; s.set(id, up); return up;
80+
},
81+
async delete(o: string, id: string) { return storeFor(o).delete(id); },
82+
async bulkCreate(o: string, rs: Record<string, unknown>[]) { return Promise.all(rs.map((r) => this.create(o, r))); },
83+
async updateMany() { return 0; }, async deleteMany() { return 0; },
84+
async bulkUpdate() { return []; }, async bulkDelete() {},
85+
async upsert(o: string, data: Record<string, unknown>) { return this.create(o, data); },
86+
async beginTransaction() { return { commit: async () => {}, rollback: async () => {} }; },
87+
async commit() {}, async rollback() {},
88+
};
89+
return driver;
90+
}
91+
92+
/** A datasource administrator: holds the row's capability (platform administrators do). */
93+
const ADMIN = { userId: 'u_admin', systemPermissions: [CAPABILITY, 'manage_metadata', 'studio.access'] };
94+
/**
95+
* The shipped organization administrator's capability set — below datasource
96+
* administration (`organization_admin`, `@objectstack/plugin-security`).
97+
*/
98+
const ORG_ADMIN = { userId: 'u_org_admin', systemPermissions: ['manage_org_users', 'setup.access', 'setup.write', 'manage_mailboxes'] };
99+
/** A caller that may enter Studio (and so read the history table) without datasource administration. */
100+
const STUDIO_AUTHOR = { userId: 'u_author', systemPermissions: ['studio.access', 'manage_metadata'] };
101+
/** A plain reader: no capability set at all. */
102+
const READER = { userId: 'u_reader' };
103+
104+
describe('[#27884] stored-row read gate on the stored-metadata tables', () => {
105+
let engine: ObjectQL;
106+
107+
beforeEach(async () => {
108+
engine = new ObjectQL();
109+
engine.registerDriver(makeDriver(), true);
110+
await engine.init();
111+
for (const table of ['sys_metadata', 'sys_metadata_history']) {
112+
engine.registry.registerObject(tableOf(table));
113+
await engine.insert(table, { id: `${table}_ds`, type: 'datasource', name: 'ds_pin', metadata: datasourceBody }, { context: { isSystem: true } } as any);
114+
await engine.insert(table, { id: `${table}_cat`, type: 'external_catalog', name: 'ds_pin', metadata: '{}' }, { context: { isSystem: true } } as any);
115+
await engine.insert(table, { id: `${table}_view`, type: 'view', name: 'v_pin', metadata: viewBody }, { context: { isSystem: true } } as any);
116+
}
117+
vi.spyOn((engine as any).logger, 'debug').mockImplementation(() => undefined);
118+
});
119+
120+
it('a datasource administrator reads the row whole: connection configuration and credentialsRef', async () => {
121+
const rows = await engine.find('sys_metadata', { where: { type: 'datasource' }, context: ADMIN } as any);
122+
expect(rows).toHaveLength(1);
123+
const body = JSON.parse(rows[0].metadata);
124+
expect(body.external.credentialsRef).toBe(REF);
125+
expect(body.config).toMatchObject({ host: HOST, port: 5432, database: 'pin_db', username: 'pin_user' });
126+
const one = await engine.findOne('sys_metadata', { where: { id: 'sys_metadata_ds' }, context: ADMIN } as any);
127+
expect(one?.type).toBe('datasource');
128+
expect(await engine.count('sys_metadata', { context: ADMIN } as any)).toBe(3);
129+
});
130+
131+
for (const [label, caller] of [['the built-in organization administrator', ORG_ADMIN], ['a plain reader', READER]] as const) {
132+
it(`${label} is served no datasource-family row on sys_metadata (find, findOne, count, aggregate)`, async () => {
133+
const listed = await engine.find('sys_metadata', { context: caller } as any);
134+
expect(listed.map((r: any) => r.type).sort()).toEqual(['view']);
135+
expect(JSON.stringify(listed)).not.toContain(REF);
136+
expect(JSON.stringify(listed)).not.toContain(HOST);
137+
138+
// Asked for by type: nothing, not a refusal that names the row.
139+
expect(await engine.find('sys_metadata', { where: { type: 'datasource' }, context: caller } as any)).toEqual([]);
140+
// A get of the row's id answers as a get of a row that does not exist.
141+
expect(await engine.findOne('sys_metadata', { where: { id: 'sys_metadata_ds' }, context: caller } as any)).toBeNull();
142+
expect(await engine.count('sys_metadata', { where: { type: 'datasource' }, context: caller } as any)).toBe(0);
143+
expect(await engine.count('sys_metadata', { context: caller } as any)).toBe(1);
144+
const agg: any = await engine.aggregate('sys_metadata', { where: { type: 'external_catalog' }, groupBy: ['type'], aggregations: [{ function: 'count', alias: 'n' }], context: caller } as any);
145+
expect(JSON.stringify(agg)).toContain('"n":0');
146+
});
147+
}
148+
149+
it('the history table (the same document, versioned) is gated by the same row', async () => {
150+
const author = await engine.find('sys_metadata_history', { context: STUDIO_AUTHOR } as any);
151+
expect(author.map((r: any) => r.type)).toEqual(['view']);
152+
expect(await engine.findOne('sys_metadata_history', { where: { id: 'sys_metadata_history_ds' }, context: STUDIO_AUTHOR } as any)).toBeNull();
153+
const admin = await engine.find('sys_metadata_history', { context: ADMIN } as any);
154+
expect(admin.map((r: any) => r.type).sort()).toEqual(['datasource', 'external_catalog', 'view']);
155+
});
156+
157+
it('a system read is the platform\'s own and is not narrowed', async () => {
158+
const rows = await engine.find('sys_metadata', { context: { isSystem: true } } as any);
159+
expect(rows).toHaveLength(3);
160+
});
161+
162+
it('the composed predicate names exactly the types whose capability the caller lacks', () => {
163+
expect(storedMetadataTypeReadFilter({ object: 'sys_metadata', operation: 'find', context: ORG_ADMIN as any }))
164+
.toEqual({ type: { $nin: ['datasource', 'external_catalog'] } });
165+
expect(storedMetadataTypeReadFilter({ object: 'sys_metadata', operation: 'find', context: ADMIN as any })).toBeUndefined();
166+
// Outside the family, and on a write, nothing is composed.
167+
expect(storedMetadataTypeReadFilter({ object: 'task', operation: 'find', context: READER as any })).toBeUndefined();
168+
expect(storedMetadataTypeReadFilter({ object: 'sys_metadata', operation: 'update', context: READER as any })).toBeUndefined();
169+
// Every type in the row is withheld from a caller with no capability set.
170+
expect(storedMetadataTypesWithheldFrom(READER)).toEqual(Object.keys(STORED_METADATA_TYPE_READ_CAPABILITIES));
171+
});
172+
});
173+
174+
describe('[#27884] realtime events of a datasource-family row carry no body to any subscriber', () => {
175+
it('insert and update events of a datasource row withhold the body; an ungated row keeps it', async () => {
176+
const published: RealtimeEventPayload[] = [];
177+
const realtime: IRealtimeService = {
178+
publish: vi.fn(async (event: RealtimeEventPayload) => { published.push(event); }),
179+
subscribe: vi.fn(async () => 'sub-1'),
180+
unsubscribe: vi.fn(async () => undefined),
181+
};
182+
const engine = new ObjectQL();
183+
engine.registerDriver(makeDriver(), true);
184+
await engine.init();
185+
engine.registry.registerObject(tableOf('sys_metadata'));
186+
engine.setRealtimeService(realtime);
187+
vi.spyOn((engine as any).logger, 'debug').mockImplementation(() => undefined);
188+
189+
await engine.insert('sys_metadata', { id: 'e_ds', type: 'datasource', name: 'ds_pin', metadata: datasourceBody }, { context: { isSystem: true } } as any);
190+
await engine.update('sys_metadata', { id: 'e_ds', metadata: datasourceBody }, { context: { isSystem: true } } as any);
191+
await engine.insert('sys_metadata', { id: 'e_cat', type: 'external_catalog', name: 'ds_pin', metadata: '{"tables":[]}' }, { context: { isSystem: true } } as any);
192+
await engine.insert('sys_metadata', { id: 'e_view', type: 'view', name: 'v_pin', metadata: viewBody }, { context: { isSystem: true } } as any);
193+
194+
expect(published).toHaveLength(4);
195+
for (const event of published.slice(0, 3)) {
196+
const payload = event.payload as Record<string, any>;
197+
expect(JSON.stringify(payload)).not.toContain(REF);
198+
expect(JSON.stringify(payload)).not.toContain(HOST);
199+
expect(payload.after).not.toHaveProperty('metadata');
200+
if (payload.changes) expect(payload.changes).not.toHaveProperty('metadata');
201+
}
202+
expect((published[3].payload as Record<string, any>).after.metadata).toBe(viewBody);
203+
});
204+
});

0 commit comments

Comments
 (0)