Repository navigation
Commit 3c2fb32
perf(objectql): drop a required master's parent chain for a View All reader over unrestricted hops (#33897)
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} -->
_Requested by **Steedos** · [project
thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbSUQWB2j9eHYsRcrCiq1X1C)_
**Do not land before #33820 (ADR-0139 status-line amendment) merges.**
## 维护者速读
-
改了什么:对"受控于父对象"且主从字段必填的明细对象,只有同时持有明细"查看全部"、且每一级主对象读取范围都不受限的读者(典型是单租户管理员),读取时不再带父链子查询;其他读者完全不变;所有写操作(条件更新/删除、按
id 写、钩子里的读取)仍带父链,行为与 main 完全一致。
- 为什么改:这类读者的父链子查询挡不住任何有主记录的行,只拖慢管理员的列表、计数和汇总(卡上 1M 行实测快 29% 到 42%,本 PR
未重测)。
-
风险与回滚:该类读者的合计、计数、汇总可能包含孤儿行(主记录缺失或为空的明细),这是维护者裁决接受的;限制读者、仅有明细查看全部的读者、普通成员仍看不到孤儿行;回滚即
revert 本 PR。
- 先看:`engine-readable-ids-permission-formula.ts` 第 5
步、`readerViewsAllOf` 与
`READABLE_IDS_WRITE_IN_FLIGHT`;`engine-middleware-and-context.ts`
新增的写作用域;`security-plugin-cbp-read-filter.test.ts` 的反向钉子与写路径钉子。
- 两分钟验证:`pnpm --filter @objectstack/plugin-security exec vitest run
--maxWorkers=2 src/security-plugin-cbp-read-filter.test.ts`,预期 12 个测试全绿。
- 需要你做的:等 #33820(ADR-0139 状态行修订)合并后再合并本 PR。
Fixes #33696
Clause-②: no
Metadata-protocol: none
## Ruling
Ruling record 6893666857 (replaces 6893097935), quoted verbatim as it
binds this card:
> - The parent chain is dropped only for a reader who holds view-all on
the detail AND whose every master hop answers unrestricted (the engine's
per-hop scope, not `hasSuperuserReadBypass`), with a required master FK.
That reader sees null and dangling-FK rows inside its own tenant wall,
consistent with #30905 ruling A. Every other reader, members included,
keeps the chain unchanged.
> - This deliberately lifts #24932 ruling B item 4 and ADR-0139 D6's ⛔
for that reader class only. **The PR must not land before the ADR-0139
status-line amendment** (citing this record; ADR-0055's 10-22 note in
step) merges. That amendment is a docs/adr/** card for the skills lane,
which the seat files.
> - Pins: the three controlled-by-parent-leaf-switch tests are rewritten
for the view-all + unrestricted reader. Reverse pins stay: a reader with
a restricted master hop, and a reader with view-all on the detail only,
still never see null or dangling rows.
> - Release note: totals, counts and aggregates for that reader may
include orphan rows. A follow-up card to close the orphan sources
(option B of the original request) is owed as the PR's one loose-ends
card.
> - Multi-tenant deployments: a tenant admin's master scope is the
tenant wall, so it is not unrestricted. No speed-up there, by design;
widening that is a separate decision.
## Review 6895328067: route (a), write doors unchanged
The review found that the drop reached the write doors. The security
plugin's predicate-write narrowing, its 2.8 gate's predicate leg and its
step-2.7 by-id read all ask `ql.find`/`findOne` under the caller's
context. The seat ruled route (a), claim amendments 6895345175 and
6895620230: the write paths stay byte-identical to main. A write's
middleware chain now runs inside an engine async scope, and step 5 drops
nothing inside it. No plugin-security write-door source file changes.
## What changes
- `packages/objectql/src/engine-readable-ids-permission-formula.ts`:
`composeReadableIdsChain` gains step 5. A leaf over the REQUIRED master
of a `controlled_by_parent` object, in an AND position and as the whole
comparand of a `$in` on its `master_detail` field, is dropped instead of
composed. Two conditions must both hold:
- the master's answered scope is `undefined` (admitted, no row
condition), per hop: a nested scope left empty once its own leaves are
dropped answers `undefined`, so a chain drops whole only when every hop
is unrestricted;
- the caller holds View All (`viewAllRecords` or `modifyAllRecords`) on
the detail, read from `ISecurityService.getEffectiveObjectPermissions`
(`readerViewsAllOf`, asked once per read). A delegated caller, a service
without that member, or a call that throws keeps the chain. A failure
costs the subquery, never a row.
An optional master, a leaf inside `$or` or `$not`, and every
non-`controlled_by_parent` object compose exactly as before.
- `packages/objectql/src/engine-middleware-and-context.ts`: **approved
widening** (claim amendment 6895620230, `executeWithMiddleware` only,
+4/−1). For a write-epoch operation (insert, update, delete, purge,
restore), the middleware chain runs inside
`READABLE_IDS_WRITE_IN_FLIGHT`, an `AsyncLocalStorage` exported from
`engine-readable-ids-permission-formula.ts`. `readerViewsAllOf` answers
false inside it, so every read made while a write runs keeps the chain:
the security write-door reads, hook reads and cascade reads. It is an
async scope rather than a context stamp, for the reason
`SecurityPlugin.engineOperationScope` gives: a context is shared and
spread across those very reads.
- `packages/objectql/src/engine.ts`: **approved widening** (coordinator,
2 lines). The private `composeReadableIdsChain` host literal gains a
`_registry` getter, the same getter shape the neighbouring host literals
use.
- `packages/plugins/plugin-security/src/security-plugin.ts`: a
prose-only paragraph in the `computeControlledByParentFilter` docblock
(review should-fix 2). It says the drop is read-only and that write
paths keep the chain.
-
`packages/plugins/plugin-security/src/security-plugin-cbp-read-filter.ts`:
a docblock paragraph only. The leaf is still written for every caller,
and the engine drops it. View All on the detail alone never drops it.
-
`packages/plugins/plugin-security/src/controlled-by-parent-leaf-switch.test.ts`:
**approved widening**. The existing pins are unchanged. A new reader
(View All on the account, contact, quote and line objects) gets legs in
the three leaf-switch tests, and those legs see the dangling and null
rows and run one statement with no `in (select`.
- `.changeset/33696-drop-unrestricted-cbp-chain.md`: patch for
`@objectstack/objectql`, with the ruling's release-note sentence.
Perf, the card's measurement, not re-run here (1M rows): page SQLite 397
to 281 ms, PG 97 to 56 ms; total SQLite 217 to 136 ms, PG 78 to 54 ms.
## Tests
Pins on a real stack (`security-plugin-cbp-read-filter.test.ts`:
ObjectQL, SqlDriver on better-sqlite3, the real SecurityPlugin and
SharingService, orphan rows seeded through the driver), 8 tests:
- (a) View All on the detail over unrestricted hops: no parent-chain
subquery, every stored row including the orphans, count 7, `findOne` of
a dangling row. Plus parity on the rows whose chain resolves, and the
analytics door (the `getReadFilter` scope passed as `where`) composing
the same way.
- (b) a reader with a restricted master hop keeps the whole chain and
sees no orphan.
- (c) a reader with View All on the detail only keeps the whole chain
and sees no orphan.
- (d) a plain member keeps the chain over a restricted master and over
an unrestricted (`public_read_write`) one. The admin control on the same
object drops it.
- (e) isolated tenancy: org A and org B never read each other's rows,
and the tenant wall keeps the chain.
- An optional master keeps its leaf.
Write-door pins (review 6895328067, route (a)), in the same file, 4
tests. The View All admin reads over unrestricted hops, and the set it
writes holds orphans:
- a hook's read inside a write, under the caller's context, keeps the
chain and sees no orphan (control: the same caller's plain read drops
it);
- a predicate delete over a set holding an empty-required-master orphan
lands on the other row and leaves the orphan;
- a predicate update over a set holding an empty-master orphan and a
dangling-master orphan lands on the other row and leaves both;
- a by-id update of a dangling orphan and a by-id delete of an
empty-master orphan both still answer `RECORD_NOT_FOUND` / 404.
Measured on head 2c6025c's objectql source (the three objectql files
set back to 2c6025c, then restored with `git diff HEAD` empty), all 4
are red:
| Pin | Red on 2c6025c |
|---|---|
| hook read | it sees the 4 orphans (`line_a_ghost`,
`line_a_ghost_account`, `line_a_null`, `line_b_ghost`) |
| predicate delete | refused: `MISSING_REQUIRED_FIELD` / 422 |
| predicate update | refused: `MISSING_REQUIRED_FIELD` / 422 |
| by-id write | refused: `PERMISSION_DENIED` / 403, not
`RECORD_NOT_FOUND` / 404 |
On origin/main's objectql source, the 3 write pins pass and the hook
pin's write leg passes. Its plain-read control leg fails there, as it
should, because main drops nothing. Ablations (`ablation-replace`, every
restore checked as blob == HEAD):
- deleting the `READABLE_IDS_WRITE_IN_FLIGHT` guard in
`readerViewsAllOf` turns all 4 red;
- running a write's chain outside the scope in `executeWithMiddleware`
turns all 4 red.
Unit pins (`engine-readable-ids-nested-composition.test.ts`, a new
describe, 17 tests in the file): the drop at the root, in an `$and` and
in a merged-key position; a restricted hop keeps both leaves; `$or`,
optional and non-cbp positions compose; five kinds of caller without
View All on the detail keep the chain (no View All, View All on another
object, no service member, a service that throws, delegated); a
non-readable master gives `$in: []`.
Ablations, on the committed implementation (`node
scripts/ablation-replace.mjs`, every restore proven `blob == HEAD
8ec17d5` and `git diff HEAD` empty):
| Mutation | Real-stack pins red | Unit pins red |
|---|---|---|
| per-hop check removed (drop whenever the master answered) | (b), (c) |
restricted hop keeps; caller without View All keeps |
| `readerViewsAllOf` answers `true` | (d) | caller without View All
keeps |
| step 5 disabled | (a), parity, analytics, (d) admin control | drop at
root and `$and`; `$or`/optional/non-cbp |
<details><summary>Full suites</summary>
Runs were under `scripts/pm/os-verify-lock.sh`, with each exit captured
before any pipe.
On head `df3fb3b9` (the implementation plus origin/main at c59198f,
which includes #33884's step-2.8 changes):
- `@objectstack/plugin-security`: `pnpm test` exit 0, 269/269 files,
6705 passed and 45 skipped. `typecheck` exit 0.
- `@objectstack/objectql`: `pnpm test` exit 0, 598/598 files, 13238
tests. `typecheck` exit 0.
On head `3089656f` (the same implementation, one main merge earlier),
the direct runtime dependents of objectql, after `pnpm --filter
'PKG^...' build` (exit 0). All exits 0:
- example-embed-objectql 1/1 files, 2 tests
- plugin-audit 94/94, 1541
- plugin-email 53/53, 1018
- plugin-pinyin-search 4/4, 39
- plugin-sharing 74/74, 1525
- plugin-webhooks 25/25, 307
- runtime 605/605, 10958 (19 skipped)
- scenarios 34/34, 859
- service-automation 282/282, 3982
- cloud-connection 82/82, 1224
- example-todo 9/9, 249
- plugin-dev 10/10, 89
- verify 52/52, 439
- cli `vitest run --project unit` 422/422 files, 6002 tests
The second main merge touches none of this PR's files.
Packages that depend on objectql only as a devDependency, the cli
integration tier, and dogfood are left to CI. The PR title has no `!`,
so the full dogfood battery is not owed.
</details>
## Acceptance notes
- Loose-ends card owed (the ruling's one): stop orphan rows at their
sources (option B of the original request). Measured sources: a
system-context insert or update that names a missing master is stored
(the documented residual in `engine-referential-integrity.ts`), and
promoting an optional master to required leaves rows with a NULL foreign
key. The seat files it.
- #33820 (skills lane) carries the ADR-0139 status-line amendment. This
PR does not touch `docs/adr/**`.
- Under walled tenancy the tenant admin's master scope carries the
Layer-0 tenant equality, so nothing is dropped there, by design. Pin (e)
measures this.
- Open PR #33652 (another seat) edits the same function in
`engine-readable-ids-permission-formula.ts`: the import line and the
final `composeReadableIdsLeaves` call. The two PRs conflict as text, and
whichever lands second rebases. They do not conflict in meaning. Its
planned relation-semi-join leaves are never asked into `chain.answers`,
and step 5 drops only a leaf whose master has an answer, so it leaves
them alone. #33703 touches `engine.ts` far from this literal.
## Verification
- At head `df3fb3b9`, `dispatch-gates --commands --repo
objectstack-ai/objectstack` derived 77 gate families.
- 74 of them ran locally, every one exit 0. `--ran` reconciliation: "77
derived, 74 run, 0 NOT-MEASURED, 3 UNRUN". The 3 are whole-repo families
left to CI: `check:i18n`, `check:dual-build-cjs-loads`,
`check:type-check-debt`.
- `node scripts/check-nul-bytes.mjs` exit 0. `pnpm lint` (whole-repo
eslint) is left to CI.
- `git merge-tree --write-tree --name-only` from `df3fb3b9`:
- origin/main: clean. Main is now 18 commits ahead, and none of them
touches this PR's files.
- #33887 (`engine-middleware-and-context.ts`, its
`claimReferentialCascadeDelete`): clean. This PR's hunk stays inside
`executeWithMiddleware`.
- #33884: merged into main as c59198f and included in this head.
- #33652: conflicts in `engine-readable-ids-permission-formula.ts`, in
two hunks: the import block and the step-5 / final
`composeReadableIdsLeaves` lines. That overlap was known before. The two
changes do not conflict in meaning, and whichever lands second rebases.
---
🤖 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>
Co-authored-by: objectstack-fleet[bot] <332303061+objectstack-fleet[bot]@users.noreply.github.com>1 parent 385ea1c commit 3c2fb32
9 files changed
Lines changed: 844 additions & 10 deletions
File tree
- .changeset
- packages
- objectql/src
- plugins/plugin-security/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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| |||
160 | 161 | | |
161 | 162 | | |
162 | 163 | | |
163 | | - | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
164 | 167 | | |
165 | 168 | | |
166 | 169 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
409 | 409 | | |
410 | 410 | | |
411 | 411 | | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
0 commit comments