Skip to content

Commit e7d7cc1

Browse files
committed
fix(runtime): a declarative update action refuses an ungranted caller 403 before the subject-load verdict, as the data door does
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4czxVZYV4CcMiCbofw3
1 parent a2f523a commit e7d7cc1

2 files changed

Lines changed: 441 additions & 0 deletions

File tree

‎packages/runtime/src/action-execution.ts‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type { ExecutionContext } from '@objectstack/spec/kernel';
2424
import { isStoredMetadataBodyObject } from '@objectstack/spec/kernel';
2525
import type { IHttpServer, IObjectQLEngine, InternalFieldEvaluationQuery, QueryEvaluationGate, ServiceSlotContract, ServiceSlotContracts } from '@objectstack/spec/contracts';
2626
import { standardErrorCodeForHttpStatus, type MetadataProtocol } from '@objectstack/spec/api';
27+
import { renderOperationMessage } from '@objectstack/spec/system';
2728
// [#15942 / #16293] The confirmation member's SPELLING is the contract, so it
2829
// is imported, never hand-spelled: the door that refuses and the client that
2930
// retries have to agree on it, and two doors each writing their own 'confirm'
@@ -3709,6 +3710,88 @@ export function refuseDeniedSubjectLoad(
37093710
throw recordNotFoundError(objectName, recordId ?? '');
37103711
}
37113712

3713+
/**
3714+
* [#30848] The declarative update's object-level update refusal, judged before
3715+
* its subject-load verdict is: the data door's order, "the update is judged
3716+
* before the record is looked up" (`http-protocol.mdx`).
3717+
*
3718+
* ## What was wrong
3719+
*
3720+
* A caller who may read an object but holds no edit grant on it, writing a
3721+
* row their scope hides (or an id that names nothing), was answered
3722+
* `404 RECORD_NOT_FOUND` by a declarative `operation: 'update'` action,
3723+
* through REST `/actions` and MCP `run_action` both, while the data door
3724+
* answered the same write `403 PERMISSION_DENIED` (`PATCH /data`, MCP
3725+
* `update_record`). The action door consumed {@link refuseDeniedSubjectLoad}'s
3726+
* 404 first, so the write, which would have been refused for the object
3727+
* before any row was read, was never asked. On a row the caller can see, the
3728+
* write is asked and both doors already answered 403.
3729+
*
3730+
* ## The ask, and why it is not a second decision
3731+
*
3732+
* Asked only when the subject load was denied: on a delivered row the write
3733+
* below carries the caller's identity and the data plane answers its own
3734+
* refusal, the same one this ask stands in for. The question is the security
3735+
* service's `explain` for `update` on the object, the contract's "would the
3736+
* middleware allow this operation?" bottom line, computed by the enforcement
3737+
* walk and not re-derived here from permission sets (the REST import-template
3738+
* gate asks `create` the same way). ⛔ No row is named: the answer must not
3739+
* depend on whether the id names a row, or the door becomes an existence
3740+
* oracle.
3741+
*
3742+
* - `allowed` ⇒ no refusal here; the caller who may update the object keeps
3743+
* the not-found answer, one answer for a hidden row and a missing id.
3744+
* - a denial, or an `explain` that throws ⇒ the data door's own refusal:
3745+
* `403 PERMISSION_DENIED` with the middleware's user-facing sentence
3746+
* (`permission_denied`, the caller's locale, the deployment's override).
3747+
* A throw is a denial, never read as a grant.
3748+
* - a system context, no security service, or one without `explain` ⇒ no
3749+
* refusal here: no permission set exists to deny with, as the data door.
3750+
*/
3751+
export async function refuseUngrantedDeclarativeUpdate(
3752+
deps: ActionExecutionDeps,
3753+
requestContext: HttpProtocolContext | undefined,
3754+
objectName: string,
3755+
ec: any,
3756+
envId?: string,
3757+
): Promise<void> {
3758+
if (ec?.isSystem === true) return;
3759+
let security: { explain?: (request: { object: string; operation: string }, context?: any) => Promise<{ allowed?: boolean } | undefined> } | undefined;
3760+
try {
3761+
security = await deps.resolveService(requestContext as HttpProtocolContext, 'security', envId);
3762+
} catch {
3763+
security = undefined;
3764+
}
3765+
if (!security || typeof security.explain !== 'function') return;
3766+
let allowed: boolean;
3767+
try {
3768+
allowed = (await security.explain({ object: objectName, operation: DECLARATIVE_UPDATE_OPERATION }, ec))?.allowed === true;
3769+
} catch {
3770+
allowed = false;
3771+
}
3772+
if (allowed) return;
3773+
let translate: ((key: string, locale: string, params?: Record<string, unknown>) => string) | undefined;
3774+
try {
3775+
const i18n: { t?: unknown } | null | undefined = await deps.resolveService(requestContext as HttpProtocolContext, 'i18n', envId);
3776+
if (i18n && typeof i18n.t === 'function') {
3777+
const t = i18n.t as (key: string, locale: string, params?: Record<string, unknown>) => string;
3778+
translate = (key, locale, params) => t.call(i18n, key, locale, params);
3779+
}
3780+
} catch {
3781+
translate = undefined;
3782+
}
3783+
const message = renderOperationMessage(
3784+
{ messageKey: 'permission_denied' },
3785+
{ locale: typeof ec?.locale === 'string' ? ec.locale : undefined, translate },
3786+
);
3787+
throw Object.assign(new Error(message), {
3788+
name: 'PermissionDeniedError',
3789+
code: 'PERMISSION_DENIED',
3790+
status: 403,
3791+
statusCode: 403,
3792+
});
3793+
}
3794+
37123795
/**
37133796
* Contract point 3 as the two action doors' dispatch reads it, for every
37143797
* branch: {@link refuseDeniedSubjectLoad}, except for a same-origin
@@ -4166,6 +4249,14 @@ export async function executeDeclarativeUpdateAction(
41664249
// the flow door and the script/body door now call too. Behaviour here is
41674250
// unchanged to the byte — what changed is that this is no longer the only
41684251
// door that reads the verdict, so the rule cannot be inert two doors over.
4252+
//
4253+
// [#30848] …but the object-level update refusal comes first, as at the
4254+
// data door: a caller with no edit grant gets the write's 403 whether or
4255+
// not the id names a row it can read. Asked only on a denied load: a
4256+
// delivered row reaches the write below, which answers that 403 itself.
4257+
if (subject.recordLoadDenied) {
4258+
await refuseUngrantedDeclarativeUpdate(_deps, wiring.requestContext, objectName, ec, envId);
4259+
}
41694260
refuseDeniedSubjectLoad(objectName, recordId, subject);
41704261

41714262
// ── contract point 2: ONE data-plane update, AS THE CALLER ───────────────

0 commit comments

Comments
 (0)