Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .changeset/35174-action-api-platform-system-api-mark.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
'@objectstack/runtime': patch
---
Expand All @@ -13,5 +13,4 @@
- The flag is not the app-authored flag: a host handler's `delete(…, { permanent: true })` still runs.
- The api the audit writer holds (a hook registered in code) is unchanged: `platformSystemApi` still takes the mark off there.
- Runtime's derivation walk now wraps the api any symbol-keyed member derives (`platformSystemApi`'s, the user-grants seeds'), awaited when it is a promise, so it is served through the stored-metadata read seam and the landed-write answer like `ctx.api.sudo()`.
- Not covered: the api `ctx.api.object(o).execute(name)` hands the action it dispatches is built by the engine, not the action door, so `platformSystemApi` still takes the mark off there.
- No new export, and `@objectstack/objectql` is unchanged but for its module header: the member's key is the `Symbol.for` key objectql already uses, which neither package entry exports.
15 changes: 15 additions & 0 deletions .changeset/35434-execute-dispatched-action-keeps-mark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@objectstack/objectql': patch
---

fix(objectql): `platformSystemApi` on the api `ObjectRepository.execute` hands the action it dispatches keeps the `'action'` mark

Clause-②: no
Metadata-protocol: none

`ctx.api.object(o).execute(name)` dispatches the named action with an api over a context it marks `'action'`. That api carried neither the app-authored flag nor the action door's origin flag, so `platformSystemApi(ctx.api)` in the dispatched handler took the mark off, and its writes to `sys_member`, `sys_user_position`, `sys_user_permission_set` and `sys_invitation` passed as a platform writer's, against the documented refusal for action handlers.

