Skip to content

Commit 22e257d

Browse files
committed
fix(objectql,plugin-security): an update's organization_id re-point the Layer 0 wall judges is stored as sent
Step 3.7 now declares judgesOrganization on the stored-image seam it composes for a single-payload update as well as an insert (tenantParts non-empty). The engine reads that member once per update and hands it to every static-readonly pass (the pre-hook hide pass, the by-id and predicate strips, and the stored view the readonlyWhen strips and the master-detail settlement read), through one helper shared with the insert side. Only the platform-injected column on an object the insert strip judges is spared, so sys_ and platform-internal objects and an authored read-only organization_id keep the strip. Claude-Session: https://claude.ai/code/session_01UmqzD97e14iws6hmVhgZ5y Co-authored-by: Claude <noreply@anthropic.com>
1 parent 2b1382a commit 22e257d

2 files changed

Lines changed: 205 additions & 71 deletions

File tree

‎packages/objectql/src/engine.ts‎

Lines changed: 154 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,23 +2411,56 @@ function callerSuppliedRow(row: unknown, sent: ReadonlySet<string> | undefined):
24112411
}
24122412

24132413
/**
2414-
* [#24822] The fields ONE insert's static-`readonly` passes may judge: the
2415-
* {@link staticReadonlyInsertSubject} view, without `organization_id` when the
2416-
* Layer 0 wall judges that column on this insert's stored rows AND the column
2417-
* is the platform's injected tenant column.
2414+
* [#24822] Whether ONE write's static-`readonly` passes leave a non-system
2415+
* caller's `organization_id` on the row for the Layer 0 wall's verdict instead
2416+
* of taking it. The ONE answer both write verbs read: the insert's subject
2417+
* ({@link insertStaticReadonlySubject}) and the update's
2418+
* ({@link updateStaticReadonlySubject}).
2419+
*
2420+
* Three things must hold, and the first is the wall's own word:
24182421
*
2419-
* The wall says so itself, on the seam it installs
2420-
* (`PostHookWriteImageCheck.judgesOrganization`). The caller passes that
2421-
* member's answer, for a non-system caller only. Nothing here derives it:
2422-
* where no wall judged the column, the strip takes a caller's value exactly
2423-
* as before.
2422+
* 1. `wallJudgesOrganization`: the seam the wall installed on THIS write
2423+
* declares that it judges the stored rows' organization
2424+
* (`PostHookWriteImageCheck.judgesOrganization`). The caller reads that
2425+
* member once, for a non-system caller only. Nothing here derives it:
2426+
* where no wall judged the column, the strip takes a caller's value
2427+
* exactly as before.
2428+
* 2. The object is one the INSERT strip judges, with the column in that
2429+
* subject ({@link staticReadonlyInsertSubject}). So never the reserved
2430+
* `sys_` namespace or a platform-internal `managedBy` bucket, whose own
2431+
* field-write governance a spare must not pre-empt. The UPDATE strip runs
2432+
* on those objects too (it applies neither exclusion), so on an update
2433+
* this condition is what keeps a `sys_` row's organization exactly as
2434+
* before: the update spare covers exactly the objects the insert spare
2435+
* covers.
2436+
* 3. The column is the platform's injected tenant column, the one
2437+
* `TENANT_SCOPE_FIELD_DEF` declares read-only on the platform's behalf. An
2438+
* AUTHORED `organization_id` declared `readonly: true` is the author's own
2439+
* declaration, and declaration is enforcement: it keeps the strip. Which
2440+
* one this is, is the registry's own provenance reader, never a second
2441+
* copy.
24242442
*
2425-
* Why the column is left on the row: the wall has already refused, before
2426-
* this engine ran, every supplied value it does not admit. It judges the
2427-
* stored row again after the hooks. So a value that reaches the strip is one
2428-
* the wall admitted, and taking it stored the active organization in its
2429-
* place. That is a silent replacement of an admitted input, which ADR-0105 D5
2430-
* and the organizations runtime's stamp rule out.
2443+
* Why the column is left on the row where all three hold: the wall has
2444+
* already refused, before this engine ran, every supplied value it does not
2445+
* admit, and it judges the stored rows again after the hooks: an insert's
2446+
* rows, an update's prior row merged with the final payload, and on the
2447+
* predicate path every matched row merged with it. So a value that reaches the
2448+
* strip is one the wall admitted, and taking it put another organization in
2449+
* its place (the active one stamped on an insert, the row's own kept on an
2450+
* update). That is a silent replacement of an admitted input, which ADR-0105
2451+
* D5 and the organizations runtime's stamp rule out.
2452+
*/
2453+
function staticReadonlySparesOrganization(schema: unknown, wallJudgesOrganization: boolean): boolean {
2454+
if (!wallJudgesOrganization) return false;
2455+
const subject = staticReadonlyInsertSubject(schema as any);
2456+
if (!subject || !Object.prototype.hasOwnProperty.call(subject.fields, SystemFieldName.ORGANIZATION_ID)) return false;
2457+
return resolveInjectedColumnProvenance(schema, SystemFieldName.ORGANIZATION_ID) !== 'author';
2458+
}
2459+
2460+
/**
2461+
* [#24822] The fields ONE insert's static-`readonly` passes may judge: the
2462+
* {@link staticReadonlyInsertSubject} view, without `organization_id` where
2463+
* {@link staticReadonlySparesOrganization} spares it.
24312464
*
24322465
* ONE function for both passes that read the subject, the pre-hook withhold
24332466
* and the post-hook strip, so the hook view and the stored row cannot
@@ -2438,19 +2471,35 @@ function insertStaticReadonlySubject(
24382471
wallJudgesOrganization: boolean,
24392472
): ReturnType<typeof staticReadonlyInsertSubject> {
24402473
const subject = staticReadonlyInsertSubject(schema as any);
2441-
if (!subject || !wallJudgesOrganization) return subject;
2442-
if (!Object.prototype.hasOwnProperty.call(subject.fields, SystemFieldName.ORGANIZATION_ID)) return subject;
2443-
// An AUTHORED `organization_id` declared `readonly: true` is the author's
2444-
// own declaration, and declaration is enforcement: it keeps the strip. Only
2445-
// the platform's injected tenant column is spared, the one
2446-
// `TENANT_SCOPE_FIELD_DEF` declares read-only on the platform's behalf. Which
2447-
// one this is, is the registry's own provenance reader, never a second copy.
2448-
if (resolveInjectedColumnProvenance(schema, SystemFieldName.ORGANIZATION_ID) === 'author') return subject;
2474+
if (!subject || !staticReadonlySparesOrganization(schema, wallJudgesOrganization)) return subject;
24492475
const fields = { ...subject.fields };
24502476
delete fields[SystemFieldName.ORGANIZATION_ID];
24512477
return Object.keys(fields).length > 0 ? { name: subject.name, fields } : null;
24522478
}
24532479

2480+
/**
2481+
* [#24822] The schema view ONE update's static-`readonly` passes judge: the
2482+
* object's whole registered schema, because the update strip applies neither
2483+
* of the insert subject's exclusions, without `organization_id` where
2484+
* {@link staticReadonlySparesOrganization} spares it. `schema` itself
2485+
* otherwise, so every update the spare does not reach is judged exactly as
2486+
* before.
2487+
*
2488+
* ONE function for every pass that asks the update's static strip: the
2489+
* pre-hook hide pass (what `beforeUpdate` is shown), the by-id and predicate
2490+
* strips (what is stored), and {@link staticReadonlyStoredView}, which the
2491+
* `readonlyWhen` strips and the master-detail settlement read. `update()`
2492+
* reads the wall's flag once and hands that one value to all of them, so the
2493+
* hook view and the stored row cannot disagree about the column.
2494+
*/
2495+
function updateStaticReadonlySubject(schema: unknown, wallJudgesOrganization: boolean): unknown {
2496+
if (!staticReadonlySparesOrganization(schema, wallJudgesOrganization)) return schema;
2497+
const { name, fields } = schema as { name?: string; fields: Record<string, unknown> };
2498+
const kept = { ...fields };
2499+
delete kept[SystemFieldName.ORGANIZATION_ID];
2500+
return { name, fields: kept };
2501+
}
2502+
24542503
/**
24552504
* [#22306] The insert side of the #16344 invariant — a hook is never handed a
24562505
* value that will not be stored. Takes out of ONE caller row every value the
@@ -3767,22 +3816,32 @@ export interface PostHookWriteImageCheck {
37673816
evaluate(rows: readonly Record<string, unknown>[]): void | Promise<void>;
37683817
honoured?: boolean;
37693818
/**
3770-
* [#24822] Declared by the installer, and only by it: on an `insert`,
3771-
* `evaluate` holds every row's `organization_id` to the Layer 0 tenant wall.
3772-
* It admits the value or refuses the write by throwing.
3819+
* [#24822] Declared by the installer, and only by it: `evaluate` holds every
3820+
* row it is handed to the Layer 0 tenant wall on `organization_id`. It
3821+
* admits the value or refuses the write by throwing. On an `insert` those
3822+
* rows are the inserted rows; on an `update` they are the stored images:
3823+
* the one prior row merged with the final payload on the by-id path, and
3824+
* every matched row merged with it on the predicate path.
37733825
* `@objectstack/plugin-security` sets it at step 3.7, only where it
3774-
* composes the wall's own parts into the insert's seam. A seam that carries
3775-
* a business `check` alone does not judge the organization, and neither
3776-
* does a platform operator's seam that crosses the wall, so neither sets it.
3777-
*
3778-
* {@link ObjectQL.insert} reads this member, and nothing else, to decide one
3779-
* thing: whether a non-system caller's explicit `organization_id` stays on
3780-
* the row for that judgement or is taken by the static-`readonly` strip
3781-
* (the platform's tenant column declares `readonly: true`). Set, the value
3782-
* is stored as sent where the wall admits it and refused where it does not.
3783-
* It is never silently replaced by the active organization (ADR-0105 D5).
3784-
* Only the platform's injected column is spared. An AUTHORED
3785-
* `organization_id` declared `readonly: true` keeps the strip.
3826+
* composes the wall's own parts into the insert's or the single-payload
3827+
* update's seam. A seam that carries a business `check` alone does not
3828+
* judge the organization, and neither does a platform operator's seam that
3829+
* crosses the wall, so neither sets it; nor is it set on an update whose
3830+
* payload is an array, where step 3.7 installs no seam of its own.
3831+
*
3832+
* {@link ObjectQL.insert} and {@link ObjectQL.update} read this member, and
3833+
* nothing else, to decide one thing: whether a non-system caller's explicit
3834+
* `organization_id` stays on the row for that judgement or is taken by the
3835+
* static-`readonly` strip (the platform's tenant column declares
3836+
* `readonly: true`). Set, the value is stored as sent where the wall admits
3837+
* it and refused where it does not: an insert's is never silently replaced
3838+
* by the active organization (ADR-0105 D5), and an update's re-point is
3839+
* never silently dropped in favour of the row's own organization. Only the
3840+
* platform's injected column is spared, and only on an object the insert's
3841+
* static-`readonly` strip judges, so never a `sys_` or platform-internal
3842+
* object. An AUTHORED `organization_id` declared `readonly: true` keeps the
3843+
* strip. An emptied value on an update is not spared into storage: the wall
3844+
* refuses it (`null` or `''`), on the payload and on the stored image.
37863845
* Absent or `false`, the strip runs exactly as before. The engine never
37873846
* sets it, and never derives it from the posture or the context: that
37883847
* would be a mirror of the wall, and a mirror is wider than the wall
@@ -4178,7 +4237,13 @@ function staticReadonlyStripTakes(
41784237
data: Record<string, unknown>,
41794238
key: string,
41804239
supplied: Readonly<Record<string, unknown>>,
4181-
strip: { runs: boolean; preserveAudit: boolean; hookWrittenKeys: ReadonlySet<string> | undefined },
4240+
strip: {
4241+
runs: boolean;
4242+
preserveAudit: boolean;
4243+
hookWrittenKeys: ReadonlySet<string> | undefined;
4244+
/** [#24822] The wall's flag, read once by `update()` — see {@link staticReadonlyStoredView}. */
4245+
wallJudgesOrganization?: boolean;
4246+
},
41824247
): boolean {
41834248
if (!strip.runs) return false;
41844249
return !(key in staticReadonlyStoredView(schema, data, supplied, strip));
@@ -4204,16 +4269,31 @@ function staticReadonlyStripTakes(
42044269
* when the strip does not run (a system caller), and a key a hook wrote, a
42054270
* value a hook overwrote or a column `preserveAudit` keeps all stay, because
42064271
* the write stores them. Silent for the same reason.
4272+
*
4273+
* [#24822] ...and over the same view of the schema: the one
4274+
* {@link updateStaticReadonlySubject} answers for `strip.wallJudgesOrganization`,
4275+
* the flag `update()` reads once off the wall's seam and hands to the strips
4276+
* themselves. So where the wall judges the update's organization, a re-point
4277+
* the strip leaves on the row is in this view too, and a `readonlyWhen`
4278+
* predicate reading `record.organization_id` reads the organization the row is
4279+
* stored in. The master-detail settlement forwards the update's descriptor
4280+
* object unchanged, so the flag reaches {@link staticReadonlyStripTakes} on that
4281+
* path too. Absent, the view is the whole schema's, exactly as before.
42074282
*/
42084283
function staticReadonlyStoredView(
42094284
schema: unknown,
42104285
data: Record<string, unknown>,
42114286
supplied: Readonly<Record<string, unknown>>,
4212-
strip: { runs: boolean; preserveAudit: boolean; hookWrittenKeys: ReadonlySet<string> | undefined },
4287+
strip: {
4288+
runs: boolean;
4289+
preserveAudit: boolean;
4290+
hookWrittenKeys: ReadonlySet<string> | undefined;
4291+
wallJudgesOrganization?: boolean;
4292+
},
42134293
): Record<string, unknown> {
42144294
if (!strip.runs) return data;
42154295
return stripReadonlyFields(
4216-
schema as any, data, supplied, undefined,
4296+
updateStaticReadonlySubject(schema, strip.wallJudgesOrganization === true) as any, data, supplied, undefined,
42174297
{ preserveAudit: strip.preserveAudit, hookWrittenKeys: strip.hookWrittenKeys },
42184298
) ?? data;
42194299
}
@@ -17412,6 +17492,20 @@ export class ObjectQL implements IObjectQLEngine {
1741217492
// judged against the prior record — per ROW on the predicate path, where
1741317493
// one shared payload cannot carry a per-row verdict — and #9107
1741417494
// deliberately leaves it hook-writable.
17495+
//
17496+
// [#24822] Read ONCE, here, and handed to every static-`readonly` pass
17497+
// below (this hide pass, the by-id and predicate strips, and the views
17498+
// the `readonlyWhen` strips and the master-detail settlement read off
17499+
// them): whether the Layer 0 wall judges this update's `organization_id`
17500+
// on the stored rows. The wall declares it on the seam it installed;
17501+
// the engine reads that member and nothing else
17502+
// (`updateStaticReadonlySubject`). The middleware chain has run by now,
17503+
// so the seam is in place. Where it holds, an admitted re-point is shown
17504+
// to `beforeUpdate` and stored as sent, instead of being taken and the
17505+
// row's own organization kept.
17506+
const wallJudgesOrganization =
17507+
!opCtx.context?.isSystem && opCtx.postHookWriteImageCheck?.judgesOrganization === true;
17508+
const readonlyUpdateSubject = updateStaticReadonlySubject(updateSchema, wallJudgesOrganization);
1741517509
let readonlyHiddenFromHooks: Record<string, unknown> | undefined;
1741617510
if (!opCtx.context?.isSystem) {
1741717511
const preHookPayload = opCtx.data as Record<string, unknown> | null | undefined;
@@ -17421,7 +17515,7 @@ export class ObjectQL implements IObjectQLEngine {
1742117515
// No logger, deliberately: this pass is SILENT by construction. The
1742217516
// strip below owns every word said about these keys.
1742317517
const hidden = stripReadonlyFields(
17424-
updateSchema as any, preHookPayload, suppliedDataOnly, undefined,
17518+
readonlyUpdateSubject as any, preHookPayload, suppliedDataOnly, undefined,
1742517519
{ preserveAudit: opCtx.context?.preserveAudit === true },
1742617520
) as Record<string, unknown>;
1742717521
if (hidden !== preHookPayload) {
@@ -18169,7 +18263,10 @@ export class ObjectQL implements IObjectQLEngine {
1816918263
// [#19887] The static strip's verdict, described once: the
1817018264
// settlement asks it of the FK, and both `readonlyWhen` strips
1817118265
// build their `record` view from the payload it leaves.
18172-
const staticStrip = { runs: staticReadonlyStripRuns, preserveAudit: opCtx.context?.preserveAudit === true, hookWrittenKeys };
18266+
// [#24822] ...with the wall's flag read once above, so the views
18267+
// these consumers read agree with the strip below about
18268+
// `organization_id`.
18269+
const staticStrip = { runs: staticReadonlyStripRuns, preserveAudit: opCtx.context?.preserveAudit === true, hookWrittenKeys, wallJudgesOrganization };
1817318270
const landing = wantsParentBinding
1817418271
? await this.settleMasterDetailLanding({
1817518272
schema: updateSchema, data: preRoWhen, supplied: suppliedValues,
@@ -18247,7 +18344,14 @@ export class ObjectQL implements IObjectQLEngine {
1824718344
// a read-only column loses its write to any caller that
1824818345
// echoed the same value back, which on `null` is every
1824918346
// whole-record form round-trip.
18250-
hookContext.input.data = stripReadonlyFields(updateSchema as any, preRo, suppliedValues, this.logger, { preserveAudit: opCtx.context?.preserveAudit === true, addressKey: idAddressesThisRow ? 'id' : undefined, strictReadonlyWrites, hookWrittenKeys }) as any;
18347+
// [#24822] Over `readonlyUpdateSubject`: without
18348+
// `organization_id` where the wall's seam judges this
18349+
// update's organization (read once, above, for the hide pass
18350+
// too). An admitted re-point is then stored as sent, not
18351+
// taken with the row's own organization kept; it is not
18352+
// reported, and not refused under `strictReadonlyWrites`,
18353+
// which refuses exactly what this strip takes.
18354+
hookContext.input.data = stripReadonlyFields(readonlyUpdateSubject as any, preRo, suppliedValues, this.logger, { preserveAudit: opCtx.context?.preserveAudit === true, addressKey: idAddressesThisRow ? 'id' : undefined, strictReadonlyWrites, hookWrittenKeys }) as any;
1825118355
reportDroppedFields(preRo, hookContext.input.data as Record<string, unknown>, 'readonly');
1825218356
}
1825318357
// [#5126] Both strip passes are done; refuse now if the caller
@@ -18483,7 +18587,7 @@ export class ObjectQL implements IObjectQLEngine {
1848318587
const staticReadonlyStripRunsMulti = !opCtx.context?.isSystem;
1848418588
// [#19887] Described once, three consumers — as on the by-id
1848518589
// branch.
18486-
const staticStripMulti = { runs: staticReadonlyStripRunsMulti, preserveAudit: opCtx.context?.preserveAudit === true, hookWrittenKeys };
18590+
const staticStripMulti = { runs: staticReadonlyStripRunsMulti, preserveAudit: opCtx.context?.preserveAudit === true, hookWrittenKeys, wallJudgesOrganization };
1848718591
const landingMulti =
1848818592
hasParentScopedReadonlyWhenInPayload(updateSchema as any, preRoWhenMulti) ||
1848918593
schemaHasParentRequiredWhenMulti
@@ -18532,7 +18636,11 @@ export class ObjectQL implements IObjectQLEngine {
1853218636
// one they were each added to close, and a bulk write that
1853318637
// reached a different verdict about authorship than a by-id
1853418638
// write is the #3106 / #4441 divergence verbatim.
18535-
hookContext.input.data = stripReadonlyFields(updateSchema as any, preRoMulti, suppliedValues, this.logger, { preserveAudit: opCtx.context?.preserveAudit === true, strictReadonlyWrites, hookWrittenKeys }) as any;
18639+
// [#24822] The same `readonlyUpdateSubject` as the by-id
18640+
// branch: the wall's seam judges every matched row merged
18641+
// with this payload, so an admitted re-point is stored as
18642+
// sent on every one of them.
18643+
hookContext.input.data = stripReadonlyFields(readonlyUpdateSubject as any, preRoMulti, suppliedValues, this.logger, { preserveAudit: opCtx.context?.preserveAudit === true, strictReadonlyWrites, hookWrittenKeys }) as any;
1853618644
reportDroppedFields(preRoMulti, hookContext.input.data as Record<string, unknown>, 'readonly');
1853718645
}
1853818646
// [#5126] Same refusal on the predicate path. A bulk strip is

0 commit comments

Comments
 (0)