Skip to content

Commit 61bba86

Browse files
committed
fix(objectql)!: the reference check runs before the record's validation rules
A reference a caller cannot read answers reference_not_found on every write door (insert, update by id, bulk update, and the validate preview), whatever a validation rule would answer. The one-hop predicate read's docblock states the rule that replaces the retired write-without-reading-the-parent persona. Claude-Session: https://claude.ai/code/session_01Utp6CYQjMkN514R78sCrTT Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6177223 commit 61bba86

3 files changed

Lines changed: 57 additions & 40 deletions

File tree

‎packages/objectql/src/engine-master-detail-related.ts‎

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,19 @@ export async function resolvePreviewParents(
354354
* The VALUES are read as SYSTEM: a validation rule's output is a pass/fail the
355355
* SYSTEM enforces, not data handed to the caller — categorically unlike an
356356
* access-control rule, which is why RLS predicates are excluded from this
357-
* capability altogether. Reading the columns as the acting user instead made
358-
* the rule unauthorable for exactly the persona it exists to constrain. What
359-
* bounds that elevation is the PROJECTION: only the columns the predicate
360-
* names, intersected with the related object's declared fields. ⛔ Never the
361-
* whole row.
357+
* capability altogether. What bounds that elevation is the PROJECTION: only
358+
* the columns the predicate names, intersected with the related object's
359+
* declared fields. ⛔ Never the whole row.
360+
*
361+
* ⛔ No caller writes a row naming a related row it cannot read (#29029,
362+
* ruling records 6573561091 and 6663849489). A non-system write may name, in
363+
* a `master_detail`, `lookup` or `user` field, only a row the caller's own
364+
* read door returns, and that reference check runs BEFORE the rules
365+
* (`assertReferencesResolve`), so a rule never answers for a reference the
366+
* caller supplied and cannot read: the write answers `reference_not_found`.
367+
* The elevated value read therefore serves a related row the caller can read,
368+
* a value the row already stores (#29062), a `tree` parent, and a system
369+
* caller.
362370
*
363371
* Which ROWS a caller that is not SYSTEM may have read is a different
364372
* question, answered before any value is read:
@@ -375,8 +383,9 @@ export async function resolvePreviewParents(
375383
* stored values, as the write judges it.
376384
* - On a related object no organization wall scopes, the own read of the
377385
* caller bounds the rows, and any other id is `unreadable`, stored or not.
378-
* - ⛔ A plain `lookup` to a walled object keeps the tenant-kept elevated read
379-
* alone: no ruling extends the header rule to it.
386+
* - A plain `lookup` to a walled object keeps the tenant-kept elevated read
387+
* alone HERE: a value the caller supplies and cannot read was already
388+
* refused by the reference check before this read is judged (above).
380389
*
381390
* A SYSTEM caller (seed replay, boot) asks no read door: the elevated read
382391
* alone, as before.
@@ -592,10 +601,12 @@ export async function resolveOneHopRelated(
592601
// A validation rule's output is a pass/fail the SYSTEM enforces, not
593602
// data handed to the caller — categorically unlike an access-control
594603
// rule, which is why RLS predicates are excluded from this capability
595-
// altogether. Reading as the acting user instead made the rule
596-
// unauthorable for exactly the persona it exists to constrain: a member
597-
// with CRUD on the child and no read on the parent faulted on every
598-
// write, so a legitimate business rule could not ship.
604+
// altogether. [#29029] A reference the caller supplies and cannot read
605+
// never reaches a rule: the write's reference check refuses it first
606+
// (`reference_not_found`), so no caller writes a row naming a related
607+
// row it cannot read. What this read still serves without a read door
608+
// is a row the caller can read, a value the row already stores, and a
609+
// system caller.
599610
//
600611
// What bounds the elevation is the PROJECTION: only the
601612
// columns this predicate names, intersected with the related object's
@@ -604,11 +615,12 @@ export async function resolveOneHopRelated(
604615
// The accepted cost, recorded so nobody widens it by accident: a caller
605616
// can INFER a value they cannot see by observing which writes refuse —
606617
// a column hidden from them on a row they may read, or a column of a
607-
// row a plain `lookup` names on a walled object. The value itself never
608-
// appears — not in the row handed to CEL beyond the predicate's own use
609-
// of it, and not in the refusal text, which names the field and the
610-
// rule and never the value. [#22892] ⛔ Never what a master-detail
611-
// header the caller cannot read holds: the door below answers first.
618+
// row a plain `lookup` the row already stores names on a walled object.
619+
// The value itself never appears — not in the row handed to CEL beyond
620+
// the predicate's own use of it, and not in the refusal text, which
621+
// names the field and the rule and never the value. [#22892] ⛔ Never
622+
// what a master-detail header the caller cannot read holds: the door
623+
// below answers first.
612624
//
613625
// ⛔ No organization wall scopes a related object with no tenant column
614626
// (e.g. `sys_user`), `tenancy.enabled: false` or `external`, so for a

‎packages/objectql/src/engine-validate.ts‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,17 @@ export async function validate(
574574
object, schemaForValidation, mode, row, mode === 'insert' ? row : judgedViews[i], options?.context, storedIds[i],
575575
);
576576
}
577+
// [#26684] The insert's admit step, run as `insert()` runs it: before
578+
// the rules (#29029), the reference check on the ids the caller supplied
579+
// (`rawRows`, before the defaults — the write's `suppliedPerRow`), so
580+
// a row the write refuses on a dangling or unreadable reference is
581+
// refused here with the same finding, and is not counted toward a later
582+
// row's parent summary. ⛔ Behind the same write-gate probe as the
583+
// related read above: a caller the probe refuses learns nothing about
584+
// which ids exist.
585+
if (mode === 'insert' && mayWrite) {
586+
await this.assertReferencesResolve(schemaForValidation, row, rawRows[i], options?.context, messages);
587+
}
577588
warnings.push(...evaluateValidationRules(schemaForValidation as any, row, mode, {
578589
// [#22445] The stored row, when this row named one: the by-id
579590
// update's `previous: priorRecord`. Absent, the record is the patch.
@@ -587,17 +598,6 @@ export async function validate(
587598
related: batchSummaries ? batchSummaries.overlay(previewRelatedForRow(judgedViews[i])) : previewRelatedForRow(judgedViews[i]),
588599
permissions: previewPermissionsFor(row),
589600
}));
590-
// [#26684] The insert's admit step, run as `insert()` runs it: after
591-
// the rules, the reference check on the ids the caller supplied
592-
// (`rawRows`, before the defaults — the write's `suppliedPerRow`), so
593-
// a row the write refuses on a dangling reference is refused here
594-
// with the same finding, and is not counted toward a later row's
595-
// parent summary. ⛔ Behind the same write-gate probe as the related
596-
// read above: the probe on a `lookup` target is elevated, so a caller
597-
// the probe refuses learns nothing about which ids exist.
598-
if (mode === 'insert' && mayWrite) {
599-
await this.assertReferencesResolve(schemaForValidation, row, rawRows[i], options?.context, messages);
600-
}
601601
} catch (e) {
602602
if (e instanceof ValidationError) {
603603
return { valid: false, errors: e.fields.map((f) => ({ ...f })), warnings };

‎packages/objectql/src/engine.ts‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6492,10 +6492,13 @@ export class ObjectQL implements IObjectQLEngine {
64926492
// [ADR-0144 D8 rule 4] A `record`-mode amount is stored with its
64936493
// currency code, or the row is refused (`ERR_CURRENCY_CODE_REQUIRED`).
64946494
assertRecordCurrencyCodes(object, (schemaForValidation as { fields?: unknown } | undefined)?.fields, rows[i], 'insert');
6495-
emitValidationAdvisories(options?.onValidationAdvisory, evaluateValidationRules(schemaForValidation as any, rows[i], 'insert', { logger: this.logger, currentUser: this.buildEvalUser(opCtx.context), skipStateMachine: shouldSkipStateMachine(opCtx.context), messages: msgCtx, parent: insertParentForRow?.(rows[i]), related: batchSummaries ? batchSummaries.overlay(insertRelatedForRow(rows[i])) : insertRelatedForRow(rows[i]), permissions: insertPermissionsFor(rows[i]) }), this.logger, object);
6495+
// [#29029] The reference check runs BEFORE the record's validation
6496+
// rules: a reference the caller cannot read answers
6497+
// `reference_not_found`, whatever a rule would answer.
64966498
await this.assertReferencesResolve(
64976499
schemaForValidation, rows[i], suppliedPerRow[i], opCtx.context, msgCtx,
64986500
);
6501+
emitValidationAdvisories(options?.onValidationAdvisory, evaluateValidationRules(schemaForValidation as any, rows[i], 'insert', { logger: this.logger, currentUser: this.buildEvalUser(opCtx.context), skipStateMachine: shouldSkipStateMachine(opCtx.context), messages: msgCtx, parent: insertParentForRow?.(rows[i]), related: batchSummaries ? batchSummaries.overlay(insertRelatedForRow(rows[i])) : insertRelatedForRow(rows[i]), permissions: insertPermissionsFor(rows[i]) }), this.logger, object);
64996502
// [#25579] Admitted: a later row of this batch reads it in the summary.
65006503
batchSummaries?.accept(rows[i]);
65016504
} catch (e) {
@@ -8322,15 +8325,16 @@ export class ObjectQL implements IObjectQLEngine {
83228325
// resolution failure fails this write closed right here.
83238326
const updatePayload = hookContext.input.data as Record<string, unknown>;
83248327
const permissionsForUpdate = (await this.resolveOptionPermissions(updateSchema, [updatePayload], permissionResolution))(updatePayload);
8325-
emitValidationAdvisories(options?.onValidationAdvisory, evaluateValidationRules(updateSchema as any, hookContext.input.data as Record<string, unknown>, 'update', { previous: priorRecord, logger: this.logger, currentUser: this.buildEvalUser(opCtx.context), skipStateMachine: shouldSkipStateMachine(opCtx.context), messages: updateMsgCtx, parent: roWhenParent, previousParent: roWhenPreviousParent, related: relatedForUpdate, permissions: permissionsForUpdate }), this.logger, object);
83268328
// [#4441] A repoint is as capable of dangling as an initial link.
83278329
// [#29062] …but a value the row already stores is not a repoint:
83288330
// the prior row is what the check forgives a refused value against.
8331+
// [#29029] Before the record's validation rules, as on insert.
83298332
await this.assertReferencesResolve(
83308333
updateSchema, hookContext.input.data as Record<string, unknown>,
83318334
opCtx.data as Record<string, unknown>, opCtx.context, updateMsgCtx,
83328335
async () => [priorRecord],
83338336
);
8337+
emitValidationAdvisories(options?.onValidationAdvisory, evaluateValidationRules(updateSchema as any, hookContext.input.data as Record<string, unknown>, 'update', { previous: priorRecord, logger: this.logger, currentUser: this.buildEvalUser(opCtx.context), skipStateMachine: shouldSkipStateMachine(opCtx.context), messages: updateMsgCtx, parent: roWhenParent, previousParent: roWhenPreviousParent, related: relatedForUpdate, permissions: permissionsForUpdate }), this.logger, object);
83348338
// [commit 9d7f7259f] The by-id driver exit — where a driver's refusal
83358339
// leaves this door, and where a recognised unique violation
83368340
// stops being the driver's error. `envelopeUniqueViolation`
@@ -8735,6 +8739,18 @@ export class ObjectQL implements IObjectQLEngine {
87358739
// set — the patch is shared, so either every row picks a
87368740
// `can`-gated option or none does. Never per row.
87378741
const bulkPermissions = (await this.resolveOptionPermissions(updateSchema, [bulkPatch], permissionResolution))(bulkPatch);
8742+
// [#4441] The bulk call site too — a guard wired into single-id
8743+
// writes only is still a hole one call site over (AGENTS.md
8744+
// PD #10's own worked example, #3106).
8745+
// [#29062] A refused value is forgiven only when EVERY matched row
8746+
// already stores it (the `readPriorRows` memo, read only then);
8747+
// a row it is new to makes it this write's reference.
8748+
// [#29029] Before the record's validation rules, as on insert.
8749+
await this.assertReferencesResolve(
8750+
updateSchema, hookContext.input.data as Record<string, unknown>,
8751+
opCtx.data as Record<string, unknown>, opCtx.context, updateMsgCtx,
8752+
readPriorRows,
8753+
);
87388754
if (rulesNeedRows) {
87398755
for (const row of priorRows ?? []) {
87408756
try {
@@ -8756,17 +8772,6 @@ export class ObjectQL implements IObjectQLEngine {
87568772
// rule families that never read a header.
87578773
emitValidationAdvisories(options?.onValidationAdvisory, evaluateValidationRules(updateSchema as any, hookContext.input.data as Record<string, unknown>, 'update', { previous: null, logger: this.logger, currentUser: bulkEvalUser, skipStateMachine: shouldSkipStateMachine(opCtx.context), messages: updateMsgCtx, permissions: bulkPermissions }), this.logger, object);
87588774
}
8759-
// [#4441] The bulk call site too — a guard wired into single-id
8760-
// writes only is still a hole one call site over (AGENTS.md
8761-
// PD #10's own worked example, #3106).
8762-
// [#29062] A refused value is forgiven only when EVERY matched row
8763-
// already stores it (the `readPriorRows` memo, read only then);
8764-
// a row it is new to makes it this write's reference.
8765-
await this.assertReferencesResolve(
8766-
updateSchema, hookContext.input.data as Record<string, unknown>,
8767-
opCtx.data as Record<string, unknown>, opCtx.context, updateMsgCtx,
8768-
readPriorRows,
8769-
);
87708775
// `updateMany` presence is part of the ladder verdict resolved above.
87718776
// [commit 9d7f7259f] The predicate driver exit, enveloped on the same
87728777
// terms as the by-id exit above. A multi-row write names no

0 commit comments

Comments
 (0)