- The api `execute` hands the dispatched action now carries its own origin flag, kept by every api derived from it (`sudo()`, `withRunAs(...)`, a transaction's context, the api `platformSystemApi` and the user-grants seeds return). On it, `platformSystemApi(api)` returns what `api.sudo()` returns: the `'action'` mark and any `flowRunId` kept. Writes to the four identity-assignment objects through it are refused `PERMISSION_DENIED` (403), whichever api dispatched the action, and its inserts are owner-stamped with the invoking user.
- The flag is not the app-authored flag: the dispatched handler's `delete(…, { permanent: true })` still runs.
- The api the audit writer holds (a hook registered in code) is unchanged: `platformSystemApi` still takes the mark off there.
- This is the path the action-door fix in this release did not reach. No new export, and no published signature changes.
2 changes: 1 addition & 1 deletion content/docs/permissions/system-context.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ bugs — it is the protection or the side effect that silently does not happen.
| # | Behaviour when `isSystem` | Package | What you get / what you lose | Symbol |
|:--|:---|:---|:---|:---|
| 1 | **The whole security middleware short-circuits** before any gate runs | plugin-security | Get: every CRUD/FLS/tenant/owner gate below skipped in one branch. Lose: every gate under it in this lane at once, down to and including the write-bypass row that widens the effective write scope to `org` (row 7) — this is the single largest behaviour on the page | `registerAuthzMiddleware` |
| 1b | **App-authored automation's system context is REFUSED governed writes to the identity-assignment objects** — a flow run's, an app-authored hook's (`runAs: 'system'`, a system context it inherits, its `sudo()`) and an action body's; checked ahead of the middleware's short-circuit, beside the append-only refusal (`assertAppAutomationAppendOnlyWriteAllowed`) | plugin-security | Get: nothing — an insert, update, delete, transfer, restore or purge of `sys_user_position`, `sys_user_permission_set`, `sys_invitation` or `sys_member` from a context carrying `isSystem` and either a `flowRunId` (a `runAs: 'system'` flow's data nodes) or the app-automation elevation mark (`@objectstack/core`; set only by a hook's `runAs: 'system'` derivation, by the action body's execution-context builder and `ObjectRepository.execute`, and on the api of a hook bound through the hook binder, whose inherited system context and `sudo()` it marks), at any depth of their writes, is refused `PERMISSION_DENIED` before the short-circuit skips the ADR-0090 D12 gate and the identity write guard. The platform's own system api (`platformSystemApi`, objectql) takes the mark and `flowRunId` off a platform hook's api, for the audit writer's own ledger rows. On an app-authored hook's api and on the api an action door hands its handler (a host code action handler's included), and on every api derived from either, it answers what that api's `sudo()` answers, mark kept, so this refusal, and the action body's owner stamp in the next row, hold through it too. Not covered: the api `ObjectRepository.execute` hands the action it dispatches (`ctx.api.object(o).execute(name)`) is a bare engine api carrying the `'action'` mark and no action-door flag, so on it `platformSystemApi` still takes the mark off. Lose: nothing a platform writer held — a system context with neither (the seeders, better-auth and its reconciliation, invitation placement, platform code's `sudo()`, which never sets the mark, and a hook registered in code) passes on to the short-circuit unchanged, reads are not judged, and a `runAs: 'user'` flow or hook carries no `isSystem`, so the D12 gate judges it as the user it acts for | `elevationSource`, `appAuthoredHookApi` |
| 1b | **App-authored automation's system context is REFUSED governed writes to the identity-assignment objects** — a flow run's, an app-authored hook's (`runAs: 'system'`, a system context it inherits, its `sudo()`) and an action body's; checked ahead of the middleware's short-circuit, beside the append-only refusal (`assertAppAutomationAppendOnlyWriteAllowed`) | plugin-security | Get: nothing — an insert, update, delete, transfer, restore or purge of `sys_user_position`, `sys_user_permission_set`, `sys_invitation` or `sys_member` from a context carrying `isSystem` and either a `flowRunId` (a `runAs: 'system'` flow's data nodes) or the app-automation elevation mark (`@objectstack/core`; set only by a hook's `runAs: 'system'` derivation, by the action body's execution-context builder and `ObjectRepository.execute`, and on the api of a hook bound through the hook binder, whose inherited system context and `sudo()` it marks), at any depth of their writes, is refused `PERMISSION_DENIED` before the short-circuit skips the ADR-0090 D12 gate and the identity write guard. The platform's own system api (`platformSystemApi`, objectql) takes the mark and `flowRunId` off a platform hook's api, for the audit writer's own ledger rows. On an app-authored hook's api, on the api an action door hands its handler (a host code action handler's included) and on the api `ObjectRepository.execute` hands the action it dispatches (`ctx.api.object(o).execute(name)`), and on every api derived from any of them, it answers what that api's `sudo()` answers, mark kept, so this refusal, and the action body's owner stamp in the next row, hold through it too. Lose: nothing a platform writer held — a system context with neither (the seeders, better-auth and its reconciliation, invitation placement, platform code's `sudo()`, which never sets the mark, and a hook registered in code) passes on to the short-circuit unchanged, reads are not judged, and a `runAs: 'user'` flow or hook carries no `isSystem`, so the D12 gate judges it as the user it acts for | `elevationSource`, `appAuthoredHookApi` |
| 2 | **`owner_id` is not auto-stamped on INSERT** (the step 3.5 anchor guard is inside the block row 1 skips) — except an action body's insert | plugin-security | Lose: the row lands `owner_id = NULL`, so the default `owner_only_writes` policy hides it **from its own creator**. Get: nothing — this is a gap, not a capability. Except an action body's: an insert from a context carrying `isSystem` and the `'action'` app-automation elevation mark, with no `flowRunId`, has every EMPTY `owner_id` stamped with the envelope's own `userId` ahead of the short-circuit, by the same stamp step 3.5 applies (nothing for the guest; an owner the body sets is kept; an `owner_id` supplied empty beside a supplied queue parks the row). Like a direct create, such an insert is the invoker's own record, so assignment rules (ADR-0142 D5), which route only a row whose owner and queue are both still empty, route neither; a body has no spelling today that asks a rule to route its row. The mark is set on every handler an action door dispatches (a host code action handler included) and by `ObjectRepository.execute`. A flow run, a hook's `runAs: 'system'`, the seeders and every other system context still land the owner they write, or none, and an unowned, queue-less one is still routed | the step 3.5 guard block and the short-circuit that skips it are both inside `registerAuthzMiddleware`; the action-body exception is `isActionBodyElevation` |
| 3 | Row-level read filter resolves to "no filter" | plugin-security | Get: unscoped reads. Lose: row-level scoping entirely | `getReadFilter` |
| 4 | Field-level security returns **all** fields | plugin-security | Get: every column readable (`getReadableFields`) and writable (`getWritableFields`), and every field a disabled package withholds named to it (`getMetadataReadableWithheldFields`, so the served object's `_withheldFields` keeps every entry). Lose: field masking | `resolveProjectionFieldMask`, `computeMetadataReadableWithheldFields` |
Expand Down
34 changes: 33 additions & 1 deletion packages/objectql/src/engine-object-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,35 @@ import { markAppAutomationElevation } from '@objectstack/core';
import { ScopedContext } from './engine.js';
import type { ObjectRepository } from './engine.js';

/**
* The dispatched action's origin flag: the api {@link ObjectRepositoryFacet.execute}
* hands the action it dispatches, and every api `ScopedContext`'s `derive`
* builds from one (`engine-scoped-context.ts`). On a flagged api the platform's
* own system api (`platformSystemApi`, `platform-system-api.ts`) is the api's
* `sudo()`: the `'action'` mark `execute` sets, and any `flowRunId`, stay, so the
* dispatched handler's writes to the identity-assignment objects through it are
* judged as an action body's (plugin-security).
*
* A flag of its own, and ⛔ not the engine's app-authored one: that one also
* refuses a permanent delete (ADR-0148 D3), which a dispatched host handler
* keeps. Keyed by instance and held here, beside the one place that sets it, so
* the facade (`engine.ts`) carries no field for it. Every reader reaches it
* through the instance's own members, so the flag and its reader are always the
* same copy of this package.
*/
const DISPATCHED_ACTION_APIS = new WeakSet<ScopedContext>();

/** Flag `api` as the api a dispatched action is handed, or as one derived from it (see {@link DISPATCHED_ACTION_APIS}). */
export function markDispatchedActionApi(api: ScopedContext): ScopedContext {
DISPATCHED_ACTION_APIS.add(api);
return api;
}

/** Is `api` the api a dispatched action is handed, or one derived from it (see {@link DISPATCHED_ACTION_APIS})? */
export function isDispatchedActionApi(api: ScopedContext): boolean {
return DISPATCHED_ACTION_APIS.has(api);
}

/**
* The `ObjectRepository` members the bodies of {@link ObjectRepositoryFacet}
* read — every one of them, and no other. Each is typed AS the class member (an
Expand Down Expand Up @@ -134,6 +163,9 @@ export class ObjectRepositoryFacet {
// (runtime) gives the REST and MCP doors' action bodies: the action this
// reaches is app-authored automation running elevated, so its writes to
// the four identity-assignment objects are refused (plugin-security).
// The api carries the dispatched action's origin flag, so
// `platformSystemApi` on it, and on every api derived from it, keeps
// the mark ({@link DISPATCHED_ACTION_APIS}).
const executionContext: ExecutionContext = markAppAutomationElevation(
{ ...this.context, isSystem: true },
'action',
Expand All @@ -144,7 +176,7 @@ export class ObjectRepositoryFacet {
tenantId: this.context.tenantId,
roles: this.context.positions,
executionContext,
api: new ScopedContext(executionContext, this.engine),
api: markDispatchedActionApi(new ScopedContext(executionContext, this.engine)),
});
}
throw new Error(`Actions not supported by engine`);
Expand Down
19 changes: 12 additions & 7 deletions packages/objectql/src/engine-scoped-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import type { HookRunAs, HookRunAsRef } from './hook-run-as.js';
import { scopedContextHost } from './engine-scoped-hosts.js';
import { ObjectRepository, ScopedContext } from './engine.js';
import type { ObjectQL } from './engine.js';
import { isDispatchedActionApi, markDispatchedActionApi } from './engine-object-repository.js';

/**
* The `ScopedContext` members the bodies of {@link ScopedContextFacet} read —
Expand All @@ -76,16 +77,18 @@ export interface ScopedContextHost {
/** The bodies of `ScopedContext`'s methods: a stateless facet, called by the class's stubs. */
export class ScopedContextFacet {
/**
* A child over `executionContext` that keeps this api's provenance.
* A child over `executionContext` that keeps this api's provenance: the
* app-authored flag, and the dispatched action's origin flag
* (`engine-object-repository.ts`, `markDispatchedActionApi`).
*
* The child's private `appAuthored` is written through the child's own host
* (`scopedContextHost.of(child)`), the port its class's static block fills:
* a module function cannot write another instance's private field directly.
*/
static derive(this: Pick<ScopedContextHost, 'appAuthored' | 'engine'>, executionContext: ExecutionContext): ScopedContext {
static derive(this: Pick<ScopedContextHost, 'appAuthored' | 'engine' | 'facade'>, executionContext: ExecutionContext): ScopedContext {
const child = new ScopedContext(executionContext, this.engine);
scopedContextHost.of(child).appAuthored = this.appAuthored;
return child;
return isDispatchedActionApi(this.facade) ? markDispatchedActionApi(child) : child;
}

/**
Expand Down Expand Up @@ -124,11 +127,13 @@ export class ScopedContextFacet {
/**
* The body of `ScopedContext#[PLATFORM_SYSTEM_API]`; its contract, and
* where its guard stops, is `platform-system-api.ts`'s module header.
* An app-authored hook's api is never the platform's: there it is that
* api's own {@link ScopedContextFacet.sudo}, provenance kept.
* An app-authored hook's api, and the api `ObjectRepository.execute` hands
* the action it dispatches (`isDispatchedActionApi`), are never the
* platform's: there it is that api's own {@link ScopedContextFacet.sudo},
* provenance kept.
*/
static platformSystemApi(this: Pick<ScopedContextHost, 'appAuthored' | 'derive' | 'executionContext'>): ScopedContext {
if (this.appAuthored) return ScopedContextFacet.sudo.call(this);
static platformSystemApi(this: Pick<ScopedContextHost, 'appAuthored' | 'derive' | 'executionContext' | 'facade'>): ScopedContext {
if (this.appAuthored || isDispatchedActionApi(this.facade)) return ScopedContextFacet.sudo.call(this);
const { flowRunId: _run, ...context } = withoutAppAutomationElevation({ ...this.executionContext, isSystem: true });
return this.derive(context as ExecutionContext);
}
Expand Down
18 changes: 10 additions & 8 deletions packages/objectql/src/platform-system-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
* ⛔ For the platform's own rows only, never for a write the automation itself
* asked for: an action body's or a hook's insert into those objects, through
* its `ctx.api` or its `ctx.api.sudo()`, keeps the provenance and is judged.
* That line holds on two of the apis the automation is handed, and on every
* api derived from either:
* That line holds on three of the apis the automation is handed, and on every
* api derived from any of them:
*
* - an app-authored hook's api (its handler's `ctx.api`,
* `APP_AUTHORED_HOOK_API`): the engine enforces it, and this answers
Expand All @@ -47,14 +47,16 @@
* the action door's own origin flag and answers this symbol member with
* the api's `sudo()`, so the `'action'` mark and any `flowRunId` stay. Not
* the engine's app-authored flag, which also refuses a permanent delete
* (ADR-0148 D3) that a host handler keeps.
* (ADR-0148 D3) that a host handler keeps;
* - the api `ObjectRepository.execute` hands the action it dispatches
* (`ctx.api.object(o).execute(name)`, `engine-object-repository.ts`): it
* carries the dispatched action's origin flag, which `derive` copies to
* every api built from it, and this answers it with the api's `sudo()`, so
* the `'action'` mark `execute` sets stays. Not the app-authored flag
* either, for the same reason.
*
* So a handler handing its own api here gains nothing, and its writes to the
* identity-assignment objects are judged as its own. ⚠️ Not covered: the api
* `ObjectRepository.execute` hands the action it dispatches
* (`ctx.api.object(o).execute(name)`, `engine-object-repository.ts`) is a bare
* `ScopedContext` carrying the `'action'` mark and neither line's flag, so on
* it this takes the mark off, as on the audit writer's hook api. The api the audit writer
* identity-assignment objects are judged as its own. The api the audit writer
* holds is neither: `buildHookApi` builds a fresh `ScopedContext` over the
* write's context, carrying the `'action'` mark when an action's write fired
* the hook, and this takes it off there. Neither line is a security boundary:
Expand Down
Loading
Loading