Skip to content

Commit e428aac

Browse files
committed
fix(plugin-approvals): a non-scalar FK on a detail write fails closed
A supplied master-detail FK that is not a scalar id (an object such as `{ id }`, or an array) read as no master, so the detail-row lock was silent on insert and on the new end of a reparent. Such a value now marks the write unresolved and is refused while any record of that master object is held by a locking approval. The unresolved refusal now carries the same end-user sentence naming the master as the resolved one. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Utp6CZrgaiMgTJCnswR6jQ
1 parent 72de91f commit e428aac

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

‎packages/plugins/plugin-approvals/src/detail-row-lock.integration.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ describe('ruling A — a locked master\'s detail rows refuse insert / update / d
143143
expect(underLocked).toHaveLength(1);
144144
});
145145

146+
it('a non-scalar FK naming a locked master ({ id }) fails closed: refused, nothing lands', async () => {
147+
expectLocked(await refusal(() => engine.insert('opportunity_line', { product: 'D', quantity: 1, opportunity: { id: lockedId } }, USER)));
148+
const lines = await engine.find('opportunity_line', { where: { product: 'D' }, context: { isSystem: true } } satisfies EngineQueryOptions);
149+
expect(lines).toHaveLength(0);
150+
});
151+
146152
it('update of a locked master\'s row is refused and nothing lands; a free master\'s row updates (CONTROL)', async () => {
147153
expectLocked(await refusal(() => engine.update('opportunity_line', { quantity: 9 }, { where: { id: lineOfLocked }, ...USER })));
148154
expect((await engine.findOne('opportunity_line', { where: { id: lineOfLocked } }))?.quantity).toBe(1);

‎packages/plugins/plugin-approvals/src/lifecycle-hooks.ts‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,17 @@ function scalarFk(value: unknown): string | null {
783783
return null;
784784
}
785785

786+
/**
787+
* A supplied FK value that names SOMETHING but not as a scalar id — an object
788+
* such as `{ id }`, or an array. The guard cannot tell which master it names
789+
* and does not rely on a later step unwrapping it, so the write is judged as
790+
* unresolved (fail closed), never as touching no master (L1-a R2, #35755).
791+
*/
792+
function unscalarFk(value: unknown): boolean {
793+
if (value === undefined || value === null || value === '') return false;
794+
return scalarFk(value) === null;
795+
}
796+
786797
/**
787798
* Bind the DETAIL-ROW lock (ruling A on the record-page edit card,
788799
* 「一起锁」): while a master record is held by a LOCKING pending approval, its
@@ -850,6 +861,7 @@ export function bindDetailRowLockHook(
850861
for (const e of edges) {
851862
const id = scalarFk(data[e.fk]);
852863
if (id) touched.push({ master: e.master, id });
864+
else if (unscalarFk(data[e.fk])) unresolved = true;
853865
}
854866
} else {
855867
let previous = ctx?.previous as Record<string, unknown> | undefined;
@@ -875,6 +887,7 @@ export function bindDetailRowLockHook(
875887
if (event === 'update' && Object.prototype.hasOwnProperty.call(data, e.fk)) {
876888
const after = scalarFk(data[e.fk]);
877889
if (after && after !== before) touched.push({ master: e.master, id: after });
890+
else if (unscalarFk(data[e.fk])) unresolved = true;
878891
}
879892
}
880893
}
@@ -883,6 +896,18 @@ export function bindDetailRowLockHook(
883896
const exempt = (pending: any) =>
884897
!!writerRun && !!pending?.flow_run_id && String(writerRun) === String(pending.flow_run_id);
885898

899+
// The end-user sentence naming the MASTER object (the record lock's own).
900+
const sentenceFor = (master: string): string => {
901+
let schema: unknown;
902+
try { schema = engine.getSchema?.(master); } catch { /* registry unavailable — label degrades */ }
903+
const declaredLabel = typeof (schema as any)?.label === 'string' && (schema as any).label
904+
? String((schema as any).label)
905+
: undefined;
906+
const { locale, service, translate } = refusalLocalization(ctx, i18nService);
907+
const masterLabel = readerLabelsFor(service, locale)?.objectLabel(master) ?? declaredLabel;
908+
return recordLockRefusal(masterLabel, undefined, locale, translate);
909+
};
910+
886911
// Fail closed on a write whose rows could not be named: refused when any
887912
// record of any master is held by a locking approval.
888913
if (unresolved) {
@@ -893,6 +918,7 @@ export function bindDetailRowLockHook(
893918
lockedError(
894919
`cannot determine which '${e.master}' record a ${event} on its detail object '${object}' falls under, ` +
895920
`and ${locking.length} record(s) of '${e.master}' carry a locking pending approval, so the write is refused`,
921+
sentenceFor(e.master),
896922
);
897923
}
898924
}
@@ -901,18 +927,11 @@ export function bindDetailRowLockHook(
901927
for (const { master, id } of touched) {
902928
const locking = (await lockingRequestsForRecords(engine, master, [id])).filter((p) => !exempt(p));
903929
if (locking.length === 0) continue;
904-
let schema: unknown;
905-
try { schema = engine.getSchema?.(master); } catch { /* registry unavailable — label degrades */ }
906-
const declaredLabel = typeof (schema as any)?.label === 'string' && (schema as any).label
907-
? String((schema as any).label)
908-
: undefined;
909-
const { locale, service, translate } = refusalLocalization(ctx, i18nService);
910-
const masterLabel = readerLabelsFor(service, locale)?.objectLabel(master) ?? declaredLabel;
911930
logger?.info?.(
912931
`[approvals] ${event} on detail object '${object}' refused RECORD_LOCKED: its master '${master}' ` +
913932
`record '${id}' is held by pending approval request '${String(locking[0]?.id ?? '')}'`,
914933
);
915-
const sentence = recordLockRefusal(masterLabel, undefined, locale, translate);
934+
const sentence = sentenceFor(master);
916935
lockedError(sentence, sentence);
917936
}
918937
};

0 commit comments

Comments
 (0)