Repository navigation
Commit 197e3ff
fix(cli, lint): os build and the body-capability rule count declared-once locals the same way (#38873)
<!-- claude-projects-attribution: {"github_login":"huangyiirene"} -->
_Requested by **Irene** · [project
thread](https://claude.ai/code/project/chan_01TJXtQ1tVD26ecLDZxFS67x?thread=cmsg_01TJXtQ1tVD26ecLDZxFS67xCc7t6qXBp9PVyzTRkHFg2s)_
Fixes #38801
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:开发者在钩子或动作里直接写函数时,`os build`
推断权限的规则和发布检查判断"只声明一次的本地变量"的方式以前不一致,于是会出现构建通过、发布检查却拒绝同一份产物的情况(例如参数叫
`e`、函数里又有 `catch (e)`)。现在两边按同一套规则数,卡上的三个例子构建后能拿到 `log` 权限并正常运行。
- 为什么做:评审在 #38799 上发现这类分歧,属于"两边读同样的写法"这一类缺陷。
-
出错会怎样:检查新增的报错只会落在沙箱本来就会在调用处拒绝(403)的函数体上;检查不再报的写法,构建也不授予权限,手写且未授权时仍由沙箱在调用处拒绝。
- 要你做什么:无。
## What changes
A local counts as a context root (`const c = ctx`) or a member alias
(`const l = c.log`, `const { log } = c`) only when it is declared once
in the handler and never assigned again. The build (`bodyMemberAliases`
in `packages/cli/src/utils/extract-hook-body.ts`) read that off its
parse; the publish rule (`boundOnce` in
`packages/lint/src/validate-action-body-capabilities.ts`) read it off
the text and counted differently. Both now count the same way, as ruled
with the seat on the card thread:
| | Build (before) | Rule (before) | Both (after) |
|:---|:---|:---|:---|
| `const` / `let` / `var` declarator, a plain name | counted | counted
when first in its list | counted, any position in the list |
| object pattern element | counted | counted (non-nested `{…}` only) |
counted, any depth |
| array pattern element | counted | not counted | counted, any depth |
| `catch (NAME)` binding | counted | not counted | **not counted** (a
parameter of its own block, like a nested function's parameter, which
neither side counts) |
| `[c] = …`, `({ c } = …)`, `for (c of/in …)` | not a reassignment | not
a reassignment | a reassignment, as `c = …` is |
Item by item:
- **Item 1 (S1).** The extractor no longer counts a `catch` binding; the
rule now counts array-pattern elements and every declarator of a list
(`scanDeclarations`). The card's three bodies and the `const log =
ctx.log; … catch (log) {}` class now infer `log` and the rule reads
them, so the lowered bodies run.
- **Item 2 (N4).** The rule reads a root, a member alias and a
destructure anywhere in a declarator list (`const c = ctx, l = c.log`,
`const a = 1, { log } = ctx`), keyed on the declarator offsets the same
scan records.
- **Item 3 (N3).** Fixed on both sides: the extractor adds
destructuring-assignment targets and declaration-free `for … of` / `for
… in` heads to `reassigned` (`assignmentTargetNames`); the rule's
`patternAssigned` reads the same shapes.
- **Item 4 (N5).** The root terminator follows JavaScript's semicolon
insertion: a line terminator is `\n`, `\r\n`, a lone `\r`, U+2028 or
U+2029, and a next line opening with `;`, `}`, `{`, a string or number
literal, `++`, `--`, `!` (not `!=`), `~` or an identifier (non-ASCII
included, not `in` / `instanceof`) ends the initializer. The docblock no
longer calls `++`, `--` and `!` continuing operators.
- **Item 5 (N6).** A step is read as JavaScript reads it: `i++` at a
line's end steps `i`, not the name opening the next line; a prefix step
after `return` / `typeof` / … still counts.
- **Same terminator for the receivers.** A member alias and a
destructure now end where a root ends, so `const l = ctx.log || null`
and `const { log } = ctx || {}` are no alias, as the build already read
them (its alias requires the member itself). A TypeScript `as` cast
after a member alias is still read (the existing pin `const api =
ctx.api as HookApi`); a lowered body carries no cast.
**Over-read, on record (seat ruling):** the count is blind to scope on
both sides, so in `const e = ctx; try {} catch (e) { e.log.info('x') }`
both sides read the inner `e` as the outer root: the build grants `log`
the call never uses, and the rule agrees. An over-grant never causes a
refusal. The parity test pins it as the `OVER-READ` row.
## Clause-② no, shown per rule-side change
Each rule-side change, and whether it can add a finding to a body that
runs today:
- **Declarator lists (item 2), root terminator (item 4), step reading
(item 5).** These read more. Each new read is a local that is `ctx` or
`ctx.MEMBER` at run time, so a new finding lands only on a call the
sandbox gates and the body does not grant. That body already fails with
403 at the call. One exception, and it already exists for a first
declarator on `main`: the count is blind to scope, so a nested function
whose parameter shadows a root (`const a = 1, c = ctx; function f(c) {
c.log.info() }`) is read through the root. A list position now reaches
that case too.
- **Array patterns and declarator lists counted, `patternAssigned`,
receiver terminator.** These read less and add no finding.
- **Catch bindings.** No change on the rule side. The rule never counted
them.
So no body that runs today is newly refused, except in the scope-blind
shadow case named above. That case already exists on `main`, and now
also reaches list positions. A freshly built body is granted the same
capability, so only a hand-written body with no grant can see that
finding.
## Tests
- `packages/cli/test/extract-hook-body-bound-once-38801.test.ts` (new,
38 rows): every body goes to both sides, on the hook spelling `async (c)
=> …` and the action spelling `async (input, c) => …`. Rows that infer
`log` must draw the rule's finding when the grant is dropped, and none
on the lowered body. Control rows must infer nothing and draw nothing.
It covers item 1 (the card's three bodies, the `main` class and the
over-read), item 2, items 4 and 5 (every new terminator and both step
shapes), and as controls item 3, array-pattern and list re-declarations,
and expression aliases.
-
`packages/lint/src/validate-action-body-capabilities.bound-once-38801.test.ts`
(new, 61 rows): the rule alone, on both doors. It covers the catch,
list, terminator and step rows, plus controls: re-declarations (nested
array patterns, an array inside an object pattern, a list with no
initializer, a for-of declaration), every reassignment shape (with `for
await`, a keyword before `[`, and an element assignment `o[c] = 1` that
is not a destructuring), and continuations (`!==`, a template, `in`, a
member step).
<details><summary>Ablations at head 2e04c47 (both new files plus the
existing extractor and rule tests, which are 195 lint + 698 cli
tests)</summary>
Each ablation edited the tree, rebuilt `@objectstack/lint`, ran, and was
restored with `git checkout HEAD --` (`git status --porcelain` empty
after each):
| Ablation | lint failed | cli failed |
|:---|:---|:---|
| A0: both source files at `main` 94d552c | 42 | 38 (every new parity
row) |
| A1: extractor counts catch bindings again | 0 | 5 (the item-1 rows) |
| A2: extractor ignores destructuring / for-head reassignment | 0 | 10
(the item-3 controls) |
| A3: rule counts declarations the old way | 12 | 8 |
| A4: rule's old root terminator | 13 | 11 (the item-4 rows) |
| A5: rule's old step reading | 4 | 3 (the item-5 rows) |
| A6: rule ignores pattern reassignment | 12 | 10 |
| A7: rule reads only the first declarator of a list | 7 | 4 (the item-2
rows) |
| A8: rule's old receiver end | 2 | 2 (the expression-alias controls) |
At A0, rows such as "a root declared again by an array pattern in a
nested block" fail on the rule side: on `main` the build infers nothing
and the rule refuses the lowered body. That is the card's class.
</details>
<details><summary>Local gates at head 2e04c47</summary>
- `pnpm --filter @objectstack/lint typecheck` and `pnpm --filter
@objectstack/cli typecheck`: exit 0, with deps built with declarations.
- `@objectstack/lint` full suite: 258 files, 10280 passed.
- `@objectstack/cli`: the 47 non-e2e test files that import the
extractor, `lower-callables`, `@objectstack/lint` or the hook-body
subpath: 1307 passed, 29 skipped. The two `published-subpath-*` pins
first needed a cli build and pass after it. The e2e layer was NOT
MEASURED; CI runs it.
- eslint on the four changed source and test files: clean.
- `check:doc-authoring`, `check:empty-changeset`,
`check:changeset-fixed`, `check:adr-0087-registration`,
`check-commit-card-trailers --range origin/main..HEAD`: pass.
</details>
## Acceptance notes
- The count stays blind to scope by design on both sides. A name
declared again in a nested block (`const c = ctx; { const [c] = x }
c.log.info()`) is no root on either side, so the build grants nothing
and the sandbox refuses the call. That matches how a second plain `const
c` in a nested block was already treated on both sides.
- Not changed: a handle bound to `.object(…)` in a declarator list
(`const a = 1, repo = ctx.api.object('x')`) is read by neither side, so
both agree. The extractor's `api` patterns are receiver-loose anyway.
- A regular-expression literal holding an unbalanced bracket inside an
initializer can hide later declarators of the same list from the rule's
scan. It is documented as the accepted remainder, as the rule already
does for regex literals.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01J8VVAjXyCrN7tYqPLmvyW4
<!-- signed: repo:objectstack@huangyiirene · huangyiirene ·
session_01J8VVAjXyCrN7tYqPLmvyW4 -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent b06efa9 commit 197e3ff
5 files changed
Lines changed: 599 additions & 22 deletions
File tree
- .changeset
- packages
- cli
- src/utils
- test
- lint/src
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1234 | 1234 | | |
1235 | 1235 | | |
1236 | 1236 | | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
1237 | 1258 | | |
1238 | 1259 | | |
1239 | 1260 | | |
| |||
1333 | 1354 | | |
1334 | 1355 | | |
1335 | 1356 | | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
1336 | 1368 | | |
1337 | 1369 | | |
1338 | 1370 | | |
| |||
1348 | 1380 | | |
1349 | 1381 | | |
1350 | 1382 | | |
1351 | | - | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
1352 | 1386 | | |
1353 | 1387 | | |
1354 | 1388 | | |
| |||
1369 | 1403 | | |
1370 | 1404 | | |
1371 | 1405 | | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
1372 | 1413 | | |
1373 | 1414 | | |
1374 | 1415 | | |
| |||
| 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 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
0 commit comments