Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0fe3c2a
test(organizations): pin an elevation's stored-image and option-bag o…
claude Nov 28, 2026
a4c2358
fix(plugin-security): an elevation's write is judged as stored, and i…
claude Nov 28, 2026
5b82319
docs(permissions): the register row states the stored-image and optio…
claude Nov 28, 2026
8a33f78
test(plugin-security): the option-bag write pin's fixture carries onl…
claude Nov 28, 2026
dbfa1ed
Merge remote-tracking branch 'origin/main' into claude/sec2-wf9p-f12
claude Nov 28, 2026
e479fb5
Merge remote-tracking branch 'origin/main' into claude/sec2-wf9p-f12
claude Nov 28, 2026
32a4592
test(organizations): pin a hook-returned, replaced or redefined optio…
claude Nov 28, 2026
e59ce7e
test(organizations): the hook-bag read pin drives the in-process repl…
claude Nov 28, 2026
99c2476
fix(objectql,plugin-security): an elevation's driver tenant scope is …
claude Nov 28, 2026
34dacef
docs(permissions): the register row and changeset state the insert an…
claude Nov 28, 2026
f149f25
test(organizations): pin a sandboxed hook's handed-back option bag on…
claude Nov 28, 2026
b0cf561
test(organizations): a sandboxed hook's handed-back bag on an elevate…
claude Nov 28, 2026
054893b
test(organizations): pin a redefined option-bag tenant key on an elev…
claude Nov 28, 2026
e21b1d1
fix(objectql): an operation's driver tenant-scope check judges only w…
claude Nov 28, 2026
7ee8603
docs(changeset): the option-bag rule states its group reach and drops…
claude Nov 28, 2026
b87b3cd
test(objectql): the beyond-projection control names its off-type opti…
claude Nov 28, 2026
122619b
test(objectql): every verb under group with no tenant key leaves the …
claude Nov 28, 2026
dab31e1
docs(plugin-security): the option-bag rule states that a bag inside t…
claude Nov 29, 2026
3c7d45f
fix(objectql): the driver tenant-scope check judges a plain copy of t…
claude Nov 29, 2026
a1e5b48
test(organizations): pin a hook-left option bag inside a group member…
claude Nov 29, 2026
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
28 changes: 28 additions & 0 deletions .changeset/sec2-wf9p-f12.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
'@objectstack/plugin-security': patch
'@objectstack/objectql': patch
---

fix(plugin-security)!: an elevation's write is judged as it will be stored, and its option bag never widens it past its caller's organization wall

Clause-②: no (narrowing)
Metadata-protocol: none

<!-- 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 under an action body's, a system hook's or a system flow's elevation: no stored row, registry key or authored value changes, so there is nothing for a migration to rewrite and no tombstone. -->

The rule: an app-authored elevation (an action body, a hook that runs as system, a flow that runs as system) is held to its caller's organization wall on the row as it will be stored and on the tenant scope its option bag asks for (ADR-0095 D1, ADR-0123 D2).

**BREAKING** for an action body, a system hook or a system flow run for a signed-in caller.

- FROM: the organization a write's before-hooks left on the row, and the `tenantId` / `tenantIds` an operation's option bag named, were not judged against the caller's organization wall.
- TO:
- a write is judged again on the rows its before-hooks leave: a row stored in an organization outside the caller's wall, or an update that leaves a row in no organization, is refused with `403 PERMISSION_DENIED`;
- a write (`insert`, `update`, `delete`, `purge`) whose option bag names an organization outside the caller's wall, or clears `tenantId`, is refused with `403 PERMISSION_DENIED`;
- a read (`find`, `findOne`) whose option bag names or clears an organization scope is answered by the read rule: the caller's wall is composed onto its predicate, so it reads only rows the wall admits;
- the option bag a before-hook leaves (a key it sets, a bag it replaces or hands back) is judged by the same rule where the engine builds the driver's options: a write's is refused with `403 PERMISSION_DENIED`, a read's is narrowed to the organizations the wall admits. A bag naming only organizations inside the scope the engine itself projects for the caller (its active organization, or under `group` its memberships) is not judged further: under `group` a member's hook-left bag naming another of its organizations is admitted, as at the plain door. A bag beyond that scope, or one that clears a key, is judged against the caller's wall where a verdict is worked out for the operation, and against the caller's active organization alone where none is.

**Not moved.** A write whose stored rows stay in organizations the caller's wall admits, and an option bag the operation is handed that names only such organizations, behave as before; an operation that names no tenant key keeps its reach, under `group` the union of the caller's memberships included. A bag a before-hook leaves that names only organizations inside that projected scope behaves as before too, under `group` any of the caller's memberships included. 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, its option bag included.

Also in this change: `@objectstack/objectql`'s `OperationContext` gains `driverTenantScopeCheck` (type `DriverTenantScopeCheck`), an enforcement layer's bound on the explicit `tenantId` / `tenantIds` of an operation's option bag. The engine asks it, at each driver bag it builds for that operation after every before-hook, about the explicit keys that ask beyond the scope it projects for the call or clear it, and hands it a plain copy of `tenantIds`; keys inside that scope reach the driver unjudged; absent, nothing changes.

