Repository navigation
Commit c47e4ea
fix(plugin-sharing)!: the write gate judges by id exactly the writes the engine runs by id (#34033)
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} -->
_Requested by **Steedos** · [project
thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbSUQWB2j9eHYsRcrCiq1X1C)_
Fixes #33928
Clause-②: no (narrowing)
Metadata-protocol: none
Skills-docs: follow-up #34219
## 维护者速读
- **改了什么:** 共享写入闸门按 id 判还是按批量范围判,改为直接采用引擎自己的分派结论:引擎按 id 写的按 id
判,其余一律套用已有的可写行范围。
- **为什么改:** 成员用 `$in` 批量删改自己的记录在私有对象上一律 403,还挡住了 #33699 的批量级联删除;同时补上闸门
id 推断的一处规则缺口。
- **风险与回滚:** 有意的收窄(BREAKING,见下):`multi: true` 下 where 里标量 id 旁带其它键、或 id
为假值,这两种写法改按批量范围判。按 id 的写法不变。回滚即还原 `engineByIdTarget` 一个函数。
- **先看:** `packages/plugins/plugin-sharing/src/sharing-plugin.ts` 末尾的
`engineByIdTarget`;`operator-id-write-gate-33928.test.ts`
的门口用例与分派表;changeset 的 BREAKING 段。
- **两分钟验证:** `pnpm --filter @objectstack/plugin-sharing exec vitest run
src/operator-id-write-gate-33928.test.ts`,预期 `Tests 44 passed
(44)`;`pnpm --filter @objectstack/plugin-security exec vitest run
src/operator-id-write-gate-33928.test.ts`,预期 `Tests 2 passed (2)`。
- **需要你做的:** 确认这一处收窄(契约复审档)后合并。
> **BREAKING** — a multi-row (`multi: true`) update or delete in either
of two shapes, both of which the engine runs as a predicate, is now
judged by the bulk write scope instead of by id:
>
> - a `where` that carries a scalar `id` beside another key;
> - an `id` that is falsy (`''` or `0`).
>
> A row the caller may not write is left as it was and the write answers
no error, where it answered 403 FORBIDDEN. A row the caller may write
only because an application row-level policy widens the sharing scope
for it is no longer written by these two shapes, as for every other
predicate write. To write such a row, address it by id alone (`where: {
id }`, or the payload `id`): that write is judged by id, as before.
>
> ADR-0087 disposition: not required (`no-migration-prescription`). A
runtime authorization answer moves; no metadata key, accepted value or
stored row changes, so `os migrate meta` has nothing to rewrite. The
changeset carries the marker.
## What changes
This closes a rule-level gap in the gate's id inference. The rule:
- **The sharing write gate judges by id exactly the writes the engine
runs by id.** The by-id or bulk branch is the engine's own dispatch
decision for the write's options: `resolveEngineDeleteDispatch` /
`resolveEngineUpdateDispatch`, the helpers the engine's `delete()` /
`update()` call on the same bag, imported from `@objectstack/objectql`.
A write the engine dispatches by id meets `admitsByIdWrite` exactly as
before: the per-verb gate, the ADR-0090 D10 delegator gate, and the
deferral to an application row policy.
- **Every other write meets the bulk write scope.** `buildWriteFilter`,
plus the delegator's, is ANDed into the AST the engine runs. That covers
an `$in` or `$eq` on `id`, the two shapes above, and a call the engine
refuses at dispatch; such a call now gets the engine's own refusal, the
answer a system caller gets, instead of this gate's 403 first. It is
refused either way and nothing is written. Such a write writes the rows
the caller may update or delete, leaves the others as they were, and
answers no error, the answer every bulk write already gives (ADR-0111
D3). An owned-only `$in` now lands where it answered 403.
- **A cascade batch** (#33699 / PR #33887, one predicate delete per
chunk) meets the same scope. The engine hands any chunk that the scope
narrows back to its by-id path. So a child the caller may not delete
refuses the cascade with the by-id refusal, and nothing is deleted. The
gate carries no flag for it.
## Tests
`packages/plugins/plugin-sharing/src/operator-id-write-gate-33928.test.ts`,
44 tests:
- **Real stack**, 14 pins (ObjectKernel + ObjectQLPlugin over
better-sqlite3 + SharingServicePlugin):
- **by id, unchanged:** an owned row deletes; a not-owned row is refused
403 and stays.
- **`$in` / `$eq`:**
- an owned-only `$in` delete lands;
- a mixed `$in` delete deletes fewer rows and answers no error;
- an `$in` update writes the editable rows only;
- an `$eq` delete writes the owned row only.
- **at the door:**
- a scalar id beside another predicate key, for delete and update, on
the caller's row and on another's;
- a falsy id (`''`, `0`, in `where` and in the payload) and `$in: []`:
the discriminating assertion is the answer, no error;
- an `$eq` / `$in` update of a row the caller holds only a read share
on, which writes nothing, and an `$eq` update of an owned row, which
writes it;
- a call the engine refuses at dispatch (a scalar id beside another key
without `multi`, a payload id beside a where predicate): the member gets
exactly the refusal the system context gets, not a sharing 403, and
nothing is written;
- a system-context control that writes every matched row.
- **cascade:**
- a child the caller may not delete gives exactly the by-id refusal
envelope (code, status, details) and deletes nothing;
- an owned cascade lands.
- **Dispatch table**, 30 rows, against the middleware itself. It covers
every option shape the gate is handed, each with and without `multi`,
for update and delete:
- an id in the payload or in the options, scalar or predicate, alone or
combined;
- falsy ids;
- calls the engine refuses at dispatch.
Each row states the branch the engine dispatches it to, by id with its
row or bulk. The branch is written out, not computed.
`packages/plugins/plugin-security/src/operator-id-write-gate-33928.test.ts`,
2 tests, with `SecurityPlugin` composed (the platform permission sets
plus an application writer set whose row-level policy admits an update
or delete of an `open` deal). The narrowed answer and the remedy sit
side by side on one row: another member's `open` deal, shared to the
caller for reading only, and readable by the caller (asserted first).
- update: `{ where: { id, stage: 'open' }, multi: true }` answers no
error and leaves the row; then `{ where: { id } }` writes it.
- delete: the same two-key predicate answers no error and leaves the
row; then `{ where: { id } }` deletes it.
<details><summary>Base against head on a composed stack (plugin-security
+ plugin-sharing), the narrowing measured</summary>
Real kernel over better-sqlite3, composing `SecurityPlugin` (the
platform permission sets plus an app writer set with an RLS
update/delete widener) and `SharingServicePlugin`.
- Base `529ed7d9`'s `sharing-plugin.ts` was built into `dist/`
(`ablation-dist-preflight --absent` on the head's marker: exit 0), then
restored to the HEAD blob and rebuilt (marker present: exit 0).
- The caller is a member with CRUD on both objects. Rows are private
(owner-scoped).
- The scratch measurement is not committed.
| shape (`multi: true`) | row | base `529ed7d9` | head |
|---|---|---|---|
| update `{ id, stage }` | the caller's | 1 row written | 1 row written
|
| delete `{ id, stage }` | the caller's | 1 row deleted | 1 row deleted
|
| update `{ id, stage }` | another's, read-shared to the caller | 403
FORBIDDEN, unchanged | no error, 0 rows |
| delete `{ id, stage }` | another's, read-shared to the caller | 403
FORBIDDEN, stays | no error, 0 rows |
| update `{ id, stage }` | another's, not shared | 403 FORBIDDEN | no
error, 0 rows |
| delete `{ id: '' }` / `{ id: 0 }`, update `{ id: '' }` | none | 403
FORBIDDEN | no error, 0 rows |
| update `{ id, stage: 'open' }` | another's, read-shared, admitted only
by the app RLS widener | 1 row written | no error, 0 rows |
| delete `{ id, stage: 'open' }` | the same | 1 row deleted | no error,
0 rows |
| update `{ id, stage }` | another's, edit-shared after 5000 earlier
edit shares | 1 row written | 1 row written |
The by-id controls answer the same on base and head:
- update `{ where: { id } }` of the caller's row: written;
- the same of a read-shared row: 403 FORBIDDEN;
- the same of the RLS-admitted read-shared row: written;
- the same of the edit-shared row: written.
Where the object's app RLS itself refuses the row, the security gate
answers first: 403 on base and head alike, for every shape above.
</details>
<details><summary>Green, ablation and S2 runs (local, under the verify
lock, head `00cff2ba9`)</summary>
Main tree, `origin/main` `dfb8ed357` merged in. The
`plugin-security^...` closure was built.
- green: plugin-sharing file `Tests 44 passed (44)`; plugin-security
file `Tests 2 passed (2)`.
- **Ablation A1**, the gate's own choice of branch, scalar values only:
plugin-sharing file `Tests 22 failed | 22 passed (44)`.
- red: the scalar-beside-predicate, falsy-id and dispatch-refusal door
pins, and table rows 11-29.
- **Ablation A2**, the gate's own choice of branch, any value (the rule
before this PR): plugin-sharing file `Tests 34 failed | 10 passed (44)`.
- red: the four `$in` / `$eq` pins, the six door pins (scalar beside
predicate, falsy id, `$in: []`, read-share update, `$eq` owned-update
control, dispatch refusal), and table rows 4-7 and 10-29.
- green: the by-id control, the system-context control, the two cascade
pins, and table rows 0-3, 8 and 9.
- **Ablation A2 through `dist/`**, plugin-security file. plugin-security
reads plugin-sharing from `dist/`, so the mutated source was built (exit
0) and `ablation-dist-preflight` found the mutation's marker in `dist/`
(exit 0). Result: `Tests 2 failed (2)`. Both fail on the narrowed
assertion, the row left as it was, because that rule writes it. After
the restore: rebuild exit 0, the marker absent from `dist/` (exit 0),
`Tests 2 passed (2)`.
Each mutation went through `scripts/ablation-replace.mjs`: the anchor
count went 1 to 0, and each run restored to the HEAD blob with `git diff
HEAD` empty. The plugin-sharing file imports `./sharing-plugin.js` from
source, so no rebuild is owed there.
- `pnpm --filter @objectstack/plugin-sharing test`: `Test Files 75
passed (75)`, `Tests 1581 passed (1581)`.
- `plugin-sharing typecheck`: exit 0.
- `pnpm --filter @objectstack/plugin-security test`: `Test Files 271
passed (271)`, `Tests 6720 passed | 45 skipped (6765)`.
- `plugin-security typecheck`: exit 0.
- eslint `--no-inline-config` on the two test files: exit 0.
The gate code is byte-identical to `185f9d424`, where it was measured on
S2's head for #33699 (`616735ceb`) with the `plugin-security^...`
closure built and `dist/` carrying it. Results there: #33927's 8 cascade
parity pins `Tests 8 passed (8)`, and this PR's plugin-sharing file
42/42. Since then only tests, the changeset and main's range changed, so
neither was re-run.
The full dogfood battery owed by the `(narrowing)` arm ran on
`185f9d424`, after its `@objectstack/dogfood^...` closure was built:
`VERDICT command-exit 0`, `Test Files 467 passed | 3 skipped (470)`,
`Tests 3962 passed | 42 skipped (4004)`. This head adds tests and
changeset text only, so the battery was not re-run.
</details>
## Acceptance notes
- `vama-write-path-convergence.test.ts`: a stale comment now states the
gate's rule. Comment only.
- `Skills-docs: follow-up #34219`: `content/docs/permissions/rls.mdx`,
`content/docs/permissions/permissions-matrix.mdx` and the v17.2 release
note still describe the two narrowed shapes the way the old rule treated
them. The docs card carries the correction, and this PR leaves those
pages unchanged.
## Verification
<details><summary>Derived gates and declarations</summary>
- `node scripts/pm/dispatch-gates.mjs --commands` at `00cff2ba9` derived
73 families. 70 were run locally, each exit 0. `--ran` names the other 3
as unrun; they are declared to CI: `check:dual-build-cjs-loads`,
`check:i18n`, `check:type-check-debt`.
- With this body as `GITHUB_EVENT_PATH`, each exit 0:
`check-changeset-no-major --base origin/main` (LEVEL AXIS: `Clause-②: no
(narrowing)`, `Metadata-protocol: none`), `check-adr-0087-registration
--base origin/main` (one declared-breaking changeset, disposition
`not-required (no-migration-prescription)`),
`check-closing-keyword-parity`, and `check-partof-closing-keyword`.
`check-closing-target-claim` needs the platform and is left to
`pr-body-guards`.
- `Clause-②: no (narrowing)`, for the two shapes above.
`Metadata-protocol: none`.
- `git merge-tree --write-tree` against `origin/main` `98a4449f8` and
S2's head `616735ceb`: exit 0 for both.
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_01QsR2jVPGBoi3MCGoKbW1Hv)_
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01QsR2jVPGBoi3MCGoKbW1Hv
<!-- signed: domain:server#5 · zhuangjianguo ·
session_01QsR2jVPGBoi3MCGoKbW1Hv -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 01598d9 commit c47e4ea
6 files changed
Lines changed: 693 additions & 17 deletions
File tree
- .changeset
- packages/plugins
- plugin-security/src
- plugin-sharing/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 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 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 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
392 | 392 | | |
393 | 393 | | |
394 | 394 | | |
395 | | - | |
396 | | - | |
397 | | - | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
398 | 398 | | |
399 | 399 | | |
400 | 400 | | |
| |||
0 commit comments