Skip to content

Commit 467b13b

Browse files
objectstack-fleet[bot]claudeos-bill
authored
fix(runtime): platformSystemApi on an action handler's ctx.api keeps the 'action' mark, and what it returns stays seam-wrapped (#35383)
repo:objectstack#5 · os-bill · session_01Utp6Ca1jJvsA1dHsPwMiQb Fixes #35174 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:用户无感;宿主代码写的动作处理器若把自己拿到的接口交给「平台自用系统接口」函数(或两个公开的预置钩子辅助函数),拿回的接口仍带动作标记,改成员、岗位、权限集分配、邀请会像文档写的那样被拒,读系统元数据表也仍是投影形态。 - 为什么做:修正「审计写入不再被当作自动化写入」那次改动留下的口子,让权限文档对动作处理器的承诺重新成立,并补上运行时两层包装漏掉的路;动作再调用动作的那条路已在文档里注明不覆盖,另立卡跟进。 - 出错会怎样:最坏是某个宿主动作处理器经此函数写身份分配对象时被拒;目前仓库里没有这样的调用方,审计写入路径不变,回滚本次合并即可。 - 要你做什么:无,席位落地。 ## 席位核对 ### What changes Ruling record 7000291725 (maintainer, verbatim: 「A+B 补包装且保留标记」), plus the scope-addition comment 6975046219 (N1, F3). Contract review 7003367719 (PASS, S1/S2) is addressed in fix round 2. - **A, the walk** (`packages/runtime/src/stored-metadata-reader-seam.ts`): `deriveThroughSeam` now walks symbol-keyed members. Since fix round 2 (L2 S1), the default arm answers EVERY symbol-keyed member through a wrapper. Its result, awaited when it is a promise, is wrapped as `sudo()`'s is when it is an api of the same kind; anything else passes through. This covers the member `platformSystemApi` reads (`Symbol.for('objectstack.objectql.platformSystemApi')`, named in runtime as `PLATFORM_SYSTEM_API`, a module export only, not on runtime's package entry) and the published `seedHookUserGrants` / `seedHookUnreadMembership`. So what they return stays inside the stored-metadata read seam and the landed-write answer. Before, the `default` arm bound the member to the unwrapped target. - **B, the origin flag** (`packages/runtime/src/action-execution.ts`): `buildActionApi` builds the action door's api over a third layer, `keepActionDoorProvenance`, beneath the landed-write and read-seam layers. It is the same shared walk with its own marker, `Symbol.for('objectstack.runtime.actionDoorOrigin')`, so the api and every api derived from it carry the flag. On each, the walk's new `answeredBy` option answers the platform-system member with the api's `sudo()`, so the `'action'` mark and any `flowRunId` stay. It is a NEW flag, not the engine's `appAuthored`, so a host handler keeps the engine's permanent delete (ADR-0148 D3). Repositories pass through this layer unchanged. - **Docs**: - N1, the `packages/objectql/src/platform-system-api.ts` module header. "its own `sudo()` marks again" becomes "not app-authored, so its own `sudo()` adds no mark". The "the line stops there" gap paragraph now describes the apis the line holds on, and names `ObjectRepository.execute`'s api as not covered (L2 S2). - F3, `content/docs/permissions/system-context.mdx` row 1b. One clause names `platformSystemApi` as the producer that takes the mark off a platform hook's api only, and answers `sudo()` on an app-authored hook's api and on the action door's api. So the refusal, and the action body's owner stamp in the next row, hold through it. The row also names `ctx.api.object(o).execute(name)`'s api as not covered; follow-up #35434. - Changeset: `@objectstack/runtime` patch, with a "Not covered" bullet. `@objectstack/objectql` changes only in a module-header comment. ### Tests Head `6bec461ee` (fix round 2). - Round-2 pins: 2 new cases, one per published seed helper. Through `platformSystemApi(await seed(ctx.api))`, an insert into `sys_member` is refused `PERMISSION_DENIED` / 403 and nothing lands. The seeded api answers the origin flag and both seam markers. The file is 10/10. - Round-2 ablation: the symbol-key wrapper in `stored-metadata-reader-seam.ts` was removed (blob `da23f6d7` → `67b1f3bf`). Result: 3 failed, 7 passed. Both seed pins and the walk pin were red; the B pins and both CONTROLs stayed green. Restored: blob equals HEAD. - Full runtime suite at `6bec461`: 11578 passed, 1 failed. The failure is a QuickJS CPU-budget case (`app-artifact-handlers.jobs.test.ts`) that timed out under load. It passes alone (41/41) and was green on `bb4bdda`. - dispatch-gates at `6bec461`: 101 derived, 99 exit 0, 2 NOT MEASURED (prerequisite builds; both exit 0 at `2919fe6ac`). - CI is green at `6bec461`. Round-1 record (head `2919fe6ac`): - New file `packages/runtime/src/action-door-platform-system-api.integration.test.ts` (real `ObjectQL`, a real SQL driver, the real `SecurityPlugin`; the store is read back past the middleware). Each ruling clause maps to a pin: 1. **B, mark kept**: through `platformSystemApi(api)`, `platformSystemApi(api.sudo())` and `platformSystemApi(trx)` inside `api.transaction`, an insert into each of `sys_user_position`, `sys_user_permission_set`, `sys_invitation`, `sys_member` is refused `PERMISSION_DENIED` / 403 with the action-body prescription, and no row lands (4 cases). Its insert into an ordinary object is owner-stamped with the caller (the row-2 stamp). 2. **A, walk**: `platformSystemApi(api)`, `platformSystemApi(api.sudo())` and `api.sudo()` each answer both layers' markers and the origin flag. CONTROL: a bare `engine.createContext` api answers none. 3. **CONTROL, the audit writer's path**: a hook registered in code, fired by the action api's insert, writes `sys_member` and `sys_audit_log` through `platformSystemApi(hctx.api)`, and both land. In the same hook, `hctx.api.sudo()` is refused, which proves the hook's context carried the mark. 4. **CONTROL, ADR-0148 D3**: the host handler's `delete({ where, permanent: true })` runs through `api` and through `platformSystemApi(api)`. - `pnpm --filter @objectstack/objectql typecheck` and `pnpm --filter @objectstack/runtime typecheck`: exit 0, with test layers OK. <details><summary>Round-1 ablations (one per clause, both on runtime src, which the suite imports relatively; no build in the path)</summary> - **A**: `ablation-replace.mjs` deleted `case PLATFORM_SYSTEM_API:` from the walk. Result: 1 failed, 7 passed; the walk pin was red. Restored. (Round 2 folded that case into the symbol-key arm; the round-2 ablation above covers it.) - **B**: `ablation-replace.mjs` replaced `keepActionDoorProvenance(scoped)` with `scoped`. Result: 6 failed, 2 passed. The four identity pins, the owner stamp, and the walk pin's origin-flag assertion were red, and the two CONTROLs stayed green. Restored. </details> ### Acceptance notes - **Where B's flag lives, measured against A4.** The flag first rode as a private `ScopedContext` field. That grew `packages/objectql/src/engine.ts` past its facade ceiling of 6854 (`check:facade-delegation-only`, ruling 6897467376; raising it is maintainer-only). So the flag is a runtime layer over the action api, on the same shared walk. `engine.ts`, `engine-scoped-context.ts` and `hook-run-as.test.ts` end byte-identical to main. - **What B is not**: not a security boundary. A host handler's package registered through the engine it holds, and that engine's `createContext({ isSystem: true })` builds an unmarked system api. B keeps the documented answer for the api a handler was handed, as the ruling says. - **Reach of the layer**: it covers every api the shared walk derives: `sudo()`, `withRunAs(...)`, a transaction's context, the `beginTransaction()` ctx, and the api any symbol-keyed member returns (awaited when it is a promise). That last group includes `platformSystemApi` and the published `seedHookUserGrants` / `seedHookUnreadMembership` (objectql entry), each pinned. - **Not covered**: the api `ObjectRepository.execute` hands the action it dispatches (`ctx.api.object(o).execute(name)`, `engine-object-repository.ts`). That api is a bare engine `ScopedContext` carrying the `'action'` mark and no action-door flag, so `platformSystemApi` still takes the mark off there. Row 1b and the objectql header say so. The engine side is follow-up #35434 (nested under #35174). - `Clause-②: no` on the final diff. No package-entry export is added, and `deriveThroughSeam`'s signature is unchanged in round 2. `platformSystemApi` is unreleased, so the narrowing reaches no published consumer. ### Verification <details><summary>Gates</summary> - Round 2, `node scripts/pm/dispatch-gates.mjs --commands` at `6bec461`: 101 derived, 99 run with exit 0, 2 NOT MEASURED. Those two (`check:skill-examples`, `check:dual-build-cjs-loads`) need a full build; both exited 0 at `2919fe6ac` and run in CI. `check:system-context-census` exit 0. - Round 1 at `2919fe6ac`: 101 of 101 exit 0. - `node scripts/pm/overlap-check.mjs --branch=HEAD`: clean (#34817 shares `system-context.mdx` and merges cleanly). - Full-repo scans (`pnpm lint`) and the `test:repo` project belong to CI. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01Utp6Ca1jJvsA1dHsPwMiQb)_ --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: os-bill <noreply@objectstack.ai>
1 parent 89364b4 commit 467b13b