The fix for a body or hook: write only into organizations the caller may write in, and leave `tenantId` / `tenantIds` off the option bag, or name an organization the caller's wall admits. A row that must land in another organization is written by a member of that organization, through its own request.
2 changes: 1 addition & 1 deletion content/docs/permissions/system-context.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ bugs — it is the protection or the side effect that silently does not happen.
|:--|:---|:---|:---|:---|
| 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, 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` |
| 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` |
| 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`); a write is judged again on the rows its before-hooks leave, by the same rule, as the plain door judges it; the option bag's `tenantId` / `tenantIds` never widen it past the caller's wall — a write (an insert included) whose bag names an organization outside it, or clears the key, is refused (`403 PERMISSION_DENIED`), and a read whose bag does is answered by the read rule, the caller's wall composed onto its predicate; the bag its before-hooks leave, set, replaced or handed back, is judged by the same rule where the engine builds the driver's options (`OperationContext.driverTenantScopeCheck`): a bag naming only organizations inside the scope the engine itself projects for the caller (its active organization, or under `group` its memberships) is not judged further, and a bag beyond that scope, or one that clears a key, is judged against the caller's wall where a verdict was worked out for the operation and against its active organization alone where none was; 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` |
| 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
10 changes: 5 additions & 5 deletions packages/objectql/src/engine-delete-and-aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ export class DeleteAndAggregateFacet {
const declaredDeleteColumns = declaredColumnSet(deleteSchema);
const readPreImage = async (targetId: unknown): Promise<Record<string, unknown> | null> => {
const preAst: QueryAST = { object, where: { id: targetId }, limit: 1 };
const preOpts = this.buildDriverOptions(object, opCtx.context, hookContext.input.options as any, { recordedVerdict: opCtx.tenantLayer0Verdict });
const preOpts = this.buildDriverOptions(object, opCtx.context, hookContext.input.options as any, { recordedVerdict: opCtx.tenantLayer0Verdict, tenantScopeCheck: opCtx.driverTenantScopeCheck });
return withDeclaredColumnsOnly(
(await driver.findOne(object, preAst, preOpts)) as Record<string, unknown> | null,
declaredDeleteColumns,
Expand Down Expand Up @@ -580,7 +580,7 @@ export class DeleteAndAggregateFacet {
// rows: they are its scope check and its per-row realtime events.
const cascadeBatch = referentialCascadeDelete === true;
if (perRowBeforeHooks || perRowAfterHooks || cascadeBatch) {
const preOpts = this.buildDriverOptions(object, opCtx.context, hookContext.input.options as any, { recordedVerdict: opCtx.tenantLayer0Verdict });
const preOpts = this.buildDriverOptions(object, opCtx.context, hookContext.input.options as any, { recordedVerdict: opCtx.tenantLayer0Verdict, tenantScopeCheck: opCtx.driverTenantScopeCheck });
// [#21613] Each doomed row is its per-row `previous`: declared set.
const doomed = rowsWithDeclaredColumnsOnly(
(await driver.find(object, ast, preOpts) as Record<string, unknown>[]) ?? [],
Expand Down Expand Up @@ -620,7 +620,7 @@ export class DeleteAndAggregateFacet {

// Every driver call below is handed `driverOptions` — the declared driver
// options and nothing else; `input.options` is the hooks' additive view.
const driverOptions = this.buildDriverOptions(object, opCtx.context, hookContext.input.options as any, { recordedVerdict: opCtx.tenantLayer0Verdict });
const driverOptions = this.buildDriverOptions(object, opCtx.context, hookContext.input.options as any, { recordedVerdict: opCtx.tenantLayer0Verdict, tenantScopeCheck: opCtx.driverTenantScopeCheck });
hookContext.input.options = mergeDriverOptionsIntoHookView(hookContext.input.options, driverOptions);

try {
Expand Down Expand Up @@ -700,7 +700,7 @@ export class DeleteAndAggregateFacet {
removed = await driver.delete(
object,
hookContext.input.id as string,
this.buildDriverOptions(object, writeContext, hookContext.input.options as any),
this.buildDriverOptions(object, writeContext, hookContext.input.options as any, { tenantScopeCheck: opCtx.driverTenantScopeCheck }),
);
// Told only for a row the driver says it removed: `false`
// (not found) and an off-contract count of 0 (kept) name no
Expand Down Expand Up @@ -839,7 +839,7 @@ export class DeleteAndAggregateFacet {
// (the `'split'` plan, a driver with no transactions), reads
// unlocked and keeps that window (`DriverCapabilities.lockRows`).
const runPredicateDelete = async (writeContext?: ExecutionContext): Promise<unknown> => {
const writeOptions = this.buildDriverOptions(object, writeContext, hookContext.input.options as any);
const writeOptions = this.buildDriverOptions(object, writeContext, hookContext.input.options as any, { tenantScopeCheck: opCtx.driverTenantScopeCheck });
const readOptions = writeOptions?.transaction != null && driver.supports?.lockRows === true
? { ...writeOptions, lockRows: true }
: writeOptions;
Expand Down
Loading
Loading