Repository navigation
Commit 7870900
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
File tree
- .changeset
- content/docs/deployment
- packages
- lint/src
- spec/src/migrations/entries/semantic
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
524 | 524 | | |
525 | 525 | | |
526 | 526 | | |
| 527 | + | |
527 | 528 | | |
| 529 | + | |
528 | 530 | | |
529 | 531 | | |
530 | 532 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3400 | 3400 | | |
3401 | 3401 | | |
3402 | 3402 | | |
3403 | | - | |
3404 | | - | |
| 3403 | + | |
| 3404 | + | |
| 3405 | + | |
| 3406 | + | |
| 3407 | + | |
3405 | 3408 | | |
3406 | 3409 | | |
3407 | 3410 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1167 | 1167 | | |
1168 | 1168 | | |
1169 | 1169 | | |
1170 | | - | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
1171 | 1175 | | |
1172 | 1176 | | |
1173 | 1177 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1423 | 1423 | | |
1424 | 1424 | | |
1425 | 1425 | | |
1426 | | - | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
1427 | 1429 | | |
1428 | 1430 | | |
1429 | 1431 | | |
| |||
1522 | 1524 | | |
1523 | 1525 | | |
1524 | 1526 | | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
| 1536 | + | |
| 1537 | + | |
| 1538 | + | |
| 1539 | + | |
| 1540 | + | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
| 1555 | + | |
| 1556 | + | |
| 1557 | + | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
| 1562 | + | |
| 1563 | + | |
| 1564 | + | |
| 1565 | + | |
| 1566 | + | |
| 1567 | + | |
| 1568 | + | |
| 1569 | + | |
| 1570 | + | |
| 1571 | + | |
| 1572 | + | |
| 1573 | + | |
| 1574 | + | |
| 1575 | + | |
| 1576 | + | |
1525 | 1577 | | |
1526 | 1578 | | |
1527 | 1579 | | |
| |||
7357 | 7409 | | |
7358 | 7410 | | |
7359 | 7411 | | |
| 7412 | + | |
| 7413 | + | |
7360 | 7414 | | |
7361 | 7415 | | |
7362 | 7416 | | |
| |||
0 commit comments