Skip to content

Commit d8e9534

Browse files
committed
fix(runtime,spec): app-authored bodies cannot pass permanent on a delete; the hook face omits it
HookDeleteOptions omits permanent, and the drift pin lists it under omitted. The sandbox body API refuses a body's delete carrying permanent (any value but false) with 400 VALIDATION_ERROR, param permanent, before the delete runs, so a job body under its system envelope cannot purge: its deletes enter the recycle bin (ADR-0148 D3, maintainer ruling A). Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibxhbtrhtRZ958iyyJp
1 parent 7c83475 commit d8e9534

3 files changed

Lines changed: 103 additions & 7 deletions

File tree

‎packages/runtime/src/sandbox/body-runner.ts‎

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import {
6464
servedRecordTitle,
6565
type FormulaServingChannel,
6666
} from '@objectstack/objectql';
67-
import { refuseStoredMetadataBodyReads, refuseStoredMetadataBodyWrites } from '../stored-metadata-reader-seam.js';
67+
import { deriveThroughSeam, refuseStoredMetadataBodyReads, refuseStoredMetadataBodyWrites } from '../stored-metadata-reader-seam.js';
6868
import { answerLandedWritesDespiteStaleSummary } from '../landed-write-answer.js';
6969
import { isStoredMetadataBodyObject } from '@objectstack/spec/kernel';
7070
import {
@@ -1161,12 +1161,93 @@ function buildEngineRepoFacade(ql: any, objectName: string, context?: any) {
11611161
* the API is built from: the body runs on to its own result. An action's
11621162
* host-built `api` already answers so (`buildActionApi`); the layer is
11631163
* idempotent.
1164+
*
1165+
* [ADR-0148 D3, ruling A recorded in 6691358077] Above every layer, a body's
1166+
* `delete` that asks for `permanent` is refused
1167+
* ({@link refuseBodyPermanentDeletes}): app-authored code cannot purge, so its
1168+
* deletes enter the recycle bin, whatever envelope the body runs under.
11641169
*/
11651170
function buildSandboxApi(engineCtx: any, ql: any, errLabel: string) {
1166-
return refuseStoredMetadataBodyWrites(
1167-
refuseStoredMetadataBodyReads(
1168-
answerLandedWritesDespiteStaleSummary(buildSandboxApiSource(engineCtx, ql, errLabel), errLabel),
1171+
return refuseBodyPermanentDeletes(
1172+
refuseStoredMetadataBodyWrites(
1173+
refuseStoredMetadataBodyReads(
1174+
answerLandedWritesDespiteStaleSummary(buildSandboxApiSource(engineCtx, ql, errLabel), errLabel),
1175+
),
11691176
),
1177+
errLabel,
1178+
);
1179+
}
1180+
1181+
/** Marks a scoped API whose permanent deletes are already refused for a body. */
1182+
const BODY_PERMANENT_DELETES_REFUSED = Symbol.for('objectstack.runtime.bodyPermanentDeletesRefused');
1183+
1184+
/**
1185+
* The refusal of a body's `delete(…, { permanent })`: `400 VALIDATION_ERROR`,
1186+
* `param: 'permanent'`. The message names the rule, because a job that asked
1187+
* for a purge has to learn it got none: dropping the key would delete into the
1188+
* bin and answer success, which reads as the purge it asked for.
1189+
*/
1190+
function bodyPermanentDeleteRefusal(objectName: string, errLabel: string): Error {
1191+
const err: any = new Error(
1192+
`A ${errLabel} cannot delete a record permanently: app-authored code cannot purge; ` +
1193+
`its deletes enter the recycle bin. Remove 'permanent' from the delete of '${objectName}'.`,
1194+
);
1195+
err.developerMessage =
1196+
`'permanent' was passed to ctx.api.object('${objectName}').delete() in a ${errLabel}. App-authored code ` +
1197+
`(hook, action and job bodies) cannot erase a record past the recycle bin, under any execution context, ` +
1198+
`a job's system envelope included; the permanent delete stays with a principal holding manageRecycleBin ` +
1199+
`(ADR-0148 D3).`;
1200+
err.code = 'VALIDATION_ERROR';
1201+
err.status = 400;
1202+
err.httpStatus = 400;
1203+
err.param = 'permanent';
1204+
err.object = objectName;
1205+
return err;
1206+
}
1207+
1208+
/**
1209+
* One repository as a body holds it: `delete(options)` whose `options` carry a
1210+
* `permanent` key with any value but `false` is refused before the underlying
1211+
* verb is called, so nothing is removed and no bin entry is skipped. `false`
1212+
* and an absent key are the ordinary delete, and go through unchanged. Every
1213+
* other member is handed through.
1214+
*/
1215+
function refuseRepositoryPermanentDelete(objectName: string, repo: unknown, errLabel: string): unknown {
1216+
if (repo === null || typeof repo !== 'object') return repo;
1217+
return new Proxy(repo as Record<PropertyKey, unknown>, {
1218+
get(target, prop) {
1219+
const value = Reflect.get(target, prop, target);
1220+
if (typeof value !== 'function') return value;
1221+
if (prop !== 'delete') return value.bind(target);
1222+
return async (...args: unknown[]) => {
1223+
const options = args[0];
1224+
if (options !== null && typeof options === 'object') {
1225+
const permanent = (options as { permanent?: unknown }).permanent;
1226+
if (permanent !== undefined && permanent !== false) {
1227+
throw bodyPermanentDeleteRefusal(objectName, errLabel);
1228+
}
1229+
}
1230+
return value.apply(target, args);
1231+
};
1232+
},
1233+
});
1234+
}
1235+
1236+
/**
1237+
* [ADR-0148 D3, ruling A recorded in 6691358077] The scoped API a sandboxed
1238+
* BODY (a hook, action or job body) holds, with the permanent delete refused:
1239+
* app-authored code cannot purge a record, so its deletes always enter the
1240+
* recycle bin. A job body runs under a fresh `{ isSystem: true, tenantId }`
1241+
* envelope and the system is never refused the engine's `purge` verb, so the
1242+
* rule is held here, at the one place every body face gets its API, and not by
1243+
* the engine, whose `delete(…, { permanent: true })` stays open to every other
1244+
* caller. Every context the API derives (`object(name)`, `sudo()`,
1245+
* `withRunAs(...)`, a transaction's context) is refused the same way.
1246+
* Idempotent.
1247+
*/
1248+
function refuseBodyPermanentDeletes<T>(api: T, errLabel: string): T {
1249+
return deriveThroughSeam(api, BODY_PERMANENT_DELETES_REFUSED, (name, repo) =>
1250+
refuseRepositoryPermanentDelete(name, repo, errLabel),
11701251
);
11711252
}
11721253

‎packages/spec/src/data/hook-api.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ describe('HookApi — the published hook ctx.api face', () => {
223223
name: 'HookDeleteOptions / EngineDeleteOptionsSchema',
224224
schema: EngineDeleteOptionsSchema,
225225
kept: ['where', 'multi'],
226-
omitted: ['context'],
226+
// `permanent`: app-authored code cannot purge; its deletes enter the
227+
// recycle bin (maintainer ruling A, recorded in 6691358077).
228+
omitted: ['context', 'permanent'],
227229
},
228230
];
229231

‎packages/spec/src/data/hook-api.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@
7878
* the schemas to carry their migration text; the engine rejects them by
7979
* quoting that text. They are not part of an authoring surface published
8080
* after their retirement.
81+
* - `permanent` (on the delete bag) — app-authored code cannot erase a record
82+
* past the recycle bin; its deletes always enter the bin, and the permanent
83+
* delete stays with a principal holding `manageRecycleBin` (ADR-0148 D3;
84+
* maintainer ruling A, recorded in 6691358077). A hook's elevated `api`
85+
* (`Hook.runAs: 'system'`) would otherwise pass the system's purge through,
86+
* so the key is off the published face, and the sandbox refuses a body's
87+
* `permanent` at runtime rather than dropping it.
8188
* - `sudo()` — the #5945 exclusion STANDS. It is privilege escalation, no
8289
* document teaches it as first-hook vocabulary, and since #14010 the
8390
* declared way for a hook to run elevated is `Hook.runAs: 'system'`, which
@@ -202,8 +209,14 @@ export type HookUpdateOptions = Omit<EngineUpdateOptions, 'context' | 'upsert'>
202209
WriteObservabilityOptions &
203210
HookDriverPassthroughOptions;
204211

205-
/** The options bag `ctx.api.object(n).delete()` accepts. */
206-
export type HookDeleteOptions = Omit<EngineDeleteOptions, 'context'> &
212+
/**
213+
* The options bag `ctx.api.object(n).delete()` accepts.
214+
*
215+
* `EngineDeleteOptions` minus the injected `context` and minus `permanent`:
216+
* a hook's delete always enters the recycle bin (ADR-0148 D3, ruling A in
217+
* 6691358077), so the purge door is not on this face.
218+
*/
219+
export type HookDeleteOptions = Omit<EngineDeleteOptions, 'context' | 'permanent'> &
207220
HookDriverPassthroughOptions;
208221

209222
/**

0 commit comments

Comments
 (0)