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
17 changes: 17 additions & 0 deletions .changeset/35174-action-api-platform-system-api-mark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@objectstack/runtime': patch
---

fix(runtime): `platformSystemApi` on an action handler's `ctx.api` keeps the `'action'` mark, and what it returns stays wrapped

Clause-②: no
Metadata-protocol: none

`platformSystemApi(api)` (new in this release, from `@objectstack/objectql`) takes the app-automation elevation mark and `flowRunId` off a hook's api, so the audit writer's own ledger rows are not judged as an automation's insert. On the `ctx.api` an action door hands a host code action handler (`engine.registerAction`) it took the `'action'` mark off too, so a handler handing its own api to it could write `sys_member`, `sys_user_position`, `sys_user_permission_set` and `sys_invitation` as a platform writer, against the documented refusal for action handlers. What it returned also bypassed the two layers runtime puts on that api: the stored-metadata read projection and the answer for a write that landed while its parent roll-up could not be recomputed.

- The api an action door builds now carries its own origin flag, a runtime layer beneath its other two, kept by every api derived from it (`sudo()`, `withRunAs(...)`, a transaction's context, and the api `seedHookUserGrants` / `seedHookUnreadMembership` 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), and its inserts are owner-stamped with the invoking user, as the handler's own are.
- 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.
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. 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 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` |
| 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
55 changes: 32 additions & 23 deletions packages/objectql/src/platform-system-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,41 @@
* The write's context, elevated as `sudo()` elevates it — every other key kept,
* so the rows stay in the write's unit of work, its organization and its
* attribution — without the app-automation mark and without `flowRunId`. The
* api is derived as every api is (`derive`), and its own `sudo()` marks again.
* api is derived as every api is (`derive`): not app-authored, so its own
* `sudo()` adds no mark.
*
* ⛔ 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.
* The engine enforces that line for the one api it knows is the automation's:
* on an app-authored hook's api (its handler's `ctx.api`, and every api
* derived from it) this answers exactly what that api's `sudo()` answers — the
* `'hook'` mark when the context carries none, the mark it carries otherwise,
* `flowRunId` kept — so a hook handing its own api here gains nothing.
* That line holds on two of the apis the automation is handed, and on every
* api derived from either:
*
* The line stops there: it guards app-authored hook apis and every api derived
* from one, and nothing else. An app author's in-process action handler,
* registered with `engine.registerAction` (as `examples/app-todo` does from
* `onEnable(ctx.ql)`), is handed runtime's `buildActionApi` api — an engine
* `createContext` carrying the `'action'` mark, and not app-authored. On that
* api this sheds the mark and any `flowRunId`, and the engine cannot tell it
* from the api the audit writer holds (`buildHookApi`, a fresh `ScopedContext`
* over the write's context: not app-authored either, and carrying the same
* mark when an action's write fired the hook). It adds no reach: the handler's
* package registered through the engine it holds, whose
* `createContext({ isSystem: true })` already builds a system api with
* neither. And what it returns is outside runtime's `deriveThroughSeam`
* wrappers on that `ctx.api` (the stored-metadata read seam and the
* landed-write answer): the walk wraps `object`, `sudo`, `withRunAs`,
* `transaction` and `beginTransaction`, and its default arm binds this symbol
* member to the unwrapped api, so the derived api comes back unwrapped.
* - an app-authored hook's api (its handler's `ctx.api`,
* `APP_AUTHORED_HOOK_API`): the engine enforces it, and this answers
* exactly what that api's `sudo()` answers — the `'hook'` mark when the
* context carries none, the mark it carries otherwise, `flowRunId` kept;
* - the api an action door hands its handler, a host code action handler's
* included (runtime's `buildActionApi`): runtime's layer over it carries
* 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.
*
* 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
* 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:
* a host handler's package registered through the engine it holds, whose
* `createContext({ isSystem: true })` already builds a system api with neither
* the mark nor the run. They keep the documented answer for the api a handler
* was handed. And what this returns over runtime's wrapped `ctx.api` stays
* wrapped: runtime's `deriveThroughSeam` (the stored-metadata read seam and
* the landed-write answer) walks this symbol member as it walks `sudo()`.
*
* A symbol, and ⛔ not re-exported from either package entry, for the reason
* `SEED_USER_GRANTS` gives (`hook-run-as.ts`): the method is no nameable member
Expand All @@ -64,7 +72,8 @@
* `Symbol()`, because the reader and the engine may be different copies of
* this package — its ESM and CJS builds, or its two entries (`.` and
* `./core`) served from different builds — and the key must be the same in
* each, or the reader would silently fall back to `sudo()`.
* each, or the reader would silently fall back to `sudo()`; and runtime names
* it too (`deriveThroughSeam`), which no export can serve.
*/

import type { ScopedContext } from './engine.js';
Expand Down
Loading
Loading