@@ -36,6 +36,7 @@ import {
3636 resolveEffectiveApiMethods ,
3737 effectiveOperationsArray ,
3838 canServeApiOperation ,
39+ type ApiOperation ,
3940 type EnableLike ,
4041} from '@objectstack/spec/data' ;
4142import { objectPermissionGrants , type EffectiveObjectPermission } from '@objectstack/spec/security' ;
@@ -478,10 +479,18 @@ export function seedSuperUserRestrictedObjects(
478479 * Which entries carry the annotation is unchanged in rule: only an
479480 * unrestricted object that keeps its whole closure — every operation served,
480481 * `export` included — gets none.
482+ *
483+ * [#30370] With `isExternalWriteForbidden`, an object whose external write
484+ * gate is closed (ADR-0015 §5.3 Gate 3) is served without every write
485+ * operation ({@link EXTERNAL_WRITE_GATED_OPERATIONS}): the data plane refuses
486+ * each one `403 EXTERNAL_WRITE_FORBIDDEN` whatever the permission sets say,
487+ * and such an object is always annotated, since the client's default-allow
488+ * path would offer every write. Without it, the answer is what it was.
481489 */
482490export function annotateEffectiveApiOperations (
483491 objects : Record < string , any > ,
484492 schemaOf : ( objectName : string ) => ApiExposureSchemaLike | undefined ,
493+ isExternalWriteForbidden ?: ( objectName : string ) => boolean ,
485494) : void {
486495 for ( const [ obj , acc ] of Object . entries ( objects ) as Array < [ string , any ] > ) {
487496 if ( obj === '*' || ! acc ) continue ;
@@ -497,18 +506,86 @@ export function annotateEffectiveApiOperations(
497506 const closure = effectiveOperationsArray ( eff ) ;
498507 // [#20135] Only what the door itself admits: `apiEnabled: false` empties
499508 // the set; any other `enable` leaves the closure as it is.
500- const served = closure . filter ( ( operation ) => canServeApiOperation ( enable , operation ) ) ;
509+ // [#30370] And only what the external write gate admits: a closed gate
510+ // refuses every write operation on the data plane.
511+ const writeForbidden = isExternalWriteForbidden ?.( obj ) === true ;
512+ const served = closure . filter ( ( operation ) =>
513+ canServeApiOperation ( enable , operation )
514+ && ! ( writeForbidden && EXTERNAL_WRITE_GATED_OPERATIONS . has ( operation ) ) ) ;
501515 // Annotate when the object tightens via `apiMethods`, when the export
502516 // axis removes `export` from an otherwise-open object (so the client
503517 // hides the Export button), OR when the door serves less than the closure
504- // (an API-disabled object). An unrestricted object with every operation
505- // still served needs no annotation — the client keeps its default-allow
506- // path.
518+ // (an API-disabled object, or [#30370] a closed external write gate). An
519+ // unrestricted object with every operation still served needs no
520+ // annotation — the client keeps its default-allow path.
507521 if ( eff . mode === 'unrestricted' && userExportAllowed && served . length === closure . length ) continue ;
508522 acc . apiOperations = served ;
509523 }
510524}
511525
526+ /**
527+ * [#30370] The effective API operations that WRITE a row — every one the
528+ * external write gate (ADR-0015 §5.3 Gate 3, objectql `assertWriteAllowed`)
529+ * refuses on an object whose gate is closed: the three primitives it judges
530+ * (`insert` is `create`), and every derived operation built on them —
531+ * `upsert` (create ∧ update), `bulk` (batched create / update / delete),
532+ * `import` (create ∨ update), and `restore` / `purge`, which never derive
533+ * today but write when they do. The read operations (`get`, `list`,
534+ * `aggregate`, `history`, `search`, `export`) are untouched: the gate judges
535+ * writes only.
536+ */
537+ const EXTERNAL_WRITE_GATED_OPERATIONS : ReadonlySet < ApiOperation > = new Set < ApiOperation > ( [
538+ 'create' , 'update' , 'delete' , 'upsert' , 'bulk' , 'import' , 'restore' , 'purge' ,
539+ ] ) ;
540+
541+ /**
542+ * [#30370] Re-clamp the map by the THIRD server-side enforcement layer
543+ * permission sets do not model: the external write gate (ADR-0015 §5.3
544+ * Gate 3). On an object whose gate is closed, objectql refuses every insert,
545+ * update and delete `403 EXTERNAL_WRITE_FORBIDDEN` for every caller — no
546+ * permission set, super-user bypass included, opens it — so the entry grants
547+ * no write: `allowCreate`, `allowEdit`, `allowDelete` and `allowTransfer`
548+ * (an owner change is an update) are `false`.
549+ *
550+ * `modifyAllRecords` is a write grant too: the spec's `objectPermissionGrants`
551+ * answers edit, delete and transfer `true` on it alone, so an entry keeping it
552+ * would still answer `current_user.can(object, 'edit')` `true`. It is lowered
553+ * to `viewAllRecords`, the read half it implies, so the entry keeps exactly
554+ * the read it had.
555+ *
556+ * An object the map does not mention already grants nothing, and is left out.
557+ */
558+ function clampExternalReadOnlyWrites ( objects : Record < string , any > , forbidden : ReadonlySet < string > ) : void {
559+ if ( forbidden . size === 0 ) return ;
560+ for ( const [ obj , acc ] of Object . entries ( objects ) as Array < [ string , any ] > ) {
561+ if ( obj === '*' || ! acc || ! forbidden . has ( obj ) ) continue ;
562+ acc . allowCreate = false ;
563+ acc . allowEdit = false ;
564+ acc . allowDelete = false ;
565+ acc . allowTransfer = false ;
566+ if ( acc . modifyAllRecords === true ) {
567+ acc . modifyAllRecords = false ;
568+ acc . viewAllRecords = true ;
569+ }
570+ }
571+ }
572+
573+ /**
574+ * [#30370] The {@link EffectiveObjectPermissionsSchemaSource.isExternalWriteForbidden}
575+ * accessor over an engine: its `isExternalWriteForbidden`, the verdict objectql's
576+ * own write gate (`assertWriteAllowed`) reads before it refuses a write
577+ * `403 EXTERNAL_WRITE_FORBIDDEN`. That member is not on the contract's engine
578+ * view, so it is asked when present; an engine without it forbids nothing.
579+ * Both builders of the map (`/auth/me/permissions` and
580+ * `ISecurityService.getEffectiveObjectPermissions`) hand over this one reading.
581+ */
582+ export function engineExternalWriteForbidden ( engine : unknown ) : ( objectName : string ) => boolean {
583+ return ( objectName ) => {
584+ const predicate = ( engine as { isExternalWriteForbidden ?: unknown } | null | undefined ) ?. isExternalWriteForbidden ;
585+ return typeof predicate === 'function' && predicate . call ( engine , objectName ) === true ;
586+ } ;
587+ }
588+
512589/**
513590 * The schema reads {@link buildEffectiveObjectPermissions} needs, as accessors
514591 * over whatever engine the caller holds. Every accessor is optional and every
@@ -534,6 +611,15 @@ export interface EffectiveObjectPermissionsSchemaSource {
534611 * the map must leave out exactly the objects the data plane refuses.
535612 */
536613 isObjectPackageDisabled ?: ( objectName : string ) => boolean ;
614+ /**
615+ * [#30370] Whether the external write gate (ADR-0015 §5.3 Gate 3) is closed
616+ * on `objectName` — the engine's `isExternalWriteForbidden`, the verdict its
617+ * own `assertWriteAllowed` refuses a write on. Hand over that predicate
618+ * itself ({@link engineExternalWriteForbidden}), never a second reading of
619+ * the datasource definitions: the map must withhold exactly the writes the
620+ * data plane refuses.
621+ */
622+ isExternalWriteForbidden ?: ( objectName : string ) => boolean ;
537623 /** Where a failed seed / annotation pass is reported. */
538624 logger ?: { warn ?: ( message : string , meta ?: Record < string , unknown > ) => void } ;
539625}
@@ -604,8 +690,15 @@ export interface EffectiveObjectPermissionsInputSet {
604690 * not a step, so an entry a super-user set names itself keeps that set's
605691 * explicit answer, and `modifyAllRecords` alone grants no create;
606692 * 5. {@link clampManagedObjectWrites};
607- * 6. {@link annotateEffectiveApiOperations} — guarded like (2);
608- * 7. {@link omitDisabledPackageObjects} — [#28877] an object whose owning
693+ * 6. {@link clampExternalReadOnlyWrites} — [#30370] an object whose external
694+ * write gate is closed grants no write, as the data plane refuses every
695+ * one; the verdict is judged for every entry before anything is clamped,
696+ * and a predicate that throws is reported and clamps nothing (guarded
697+ * like (2));
698+ * 7. {@link annotateEffectiveApiOperations} — guarded like (2); [#30370] it
699+ * reads the same verdicts, so a closed gate's object is served no write
700+ * operation;
701+ * 8. {@link omitDisabledPackageObjects} — [#28877] an object whose owning
609702 * package is disabled is left out, as the data plane refuses it; guarded
610703 * like (2).
611704 *
@@ -659,19 +752,33 @@ export function buildEffectiveObjectPermissions(
659752 }
660753 // Make the per-object map reflect the server's ACTUAL effective enforcement
661754 // = permission-set grant ∩ identity write guard (ADR-0057 D10, cited as an
662- // attribution, #9628): (1) fold each set's `'*'` super-user grant into every
755+ // attribution, #9628) ∩ [#30370] external write gate (ADR-0015 §5.3) : (1) fold each set's `'*'` super-user grant into every
663756 // entry that set does not name, so an admin's wildcard is not shadowed by
664757 // another set's explicit deny — [#20134] every bit it grants, `transfer` and
665758 // its own plain bits included; [#20136] per set only, never the merged
666759 // bypass, which would also override the super-user set's OWN narrower entry;
667- // (2) re-clamp guarded managed objects by their write affordance.
760+ // (2) re-clamp guarded managed objects by their write affordance; (3)
761+ // [#30370] re-clamp objects whose external write gate is closed.
668762 foldSuperUserWildcardGrants ( objects , sets ) ;
669763 clampManagedObjectWrites ( objects , schemaOf ) ;
764+ // [#30370] Judged in full before anything is clamped, so a predicate that
765+ // throws midway clamps nothing rather than half the map — and the
766+ // annotation below reads these same verdicts, never a second asking.
767+ let externalWriteForbidden : ReadonlySet < string > = new Set ( ) ;
768+ const isForbidden = source . isExternalWriteForbidden ;
769+ if ( typeof isForbidden === 'function' ) {
770+ try {
771+ externalWriteForbidden = new Set ( Object . keys ( objects ) . filter ( ( name ) => name !== '*' && isForbidden ( name ) === true ) ) ;
772+ } catch ( e : any ) {
773+ source . logger ?. warn ?.( '[effective-permissions] external write gate read failed' , { err : e ?. message } ) ;
774+ }
775+ }
776+ clampExternalReadOnlyWrites ( objects , externalWriteForbidden ) ;
670777 // [#3391] Annotate the per-object effective API operation set. Guarded: on
671778 // failure `apiOperations` is simply omitted and a client falls back to its
672779 // default-allow behaviour.
673780 try {
674- annotateEffectiveApiOperations ( objects , schemaOf ) ;
781+ annotateEffectiveApiOperations ( objects , schemaOf , ( name ) => externalWriteForbidden . has ( name ) ) ;
675782 } catch ( e : any ) {
676783 source . logger ?. warn ?.( '[effective-permissions] apiOperations annotate failed' , { err : e ?. message } ) ;
677784 }
0 commit comments