6 files changed

Lines changed: 375 additions & 35 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'@objectstack/runtime': patch
3+
---
4+
5+
fix(runtime): `platformSystemApi` on an action handler's `ctx.api` keeps the `'action'` mark, and what it returns stays wrapped
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
`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.
11+
12+
- 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.
13+
- The flag is not the app-authored flag: a host handler's `delete(…, { permanent: true })` still runs.
14+
- The api the audit writer holds (a hook registered in code) is unchanged: `platformSystemApi` still takes the mark off there.
15+
- 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()`.
16+
- 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.
17+
- 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.

‎content/docs/permissions/system-context.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ bugs — it is the protection or the side effect that silently does not happen.
100100
| # | Behaviour when `isSystem` | Package | What you get / what you lose | Symbol |
101101
|:--|:---|:---|:---|:---|
102102
| 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` |
103-
| 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` |
103+
| 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` |
104104
| 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` |
105105
| 3 | Row-level read filter resolves to "no filter" | plugin-security | Get: unscoped reads. Lose: row-level scoping entirely | `getReadFilter` |
106106
| 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` |

‎packages/objectql/src/platform-system-api.ts‎

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,41 @@
2929
* The write's context, elevated as `sudo()` elevates it — every other key kept,
3030
* so the rows stay in the write's unit of work, its organization and its
3131
* attribution — without the app-automation mark and without `flowRunId`. The
32-
* api is derived as every api is (`derive`), and its own `sudo()` marks again.
32+
* api is derived as every api is (`derive`): not app-authored, so its own
33+
* `sudo()` adds no mark.
3334
*
3435
* ⛔ For the platform's own rows only, never for a write the automation itself
3536
* asked for: an action body's or a hook's insert into those objects, through
3637
* its `ctx.api` or its `ctx.api.sudo()`, keeps the provenance and is judged.
37-
* The engine enforces that line for the one api it knows is the automation's:
38-
* on an app-authored hook's api (its handler's `ctx.api`, and every api
39-
* derived from it) this answers exactly what that api's `sudo()` answers — the
40-
* `'hook'` mark when the context carries none, the mark it carries otherwise,
41-
* `flowRunId` kept — so a hook handing its own api here gains nothing.
38+
* That line holds on two of the apis the automation is handed, and on every
39+
* api derived from either:
4240
*
43-
* The line stops there: it guards app-authored hook apis and every api derived
44-
* from one, and nothing else. An app author's in-process action handler,
45-
* registered with `engine.registerAction` (as `examples/app-todo` does from
46-
* `onEnable(ctx.ql)`), is handed runtime's `buildActionApi` api — an engine
47-
* `createContext` carrying the `'action'` mark, and not app-authored. On that
48-
* api this sheds the mark and any `flowRunId`, and the engine cannot tell it
49-
* from the api the audit writer holds (`buildHookApi`, a fresh `ScopedContext`
50-
* over the write's context: not app-authored either, and carrying the same
51-
* mark when an action's write fired the hook). It adds no reach: the handler's
52-
* package registered through the engine it holds, whose
53-
* `createContext({ isSystem: true })` already builds a system api with
54-
* neither. And what it returns is outside runtime's `deriveThroughSeam`
55-
* wrappers on that `ctx.api` (the stored-metadata read seam and the
56-
* landed-write answer): the walk wraps `object`, `sudo`, `withRunAs`,
57-
* `transaction` and `beginTransaction`, and its default arm binds this symbol
58-
* member to the unwrapped api, so the derived api comes back unwrapped.
41+
* - an app-authored hook's api (its handler's `ctx.api`,
42+
* `APP_AUTHORED_HOOK_API`): the engine enforces it, and this answers
43+
* exactly what that api's `sudo()` answers — the `'hook'` mark when the
44+
* context carries none, the mark it carries otherwise, `flowRunId` kept;
45+
* - the api an action door hands its handler, a host code action handler's
46+
* included (runtime's `buildActionApi`): runtime's layer over it carries
47+
* the action door's own origin flag and answers this symbol member with
48+
* the api's `sudo()`, so the `'action'` mark and any `flowRunId` stay. Not
49+
* the engine's app-authored flag, which also refuses a permanent delete
50+
* (ADR-0148 D3) that a host handler keeps.
51+
*
52+
* So a handler handing its own api here gains nothing, and its writes to the
53+
* identity-assignment objects are judged as its own. ⚠️ Not covered: the api
54+
* `ObjectRepository.execute` hands the action it dispatches
55+
* (`ctx.api.object(o).execute(name)`, `engine-object-repository.ts`) is a bare
56+
* `ScopedContext` carrying the `'action'` mark and neither line's flag, so on
57+
* it this takes the mark off, as on the audit writer's hook api. The api the audit writer
58+
* holds is neither: `buildHookApi` builds a fresh `ScopedContext` over the
59+
* write's context, carrying the `'action'` mark when an action's write fired
60+
* the hook, and this takes it off there. Neither line is a security boundary:
61+
* a host handler's package registered through the engine it holds, whose
62+
* `createContext({ isSystem: true })` already builds a system api with neither
63+
* the mark nor the run. They keep the documented answer for the api a handler
64+
* was handed. And what this returns over runtime's wrapped `ctx.api` stays
65+
* wrapped: runtime's `deriveThroughSeam` (the stored-metadata read seam and
66+
* the landed-write answer) walks this symbol member as it walks `sudo()`.
5967
*
6068
* A symbol, and ⛔ not re-exported from either package entry, for the reason
6169
* `SEED_USER_GRANTS` gives (`hook-run-as.ts`): the method is no nameable member
@@ -64,7 +72,8 @@
6472
* `Symbol()`, because the reader and the engine may be different copies of
6573
* this package — its ESM and CJS builds, or its two entries (`.` and
6674
* `./core`) served from different builds — and the key must be the same in
67-
* each, or the reader would silently fall back to `sudo()`.
75+
* each, or the reader would silently fall back to `sudo()`; and runtime names
76+
* it too (`deriveThroughSeam`), which no export can serve.
6877
*/
6978

7079
import type { ScopedContext } from './engine.js';

0 commit comments

Comments
 (0)