Skip to content

Commit 0ae1bab

Browse files
committed
fix(objectql): the update's reference forgiveness compares only stored values the caller may read
A refused reference value an update carries back is forgiven only when every row the write lands on stores it as the caller's own read door serves that row. A column the caller cannot read holds no stored value for it, so it forgives nothing and the value is judged like any other. A caller who reads the column keeps the forgiveness; a system write compares the rows as stored, as before. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibvynRbCtMndW8EHNGc
1 parent 5b7d00f commit 0ae1bab

2 files changed

Lines changed: 82 additions & 2 deletions

File tree

‎packages/objectql/src/engine-referential-integrity.ts‎

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import { referenceTargetOf, type EngineQueryOptions } from '@objectstack/spec/da
5454
import type { ExecutionContext } from '@objectstack/spec/kernel';
5555
import { isUserBanInForce, keepsFlowWriteReferenceCheck, USER_BAN_FIELDS } from '@objectstack/core';
5656
import { ValidationError, buildFieldError } from './validation/record-validator.js';
57+
import { servedAsStored } from './engine-top-level.js';
5758
import type { ObjectQL } from './engine.js';
5859

5960
/**
@@ -166,6 +167,11 @@ export function referencesCheckedBeforeRules(context: unknown): boolean {
166167
* value in place, `DELETE_SEMANTICS_REFERENCE_TYPES`) is not the editor's
167168
* to answer for. A value new to any of the rows is refused as before, and
168169
* an insert has no stored rows, so every value it supplies is judged.
170+
* [#29029] The stored rows are compared as the caller may read them
171+
* ({@link storedReferencesCallerReads}): a column the caller cannot read
172+
* holds no stored value for it, so it forgives nothing, and the answer is
173+
* the same whatever the column holds. A system write compares them as
174+
* stored, as before.
169175
*
170176
* ## Why the existence probe bypasses RLS — and why it keeps the tenant wall
171177
*
@@ -223,7 +229,7 @@ export function referencesCheckedBeforeRules(context: unknown): boolean {
223229
* (see {@link masterDetailHeadersCallerReads}).
224230
*/
225231
export async function assertReferencesResolve(
226-
this: Pick<ReferentialIntegrityHost, 'logger' | 'callerReadableReferenceExists' | 'callerReadableReferenceRow' | 'referenceExists' | 'referenceRow'>,
232+
this: Pick<ReferentialIntegrityHost, 'logger' | 'find' | 'callerReadableReferenceExists' | 'callerReadableReferenceRow' | 'referenceExists' | 'referenceRow'>,
227233
schema: any,
228234
data: Record<string, unknown> | null | undefined,
229235
supplied: Record<string, unknown> | null | undefined,
@@ -328,7 +334,14 @@ export async function assertReferencesResolve(
328334
// [#29062] A refused value every row this write lands on already stores
329335
// is not a reference the write ADDS, so it is not this caller's to answer
330336
// for. The stored rows are read only now, once a probe has refused.
331-
const stored = storedRows ? (await storedRows() ?? []).filter(isRow) : [];
337+
// [#29029] …and only as this caller may read them
338+
// ({@link storedReferencesCallerReads}): the update's reference forgiveness
339+
// compares only stored values the caller may read, so a column it cannot
340+
// read forgives nothing, and its value is refused like any other.
341+
const storedAsStored = storedRows ? (await storedRows() ?? []).filter(isRow) : [];
342+
const stored = storedAsStored.length === 0
343+
? storedAsStored
344+
: await storedReferencesCallerReads.call(this, schema?.name, storedAsStored, [...new Set(refused.map((r) => r.name))], context);
332345
const failures = refused
333346
.filter((r) => stored.length === 0 || !stored.every((row) => storedReferenceIds(row[r.name]).has(String(r.value))))
334347
.map((r) => buildFieldError(
@@ -378,6 +391,71 @@ function isRow(row: unknown): row is Record<string, unknown> {
378391
return row !== null && typeof row === 'object';
379392
}
380393

394+
/**
395+
* [#29029] The stored rows an update's reference forgiveness compares against,
396+
* as THIS caller may read them: for each of `rows`, only the `fields` the
397+
* caller's own read door serves for that row exactly as stored
398+
* ({@link servedAsStored}). One `find` for the batch, under the caller's own
399+
* context, so every enforcement layer in the middleware chain (RBAC, RLS,
400+
* sharing, field-level security) answers it, as it answers
401+
* {@link masterDetailHeadersCallerReads}.
402+
*
403+
* A column the caller cannot read — hidden, masked or rewritten for it — is
404+
* absent from the row returned here, so it holds no stored id and forgives
405+
* nothing: the value the write names is judged like any other, and the
406+
* answer does not depend on what the column holds. A row the read door does
407+
* not return, or a read that fails, keeps no column either; ⛔ never the row
408+
* as stored. A caller who can read the column keeps the forgiveness: an
409+
* unchanged stored reference is still accepted.
410+
*
411+
* A SYSTEM caller (a flow run's elevated write, the one system write the
412+
* reference check judges) is answered with `rows` unchanged, as before.
413+
*/
414+
export async function storedReferencesCallerReads(
415+
this: Pick<ReferentialIntegrityHost, 'find' | 'logger'>,
416+
object: unknown,
417+
rows: ReadonlyArray<Record<string, unknown>>,
418+
fields: ReadonlyArray<string>,
419+
context: ExecutionContext | undefined,
420+
): Promise<ReadonlyArray<Record<string, unknown>>> {
421+
if (context?.isSystem) return rows;
422+
const ids = new Map<string, unknown>();
423+
for (const row of rows) {
424+
const id = row.id;
425+
if (typeof id === 'string' || typeof id === 'number') ids.set(String(id), id);
426+
}
427+
const served = new Map<string, Record<string, unknown>>();
428+
if (typeof object === 'string' && object && ids.size > 0) {
429+
const values = [...ids.values()];
430+
try {
431+
const found = await this.find(object, {
432+
where: { id: values.length === 1 ? values[0] : { $in: values } },
433+
fields: ['id', ...fields],
434+
context,
435+
} as EngineQueryOptions) as Array<Record<string, unknown>>;
436+
for (const row of Array.isArray(found) ? found : []) {
437+
if (isRow(row) && row.id != null && ids.has(String(row.id))) served.set(String(row.id), row);
438+
}
439+
} catch (err) {
440+
this.logger?.warn?.('the caller\'s read of the stored record failed — its stored references forgive nothing', {
441+
object, error: err,
442+
});
443+
}
444+
}
445+
return rows.map((row) => {
446+
const view = row.id != null ? served.get(String(row.id)) : undefined;
447+
const kept: Record<string, unknown> = {};
448+
if (!view) return kept;
449+
for (const field of fields) {
450+
if (Object.prototype.hasOwnProperty.call(view, field) && Object.prototype.hasOwnProperty.call(row, field)
451+
&& servedAsStored(view[field], row[field])) {
452+
kept[field] = row[field];
453+
}
454+
}
455+
return kept;
456+
});
457+
}
458+
381459
/** The ids a stored reference value holds: each member of a multi-valued one, or the one scalar. */
382460
function storedReferenceIds(value: unknown): ReadonlySet<string> {
383461
const members = Array.isArray(value) ? value : [value];

‎packages/objectql/src/engine.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,8 @@ export class ObjectQL implements IObjectQLEngine {
27832783
const readLogger = () => this.logger;
27842784
const host: ThisParameterType<typeof assertReferencesResolve> = {
27852785
get logger() { return readLogger(); },
2786+
// [#29029] The stored rows a refused value is forgiven against are read through the caller's own door.
2787+
find: (...a) => this.find(...a),
27862788
callerReadableReferenceExists: (...a) => this.callerReadableReferenceExists(...a),
27872789
callerReadableReferenceRow: (...a) => this.callerReadableReferenceRow(...a),
27882790
referenceExists: (...a) => this.referenceExists(...a),

0 commit comments

Comments
 (0)