Repository navigation
Commit 94b3463
feat(lint)!: judge a lowered hook or action body's capabilities like an authored one (#38755)
<!-- claude-projects-attribution: {"github_login":"os-steve"} -->
_Requested by **Steve** · [project
thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi92EfsyzCyjwc8X5TRpmNj6z)_
Fixes #38690
Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: none — no `skills/**` text taught the lowered exemption;
the one docs page that did
(`content/docs/deployment/validating-metadata.mdx`, the two capability
rows) is corrected here
## 维护者速读
- 用户能看到什么变化:挂在字符串 handler / target
旁的钩子和脚本动作函数体(构建产物,或手写的)现在也会被校验权限声明,缺权限时在发布和 os validate
时就报错,而不是上线后每次运行被沙箱拒掉;同时构建能认出 `ctx.log?.info(…)` 这类可选链、空白、注释写法,自动补上权限。
- 为什么做:这是「钩子与动作函数体权限校验」这张卡的第二步;取消当时为构建产物留的豁免,并把构建的推断补到与校验规则逐个写法一致。
- 出错会怎样:手写在字符串 handler / target
旁、漏声明权限的函数体,以及构建后丢了权限的产物,发布会被拒并点名缺的权限;首方示例与连接器应用实测零新增拒绝,可整 PR 撤回。
- 要你做什么:无。
## What changes
`validate-action-body-capabilities` (`@objectstack/lint`) skipped a body
it took for one the build lowered: a hook body `os build` / `os verify`
lowered from an inline `handler`, and at the runtime publish doors any
hook body beside a string `handler` (`isLoweredHandlerBody`); and any
action body beside a string `target`, judged for `api.write` alone
(`isLoweredTargetBody`). The skip covered a body written by hand beside
a string ref as well, since the rule cannot tell the two apart. The
reason was that the build's capability inference did not read every
gated call the rule reads. Step 1 of this card (PR #38731) and #38653
(PR #38686) closed that gate by gate; this PR closes it spelling by
spelling.
So this PR:
- drops both skips and the three helpers that only served them
(`isLoweredHandlerBody`, `isLoweredTargetBody`,
`loweredActionBodyPaths`). `validateActionBodyCapabilities` no longer
takes the `loweredHookRefs` context, and its registry entry passes the
stack alone. Neither function was on the package's barrel.
- makes the CLI extractor
(`packages/cli/src/utils/extract-hook-body.ts`) infer each gated call in
each spelling the rule reads. Its `ctx`-anchored member patterns, and
their rebound siblings for a renamed or destructured hook context, were
literal `ctx.log.info`: `ctx.log?.info(…)`, `ctx?.crypto.randomUUID()`,
`ctx.activity?.append(…)`, `ctx.email?.send(…)`, `ctx . log . info(…)`
and `ctx.title?.(field)` lowered without their token, and the rule read
each as the gated call. Each step now takes `?.` and whitespace, `title`
takes the optional call, and inference also reads a comment-blanked copy
of the source (the rule's own walk, strings kept), so a comment between
steps no longer hides a call. Every change widens: what inferred a token
before still does. A hook handler that calls `ctx.email.send` in any of
these spellings now lowers with the grant and is refused at build by the
hook parse, as `ctx.email.send(…)` already was.
- exports `SANDBOX_CAPABILITY_GATES` and `type SandboxCapabilityGate`
from the lint barrel, and moves the step-1 CLI enumeration pin off its
hand-kept mirror onto that table.
- adds a PARITY pin in the CLI. Every gate, leaf, receiver shape and
face, in each spelling the rule reads (plain, `?.` before the member,
before the leaf and before the call, whitespace, a block or line comment
between steps), is lowered through `extractLoweredBody` and judged by
`runAuthoringRules('build', …)` twice: ungranted it must draw the gate's
finding (so the rule reads that spelling and the pin is not vacuous),
lowered it must draw none. A REBOUND block does the same through a hook
handler's renamed and destructured context parameter. The one exclusion
is a hook body's `email.send`, pinned instead as a build refusal in
every spelling.
- corrects the text that described the exemption: the two `os explain`
entries, the docs rule table, the rule's docblock and the comment over
the `lowerCallables` target-shape pin.
- registers the narrowing as ADR-0087 semantic entry
`lowered-hook-and-action-bodies-capabilities-judged`, with a BREAKING
changeset carrying FROM → TO for both authors (a body lowered by the
build or written by hand beside a string ref) and the remedy for each:
rebuild, or declare the token.
- corrects the pending #37815 changeset and its semantic entry, whose
sentences this PR makes false (see Deliberate correction).
The limit, stated: the build's reading covers the rule's by the pin's
enumeration (census × receivers × spellings × faces), not by proof.
Where the two differ outside it, the build reads more (a renamed context
root, a spelling inside a string), and the over-inferred grant is one
the sandbox never checks, except `email.send` on a hook, which is
refused at build as before.
Measured cost before the change, on `origin/main` at `f782a12033`
(scratch script, not committed): every first-party stack
(`examples/app-crm`, `app-multi-package` with its two packages,
`app-showcase`, `app-starter`, `app-todo`,
`packages/apps/connector-docusign`, `connector-github`,
`connector-stripe`) loaded, normalized, lowered and parsed as
`judgeAuthorTimeRules` does, then judged by the shipped rule and by the
rule with both skips emulated away. 70 hook and action bodies, 1 of them
lowered (app-crm's opportunity hook): 0 findings shipped, 0 without the
skips, **0 newly refused**. The population holds one lowered body, so it
bounds authored bodies; the lowered side is bounded by the PARITY pin.
## Tests
- New
`packages/lint/src/validate-action-body-capabilities.lowered-38690.test.ts`
(26 tests). Per removed skip, a lowered body calling each gate
ungranted, spelled directly and through an alias: refused (hook: `os
validate` / `build` / `lint` and the hook save door; action: top level,
object-inline, `os validate` / `build` / `lint`, the action and object
doors). Controls: the same lowered body granted is not refused. A
lowered body's write keeps its own id beside the other tokens.
- `packages/cli/test/extract-hook-body-local-alias-38690.test.ts` (284
tests): the mirror table is deleted; ENUMERATION reads
`SANDBOX_CAPABILITY_GATES` and spells every leaf in every spelling
through seven receivers; PARITY and REBOUND as above; a hook handler
calling `ctx.email.send` in seven spellings plus a renamed context is
refused by `HookSchema` after `lowerCallables`.
Red first, each fix:
- PARITY harness: the earlier PARITY judged the extracted record without
`language: 'js'`, which the rule skips, so it could not go red. With the
body record the build writes and the widened spellings, against the old
extractor: 51 failed.
- Extractor `?.` / whitespace / optional call / rebound: the pins
against the extractor at `a68c0a41cc`: 76 failed of 247 (ENUMERATION 27,
PARITY 24, REBOUND 25). Hook mail in the new spellings: 4 failed of 8
(they built clean before).
- Comment between steps: against the extractor at `2aec12e986`: 210
failed of 284, every one a comment spelling.
Ablations at the head (`scripts/ablation-replace.mjs`, restore proven
blob == HEAD): `MEMBER_STEP` back to a plain `\.`: 84 failed
(ENUMERATION 32, PARITY 28, REBOUND 20, mail 4). `title` without the
optional call: 11 failed. Inference over the raw source only: 210
failed. The skips re-inserted in the rule, per removed skip: hook 14
failed / 12 passed, non-write action 12 failed / 14 passed of the lint
file.
Forced test edits, named:
-
`packages/lint/src/validate-action-body-capabilities.hook-and-tokens-37815.test.ts`:
two describe blocks deleted, `authored hook bodies only: a body lowered
from a handler is not judged (#37815 route C)` and `authored action
bodies only past the writes: a body lowered from a target is judged for
api.write alone (#37815 M1)`. They pinned the two skips this PR removes;
the new file pins the opposite. Their authored-body halves stay covered
by the same file's arm (a)/(b) cases. The two predicate imports go with
them.
- `packages/cli/test/lower-callables.test.ts`: comment only, over the
`[#38653]` target-shape pin, which named `isLoweredTargetBody`. The pin
itself is unchanged.
## Acceptance notes
- The lowered hook finding's path stays `hooks[N].body.capabilities` on
the seat's answer (claim amendment 5). Its premise, that a fresh build
never draws the finding, holds again at this head for every spelling the
PARITY pin enumerates; for an artifact that lost a grant, or a
hand-written body, that key is the right one.
## Deliberate correction
`Check Changeset` (`check-empty-changeset`) is red on purpose: this PR
edits a pending release note it did not add. DELIBERATE CORRECTION, not
a collision; ⛔ do not restore the base text. Both files below are
unreleased (merged after 19.0.0-next.2) and ship in the same release as
this PR, and this PR makes their sentences about lowered bodies false.
Text only: no code, no entry id, no level or disposition line changes.
Scope per claim amendment 5 (7276409399); rewrites 2 and 4 of the
changeset and 3 and 4 of the entry re-trued per L2 7278772654.
**`.changeset/37815-body-capabilities-every-gate.md`**
1. BREAKING paragraph. Before: "whose authored L2 body calls a member".
After: "whose L2 body calls a member".
2. `hook-body-capability-undeclared` bullet. Before: "an authored L2
hook body that calls any of those gates" … "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)." After: "an L2 hook body that calls any of those
gates" … "A hook body `os build` / `os verify` lowered from an inline
`handler` is judged the same way: its grant is the one the build
inferred from its calls, and the build reads each gated call in each
spelling the rule reads."
3. `action-body-capability-undeclared` bullet. Before: "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`." After: "an 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 judged the same way, against the grant the build inferred
from its calls."
4. Paragraph after FROM → TO. Before: "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`: … TO, it
still builds clean, and its writes are judged as before." After: "A hook
written as an inline `handler`, and an action written as an inline
`target` function, need no change: the build infers the grant from each
gated call in each spelling the rule reads (`?.`, whitespace or a
comment between steps, the optional call), so the body it lowers carries
the token. … TO, it still builds clean, its lowered body granted
`api.transaction`."
**`packages/spec/src/migrations/entries/semantic/19.hook-and-action-bodies-calling-an-ungranted-gate-refused.ts`**
(entry `hook-and-action-bodies-calling-an-ungranted-gate-refused`, id
unchanged)
1. Header comment. Before: "an authored L2 hook body that calls". After:
"an L2 hook body that calls".
2. `surface`. Before: "only a body the author wrote (a hook body lowered
from an inline handler is not judged, and an action body lowered from an
inline target function is judged for its writes alone, as before)".
After: "whether the author wrote the body or os build lowered it from an
inline handler or target function".
3. `replacement`, last sentence. Before: "needs no change for the other
tokens: its lowered body is not judged for them (a later change carries
the lowered reading)." After: "needs no change: os build infers the
grant from each gated call in each spelling the rule reads (?.,
whitespace or a comment between steps, the optional call), so the body
it lowers carries the token, and that lowered body is judged like an
authored one."
4. `reason`. Before: "A hook body os build lowered from an inline
handler is not judged, and an action body it lowered from an inline
target function is judged only for api.write, as before: such a grant is
the build's inference, not the author's, and the inference does not yet
read every gated shape, so a hook written as a handler and an action
written as a target function are unchanged." After: "A hook body os
build lowered from an inline handler, and an action body it lowered from
an inline target function, are judged the same way: their grant is the
build's inference, which reads each gated call in each spelling the rule
reads, pinned spelling by spelling against the rule, so a hook written
as a handler and an action written as a target function build clean."
## Verification
Two-minute check: `pnpm --filter @objectstack/cli exec vitest run
test/extract-hook-body-local-alias-38690.test.ts`. Expect 1 file, 284
tests passed. With `MEMBER_STEP` in `extract-hook-body.ts` set back to a
plain `\.`, 84 of them fail.
Read first: the `MEMBER_STEP` / `memberLeafRx` / `blankComments` block
and the inference loop in `packages/cli/src/utils/extract-hook-body.ts`,
then the PARITY block in the test above, then the two removed skips in
`packages/lint/src/validate-action-body-capabilities.ts`.
<details><summary>Local gates at 3ca3538 (head)</summary>
- Derived round: `node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack` listed 122 commands (the 119 of the last
round plus `check:i18n`, `check:i18n-coverage`,
`check:i18n-walk-parity`); each ran with stdin closed and its exit
recorded before any pipe: 121 :: exit 0, and `node
scripts/check-empty-changeset.mjs --base origin/main` :: exit 1, the
DELIBERATE CORRECTION refusal above, by design. `dispatch-gates --ran`:
122 derived, 122 run, 0 NOT-MEASURED, 0 UNRUN.
- Packages: `pnpm --filter @objectstack/lint test` 252 files / 10106
tests passed; cli `vitest run --project unit` 451 files / 6737 tests
passed; `pnpm --filter @objectstack/lint typecheck` and `pnpm --filter
@objectstack/cli typecheck` exit 0; spec rebuilt, `src/migrations` 5
files / 209 tests passed.
- `node scripts/check-changeset-no-major.mjs --base origin/main --event`
over this body: exit 0 (level axis: `@objectstack/lint` minor carries
`Metadata-protocol: narrowed`; ADR-0087 `registered
lowered-hook-and-action-bodies-capabilities-judged`).
- `node scripts/pm/overlap-check.mjs` at this head: clean against
origin/main (f576dee), #38497 and #38192.
- Not run locally: the cli `integration` layer and `test:repo` (CI's
Test Core); the dogfood battery is CI's on this head (`!` title,
narrowing arm).
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_01ADHkmSa9QNE6qXdrFAvnUr)_
<!-- signed: repo:objectstack#3 · os-steve ·
session_01ADHkmSa9QNE6qXdrFAvnUr -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 16aa8b9 commit 94b3463
14 files changed
Lines changed: 609 additions & 246 deletions
File tree
- .changeset
- content/docs/deployment
- packages
- cli
- src/utils
- test
- lint/src
- spec/src/migrations/entries/semantic
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
| 18 | + | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
524 | 524 | | |
525 | 525 | | |
526 | 526 | | |
527 | | - | |
| 527 | + | |
528 | 528 | | |
529 | | - | |
| 529 | + | |
530 | 530 | | |
531 | 531 | | |
532 | 532 | | |
| |||
0 commit comments