@@ -86,6 +86,7 @@ import {
8686} from './platform-owner-wall-bypass.js';
8787import { isConfiguredPlatformAdminEmail, resolvePlatformAdminEmails, vetOrganizationClaim, createSecurityCatalogReader, bindSecurityCatalogReader, securityCatalogReaderOf, readDisabledCatalogNames } from '@objectstack/core';
8888import { isPlatformTenantPolicy, isAuthoredTenantPolicy } from './platform-tenant-policies.js';
89+ import { objectDefaultRowScopePolicy } from './object-default-row-scope.js';
8990import {
9091 isPlatformOwnershipFloorPolicy,
9192 masterGateCoversOperation,
@@ -8993,7 +8994,26 @@ export class SecurityPlugin implements Plugin {
89938994 }
89948995
89958996 /**
8996- * Collect all RLS policies from permission sets applicable to the given object/operation.
8997+ * The row-level security policies that apply to `objectName` for
8998+ * `operation`: the one collection every reader composes from — the read
8999+ * door's Layer 1, the by-id pre-image gate, the bulk write filter, the
9000+ * post-image check, `checkAuthoredRowWrite` and explain.
9001+ *
9002+ * [#24164 ruling E′, item 2] When no held set carries a row policy for this
9003+ * object and operation, the object's declared default row scope
9004+ * (`ObjectSchema.defaultRowScope`) applies in its place:
9005+ * - "carries" is this collection's own emptiness, after the ADR-0105 D3
9006+ * admission, `enabled`, the `positions` domain and `'*'` matching, so a
9007+ * held `'*'` policy counts and a held policy for another operation does
9008+ * not (`object-default-row-scope.ts` states the reading);
9009+ * - an authored set policy is used as authored: the default is never added
9010+ * to a non-empty collection;
9011+ * - the default takes the path a held policy takes from the admission on,
9012+ * so its `using` / `check` compile, and fail closed, exactly as an
9013+ * authored policy's do;
9014+ * - a superuser-bypass principal never reaches this method (the bypass is
9015+ * decided by the caller, before the collection), and an object that
9016+ * declares no default collects exactly what it collected before.
89979017 */
89989018 private collectRLSPolicies(
89999019 permissionSets: PermissionSet[],
@@ -9013,28 +9033,48 @@ export class SecurityPlugin implements Plugin {
90139033 // filter, the post-image check, `checkAuthoredRowWrite`, explain — sees
90149034 // the same list, because this is the one place they all collect from.
90159035 for (const policy of withOwnershipFloorAlternates(ps.rowLevelSecurity, floorAlternates)) {
9016- // [ADR-0105 D3] When org isolation is NOT active, strip the tenant
9017- // policies the PLATFORM itself ships — there is no meaningful active
9018- // organization to compare against, so they would match zero rows (or
9019- // be dropped by the field-existence safety net) for no benefit.
9020- //
9021- // Provenance, not pattern-matching (finding F1): the former substring
9022- // test on `current_user.organization_id` also swallowed app-authored
9023- // policies, silently unenforcing a declared security property. An
9024- // authored policy now always reaches the compiler and fails closed
9025- // there, where the operator can see it.
9026- if (!this.orgScopingEnabled && isPlatformTenantPolicy(policy)) {
9027- continue;
9028- }
9029- if (!this.orgScopingEnabled && isAuthoredTenantPolicy(policy)) {
9030- this.warnAuthoredTenantPolicyOnce(policy, objectName);
9031- }
9032- allPolicies.push(policy);
9036+ if (this.admitCollectedPolicy(policy, objectName)) allPolicies.push(policy);
90339037 }
90349038 }
90359039 }
90369040
9037- return this.rlsCompiler.getApplicablePolicies(objectName, operation, allPolicies, heldPositions);
9041+ const held = this.rlsCompiler.getApplicablePolicies(objectName, operation, allPolicies, heldPositions);
9042+ if (held.length > 0) return held;
9043+ // [#24164 ruling E′] The held sets are silent on this object and
9044+ // operation: the object's own `defaultRowScope`, if it declares one. Read
9045+ // from the registry the engine compiles every find against, only here, on
9046+ // an empty collection (two in-memory map lookups, no I/O).
9047+ const schema = typeof this.ql?.getSchema === 'function' ? this.ql.getSchema(objectName) : null;
9048+ const objectDefault = objectDefaultRowScopePolicy(objectName, schema);
9049+ if (objectDefault === null || !this.admitCollectedPolicy(objectDefault, objectName)) return held;
9050+ return this.rlsCompiler.getApplicablePolicies(objectName, operation, [objectDefault], heldPositions);
9051+ }
9052+
9053+ /**
9054+ * [ADR-0105 D3] Whether one policy enters the collection under this
9055+ * deployment's tenancy posture. When org isolation is NOT active, the tenant
9056+ * policies the PLATFORM itself ships are stripped — there is no meaningful
9057+ * active organization to compare against, so they would match zero rows (or
9058+ * be dropped by the field-existence safety net) for no benefit.
9059+ *
9060+ * Provenance, not pattern-matching (finding F1): the former substring test
9061+ * on `current_user.organization_id` also swallowed app-authored policies,
9062+ * silently unenforcing a declared security property. An authored policy now
9063+ * always reaches the compiler and fails closed there, where the operator can
9064+ * see it.
9065+ *
9066+ * An object's default row scope passes the same test, so a default that
9067+ * spells a shipped tenant policy is stripped exactly as that policy's set
9068+ * copy is (the provenance key is `(object, name, using)`).
9069+ */
9070+ private admitCollectedPolicy(policy: RowLevelSecurityPolicy, objectName: string): boolean {
9071+ if (!this.orgScopingEnabled && isPlatformTenantPolicy(policy)) {
9072+ return false;
9073+ }
9074+ if (!this.orgScopingEnabled && isAuthoredTenantPolicy(policy)) {
9075+ this.warnAuthoredTenantPolicyOnce(policy, objectName);
9076+ }
9077+ return true;
90389078 }
90399079
90409080 /**
0 commit comments