Repository navigation
Commit ee874b1
fix(plugin-sharing)!: a sharing rule on an object without record sharing is refused at save (#30953)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} -->
_Requested by **Elon** · [project
thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9DRnRx2ArvvnWA8tEzVDhwya)_
domain:server#4 · os-elon-musk · session_016WV4cyFB75QwwCDu3S5GNZ
Fixes #30621
Clause-②: no (narrowing)
Metadata-protocol: none
## 维护者速读
- **改了什么**:在没有记录共享的对象上(如
`sys_session`、`sys_account`、`sys_api_key`)保存共享规则,现在两个保存入口(Setup 表单走的
`/data` 门,和 `POST /api/v1/sharing/rules`)都会拒绝,答 `422
SHARING_NOT_ENABLED`,句子点名对象并说明怎样为它开启记录共享;改对象的编辑同样拒绝。
- **为什么改**:以前这样的规则保存成功(`201`)、在 Setup 里显示为启用,但调和时每条授权都被拒、一条
`sys_record_share` 也不写,管理员以为生效了,其实什么都没共享。
- **风险与回滚**:收窄(BREAKING 横幅已加):以前能存的这类规则现在存不进。判定复用调和本身的 D7
惰性判定,两边不会不一致;有属主列的公开对象仍允许(外部成员仍能拿到授权)。已存的旧规则不迁移、不删除,仍可关闭。回滚即 revert。
- **先看**:① `packages/plugins/plugin-sharing/src/sharing-service.ts` 的
`sharingRuleObjectRefusal`(判定与 `assertNotInertGrant` 同源);②
`rule-hooks.ts` 的 `bindRuleObjectEnforcementGuard`(更新只在换对象时检查);③
`sharing-rule-service.ts` 的 `defineRule` 新增检查。
- **两分钟验证**:`pnpm --filter @objectstack/plugin-sharing exec vitest run
src/sharing-rule-object-enforcement.test.ts`,预期 9 个测试全绿。
- **需要你做的**:无。你已在席位线程的决策卡上点头(破坏性),由席位落地。
## What changes
Before, an administrator could save a sharing rule (`sys_sharing_rule`)
on an object no sharing gate consults. The save answered `201`, the rule
showed as active in Setup, and its reconcile then met the ADR-0111 D7
inert-grant guard (`SharingService.assertNotInertGrant`,
`SHARING_NOT_ENABLED`) and wrote no `sys_record_share`, so the
recipients saw nothing more than before.
Now both save doors refuse such a rule and save nothing:
1. **The verdict, from one place.**
`SharingService.sharingRuleObjectRefusal(object)` returns the refusal
sentence, or `null`. It reads the same caller-independent verdict the
grant pre-flight throws on (the new private `objectInertReason`, which
`assertNotInertGrant` now calls too), so the save door and the reconcile
cannot disagree about which objects are under enforcement. The sentence
names the object, gives the D7 reason, and adds the remedy for that arm:
a private or read-only `sharingModel` plus an `owner_id` field; the
master object for a `controlled_by_parent` detail; a real remote
`owner_id` for a federated phantom-anchor object; or "cannot be turned
on" for a bypass-listed platform object. A public object that has an
owner column stays admitted, because the grant service there still
grants an external member of the record's organization (#25549).
2. **The `/data` door** (Setup's New and edit forms, scripts, imports):
`bindRuleObjectEnforcementGuard` binds `beforeInsert` / `beforeUpdate`
on `sys_sharing_rule`, bound in `sharing-plugin.ts` beside the criteria
guard. An insert is checked; an update only when it moves the rule onto
another object (`object_name` supplied and different from
`ctx.previous`), so an administrator can still switch off a rule saved
before this change. System writes pass, as with the declared-name guard.
3. **`POST /api/v1/sharing/rules`** (`SharingRuleService.defineRule`):
the same check for non-system callers, before the insert or upsert; an
upsert that keeps a stored rule on its object is not a move.
The refusal is `SharingRuleObjectNotEnforcedError` (`rule-criteria.ts`)
with `code: 'SHARING_NOT_ENABLED'` and `status: 422`: an existing code
in the `@objectstack/plugin-sharing` ledger row, and the pair the
per-record share routes and `POST .../evaluate` already answer for the
same verdict. No new code, no `packages/spec` change. Both doors answer
a declared `code` + `status` at its own status, the same mechanism
`DeclaredSharingRuleNameConflictError` (409) uses on these doors.
Docs: `content/docs/permissions/sharing-rules.mdx` and
`content/docs/kernel/runtime-services/sharing-service.mdx` say the rule
is refused at save. One changeset, `@objectstack/plugin-sharing: patch`,
with the BREAKING banner and one ADR-0087 marker.
## Tests
`packages/plugins/plugin-sharing/src/sharing-rule-object-enforcement.test.ts`,
real `ObjectQL` + `SqlDriver` (better-sqlite3 `:memory:`), so the hook
runs on the engine's own write path:
- `/data` door: an insert on an owner-less object (the `sys_session`
shape) is refused with `SHARING_NOT_ENABLED` / `422`, the sentence names
the object, `sharingModel` and `owner_id`, and nothing is saved;
CONTROL: an insert on an enforced object saves; CONTROL: a public object
with an owner column is admitted; an update that moves a rule onto a
non-enforced object is refused and the row keeps its object; an update
that leaves a legacy rule on its object passes.
- `defineRule`: a rule on an owner-less object is refused (`code` +
`status`) and nothing is saved; CONTROL: a rule on an enforced object
saves; an upsert that moves an existing rule onto a non-enforced object
is refused.
- Federated remedy (contract review should-fix, dc804fb): a
`public_read_write` federated object (`external: { remoteName }`) whose
`owner_id` is the registry's injected anchor is refused at the `/data`
door with `422 SHARING_NOT_ENABLED`, nothing is saved, and the sentence
says to declare a real remote `owner_id` column, not the `sharingModel:
'private'` remedy. Before the fix: `1 failed | 8 passed (9)`; after: `9
passed (9)`.
<details><summary>Readings (final head dc804fb: plugin-sharing `Test
Files 68 passed`, `Tests 1445 passed`, typecheck exit 0, derived gates
96/96 exit 0 with `--ran` 0 NOT-MEASURED; full dogfood not re-run, as
the delta is one remedy sentence, its pin and docs. Earlier readings
below are on head 6a24dc2; the code is unchanged since 1390d36, the
last commit is the census row on a docs page)</summary>
- New pins, before the fix: `Tests 8 failed (8)`
(`bindRuleObjectEnforcementGuard is not a function`). After: `Tests 8
passed (8)`, lock `VERDICT command-exit 0`.
- Ablation on the committed fix, through `scripts/ablation-replace.mjs`
(`if (!reason) return null;` → `if (reason || !reason) return null;` in
`sharingRuleObjectRefusal`): `Tests 4 failed | 4 passed (8)`. The 4 red
are exactly the 4 refusal pins; the 4 controls stay green. Restore
proven: blob after restore == HEAD blob `2afbcaad0cda`, `git diff HEAD`
empty. The suite imports `src/` by relative path, so no dist rebuild is
on the path.
- `@objectstack/plugin-sharing` `pnpm test`: `Test Files 68 passed
(68)`, `Tests 1444 passed (1444)`; `pnpm typecheck` (incl.
`check:test-typecheck`) exit 0. One lock run, `VERDICT command-exit 0`.
- Full dogfood (`pnpm --filter @objectstack/dogfood test`, no shard,
after a whole-tree `turbo run build --concurrency=2`, 119/119 tasks):
`Test Files 425 passed | 3 skipped (428)`, `Tests 3543 passed | 41
skipped (3584)`, `VERDICT command-exit 0`. Measured on 1390d36;
6a24dc2 adds only the census row in
`content/docs/permissions/system-context.mdx`.
</details>
## Acceptance notes
- **Measured, not assumed: which objects are refused.** Registering
every object `@objectstack/platform-objects` exports into an `ObjectQL`
registry and asking `sharingRuleObjectRefusal`: 58 of 58 are refused,
among them `sys_session`, `sys_account` and `sys_api_key` (no
`owner_id`, no declared model), the three the card names. A custom
object with an `owner_id` and a private model is admitted (the CONTROL
pins).
- **Error code and status: existing.** `SHARING_NOT_ENABLED` is already
in the `@objectstack/plugin-sharing` row of `ERROR_CODE_LEDGER`; 422 is
its published status on the share routes and `POST .../evaluate`. A
scratch probe (not committed) of `@objectstack/rest`'s `mapDataError`
and `classifiedRefusalAnswer` with this error answered `422` /
`SHARING_NOT_ENABLED` with the sentence as `error` on both the `/data`
door and the sharing-rule routes.
- **Stored rules on non-enforced objects in the fixtures: 0.** The
upgrade-harness fixtures hold one `sys_sharing_rule` row
(`fixture-18.0`, `fixture-18.0.0`: `upgrade_harness_red_projects` on
`showcase_project`, enforced; `fixture-18.0.0-next.5`: none). Declared
`sharingRules` are not stored rows and are already held by
`validate-sharing-rule-enforceability`'s anchor findings. Stored rules
are not migrated; one already saved keeps sharing nothing and can still
be switched off.
- **Same predicate as the reconcile.** The save doors refuse exactly
when `assertNotInertGrant` would throw: the private `objectInertReason`
is now the one reading both call. A public object with an owner column
is admitted on purpose (#25549, external recipients still receive
grants). An object the engine has no schema for is admitted, as the
grant pre-flight's inertness half admits it.
- **System writes pass**, mirroring `bindDeclaredRuleNameGuard`: the
platform's own writers (the ownership ceremony, migrations) and the
existing system-caller pins (`system-caller-inert-grant.test.ts`, which
defines such rules to pin the reconcile refusal) are unchanged. The
system-context census row 40 now names `bindRuleObjectEnforcementGuard`
and `defineRule` (register regenerated with `--write`).
- **Not in this PR:** #30209 row 5 (the object picker still lists every
object); this refusal is that card's option C on the save side only.
## Verification
<details><summary>Derived gates (head 6a24dc2)</summary>
- `node scripts/pm/dispatch-gates.mjs --commands`: 96 families over 10
changed paths. All 96 ran on 6a24dc2, each `:: exit 0`.
- `node scripts/pm/dispatch-gates.mjs --ran FILE`: `✓ dispatch-gates
--ran: 96 derived famil(ies) accounted for — 96 run, 0 NOT-MEASURED`.
- First pass on 1390d36: 95 green, `pnpm check:system-context-census`
red (`[read-without-a-row]` for `bindRuleObjectEnforcementGuard` and
`defineRule`); fixed in 6a24dc2 by keying row 40, then `--write`;
re-run green.
- Families CI runs beyond the derivation (lint farm, `Test Core`,
type-check lanes) are CI's; this PR cites them from its check-runs.
</details>
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_016WV4cyFB75QwwCDu3S5GNZ
<!-- signed: domain:server#4 · os-elon-musk ·
session_016WV4czvZykUqhYxYXSSDt4 -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent c77dd17 commit ee874b1
10 files changed
Lines changed: 409 additions & 11 deletions
File tree
- .changeset
- content/docs
- kernel/runtime-services
- permissions
- packages/plugins/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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
140 | | - | |
141 | | - | |
142 | | - | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
146 | 156 | | |
147 | 157 | | |
148 | 158 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
173 | | - | |
| 173 | + | |
174 | 174 | | |
175 | 175 | | |
176 | 176 | | |
| |||
601 | 601 | | |
602 | 602 | | |
603 | 603 | | |
| 604 | + | |
604 | 605 | | |
605 | 606 | | |
606 | 607 | | |
607 | 608 | | |
608 | 609 | | |
609 | 610 | | |
610 | 611 | | |
| 612 | + | |
611 | 613 | | |
612 | 614 | | |
613 | 615 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
32 | 36 | | |
33 | 37 | | |
34 | 38 | | |
| |||
355 | 359 | | |
356 | 360 | | |
357 | 361 | | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
77 | 83 | | |
78 | 84 | | |
79 | 85 | | |
| |||
981 | 987 | | |
982 | 988 | | |
983 | 989 | | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
984 | 994 | | |
985 | 995 | | |
986 | 996 | | |
| |||
0 commit comments