Skip to content

Commit 695b9db

Browse files
fix(plugin-security)!: an app-authored elevation's reads and writes are judged by its caller's organization wall (#36386)
Clause-②: no (narrowing) Metadata-protocol: none ## 维护者速读 - **改了什么:** 动作代码、以系统身份运行的钩子和以系统身份运行的流程,在替某个登录用户读写数据时,按该用户自己的组织边界判定。 - 用户的组织边界不放行时,读不到任何租户数据;写入一律拒绝,删除按「找不到」处理。所谓不放行:isolated 下是没有选当前组织,group 下是没有任何成员关系,single 下是不属于任何组织。group 下的成员可以读写自己所有组织的并集。 - 已选组织的用户照常使用。 - 写入数据里点名别的组织,会被拒绝。 - **为什么:** 这是安全席位的高危派单,维护者裁定 ⑤ A。 - **若出错:** 只会多拒,不会多放。平台自身的系统任务、定时任务不受影响。可以撤回。 - **要你做什么:** 本 PR 申报为收窄,契约审核 PASS 在案后,需要授权账号批准当前 head。 ## What changes Rule: **elevation lifts a caller's business rules, never the organization wall.** This applies to an action body, a hook that runs as system, and a flow that runs as system. Their reads and writes are judged by the organization wall of the caller they run for (ADR-0095 D1, ADR-0123 D1/D2). The full FROM/TO is in `.changeset/sec2-wf9p.md`. - **A caller the wall denies:** - reads resolve to nothing; - delete and purge follow the read rule (a predicate delete matches nothing; a by-id delete answers `404`); - other writes are refused `403 PERMISSION_DENIED`, naming the missing organization or membership. - **A write whose own data names an organization outside the caller's wall, or empties a row's organization:** refused `403`. - **On a federated object:** reads are those of the caller's organization scope, and a by-id write of a row outside it answers `404`. The check is read inside the write's own transaction. - **A run that began for no caller:** unchanged, including after an approval decision. The decider is its attribution user, not its caller. Its trigger user stays the writer, so approval submitter exclusion and run history answer as before. Packages: - `@objectstack/plugin-security`: the wall. - `@objectstack/core`: exports `RUN_CALLER`, `markRunCaller` and `runCallerOf`, a server-constructed mark. - `@objectstack/service-automation`: a system run's data context carries its user's membership set and posture rung, seeded at run setup. A run whose own user is not its caller carries neither. - `@objectstack/objectql` and `@objectstack/trigger-record-change`: record that a run began for no caller. - `@objectstack/spec`: the `runAs` descriptions of flows and hooks and the `body` description of actions state the limit. The change is description-only, and the reference pages are regenerated. ## Why A no-card hardening order from the security seat. The details stay with the seat. ## Tests - **Real stack** (`packages/plugins/organizations/src/elevated-organization-wall*.wall.test.ts`): - each elevation kind under each tenancy posture, with controls for a caller with an active organization; - the write rules, and the by-id check inside a body's own transaction; - the run-that-began-for-no-caller cases, including a child run. - **Module pins** in plugin-security, service-automation, objectql and trigger-record-change. - **plugin-approvals:** the decider stays excluded as submitter on a downstream approval. Every pin is red on its base and green on the head, and each guard was ablated and restored. ## Verification - CI on the head: every required check is green. - The dogfood battery runs on the final head. - Contract review: by the security seat, recorded on this PR. ## Human Minimum - [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic. - [x] 2. Every name I added uses the casing for its kind. - [x] 3. No formatter ran; `pnpm lint` passes. - [x] 4. One issue, one branch off an up-to-date `main`, one pull request. (A no-card order.) - [x] 5. A change that publishes carries a changeset, and both declaration lines are filled in here and in the changeset. - [x] 6. `packages/spec` touched: its generated artifacts are regenerated and committed. - [x] 7. No hand edits to `content/docs/references/`, `packages/*/CHANGELOG.md` or `content/docs/releases/`. (The reference pages changed are regenerated.) - [x] 8. Title, body and comments are in English. - [x] The *Before Submitting* list in CONTRIBUTING.md is done. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_017xRBcW9L8Noz9BxEwjwiov <!-- signed: security#2 · sunhaolin · session_017xRBcW9L8Noz9BxEwjwiov --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6e643cc commit 695b9db

45 files changed

Lines changed: 3261 additions & 68 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.changeset/sec2-wf9p.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
'@objectstack/plugin-security': patch
3+
'@objectstack/core': patch
4+
'@objectstack/service-automation': patch
5+
'@objectstack/spec': patch
6+
'@objectstack/objectql': patch
7+
'@objectstack/trigger-record-change': patch
8+
---
9+
10+
fix(plugin-security)!: an app-authored elevation's reads and writes are judged by its caller's organization wall
11+
12+
Clause-②: no (narrowing)
13+
Metadata-protocol: none
14+
15+
<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export changes shape. What moves is the answer a data operation gets when it runs under an action body's, a system hook's or a system flow's elevation, and three schema descriptions that state it: no stored row, registry key or authored value changes, so there is nothing for a migration to rewrite and no tombstone. -->
16+
17+
The rule: elevation lifts a caller's business rules, never the organization wall. An action body, a hook that runs as system (`runAs: 'system'`, a system context it inherits, its `ctx.api.sudo()`) and a flow that runs as system have their reads and writes judged by the organization wall of the caller they run for (ADR-0095 D1, ADR-0123 D1/D2), under every tenancy posture and on every object, a federated one included.
18+
19+
**BREAKING** for an action body, a system hook or a system flow run for a signed-in caller.
20+
21+
- FROM: the elevation, not the caller's organization wall, decided what such a context reached of a tenant-scoped object.
22+
- TO: the caller's own organization wall decides it:
23+
- with no active organization under `isolated`, an empty membership set under `group`, or no membership at all under `single`: its reads of a tenant-scoped object resolve to nothing (zero rows, no error); its delete and purge are answered by that read rule (a predicate delete matches nothing, a by-id delete answers `404 RECORD_NOT_FOUND`); and its other writes (insert, update, transfer, and the recycle bin's restore) are refused with `403 PERMISSION_DENIED`, naming the missing active organization or membership;
24+
- under `group`, a member reads and writes the union of the organizations they hold a membership in, with or without an active organization;
25+
- on a federated object it reads only rows of the caller's organization scope, and a by-id write of a row outside it answers `404 RECORD_NOT_FOUND`, as a row that is not there;
26+
- an insert with no active organization on a walled tenant object is refused with `403 PERMISSION_DENIED`;
27+
- for every caller, an active organization included: a write whose own data names an organization outside the caller's wall, and an update whose own data empties a row's organization, are refused with `403 PERMISSION_DENIED`;
28+
- the check of the row a by-id write names is read inside the transaction the write carries, so a row the body's own transaction wrote is the caller's to write again in it;
29+
- a system flow started by a record change, and every system run whose own user is its caller, carries that user's membership set and posture rung from run setup: under `group` it reads the union of the user's memberships, with or without an active organization. The set and rung are those of run setup: a membership the user gains or loses while the run waits is read by the next run. A decided segment, a run that began for no caller and a child run of either carry neither: a decided segment's caller's facts are resolved for it again.
30+
31+
**Not moved.** A caller with an active organization keeps exactly that organization's reach. Objects outside tenancy, objects with no organization column, and a platform operator crossing the wall on an object whose posture permits it are answered as the caller's own requests answer them. System work that runs for no caller (a scheduled run, a platform plugin's own system context, the seeders) is unchanged, and so is a system flow that began for no caller after an approval decision: the decider is its attribution user, not its caller. A run that a write of such a segment starts begins for no caller too, and carries neither the writer's membership set nor its posture rung; its trigger user stays the writer, so an approval's submitter exclusion, who may read the run's state, and the run's history answer as before. A run that a write of a decided segment of a run that began for a caller starts begins for the decider, as before. A system flow that began for a caller is judged by that caller after a decision, not by the decider.
32+
33+
The fix for a caller: select an active organization before running the action, or start the run from a session that has one. A body that must place a row in another organization does so as a member of that organization, through its own request. The `runAs` descriptions of flows and hooks and the `body` description of actions now state this limit beside the other two.
34+
35+
Also in this change:
36+
- `@objectstack/core` exports `RUN_CALLER`, `markRunCaller` and `runCallerOf`: the server-constructed mark a flow run's data context carries wherever the run's own user is not its caller (a decided segment, a run recorded as begun for no caller in every segment, and a child run of either), naming the user its run began for, or that it began for none.
37+
- `@objectstack/service-automation`: a `runAs: 'system'` run's data context carries the run user's membership set (`accessible_org_ids`) and posture rung, seeded once at run setup; it carries no grants. A run context may record `beganForNoCaller: true`. A run whose own user is not its caller is seeded with neither, and its data context carries neither.
38+
- `@objectstack/objectql`: the hook session of a write made under that mark, for a run that began for no caller, carries it; `@objectstack/trigger-record-change` records that the run such a write fires began for no caller, and keeps the writer as its trigger user.

‎content/docs/automation/flows.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const approvalFlow = {
7373
| `version` | `number` | optional | Version number (defaults to `1`) |
7474
| `status` | `enum` | optional | `'draft'`, `'active'`, `'obsolete'`, `'invalid'` (defaults to `'draft'`) |
7575
| `type` | `FlowType` | ✅ | Flow trigger type (see below) |
76-
| `runAs` | `enum` | optional | `'system'` (elevated, bypasses RLS; after an approval decision its attribution user is the decider) or `'user'` (the default, RLS-respecting: the trigger user until a human decision, the decider after it, with the decider's permissions). A `'user'` run that resolved **no** trigger user has nothing to scope to, so its data operations are refused — declare `'system'` for schedule / time-relative / API triggers and for record-change flows fired by a write that carried no user |
76+
| `runAs` | `enum` | optional | `'system'` (elevated, bypasses RLS but not the organization wall: a run for a user has its reads and writes judged by that user's organization wall, and a run that began for no caller is not judged by it; after an approval decision its attribution user is the decider) or `'user'` (the default, RLS-respecting: the trigger user until a human decision, the decider after it, with the decider's permissions). A `'user'` run that resolved **no** trigger user has nothing to scope to, so its data operations are refused — declare `'system'` for schedule / time-relative / API triggers and for record-change flows fired by a write that carried no user |
7777
| `requiredPermissions` | `string[]` | optional | Capabilities a caller must hold, every one listed, to START the flow. Checked at every door that starts a flow by name (the trigger route, a `type: 'flow'` action over REST and MCP `run_action`, a declared `type: 'flow'` endpoint): anyone else but the system principal is refused `403` `PERMISSION_DENIED` and nothing runs. An action that starts the flow inherits the list. A parent flow's `subflow` node is not a door and is not checked again. This is how to say who may start an elevated `screen` or `api` flow; `current_user.positions` is not an authorization input |
7878
| `variables` | `FlowVariable[]` | optional | Input/output variables |
7979
| `nodes` | `FlowNode[]` | ✅ | Flow nodes |

‎content/docs/permissions/access-recipes.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Two separate questions:
9898
1. **Who can configure it?** Editing flows/automations needs `manage_metadata` (typically Studio users). Don't expose automation config to end users — see [audience-based interfaces](/docs/ui/audience-based-interfaces).
9999
2. **As whom does it run?** A flow's `runAs` (ADR-0049):
100100
- `runAs: 'user'` (default) — runs as the triggering user; CRUD nodes respect that user's RLS. **Safer default.**
101-
- `runAs: 'system'` — elevated, bypasses RLS. Make elevation *explicit*, and surface it in the UI as "runs as system".
101+
- `runAs: 'system'` — elevated, bypasses RLS but not the organization wall: a run for a user has its reads and writes judged by that user's organization wall ([system context](/docs/permissions/system-context)). Make elevation *explicit*, and surface it in the UI as "runs as system".
102102

103103
A schedule-triggered run has no user, so under `user` its data operations are refused — declare `system` to make the elevation explicit and intended.
104104

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ bugs — it is the protection or the side effect that silently does not happen.
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` |
103103
| 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` |
104+
| 1c | **App-authored automation's system context meets its caller's organization wall** — an action body's, an app-authored hook's (`runAs: 'system'`, a system context it inherits, its `sudo()`) and a flow run's, read by the same predicate as the identity refusal above (`applyAppAutomationOrganizationWall`, plugin-security); checked ahead of the middleware's short-circuit | plugin-security | Get: its reads and writes are judged by the organization wall of the caller it runs for, under every posture and on every object. Such a context that carries a caller (a `userId`, or the guest entry's envelope) is judged by the caller's own Layer 0 (ADR-0095 D1), computed over the caller's own context and permission sets (resolved for the caller where the context does not carry them, as a flow run's does not). An insert with no active organization on a walled tenant object is refused (`403 PERMISSION_DENIED`); for every such caller, one with an active organization included, a write whose own data names an organization outside its scope or empties a row's organization is refused (`403 PERMISSION_DENIED`); on a federated object its reads are those of the caller's organization scope and a by-id write of a row outside it answers `404 RECORD_NOT_FOUND`, the check read inside the transaction the write carries. A caller the wall denies — no active organization under `isolated`, an empty membership set under `group`, no membership under `single` — reads nothing of a tenant-scoped object, and its delete and purge are answered by that read rule as its plain door answers them (ADR-0123 D2): a predicate delete matches nothing and a by-id delete answers `404 RECORD_NOT_FOUND`. Every other write is refused `PERMISSION_DENIED` (403), naming what is missing (ADR-0123 D4). A system flow run whose own user is its caller carries that user's membership set and posture rung from run setup (a decided segment, a run that began for no caller and a child run of either carry neither); the wall asks Layer 0 alone and resolves what a context lacks once per run. Lose: nothing the caller held — a caller with an active organization keeps its organization's reach (the union of its memberships under `group`); objects outside tenancy, objects with no organization column and a platform operator crossing the wall on a posture-permitting object answer as the caller's plain door answers them. An elevation that carries no caller (a scheduled run, a record-change run fired by a write that carried no user), a system flow that began for no caller in every segment (after an approval decision too, `runCallerOf`, core) and a run a write of such a segment starts, and every platform system context are unchanged | `elevationSource` |
104105
| 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` |
105106
| 3 | Row-level read filter resolves to "no filter" | plugin-security | Get: unscoped reads. Lose: row-level scoping entirely | `getReadFilter` |
106107
| 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` |
@@ -604,7 +605,7 @@ and CI refuses one that differs from what the tree produces.
604605
| `packages/plugins/plugin-security/src/agent-account-grant-refusal.ts#onInsert` | 1 | 23g |
605606
| `packages/plugins/plugin-security/src/agent-account-grant-refusal.ts#onUpdate` | 1 | 23g |
606607
| `packages/plugins/plugin-security/src/agent-ceiling-grant-refusal.ts#judges` | 1 | 23f |
607-
| `packages/plugins/plugin-security/src/flow-system-identity-write-refusal.ts#elevationSource` | 1 | 1b, 25 |
608+
| `packages/plugins/plugin-security/src/flow-system-identity-write-refusal.ts#elevationSource` | 1 | 1b, 1c, 25 |
608609
| `packages/plugins/plugin-security/src/grant-holder-membership-refusal.ts#onInsert` | 1 | 23d |
609610
| `packages/plugins/plugin-security/src/grant-holder-membership-refusal.ts#onUpdate` | 1 | 23d |
610611
| `packages/plugins/plugin-security/src/grant-permission-set-name.ts#onInsert` | 1 | 23c |

0 commit comments

Comments
 (0)