Repository navigation
Commit 4b57756
fix(runtime,cli,lint,spec)!: a sandboxed body's ctx.api.object(X).updateById(id, patch) writes the row (#38643)
<!-- claude-projects-attribution: {"github_login":"os-steve"} -->
_Requested by **Steve** · [project
thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9JeHmqv6CKCkW8ewrPBLekk)_
Fixes #38630
Clause-②: no (narrowing)
Metadata-protocol: narrowed
Skills-docs: follow-up #38655
## 维护者速读
-
用户能看到什么变化:应用作者在脚本动作和触发器里按文档写的「按编号更新一条记录」现在真的能保存改动,之前这样写第一次运行就报错、数据不变;传错编号(比如传了一个查询条件)会得到一句清楚的拒绝,什么也不写。
- 为什么做:文档和对外类型说明都承诺有这个写法,只有脚本运行的沙箱缺了它;属于脚本动作写入能力补齐的工作。
-
出错会怎样:只影响调用这个写法的脚本,原来的另一种更新写法不变;保存检查同时变严:没有声明写权限就用这个写法的脚本动作,以及以系统身份用它改人员授权记录的脚本,保存时会被拒绝(这两种脚本以前也从来运行不成功);有问题可直接撤回。
- 要你做什么:这是收紧保存检查的改动,需要一次契约级审核后才能落地。
## What changes
- **Runtime (the fix).** The QuickJS sandbox's `ctx.api.object(X)` now
installs `updateById` among its `WRITE` leaves (`installCtx`), behind
the same `api.write` capability gate. It forwards by name to the host's
scoped repository, as every leaf does; no per-method argument mapping
was needed.
- **Runtime (the id guard).** `updateById`'s id argument is judged at
the crossing, before the host is called (`ID_ARGUMENT_INDEX`,
`refuseNonScalarIdArgument`). Anything but a non-empty string or a
non-zero number is refused as `VALIDATION_ERROR` / 400 with a sentence
naming the call, the shape it was given, and the bulk form
(`update(data, { where, multi: true })`). `0` and `''` are refused
because `HookObjectApi.updateById` already declares them a refusal (they
identify no row).
- **CLI.** `objectstack build`'s `api.write` inference names
`updateById` (the `api.write` regex and `OBJECT_WRITE_LEAVES`), so a
lowered handler whose only write is `updateById` is granted the call.
The word boundary matters: `update\b` does not match inside
`updateById`.
- **Lint.** `SANDBOX_API_WRITE_METHODS` gains `updateById`; its existing
pin to the runner's `WRITE` list holds with both sides moved.
`action-body-write-capability-undeclared` therefore finds an ungranted
`updateById` call.
- **Spec (save-time identity refusal).** `BODY_WRITE_METHODS` in
`packages/spec/src/identity/identity-assignment-body-writes.ts`, the
list that names itself the runner's `api.write` set, gains `updateById`.
So `HookSchema` and `ActionSchema` refuse an elevated body (a `runAs:
'system'` hook, a `script` action) that writes `sys_user_position`,
`sys_user_permission_set`, `sys_invitation` or `sys_member` through
`updateById`, as they refuse the other write calls. Before this change
the call threw `TypeError: not a function` in the sandbox; now that it
is installed, the run-time refusal (plugin-security's data middleware)
refuses the write as well.
- **BREAKING, at two authoring doors** (`!`, `Clause-②: no (narrowing)`,
`Metadata-protocol: narrowed`). At save and in `os validate`: an action
body calling `updateById` without `api.write` is refused as
`action-body-write-capability-undeclared` (FROM saved and threw at every
run → TO declare `api.write`), and the elevated identity-assignment
write above is refused at `body.source`. Neither body ever worked.
ADR-0087: `registered` — the new v19 entry
`hook-action-body-system-identity-assignment-update-by-id-refused` for
the identity-assignment door, beside the existing
`publish-refuses-seeds-and-actions-that-save-and-do-not-work` that
covers the ungranted `api.write` write.
- **Text that this change made false, fixed in the same files.** Two
author-facing sentences listed the sandbox's write leaves and would have
gone on saying `updateById` is not one: the `os explain
action-body-write-capability-undeclared` paragraph
(`rule-explanations.ts`, it said "the six `ctx.api` write leaves") and
the build's `.create()` refusal (`extract-hook-body.ts`). Both now list
`updateById`; "six" is dropped.
- **Write-leaf lists brought to the installed set:** the comments in
`validate-hook-body-writes.ts` (`API_WRITE_METHODS` note),
`validate-action-body-writes.ts` (two comments that said a body has no
`updateById`; the hint itself is unchanged) and `authoring-rules.ts`,
and the two docs lists (`content/docs/automation/hook-bodies.mdx`, the
`.create()` paragraph;
`content/docs/deployment/validating-metadata.mdx`, the rule's row).
- **The build's capability-inference table names every write leaf it
infers from.** In `content/docs/automation/hook-bodies.mdx`, the
`api.write` row of the Capability inference table now reads `insert /
update / updateById / updateMany / upsert / delete / deleteMany / patch
/ remove / create`, the alternation `extract-hook-body.ts` matches.
`updateById` is new here. `updateMany` and `deleteMany` were already
inferred, and the row had omitted them before this PR; they are fixed in
the same row.
- **A v19 semantic entry records the identity-assignment narrowing.**
`packages/spec/src/migrations/entries/semantic/19.hook-action-body-system-identity-assignment-update-by-id-refused.ts`
records that a save now refuses an elevated body (a `runAs: 'system'`
hook or a `script` action) that writes `sys_user_position`,
`sys_user_permission_set`, `sys_invitation` or `sys_member` through
`updateById`, a call that threw `TypeError: not a function` before this
release. The v18 entry
`hook-action-body-system-identity-assignment-write-refused` is
unchanged: in v18 `updateById` was never refused, it threw. The protocol
upgrade guide is projected from the registries by `gen:upgrade-guide`
into the gitignored `content/docs/protocol-upgrade/`; nothing generated
is committed.
- **Re-read, unchanged.** `rule-explanations.ts` ~1365 / ~1376 / ~1838
and `validate-hook-body-writes.ts` ~246 / ~256 name `updateById` as a
write channel; with the leaf installed each is true, so none is edited.
The `hook-bodies.mdx` snippet is now true, and the
`action-record-write-discarded` hint (`update({ id: ctx.recordId, … })`)
stays.
<details><summary>Measured before the change, and what the host does
with a non-scalar id</summary>
Probe on real `ObjectQL` + better-sqlite3 `SqlDriver` +
`QuickJSScriptRunner`, at `origin/main` f3028c5 (throwaway file, not
committed):
```text
PROBE ACTION updateById :: {"err":{"name":"SandboxError","message":"action 'close' threw: TypeError: not a function"}} :: A=open,B=open
PROBE ACTION typeof :: {"ok":"undefined"}
PROBE ACTION update({id}) :: {"ok":"ok"} :: A=won2,B=open
PROBE HOOK updateById :: {"name":"SandboxError","message":"hook 'win_deal' threw: TypeError: not a function"} :: A=open,B=open
```
The host repository called in-process
(`ScopedContext.object('ubi_deal').updateById(id, …)`), two rows seeded:
every non-scalar or falsy id was refused by the engine and no row moved.
The messages are about filters or `options.multi`, and several carry no
code:
| id | host answer |
|:--|:--|
| `{ $ne: null }`, `{ $in: [id] }`, `null`, `''`, `0`, `NaN`, `true` |
`Error: Update requires an ID or options.multi=true` (no code) |
| `{}` | `INVALID_FILTER` / 400 (empty object at `where.id`) |
| `[id]` | `INVALID_FILTER` / 400 (array comparand at `where.id`) |
| `undefined` | `INVALID_FILTER` / 400 (undefined comparand at
`where.id`) |
So no shape reaches a wider write than one row today; the guard turns
each into one coded refusal before the engine is called. After the
change, same probe: `updateById` writes `A=won`, a `null` id answers
`VALIDATION_ERROR` / 400 with `ctx.api.object('ubi_deal').updateById(id,
data) needs the id of one record (a non-empty string or a non-zero
number) and was given null, so nothing was written.`
</details>
## Tests
- `packages/runtime/src/sandbox/body-update-by-id.integration.test.ts`
(new, real runner): an action body and a hook body calling
`updateById(id, patch)` write that row and only that row (before/after
reads of both rows); a control on `update({ id, … })` on both faces; the
`api.write` gate (`INSUFFICIENT_PRIVILEGES` / 403, nothing written); one
case per refused id shape (operator object, array, `null`, `undefined`,
`''`, `0`), each asserting `code` + `status`, no `update` reaching the
engine (an engine middleware counts them) and no row moved; and a hook
body that catches the coded refusal.
- `packages/cli/test/extract-hook-body.test.ts`: `updateById` alone
infers exactly `['api.write']`, directly and on a bound handle; the
`.create()` refusal's leaf list.
- `packages/spec/src/identity/identity-assignment-body-writes.test.ts`:
`updateById` joins the per-method battery (every quote form, all four
objects); a `runAs: 'system'` hook and a script action writing
`sys_user_position` through `updateById` are refused at `body.source`
with their sentence; CONTROL: the same call on `crm_account` parses on
both faces.
- `packages/lint/src/validate-action-body-capabilities.test.ts`: an
ungranted `updateById` is refused and named; granted, it passes. The
runner-list pin is unchanged and green with both lists moved.
<details><summary>Readings</summary>
- New runtime file: `Test Files 1 passed (1)`, `Tests 11 passed (11)`.
- Ablation 1, the leaf removed (`scripts/ablation-replace.mjs`, anchor
x1 → x0, blob b52e071 → a62699b7b677): `Tests 10 failed | 1 passed
(11)`, the one green being the `update({ id })` control; restored, blob
== HEAD, `git diff HEAD` empty.
- Ablation 2, the guard call removed (blob b52e071 → a36d5c25cb9d):
`Tests 7 failed | 4 passed (11)`, the seven guard cases red (`expected
undefined to be 'VALIDATION_ERROR'`, `expected 'INVALID_FILTER' to be
'VALIDATION_ERROR'`), the write pins, control and gate green; restored,
`git diff HEAD` empty.
- `@objectstack/cli` `test/extract-hook-body.test.ts`: `Tests 67 passed
(67)`.
- `@objectstack/lint` whole suite: `Test Files 246 passed (246)`, `Tests
9960 passed (9960)`.
- `pnpm --filter @objectstack/runtime --filter @objectstack/cli --filter
@objectstack/lint typecheck`: VERDICT command-exit 0.
- `@objectstack/spec` whole suite at f7b947f: `Test Files 1007 passed
(1007)`, `Tests 30504 passed | 1 todo (30505)`.
- Ablation 3, `updateById` removed from the spec's `BODY_WRITE_METHODS`
(blob 408d74e → b126506): `Tests 6 failed | 25 passed (31)` in
`identity-assignment-body-writes.test.ts`, the four per-object method
batteries and the two `updateById` refusals red, the `crm_account`
control green; restored, blob == HEAD, `git diff HEAD` empty.
- `@objectstack/lint` whole suite at f7b947f: `Test Files 246 passed
(246)`, `Tests 9960 passed (9960)`.
- New v19 entry at 4c186ea: `check-adr-0087-registration --base
origin/main` exits 0 (`registered
hook-action-body-system-identity-assignment-update-by-id-refused,
publish-refuses-seeds-and-actions-that-save-and-do-not-work`; `1
migration entry added, each at or above PROTOCOL_MAJOR 19`);
`check:spec-changes` and `check:upgrade-guide` generate in memory
(protocol 19.0.0); `gen:upgrade-guide` writes the docs pages and `19.md`
carries the entry; `check:generated` after a spec build: `All 14
generated artifacts are up to date`; the spec migrations, registry and
upgrade-guide tests: `Test Files 8 passed (8)`, `Tests 246 passed
(246)`; cli `test/migrate-meta-engine-guidance.test.ts`: `Tests 3 passed
(3)`; `check-changeset-no-major --event` with this PR's body: exit 0;
`overlap-check --branch=HEAD`: VERDICT clean.
- `@objectstack/spec` `pnpm test` (local project) at 8e38653: `Test
Files 913 passed (913)`, `Tests 28896 passed | 1 todo (28897)`; the
upgrade-guide span and docs tests with the migrations tests: `Test Files
7 passed (7)`, `Tests 239 passed (239)`. `dispatch-gates --ran` at
8e38653: 124 derived, 124 run, 0 UNRUN; every family exit 0 once the
cli and formula closures were built.
- Inference-row edit at 60c75f9: the 45 families `dispatch-gates
--commands` derives for `content/docs/automation/hook-bodies.mdx` all
exit 0, among them `check:doc-anchors` (1179 fragment links resolve),
`check:docs-spec-enumerations`, `check:doc-security-posture` and
`check:doc-formula-expressions`; `overlap-check --branch=HEAD`: VERDICT
clean.
- Changeset correction at 0382b34: the BREAKING table's second FROM
cell and the sentence after the table now say both bodies threw
`TypeError: not a function` before this change.
- Dogfood battery (`pnpm --filter @objectstack/dogfood test`, no shard,
at f7b947f): VERDICT command-exit 0, `Test Files 495 passed | 3
skipped (498)`, `Tests 4187 passed | 68 skipped (4255)`.
- `dispatch-gates --ran`: 121 derived, 121 run, 0 UNRUN. Four exited 3
for a prerequisite, not a finding: `check-plugin-teardown-shape
--self-test` (shallow clone), `check:skill-examples` (client-react dist
not built), `check:dual-build-cjs-loads`; `check:type-check-debt` re-run
under the lock afterwards: `OK — 0 ledger entr(ies) re-measured … none
above its recorded number`. CI's `Type Check · debt ledger` is green.
</details>
## Acceptance notes
- First-party bodies calling `updateById`: none, so nothing in this
repository is newly refused.
## Verification
Read first, by risk:
1. `packages/runtime/src/sandbox/quickjs-runner.ts`:
`refuseNonScalarIdArgument` and its call at the top of
`installApiMethod`'s leaf.
2. `packages/runtime/src/sandbox/body-update-by-id.integration.test.ts`:
the refused-shape table.
3. `packages/cli/src/utils/extract-hook-body.ts`: the `api.write`
alternation.
Two-minute check: `pnpm --filter @objectstack/runtime exec vitest run
--project local src/sandbox/body-update-by-id.integration.test.ts`
prints `Tests 11 passed (11)`.
For the save-time narrowing: `pnpm --filter @objectstack/spec exec
vitest run src/identity/identity-assignment-body-writes.test.ts` prints
`Tests 31 passed (31)`.
---
_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 34daac1 commit 4b57756
18 files changed
Lines changed: 400 additions & 34 deletions
File tree
- .changeset
- content/docs
- automation
- deployment
- packages
- cli
- src/utils
- test
- lint/src
- runtime/src/sandbox
- spec/src
- data
- identity
- 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
323 | 323 | | |
324 | 324 | | |
325 | 325 | | |
326 | | - | |
| 326 | + | |
327 | 327 | | |
328 | 328 | | |
329 | 329 | | |
| |||
452 | 452 | | |
453 | 453 | | |
454 | 454 | | |
455 | | - | |
| 455 | + | |
456 | 456 | | |
457 | 457 | | |
458 | 458 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
522 | 522 | | |
523 | 523 | | |
524 | 524 | | |
525 | | - | |
| 525 | + | |
526 | 526 | | |
527 | 527 | | |
528 | 528 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
356 | 356 | | |
357 | 357 | | |
358 | 358 | | |
359 | | - | |
| 359 | + | |
360 | 360 | | |
361 | 361 | | |
362 | 362 | | |
| |||
387 | 387 | | |
388 | 388 | | |
389 | 389 | | |
390 | | - | |
| 390 | + | |
| 391 | + | |
391 | 392 | | |
392 | 393 | | |
393 | 394 | | |
| |||
515 | 516 | | |
516 | 517 | | |
517 | 518 | | |
518 | | - | |
519 | | - | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
520 | 523 | | |
521 | 524 | | |
522 | 525 | | |
| |||
556 | 559 | | |
557 | 560 | | |
558 | 561 | | |
559 | | - | |
| 562 | + | |
560 | 563 | | |
561 | 564 | | |
562 | 565 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
75 | 93 | | |
76 | 94 | | |
77 | 95 | | |
| |||
414 | 432 | | |
415 | 433 | | |
416 | 434 | | |
417 | | - | |
| 435 | + | |
418 | 436 | | |
419 | 437 | | |
420 | 438 | | |
| |||
442 | 460 | | |
443 | 461 | | |
444 | 462 | | |
445 | | - | |
| 463 | + | |
446 | 464 | | |
447 | 465 | | |
448 | 466 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3385 | 3385 | | |
3386 | 3386 | | |
3387 | 3387 | | |
3388 | | - | |
| 3388 | + | |
3389 | 3389 | | |
3390 | 3390 | | |
3391 | 3391 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1506 | 1506 | | |
1507 | 1507 | | |
1508 | 1508 | | |
1509 | | - | |
1510 | | - | |
1511 | | - | |
1512 | | - | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
1513 | 1513 | | |
1514 | 1514 | | |
1515 | 1515 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
61 | 72 | | |
62 | 73 | | |
63 | 74 | | |
| |||
0 commit comments