Skip to content

Commit 0857b74

Browse files
committed
wip(objectql): release a deleted record's sys_secret handles
Claude-Session: https://claude.ai/code/session_01L6bhgUFEohazYphk3zDAum Co-authored-by: Claude <noreply@anthropic.com>
1 parent 9c51314 commit 0857b74

3 files changed

Lines changed: 225 additions & 12 deletions

File tree

‎packages/objectql/src/engine-secret-internal-fields.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
* bodies joined it — `persistSecretRows` writes the sealed rows immediately
2121
* before the record's own driver write, and `releaseSecretHandles` removes a
2222
* handle once a write that did not land, or a committed one that moved the
23-
* column off it, has left it unreferenced.
23+
* column off it, has left it unreferenced. [#27581] A committed delete hands
24+
* the same release every handle the deleted rows held (`deletedSecretSlots`).
2425
*
2526
* ## How the moved bodies reach the class
2627
*
@@ -228,6 +229,30 @@ export function retiredSecretSlots(
228229
return out;
229230
}
230231

232+
/**
233+
* [#27581] The `secret` columns a delete reads off the rows it removes, before
234+
* its statement takes them away; empty for an object that declares none, so a
235+
* delete anywhere else reads nothing more.
236+
*/
237+
export function deletedSecretColumns(schema: Parameters<typeof collectSecretFields>[0]): string[] {
238+
return collectSecretFields(schema);
239+
}
240+
241+
/**
242+
* [#27581] The handles a committed delete let go of: every handle the deleted
243+
* rows held in a `secret` column. A delete lets go of each column's value at
244+
* once, so the candidates are exactly what a `null` clear of every one of
245+
* those columns retires ({@link retiredSecretSlots}), and the same
246+
* `releaseSecretHandles` checks decide each one: there is one release rule.
247+
*/
248+
export function deletedSecretSlots(
249+
schema: Parameters<typeof collectSecretFields>[0],
250+
deletedRows: readonly (Record<string, unknown> | null | undefined)[],
251+
): SecretHandleSlot[] {
252+
const clearEvery = Object.fromEntries(deletedSecretColumns(schema).map((field) => [field, null]));
253+
return retiredSecretSlots(schema, deletedRows, clearEvery);
254+
}
255+
231256
/** The body of `ObjectQL#persistSecretRows`; the method's docblock there is its contract. */
232257
export async function persistSecretRows(
233258
this: Pick<SecretInternalFieldsHost, 'getDriver'>,

‎packages/objectql/src/engine.ts‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ import { publishBulkDataEvent, publishDataEvent } from './engine-realtime-publis
108108
// [#25522 cut 5] The secret and internal-field bodies; the methods of the same
109109
// names below hand each body the members it reads (`SecretInternalFieldsHost`).
110110
import {
111+
deletedSecretColumns,
112+
deletedSecretSlots,
111113
encryptSecretFields,
112114
maskSecretFields,
113115
omitInternalFields,
@@ -10615,7 +10617,9 @@ export class ObjectQL implements IObjectQLEngine {
1061510617
* Two occasions reach it. `'refused'`: a write that sealed and wrote a row
1061610618
* did not complete, so the row may name nothing. `'retired'`: a committed
1061710619
* update cleared the field (`null`) or replaced its value, so the handle the
10618-
* column named before may name nothing. The same three checks decide both,
10620+
* column named before may name nothing; [#27581] a committed delete reaches
10621+
* it the same way, for every handle the removed rows held. The same three
10622+
* checks decide both,
1061910623
* and every one that cannot pass KEEPS the row, the recoverable direction
1062010624
* (deleting a handle a row still names destroys that credential, and nothing
1062110625
* can name what was lost):
@@ -19811,6 +19815,11 @@ export class ObjectQL implements IObjectQLEngine {
1981119815
// delete (`null` when no roll-up aggregates this object).
1981219816
let predicateMatchedRows: Record<string, unknown>[] | null = null;
1981319817
let summaryPriorRows: Record<string, unknown>[] | null = null;
19818+
// [#27581] The `secret` columns this object declares, and the handles the
19819+
// removed rows held in them: the by-id pre-image, or the predicate
19820+
// path's matched rows, read before the statement takes them away.
19821+
const secretColumns = deletedSecretColumns(deleteSchema);
19822+
let deletedSecrets: SecretHandleSlot[] = [];
1981419823

1981519824
// [#5574] The dispatch ladder, resolved BEFORE the before phase — see
1981619825
// update()'s twin for the full reasoning. Keyed on the SAME falsy-`id`
@@ -20032,6 +20041,8 @@ export class ObjectQL implements IObjectQLEngine {
2003220041
if (plan === 'split') this.warnCascadeNotAtomic(object);
2003320042
result = await runByIdDelete(opCtx.context);
2003420043
}
20044+
// [#27581] The handles the removed row held, off its pre-image.
20045+
deletedSecrets = deletedSecretSlots(deleteSchema, [priorRecord]);
2003520046
} else {
2003620047
// [#2982] The AST asserted present and already used for the
2003720048
// pre-phase row read above.
@@ -20066,6 +20077,14 @@ export class ObjectQL implements IObjectQLEngine {
2006620077
summaryPriorRows = predicateMatchedRows
2006720078
?? (((await driver.find(object, { ...opCtx.ast!, fields: summaryKeyColumns }, hookContext.input.options as Parameters<IDataDriver['find']>[2])) ?? []) as Record<string, unknown>[]);
2006820079
}
20080+
// [#27581] The handles the matched rows hold, read BEFORE the
20081+
// statement removes them: the per-row hook read when it was
20082+
// made, otherwise only `id` and the secret columns, through
20083+
// this write's AST and options. No secret column, no read.
20084+
if (secretColumns.length > 0) {
20085+
deletedSecrets = deletedSecretSlots(deleteSchema, predicateMatchedRows
20086+
?? (((await driver.find(object, { ...opCtx.ast!, fields: ['id', ...secretColumns] }, hookContext.input.options as Parameters<IDataDriver['find']>[2])) ?? []) as Record<string, unknown>[]));
20087+
}
2006920088
result = await driver.deleteMany!(object, opCtx.ast!, hookContext.input.options as any);
2007020089
} else {
2007120090
// Otherwise: resolve the matched rows through the composed,
@@ -20081,9 +20100,12 @@ export class ObjectQL implements IObjectQLEngine {
2008120100
// branch's, for the same reasons (#7413).
2008220101
const runPredicateDelete = async (writeContext?: ExecutionContext): Promise<unknown> => {
2008320102
const writeOptions = this.buildDriverOptions(object, writeContext, hookContext.input.options as any);
20084-
const matched = ((await driver.find(object, { ...opCtx.ast!, fields: ['id', ...summaryKeyColumns] }, writeOptions)) ?? []) as Array<Record<string, unknown>>;
20103+
// [#27581] The same read carries the secret columns, so the
20104+
// handles released are those of the rows deleted.
20105+
const matched = ((await driver.find(object, { ...opCtx.ast!, fields: ['id', ...summaryKeyColumns, ...secretColumns] }, writeOptions)) ?? []) as Array<Record<string, unknown>>;
2008520106
const matchedIds = matched.map((row) => row?.id).filter((v): v is string | number => v != null);
2008620107
if (summaryKeyColumns.length > 0) summaryPriorRows = withoutDeletedSelfParents(object, matched, matchedIds, this.getSummaryDescriptors(object));
20108+
deletedSecrets = deletedSecretSlots(deleteSchema, matched);
2008720109
// [#27004] One walk, rooted at the whole matched set.
2008820110
await this.cascadeDeleteRelations(object, matchedIds, writeContext);
2008920111
// The id set rides an `$in`; batched so one statement never
@@ -20146,6 +20168,14 @@ export class ObjectQL implements IObjectQLEngine {
2014620168
});
2014720169
}
2014820170

20171+
// [#27581] The delete is complete: release the handles the removed
20172+
// rows held, through the release a committed update uses, so each one
20173+
// goes only once storage confirms no row of its column still names it,
20174+
// inside this write's unit. After the `afterDelete` chain, so a hook
20175+
// can still dereference the value and a refusal that rolls the delete
20176+
// back never reaches here.
20177+
await this.releaseSecretHandles(object, deletedSecrets, opCtx.context, 'retired');
20178+
2014920179
// Roll-up: recompute the parent summary now that the child is gone,
2015020180
// from the row's FK values captured BEFORE deletion. [#5272] That
2015120181
// capture is now the same single pre-image read `previous` rides on

‎packages/objectql/src/secret-field-write-commit.test.ts‎

Lines changed: 167 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
* before, once storage confirms no row of that column still names it and
2222
* the handle is this column's own. A handle a predicate write shared across
2323
* rows outlives every row but the last.
24+
* - **Released when the record goes** ([#27581]). A committed delete, by id,
25+
* by predicate or by cascade, releases every handle the removed rows held,
26+
* through the same release and its same checks; a delete that is refused or
27+
* rolled back keeps the row.
2428
*/
2529

2630
import { describe, it, expect, beforeEach } from 'vitest';
@@ -32,7 +36,13 @@ import { SECRET_MASK, isSecretRef, parseSecretRef } from './secret-fields.js';
3236

3337
type Row = Record<string, unknown>;
3438

35-
/** A stub driver whose stores can be counted, rolled back and told to refuse a statement. */
39+
/**
40+
* A stub driver whose stores can be counted, rolled back and told to refuse a statement.
41+
*
42+
* A rollback restores the snapshot the unit began with, unless `isolation.on`: then it undoes
43+
* only the writes made under that unit's handle, as a database does, so a write that escaped
44+
* the unit survives the rollback ([#27581]).
45+
*/
3646
function makeDriver() {
3747
let stores = new Map<string, Map<string, Row>>();
3848
const storeFor = (o: string) => stores.get(o) ?? (stores.set(o, new Map()), stores.get(o)!);
@@ -48,6 +58,10 @@ function makeDriver() {
4858
/** Objects whose `count` throws, so a release cannot confirm anything. */
4959
const countFails = new Set<string>();
5060
const snapshots = new Map<unknown, typeof stores>();
61+
const isolation = { on: false };
62+
const undoLogs = new Map<unknown, Array<() => void>>();
63+
/** Record how to take back one write, when it was made under an open unit's handle. */
64+
const undoable = (opts: any, take: () => void) => { undoLogs.get(opts?.transaction)?.push(take); };
5165
let n = 0;
5266
const refused = (o: string) => {
5367
if (refuse.has(o)) throw Object.assign(new Error(`statement refused on ${o}`), { code: 'ETIMEDOUT' });
@@ -62,29 +76,46 @@ function makeDriver() {
6276
return page.map(copy);
6377
},
6478
async findOne(o: string, ast: any) { return (await this.find(o, ast))[0] ?? null; },
65-
async create(o: string, data: Row) {
79+
async create(o: string, data: Row, opts?: any) {
6680
refused(o);
6781
const row = { ...data, id: (data.id as string) ?? `${o}_${++n}` };
6882
storeFor(o).set(String(row.id), row);
83+
undoable(opts, () => storeFor(o).delete(String(row.id)));
6984
return copy(row);
7085
},
71-
async update(o: string, id: string, data: Row) {
86+
async update(o: string, id: string, data: Row, opts?: any) {
7287
refused(o);
73-
const row = { ...storeFor(o).get(String(id)), ...data, id };
88+
const prior = storeFor(o).get(String(id));
89+
const row = { ...prior, ...data, id };
7490
storeFor(o).set(String(id), row);
91+
undoable(opts, () => (prior ? storeFor(o).set(String(id), prior) : storeFor(o).delete(String(id))));
7592
return copy(row);
7693
},
77-
async updateMany(o: string, ast: any, data: Row) {
94+
async updateMany(o: string, ast: any, data: Row, opts?: any) {
7895
refused(o);
7996
let k = 0;
8097
for (const [id, row] of storeFor(o)) {
8198
if (!matches(row, ast?.where)) continue;
8299
storeFor(o).set(id, { ...row, ...data, id });
100+
undoable(opts, () => storeFor(o).set(id, row));
101+
k += 1;
102+
}
103+
return k;
104+
},
105+
async delete(o: string, id: string, opts?: any) {
106+
const prior = storeFor(o).get(String(id));
107+
if (prior) undoable(opts, () => storeFor(o).set(String(id), prior));
108+
return storeFor(o).delete(String(id));
109+
},
110+
async deleteMany(o: string, ast: any, opts?: any) {
111+
let k = 0;
112+
for (const [id, row] of Array.from(storeFor(o))) {
113+
if (!matches(row, ast?.where)) continue;
114+
await this.delete(o, id, opts);
83115
k += 1;
84116
}
85117
return k;
86118
},
87-
async delete(o: string, id: string) { return storeFor(o).delete(String(id)); },
88119
async count(o: string, ast: any) {
89120
if (countFails.has(o)) throw new Error(`count unavailable on ${o}`);
90121
return (await this.find(o, ast)).length;
@@ -94,13 +125,19 @@ function makeDriver() {
94125
async beginTransaction() {
95126
const handle = { trx: snapshots.size + 1 };
96127
snapshots.set(handle, new Map(Array.from(stores, ([o, s]) => [o, new Map(Array.from(s, ([k, v]) => [k, { ...v }]))])));
128+
undoLogs.set(handle, []);
97129
return handle;
98130
},
99-
async commit(h: unknown) { snapshots.delete(h); },
100-
async rollback(h: unknown) { stores = snapshots.get(h) ?? stores; snapshots.delete(h); },
131+
async commit(h: unknown) { snapshots.delete(h); undoLogs.delete(h); },
132+
async rollback(h: unknown) {
133+
if (isolation.on) for (const take of (undoLogs.get(h) ?? []).reverse()) take();
134+
else stores = snapshots.get(h) ?? stores;
135+
snapshots.delete(h);
136+
undoLogs.delete(h);
137+
},
101138
};
102139
return {
103-
driver, refuse, countFails,
140+
driver, refuse, countFails, isolation,
104141
rowsOf: (o: string) => Array.from(storeFor(o).values()),
105142
};
106143
}
@@ -145,6 +182,16 @@ const other = {
145182
};
146183

147184
const SYSTEM = { context: { isSystem: true } } as any;
185+
/** [#27581] A detail of the vault, holding its own secret: a cascade target. */
186+
const KEY = 'qa_vault_key';
187+
const vaultKey = {
188+
name: KEY, label: 'Vault key',
189+
fields: {
190+
id: { name: 'id', type: 'text' }, name: { name: 'name', type: 'text' },
191+
vault: { name: 'vault', type: 'master_detail', reference: VAULT },
192+
key_token: { name: 'key_token', type: 'secret' },
193+
},
194+
};
148195

149196
describe('[#27495] a sys_secret row exists only while a committed row names it', () => {
150197
let engine: ObjectQL;
@@ -371,4 +418,115 @@ describe('[#27495] a sys_secret row exists only while a committed row names it',
371418
expect(errors[0]).not.toContain(before);
372419
});
373420
});
421+
422+
describe('[#27581] a committed delete releases the handles the removed rows held', () => {
423+
it('a by-id delete removes the row its record named', async () => {
424+
const gone = await seed('r1', 'gone');
425+
const kept = await seed('r2', 'kept');
426+
await expect(engine.delete(VAULT, { where: { id: 'r1' } })).resolves.toBe(true);
427+
expect(secrets().map((s) => s.id)).toEqual([kept]);
428+
expect(secrets().some((s) => s.id === gone)).toBe(false);
429+
expect(await engine.resolveSecretField(VAULT, 'r2', 'token')).toBe('kept');
430+
expect(errors).toEqual([]);
431+
});
432+
433+
it('a predicate delete removes every matched row\'s handle and no other', async () => {
434+
await seed('r1', 'one');
435+
await seed('r2', 'two');
436+
await engine.update(VAULT, { id: 'r2', tier: 'silver' });
437+
const silver = handleOf('r2');
438+
await seed('r3', 'three');
439+
await expect(engine.delete(VAULT, { where: { tier: 'gold' }, multi: true } as any)).resolves.toBe(2);
440+
expect(secrets().map((s) => s.id)).toEqual([silver]);
441+
});
442+
443+
it('a predicate delete with a per-row hook releases from the rows that hook read', async () => {
444+
const seen: unknown[] = [];
445+
engine.registerHook('afterDelete', (ctx: any) => { seen.push(ctx.previous?.id); }, { object: VAULT });
446+
await seed('r1', 'one');
447+
await seed('r2', 'two');
448+
await engine.delete(VAULT, { where: { tier: 'gold' }, multi: true } as any);
449+
expect(seen.sort()).toEqual(['r1', 'r2']);
450+
expect(secrets()).toHaveLength(0);
451+
});
452+
453+
it('a cascade removes the detail rows\' handles with the parent\'s, by id and by predicate', async () => {
454+
engine.registry.registerObject(vaultKey as any);
455+
await seed('r1', 'parent-1');
456+
await seed('r2', 'parent-2');
457+
await engine.insert(KEY, { id: 'k1', name: 'k1', vault: 'r1', key_token: 'child-1' }, SYSTEM);
458+
await engine.insert(KEY, { id: 'k2', name: 'k2', vault: 'r2', key_token: 'child-2' }, SYSTEM);
459+
expect(secrets()).toHaveLength(4);
460+
await engine.delete(VAULT, { where: { id: 'r1' } });
461+
expect(d.rowsOf(KEY).map((r) => r.id)).toEqual(['k2']);
462+
expect(secrets().map((s) => s.key).sort()).toEqual(['key_token', 'token']);
463+
await engine.delete(VAULT, { where: { tier: 'gold' }, multi: true } as any);
464+
expect(d.rowsOf(KEY)).toHaveLength(0);
465+
expect(secrets()).toHaveLength(0);
466+
});
467+
468+
it('a handle another row still names outlives the delete of every row but the last', async () => {
469+
await seed('r1', 'one');
470+
await seed('r2', 'two');
471+
await engine.update(VAULT, { token: 'shared' }, { where: { tier: 'gold' }, multi: true } as any);
472+
const shared = handleOf('r1');
473+
expect(handleOf('r2')).toBe(shared);
474+
475+
await engine.delete(VAULT, { where: { id: 'r1' } });
476+
expect(secrets().map((s) => s.id)).toEqual([shared]);
477+
expect(await engine.resolveSecretField(VAULT, 'r2', 'token')).toBe('shared');
478+
479+
await engine.delete(VAULT, { where: { id: 'r2' } });
480+
expect(secrets()).toHaveLength(0);
481+
});
482+
483+
it('deleting a row that holds another column\'s handle releases nothing', async () => {
484+
const foreign = await seed('r1', 'theirs');
485+
await engine.insert(OTHER, { id: 'o1', name: 'copy', other_token: `secret:${foreign}` }, SYSTEM);
486+
await engine.delete(OTHER, { where: { id: 'o1' } });
487+
expect(secrets().map((s) => s.id)).toEqual([foreign]);
488+
expect(await engine.resolveSecretField(VAULT, 'r1', 'token')).toBe('theirs');
489+
});
490+
491+
it('a delete rolled back with its unit keeps the row, though the record write was undone alone', async () => {
492+
d.isolation.on = true;
493+
const before = await seed('r1', 'stays');
494+
await expect(engine.transaction(async (trx: any) => {
495+
await engine.delete(VAULT, { where: { id: 'r1' }, context: trx });
496+
throw new Error('roll the unit back');
497+
}, SYSTEM.context)).rejects.toThrow(/roll the unit back/);
498+
expect(stored('r1')).toBeDefined();
499+
expect(secrets().map((s) => s.id)).toEqual([before]);
500+
expect(await engine.resolveSecretField(VAULT, 'r1', 'token')).toBe('stays');
501+
});
502+
503+
it('a delete refused by a beforeDelete hook, or rolled back by an aborting afterDelete, keeps the row', async () => {
504+
const before = await seed('r1', 'stays');
505+
engine.registerHook('beforeDelete', () => { throw Object.assign(new Error('hook says no'), { code: 'HOOK_REFUSED', status: 400 }); }, { object: VAULT });
506+
await expect(engine.delete(VAULT, { where: { id: 'r1' } })).rejects.toMatchObject({ code: 'HOOK_REFUSED', status: 400 });
507+
expect(secrets().map((s) => s.id)).toEqual([before]);
508+
509+
const other = await seed('o1', 'also-stays');
510+
engine.registry.registerObject({ ...vault, name: 'qa_vault_two' } as any);
511+
await engine.insert('qa_vault_two', { id: 't1', name: 't1', token: 'two-stays' }, SYSTEM);
512+
const twoHandle = parseSecretRef(d.rowsOf('qa_vault_two')[0].token);
513+
bindHooksToEngine(engine, [
514+
{ name: 'veto', object: 'qa_vault_two', events: ['afterDelete'], handler: () => { throw new Error('refused by the hook'); } } as unknown as Hook,
515+
], { packageId: 'app:t', logger });
516+
await expect(engine.delete('qa_vault_two', { where: { id: 't1' } })).rejects.toThrow(/refused by the hook/);
517+
expect(d.rowsOf('qa_vault_two')).toHaveLength(1);
518+
expect(secrets().map((s) => s.id).sort()).toEqual([before, other, twoHandle].sort());
519+
});
520+
521+
it('a release that cannot confirm the column let go keeps the row and says so', async () => {
522+
const before = await seed('r1', 'stays-stored');
523+
d.countFails.add(VAULT);
524+
await engine.delete(VAULT, { where: { id: 'r1' } });
525+
expect(d.rowsOf(VAULT)).toHaveLength(0);
526+
expect(secrets().map((s) => s.id)).toEqual([before]);
527+
expect(errors).toHaveLength(1);
528+
expect(errors[0]).toContain(`${VAULT}.token`);
529+
expect(errors[0]).not.toContain(before);
530+
});
531+
});
374532
});

0 commit comments

Comments
 (0)