Skip to content

Commit 7870900

Browse files
feat(lint)!: refuse an action or hook body that calls a sandbox gate it was not granted (#38650)
<!-- claude-projects-attribution: {"github_login":"os-steve"} --> _Requested by **Steve** · [project thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9JeHmqv6CKCkW8ewrPBLekk)_ Refs #37815 (items a, b) Clause-②: no (narrowing) Metadata-protocol: narrowed Skills-docs: follow-up #38655 ## 维护者速读 - 用户能看到什么变化:应用作者保存或校验一个脚本动作或自己写的钩子脚本时,如果代码调用了沙箱需要授权的功能(写数据、事务、日志、发邮件、写活动记录等)却没有声明对应权限,现在会在保存和本地校验时直接被拒并说明要加哪个权限;以前能保存,但每次运行都报错。 - 为什么做:属于脚本动作与钩子的权限检查跟进工作,把只覆盖动作写数据的检查扩展到钩子和沙箱的全部授权项。 - 出错会怎样:误判会让一个本来能运行的动作或钩子保存不了;用函数写、由构建转换出来的钩子和动作这次不做新增检查;已存的元数据不受影响,可以直接撤回;现有示例应用实测无一被拒。 - 要你做什么:无。 ## What changes The QuickJS sandbox gates seven tokens, and before this PR lint judged one of them on one kind of body (`ctx.api` writes on action bodies, `action-body-write-capability-undeclared`). A hook body, and an action body calling any other gate, saved and published clean and then threw `403 INSUFFICIENT_PRIVILEGES` at every call. - **Runner census (measured first, read-only).** Every capability gate in `packages/runtime/src/sandbox/quickjs-runner.ts` at `0ba5e06ae7`: `api.read` on the `READ` leaves `find` / `findOne` / `count` / `aggregate` (`installApiMethod`, line 755) and on `ctx.title(FIELD)` with an argument (line 1035, hook bodies only: the action face builds no `ctx.title`); `api.write` on the `WRITE` leaves (line 756); `api.transaction` on `ctx.api.transaction` (`installTxLeaf`, line 783); `log` on `ctx.log.debug|info|warn|error` (line 881); `crypto.uuid` on `ctx.crypto.randomUUID` (line 907); `email.send` on `ctx.email.send` and `activity.append` on `ctx.activity.append` (`installDoor`, lines 941 to 994). That set is exactly `HookBodyCapability` in the spec. The rule judges exactly that set, as `SANDBOX_CAPABILITY_GATES`. - **Arm (a), authored hook bodies.** `validateActionBodyCapabilities` now walks `stack.hooks` too, with the same reading (blanked source; the member itself, a local bound once to it, or a handle bound once to `.object(…)`). New id `hook-body-capability-undeclared`, one finding per body and token, at `hooks[i].body.capabilities`. A hook body is never granted `email.send` (the hook's parse refuses the grant), so its `ctx.email.send` call is refused with the remedy that works: remove the call and send the mail from an action, a job or a flow. **Scope (route C): authored hook bodies only.** A hook body that `os build` / `os verify` lowered from an inline `handler` is not judged (`isLoweredHandlerBody`): under the CLI exactly when `ctx.loweredHookRefs` holds the hook's string `handler`, and at a door that hands no lowered refs (the runtime publish doors) whenever a string `handler` sits beside the body, the shape a build artifact carries. Its grant is the build's inference, and the extractor does not yet infer `api.transaction` or a read through a bound handle, so a finding there would name a grant the handler author cannot write. #38653 carries the lowered-handler reading and both extractor gaps. The registry entry keeps its name and gains `hook` in `runtimeTypes` (the `validateHookAndJobBodyCompiles` precedent: one entry, ids per kind), and now receives `ctx`. - **Arm (b), the other tokens, authored action bodies.** On action bodies, every gate other than the writes is judged under the new id `action-body-capability-undeclared`. A missing `api.write` keeps its own id and its exact words, so nothing that matches on it today changes. **Scope (contract review M1, claim amendment 3): authored action bodies only, past the writes.** An action body that `os build` / `os verify` lowered from an inline `target` function is judged for `api.write` only, as before (`isLoweredTargetBody`). Measured before the edit: the CLI hands no lowered-action ref set (`LoweringResult` carries `loweredHookRefs` only), and `lowerActionCallable` leaves a string `target` (the registered ref) beside the minted body. So on every door, the CLI included, a body is read as lowered when a string `target` sits beside it. No `packages/cli` edit was needed. A JS body runs only on a `script` action, where `target` is kept for legacy `functions` references alone; an authored body beside a string `target` is therefore not judged for the other tokens either, a missed finding, never a false one. In `examples/app-showcase`, none of the 296 JS-body actions (318 actions in all) carries a string `target`. #38653 carries the lowered reading. - **Pin.** The new test file reads the runner's source: the token set from `caps.has('…')`, the `installApiMethod` tokens and the `installDoor` calls must equal the census; the `caps.has(NAME)` calls with a variable must be exactly the two known ones, so a third parametrised gate turns it red; the `READ` list, the log levels and the door triples are pinned; and the census token set must equal `HookBodyCapability.options`. - **L1-a's nit from #38628.** The `action-body-write-unknown-field` explanation now lists the installed field-carrying leaves: `insert`, `update({ id, … })`, `updateById(id, { … })`, `upsert`, `updateMany`. `updateById` landed with #38643, which this branch merged from `main`; the census and the gate table read the runner's `WRITE` list and needed no edit. - `os explain` entries for both new ids, and two rows in `content/docs/deployment/validating-metadata.mdx`. - **Upgrade guide and barrel.** `packages/lint/src/index.ts` exports both new id constants. A new upgrade-guide entry, `packages/spec/src/migrations/entries/semantic/19.hook-and-action-bodies-calling-an-ungranted-gate-refused.ts`, follows the `hook-job-body-does-not-compile-refused` precedent and says the hook arm covers authored bodies only; the changeset marker is `registered` with that id, and the changeset carries an `@objectstack/spec` patch line. The `os build` extractor (`packages/cli`) is unchanged: byte-identical to `main`. The rule file's header names `hook` as the third runtime door (review N2), and the upgrade-guide entry's `acceptanceCriteria` names the inline path `objects[N].actions[M].body.capabilities` (N2). S1 (`updateById` in the `action-body-write-unknown-field` leaf list) was met at `a60a6de915`. This touches a contract face: the lint rule family's accept set at the save door and in `os validate` (a narrowing). ## Tests - New file `packages/lint/src/validate-action-body-capabilities.hook-and-tokens-37815.test.ts`: per arm a refused case, a granted control that passes, and an ungated call that stays quiet; the hook case through the save door (`runRuntimeAuthoringRules` on `type: 'hook'`) and through `runAuthoringRules` for `validate` / `build` / `lint`; the hook `email.send` remedy; `ctx.title(FIELD)` against `ctx.title()`; a shape table for `bodyGatedCalls`; the runner pin. - M1 pins, both sides, on one source (a write inside `ctx.api.transaction`, granted `api.write` only, as the extractor infers): the authored action body is refused; the same body beside a string `target` is not judged, through `validateActionBodyCapabilities`, through `runAuthoringRules` for `validate` / `build` / `lint` at the top level and inline on an object, and at the `action` and `object` doors (each with an authored positive control); a lowered body without `api.write` is still refused under the write id. - Route C pins, both sides, on one source (a read through a bound handle): the authored body is refused; the same body lowered from a handler is not judged, through `validateActionBodyCapabilities`, through `runAuthoringRules` for `validate` / `build` / `lint` with `loweredHookRefs`, and at the save door with a string `handler` beside the body; a string `handler` the build did not lower leaves the authored body judged. - One ablation per behaviour with `scripts/ablation-replace.mjs`, each restored to a clean tree. <details><summary>Readings at 4c07e0d, 29d3b0f, a60a6de and c3319db</summary> - At 29d3b0f: `pnpm --filter @objectstack/lint typecheck && pnpm --filter @objectstack/lint test`: typecheck clean, `check:test-typecheck` OK; Test Files 248 passed, Tests 10035 passed. The new test file: 46 passed. Ablation of the M1 exemption (`if (lowered) continue;` deleted): Tests 3 failed / 43 passed, exactly the three lowered-target pins; restored, blob equal to HEAD. `check-adr-0087-registration --base origin/main`, `check-changeset-no-major`, `check:spec-changes`, `check:upgrade-guide` and `check-nul-bytes` exit 0; overlap-check clean. - Full dogfood battery on 29d3b0f (`pnpm --filter @objectstack/dogfood test` under the lock, after building the dogfood closure): Test Files 495 passed / 3 skipped (498), Tests 4187 passed / 68 skipped (4255), VERDICT command-exit 0. The head after it, 4c07e0d, changes only one sentence of the upgrade-guide entry's guidance text. - At 4c07e0d, after a cli build: `pnpm --filter @objectstack/cli exec vitest run --maxWorkers=2 test/migrate-meta-engine-guidance.test.ts test/explain-rule-id.test.ts test/verify-delete-behaviors.test.ts`: Test Files 3 passed, Tests 20 passed. The entry's printed guidance names no tracker, so the Test Core (2/12) red at 29d3b0f is fixed. `check-adr-0087-registration`, `check-changeset-no-major`, `check:spec-changes`, `check:upgrade-guide`, `check:generated` and `check-nul-bytes` exit 0; overlap-check clean. - At a60a6de (after merging `main` with #38643): `pnpm --filter @objectstack/lint typecheck && pnpm --filter @objectstack/lint test`: typecheck clean, `check:test-typecheck` OK; Test Files 248 passed, Tests 10031 passed. The new test file, census pin included: 42 passed. `check-adr-0087-registration --base origin/main` exit 0; `check-changeset-no-major` exit 0. - At c3319db: lint suite Test Files 248 passed, Tests 10019 passed. - `pnpm --filter @objectstack/cli exec vitest run --maxWorkers=2 test/verify-delete-behaviors.test.ts test/published-subpath-hook-body.pin.test.ts test/explain-rule-id.test.ts test/extract-hook-body.test.ts test/lint-hook-rules-reach-handler-hooks.test.ts` (after a cli build): Test Files 5 passed, Tests 109 passed. - Ablation of the narrowing (the `isLoweredHandlerBody` early return deleted): Tests 2 failed / 40 passed, exactly the two not-judged pins; restored, blob equal to HEAD. - Round 1 ablations: the hook walk removed, 8 failed / 31 passed; the `api.transaction` gate entry removed, 8 failed / 31 passed; both restored. - After the build: `check-adr-0087-registration` exit 0 (1 migration entry added); `check:spec-changes` exit 0; `check:upgrade-guide` exit 0; `check:generated` all 14 artifacts current; `check-nul-bytes` OK. The upgrade guide was regenerated with `gen:upgrade-guide` only. - `node scripts/pm/dispatch-gates.mjs --commands`: 118 families derived; 116 exit 0, 2 NOT MEASURED (exit 3, PREREQUISITE NOT MET: `check:skill-examples`, `check:dual-build-cjs-loads`); `--ran` reconciliation: 118 accounted for. CI on c3319db: 72 check-runs, 62 success and 10 skipped, among them `Lint & Repo Gates`, `Test Core`, `Check Changeset` and `Dogfood Regression Gate` green. - Full dogfood battery on c3319db (`pnpm --filter @objectstack/dogfood test` under the lock, after building the dogfood closure): Test Files 495 passed / 3 skipped (498), Tests 4187 passed / 68 skipped (4255), VERDICT command-exit 0. - First-party measurement of example apps before any edit: 0 findings. A sweep of 50 handlers under `packages/*/test/**` and `examples/**` found one body refused under the lowered-handler reading (`packages/cli/test/verify-delete-behaviors.test.ts`); under route C it is not judged, and that test is green. </details> ## Acceptance notes - Scope, route C (claim amendment 2): the hook arm judges authored hook bodies only. Lowered handlers, and the two extractor gaps under them (no `api.transaction` inference; no `api.read` inference through a bound handle, `?.` before the leaf or an argument holding parentheses), are carried by #38653, a widening that follows this PR. The published `@objectstack/cli/hook-body` type and its pin are untouched, so this PR keeps its one Clause-② arm. - At the runtime publish doors a hook carrying both a string `handler` and a body is read as a build artifact and not judged. A hand-written hook with both keys is therefore not judged either: a missed finding, never a false one. - An action carrying both a string `target` and a JS body is read as lowered on every door, the CLI included, and judged for `api.write` only. That covers an author who wrote a body beside a `target` function, too: `lowerActionCallable` keeps that authored body and still turns the `target` into a string ref. A missed finding, never a false one. #38653 carries the lowered reading for both arms. - Not changed, per the review: N1 (`ctx.title(undefined)` / `ctx.title(null)` read as the related-record form) and N3 (job bodies are not judged). ## Verification Read first: `packages/lint/src/validate-action-body-capabilities.ts` (`SANDBOX_CAPABILITY_GATES`, `bodyGatedCalls`, the hook walk), then `isLoweredTargetBody` and the action loop's `lowered` skip, then the new test file's runner-pin block, its M1 pins and its route C pins, then the `authoring-rules.ts` entry (`runtimeTypes` gains `hook`, `run` receives `ctx`). Two-minute check: `pnpm --filter @objectstack/lint exec vitest run src/validate-action-body-capabilities.hook-and-tokens-37815.test.ts` shows 46 passed. --- _Generated by [Claude Code](https://claude.ai/code/session_01ADHkmSwS4mQyXWrddL4r6B)_ <!-- signed: repo:objectstack#3 · os-steve · session_01ADHkmSwS4mQyXWrddL4r6B --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 302a4c3 commit 7870900

8 files changed

Lines changed: 790 additions & 46 deletions
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
'@objectstack/lint': minor
3+
'@objectstack/spec': patch
4+
---
5+
6+
feat(lint)!: a hook body's gated calls, and an action body's calls to every gate besides `api.write`, are refused at publish when the body is not granted the token
7+
8+
Clause-②: no (narrowing)
9+
Metadata-protocol: narrowed
10+
Skills-docs: follow-up #38655
11+
12+
<!-- adr-0087: registered hook-and-action-bodies-calling-an-ungranted-gate-refused -->
13+
14+
**BREAKING** for the publish of an action, an object or a hook whose authored L2 body calls a member the QuickJS sandbox gates without that member's token in `body.capabilities`. Such a body saved and published before; the sandbox refused the call with `403 INSUFFICIENT_PRIVILEGES` every time the body reached it, so it never worked. It is refused now, at the runtime publish door and in `os validate` / `lint` / `compile` / `build`, with the token named. The schema is unchanged and stored metadata is not rewritten or refused at boot; the refusal comes at the next publish.
15+
16+
The gates are the sandbox runner's own, token by token, and the rule's list is pinned to the runner's source: the `ctx.api.object(…)` read leaves (`find`, `findOne`, `count`, `aggregate`) behind `api.read`, the write leaves behind `api.write`, `ctx.api.transaction(…)` behind `api.transaction`, `ctx.log.debug|info|warn|error(…)` behind `log`, `ctx.crypto.randomUUID()` behind `crypto.uuid`, `ctx.email.send(…)` behind `email.send`, `ctx.activity.append(…)` behind `activity.append`, and on a hook body `ctx.title(FIELD)` with an argument behind `api.read` (`ctx.title()` needs no token).
17+
18+
- `hook-body-capability-undeclared` (new): an authored L2 hook body that calls any of those gates without its token. A hook body is never granted `email.send`, so its `ctx.email.send(…)` call is refused with the remedy that works: remove the call and send the mail from an action, a job or a flow. Only a body the author wrote is judged: a hook body `os build` / `os verify` lowered from an inline `handler` is not, since its grant is the build's inference (the lowered reading is #38653). Runs on the runtime `hook` door.
19+
- `action-body-capability-undeclared` (new): an authored L2 action body that calls a gate other than the writes without its token. An action body `os build` / `os verify` lowered from an inline `target` function is not judged for these tokens, since its grant is the build's inference (the lowered reading is #38653); its writes are still judged under `action-body-write-capability-undeclared`. Runs on the runtime `action` and `object` doors.
20+
- `action-body-write-capability-undeclared` is unchanged: an action body's write without `api.write` keeps its id and its words.
21+
22+
## FROM → TO
23+
24+
- Authored hook body (arm a). FROM: `hooks: [{ …, body: { language: 'js', source: "await ctx.api.object('task').update({ id, done: true })", capabilities: [] } }]` saved and threw at every run. TO: declare the token, `capabilities: ['api.write']`.
25+
- Authored action body, other tokens (arm b). FROM: `body: { language: 'js', source: 'await ctx.api.transaction(async () => { … })', capabilities: ['api.write'] }` saved and threw at every run. TO: declare the token, `capabilities: ['api.write', 'api.transaction']` (likewise `log`, `crypto.uuid`, `email.send`, `activity.append`, `api.read` for the call the finding names).
26+
- Authored hook body calling `ctx.email.send(…)`. FROM: saved and threw at every run. TO: remove the call and send the mail from an action, a job or a flow.
27+
28+
A hook whose body was lowered from an inline `handler` is unchanged: FROM and TO alike, it is not judged by this rule. So is an action whose body was lowered from an inline `target` function, for every token but `api.write`: FROM, `actions: [{ …, target: async (ctx) => { await ctx.api.transaction(…) } }]` built clean; TO, it still builds clean, and its writes are judged as before.
29+
30+
`os explain hook-body-capability-undeclared` and `os explain action-body-capability-undeclared` carry an entry for each. `@objectstack/lint` exports the two rule id constants, `ACTION_BODY_CAPABILITY_UNDECLARED` and `HOOK_BODY_CAPABILITY_UNDECLARED`. `@objectstack/spec` adds only the upgrade-guide entry `hook-and-action-bodies-calling-an-ungranted-gate-refused`.

‎content/docs/deployment/validating-metadata.mdx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,9 @@ orthogonal to both, and no cell here can carry it; it is written out in
524524
| Script action bodies compile in the runtime's wrapper — an L2 action's `body.source` that does not compile as the body of `(async (input, ctx) => { … })`, most often `const input = …` redeclaring a parameter; an object write runs it over its inline `actions[]`, so an object write cannot republish a refused body (`action-body-does-not-compile`, #34884) | ✓ | ✓ | ✓ | ✓ᵃᶜᵒ |
525525
| Script action bodies hold the grant they call — an L2 action body that calls a `ctx.api` write (`insert`, `update`, `updateById`, `delete`, `updateMany`, `deleteMany`, `upsert`) without `api.write` in its `body.capabilities`, which the sandbox refuses at every call (`action-body-write-capability-undeclared`, #35143) | ✓ | ✓ | ✓ | ✓ᵃᶜᵒ |
526526
| Seed record keys — a seed record key, or a seed `columns` key, that names no field of the dataset's object, no system column injected on it and none of `id` / `created_at` / `updated_at`; the engine's insert door refuses such a record and the seed load loads the rest (`seed-record-unknown-field`, #35143) | ✓ | ✓ | ✓ | ✓ˢ |
527+
| Script action bodies hold every other grant they call — an authored L2 action body (one `os build` lowered from a `target` function is judged for its writes only) that calls a member the sandbox gates behind a token other than `api.write` without that token in its `body.capabilities`: a `ctx.api.object(…)` read leaf (`api.read`), `ctx.api.transaction(…)` (`api.transaction`), `ctx.log.*` (`log`), `ctx.crypto.randomUUID()` (`crypto.uuid`), `ctx.email.send(…)` (`email.send`) or `ctx.activity.append(…)` (`activity.append`), each refused by the sandbox at every call (`action-body-capability-undeclared`, #37815) | ✓ | ✓ | ✓ | ✓ᵃᶜᵒ |
527528
| Hook and job bodies compile in the runtime's wrapper — an L2 hook or job `body.source` that does not compile as the body of `(async (ctx) => { … })`, most often `const ctx = …` redeclaring the parameter or one name declared twice; no door publishes a job, so the publish column is the `hook` write alone (`hook-body-does-not-compile`, `job-body-does-not-compile`, #34987) | ✓ | ✓ | ✓ | ✓ʰ |
529+
| Hook bodies hold the grant they call — an authored L2 hook body (one `os build` lowered from a `handler` is not judged) that calls a member the sandbox gates without its token in `body.capabilities`: a `ctx.api.object(…)` read or write leaf, `ctx.api.transaction(…)`, `ctx.log.*`, `ctx.crypto.randomUUID()`, `ctx.activity.append(…)` or the related-record form `ctx.title(FIELD)`, each refused by the sandbox at every call; and any `ctx.email.send(…)`, which a hook body is never granted (`hook-body-capability-undeclared`, #37815) | ✓ | ✓ | ✓ | ✓ʰ |
528530
| Approval-node approvers (ADR-0090 D3) | ✓ | ✓ | ✓ | ✓ᶠ |
529531
| Security posture (ADR-0090 — e.g. every custom object declares `sharingModel`) | ✓ | ✓ | ✓ | ✓ˢᵖᵉᵇᵒ |
530532
| Security vocabulary freeze (ADR-0090 D3 — the reserved word, replaced by `permission_set` / `position` / `business_unit`) | ✓ | ✓ | ✓ | ✓ᵒᵖᵉᵇᵖᵒᵃᵒˣ |

‎packages/lint/src/authoring-rules.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3400,8 +3400,11 @@ export const AUTHORING_RULES: readonly AuthoringRule[] = [
34003400
commands: ALL,
34013401
source: 'packages/lint/src/validate-action-body-capabilities.ts',
34023402
surfaces: CLI_AND_RUNTIME,
3403-
runtimeTypes: ['action', 'object'],
3404-
run: (stack) => validateActionBodyCapabilities(stack),
3403+
// [#37815] `hook`: the same judgement over an L2 hook body, every token
3404+
// the sandbox gates (`hook-body-capability-undeclared`); no object write
3405+
// carries a hook.
3406+
runtimeTypes: ['action', 'object', 'hook'],
3407+
run: (stack, ctx) => validateActionBodyCapabilities(stack, ctx),
34053408
},
34063409
// The same compile check over an L2 hook body and an L2 job body, in the
34073410
// wrapper the runtime runs both in (`(async (ctx) => { SOURCE })`, built from

‎packages/lint/src/index.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,11 @@ export {
11671167
// `os explain` can carry an entry for each (the explanation table holds an
11681168
// entry only for an id the barrel exports). The rules themselves run through
11691169
// the registry and are not exported.
1170-
export { ACTION_BODY_WRITE_CAPABILITY_UNDECLARED } from './validate-action-body-capabilities.js';
1170+
export {
1171+
ACTION_BODY_WRITE_CAPABILITY_UNDECLARED,
1172+
ACTION_BODY_CAPABILITY_UNDECLARED,
1173+
HOOK_BODY_CAPABILITY_UNDECLARED,
1174+
} from './validate-action-body-capabilities.js';
11711175
export { SEED_RECORD_UNKNOWN_FIELD } from './validate-seed-record-keys.js';
11721176
// [#35143] The Views, dashboards and app shell family's app-shell ids, all
11731177
// advisory, exported for `os explain` as the two above; the rules run through

‎packages/lint/src/rule-explanations.ts‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,9 @@ const ACTION_BODY_WRITE_UNKNOWN_FIELD_EXPLANATION: RuleExplanation = {
14231423
covers: 'why an undeclared field write is refused',
14241424
paragraphs: [
14251425
'An action body persists records only through `ctx.api`: ' +
1426-
'`ctx.api.object(NAME).insert({ … })`, or `.update({ id, … })` for one record. `ctx.api` is a scoped handle on ' +
1426+
'`ctx.api.object(NAME).insert({ … })`, `.update({ id, … })` or `.updateById(id, { … })` for one record, ' +
1427+
'`.upsert(…)` or `.updateMany(…)`, the leaves the sandbox installs that carry fields. `ctx.api` is a ' +
1428+
'scoped handle on ' +
14271429
'the running engine, so the payload arrives as an ordinary CALLER write. An action\'s `ctx.input` ' +
14281430
'is its params bag, validated against the action\'s own `params`, so it is never resolved against ' +
14291431
'fields.',
@@ -1522,6 +1524,56 @@ const ACTION_BODY_WRITE_CAPABILITY_UNDECLARED_EXPLANATION: RuleExplanation = {
15221524
],
15231525
};
15241526

1527+
/**
1528+
* [#37815] The two ids beside `action-body-write-capability-undeclared`: every
1529+
* other sandbox gate on an action body, and every gate on a hook body. One
1530+
* paragraph set, worded per kind.
1531+
*/
1532+
const BODY_GATES_PARAGRAPH =
1533+
'The sandbox gates these calls, each behind its own token: the `ctx.api.object(…)` read leaves (`find`, ' +
1534+
'`findOne`, `count`, `aggregate`) behind `api.read`, the write leaves behind `api.write`, ' +
1535+
'`ctx.api.transaction(…)` behind `api.transaction`, `ctx.log.debug|info|warn|error(…)` behind `log`, ' +
1536+
'`ctx.crypto.randomUUID()` behind `crypto.uuid`, `ctx.email.send(…)` behind `email.send` and ' +
1537+
'`ctx.activity.append(…)` behind `activity.append`. A call the body was not granted throws at the call site ' +
1538+
'with `403 INSUFFICIENT_PRIVILEGES`.';
1539+
const BODY_GATES_READING =
1540+
'The rule reads the body\'s source with comments, strings and template literals blanked, and finds a gated ' +
1541+
'call on the `ctx` member itself (with `?.` allowed), on a local bound once to it (`const log = ctx.log`, or ' +
1542+
'`log` destructured from `ctx`), or, for the `ctx.api.object(…)` leaves, on a handle bound once to ' +
1543+
'`.object(…)`. It does not parse or run the body: the runtime save door may load no parser. A call spelled ' +
1544+
'another way is not found, which leaves the author where the sandbox would.';
1545+
1546+
const ACTION_BODY_CAPABILITY_UNDECLARED_EXPLANATION: RuleExplanation = {
1547+
rule: 'action-body-capability-undeclared',
1548+
covers: 'the grant an action body\'s other gated calls need',
1549+
paragraphs: [
1550+
'A script action body runs in the QuickJS sandbox with only the capabilities its `body.capabilities` ' +
1551+
'lists (none by default). ' + BODY_GATES_PARAGRAPH + ' The action answers that refusal every time it ' +
1552+
'reaches the call. A write without `api.write` has its own id, `action-body-write-capability-undeclared`.',
1553+
BODY_GATES_READING,
1554+
'To fix it, add the token the message names to the action\'s `body.capabilities` (Studio\'s action ' +
1555+
'inspector draws the list), or remove the call. Only a body the author wrote is judged: an action body ' +
1556+
'the build lowered from an inline `target` function is not, since its grant is the build\'s inference ' +
1557+
'(its writes are still judged under `action-body-write-capability-undeclared`).',
1558+
],
1559+
};
1560+
1561+
const HOOK_BODY_CAPABILITY_UNDECLARED_EXPLANATION: RuleExplanation = {
1562+
rule: 'hook-body-capability-undeclared',
1563+
covers: 'the grant a hook body\'s gated calls need',
1564+
paragraphs: [
1565+
'A hook body runs in the same QuickJS sandbox as an action body, with only the capabilities its ' +
1566+
'`body.capabilities` lists (none by default). ' + BODY_GATES_PARAGRAPH + ' A hook body also has ' +
1567+
'`ctx.title(FIELD)`, which reads the related record and needs `api.read`; `ctx.title()` with no argument ' +
1568+
'needs no token. Under the hook\'s default `onError: \'abort\'` the refusal aborts the write that ran it.',
1569+
BODY_GATES_READING,
1570+
'To fix it, add the token the message names to the hook\'s `body.capabilities`, or remove the call. A hook ' +
1571+
'body is never granted `email.send`, because a message cannot be taken back if the write that runs the ' +
1572+
'hook rolls back: remove the `ctx.email.send` call and send the mail from an action, a job or a flow. ' +
1573+
'Only a body the author wrote is judged: a hook body the build lowered from an inline `handler` is not.',
1574+
],
1575+
};
1576+
15251577
const SEED_RECORD_UNKNOWN_FIELD_EXPLANATION: RuleExplanation = {
15261578
rule: 'seed-record-unknown-field',
15271579
covers: 'which keys a seed record may carry',
@@ -7357,6 +7409,8 @@ export const RULE_EXPLANATIONS: Readonly<Record<string, RuleExplanation>> = Obje
73577409
[ACTION_BODY_SOURCE_UNPARSEABLE_EXPLANATION.rule]: ACTION_BODY_SOURCE_UNPARSEABLE_EXPLANATION,
73587410
[ACTION_BODY_DOES_NOT_COMPILE_EXPLANATION.rule]: ACTION_BODY_DOES_NOT_COMPILE_EXPLANATION,
73597411
[ACTION_BODY_WRITE_CAPABILITY_UNDECLARED_EXPLANATION.rule]: ACTION_BODY_WRITE_CAPABILITY_UNDECLARED_EXPLANATION,
7412+
[ACTION_BODY_CAPABILITY_UNDECLARED_EXPLANATION.rule]: ACTION_BODY_CAPABILITY_UNDECLARED_EXPLANATION,
7413+
[HOOK_BODY_CAPABILITY_UNDECLARED_EXPLANATION.rule]: HOOK_BODY_CAPABILITY_UNDECLARED_EXPLANATION,
73607414
[SEED_RECORD_UNKNOWN_FIELD_EXPLANATION.rule]: SEED_RECORD_UNKNOWN_FIELD_EXPLANATION,
73617415
[LIST_VIEW_KANBAN_GROUP_FIELD_UNCATEGORICAL_EXPLANATION.rule]: LIST_VIEW_KANBAN_GROUP_FIELD_UNCATEGORICAL_EXPLANATION,
73627416
[LIST_VIEW_FILTER_FIELD_VIRTUAL_EXPLANATION.rule]: LIST_VIEW_FILTER_FIELD_VIRTUAL_EXPLANATION,

0 commit comments

Comments
 (0)