@@ -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' ;
6868import { answerLandedWritesDespiteStaleSummary } from '../landed-write-answer.js' ;
6969import { isStoredMetadataBodyObject } from '@objectstack/spec/kernel' ;
7070import {
@@ -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 */
11651170function 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
0 commit comments