Repository navigation
Commit 0a3dd40
feat(service-analytics, plugin-security, driver-odata): compile, resolve at the record, and refuse the SharedRecordIds leaf (ADR-0155 step 5) (#35945)
<!-- claude-projects-attribution: {"github_login":"os-tesla"} -->
_Requested by **Tesla** · [project
thread](https://claude.ai/code/project/chan_01AXsQkLFKRdQCqQ3RTr6Jao?thread=cmsg_01AXsQkLFKRdQCqQ3RTr6Jao8mXHQniWvfYM2uHoosm2Da)_
Fixes #35874
Refs #33787
Clause-②: yes
The explainer's published sharing-layer `rowFilter` changes content
(ADR-0155 Q2 costs: the share arm is the judged record's id when a grant
admits it and is left out otherwise, and never the leaf), and the
analytics merges admit the `SharedRecordIds` brand at a second door.
`packages/spec` changes too (B6): `parseFilterAST` answers a leaf it
refuses in the array form (an `in` / `nin` comparand, an `in` / `nin`
list member, a scalar operator's comparand) with `INTERNAL_ERROR` / 500
instead of `INVALID_FILTER` / 400.
Metadata-protocol: none
## 维护者速读
- 问题:共享记录权限改造(把共享授权从限量名单改成数据库子查询)的第 5 步,用户无感。
- 这一步做什么:分析报表能直接编译这种子查询,权限解释器逐条记录判断共享,OData 数据源遇到它明确拒绝。
- 不做会怎样:切换上线后,仪表盘、分析查询和“为什么我能看到这条记录”的解释会报错或给出不一致的结果;本步改动可整体撤回。
- 要你做的:无。接线问题(分析服务怎样问引擎“这个对象能否用子查询”)已拆到 #35946,要在第 6
步之前裁定;在那之前,所有环境的分析查询照旧走引擎路径。
## Read first (by risk)
1. `packages/plugins/plugin-security/src/explain-fixed-read-leaf.ts`:
the leaf resolved at the record and the copy-on-write substitution that
drops an emptied `$or` arm. This is the Clause-② content change.
2. `packages/services/service-analytics/src/read-scope-sql.ts`:
`findUnservedFixedReadLeaf` (the role-aware walk) and
`fixedReadLeafSubquerySql` (every value bound, the `tenantScope: 'none'`
check).
3.
`packages/services/service-analytics/src/strategies/native-sql-strategy.ts`:
`readScopeCarriesUncompilableFixedReadLeaf`, the fail-closed decline.
## Two-minute check
`pnpm --filter @objectstack/service-analytics exec vitest run
--maxWorkers=2 src/__tests__/fixed-read-leaf-native-compile.test.ts`.
Expect 31 passed: 11 conformance rows matched live on SQLite, 4 refused,
and the native statement agreeing with the engine path.
## What changes
- **service-analytics, the native compiler**
(`compileScopedFilterToSql`). A read-form leaf placed as `{ id: { $in:
leaf } }` on the scope's own object compiles to `"id" IN (SELECT
"record_id" FROM "sys_record_share" WHERE "object_name" = ? AND
"recipient_type" = ? AND "recipient_id" = ? AND ...)`, closed by the
blank-id guard on `record_id` (not null, not the empty string). It is
built from the leaf's own `fixedInnerReadOf` description, binds every
value, and puts no tenant scope on the subquery (① ruled A). Any other
leaf is refused in D4's envelope (`INTERNAL_ERROR` / 500, logged at
`error` once per service logger): one placed elsewhere, under `$nin`, on
another object, or in its write form.
- **service-analytics, the decline.** `NativeSQLStrategy.canHandle`
declines a scope carrying a leaf unless the host's
`AnalyticsServiceConfig.fixedReadLeafClassOf` answers `null` (neither of
D4's two classes) for every leaf and the dialect is known. A missing
hook, a throwing hook, or any other answer also declines. The engine
path then answers. This change ships the hook unwired:
`AnalyticsServicePlugin` passes no answer, so the native path declines a
scope carrying the leaf and the engine path answers, until #35946 wires
it.
- **service-analytics, the merges and the echo.** `withReadScope`, the
related-object scope and the plugin's label read pass
`admitFixedReadLeaves` to `assertReadScopeComparandsRunnable`, which
refuses an unplaced leaf first and then admits the placed one.
`ObjectQLStrategy.generateSql` prints the subquery.
- **service-analytics, the query's `where`** (compile surface 2). A leaf
anywhere in a query's `where`, in either spelling, is refused in the
envelope before either strategy runs.
- **plugin-security, the explainer.** `resolveFixedReadLeavesAtRecord`
replaces each leaf in a copy of the sharing read filter with
`[recordId]` or `[]`, from one `sys_record_share` read under
`fixedInnerReadOf(leaf)`, keyed by the judged ids. A batch makes one
read per leaf, keyed by every id. An emptied `$or` arm is dropped, and
an `$or` left with one arm is that arm. A leaf it cannot resolve is
refused, logged once, and so is a leaf anywhere but `{ id: { $in: leaf }
}` in the object's own filter: the walk reads roles, as the engine's
seam and the analytics compiler do, so a leaf inside a relation
condition or a composed `ReadableIds` scope is refused. A failing share
read is the read filter failing: the record is not visible, and no
`rowFilter` is published.
- **driver-odata.** `find` and `count` refuse the leaf before any
request, `INTERNAL_ERROR` / 500.
- **spec** (B6). `parseFilterAST` refuses a leaf that either of its
doors refuses with `INTERNAL_ERROR` / 500: the shape door (an array-form
`in` / `nin` comparand) or the type door (an `in` / `nin` list member, a
scalar operator's comparand such as `!=` or `>`). The message names the
leaf and its table, not the recipient. An unbranded copy keeps
`INVALID_FILTER` / 400.
- **objectql.** `index.ts` exports `fixedReadLeafClassOfEngine` and
`driverEvaluatesFixedReadLeaf` from `fixed-read-leaf-seam.ts`.
`engine.ts` is untouched.
## Compile surfaces (one conclusion each)
1. `compileScopedFilterToSql`: **changed here.** Compiles the placed
read form and refuses the rest. Pinned against
`SHARED_RECORD_IDS_CASE_SET` live on SQLite: 11 cases answer exactly
their rows, the 2 write-form cases are refused 500, and the 2 composed
cases are refused 500 with no composition run.
2. `lowerAnalyticsWhere`: **changed here.** At 17ed9e7, before this
change: `{ id: { $in: leaf } }` answered `INVALID_FILTER` / 400 and
printed the recipient; a bare `{ id: leaf }` and an array-form `=` were
lowered to `{ kind: 'relation', member: 'id', condition: { object,
recipient } }`. Now every spelling answers 500 with no recipient. No
producer writes a leaf into a `where`: the leaf enters analytics only as
the read scope.
3. `assertReadScopeComparandsRunnable`: **changed here.** Admits the
brand only for a caller that passes `admitFixedReadLeaves`. Without it
the answer is `READ_SCOPE_COMPILE_FAILED` / 500, as before (pinned).
4. The native decline: **changed here** (above). Pinned three ways: with
the seam's answer, the native statement runs and agrees with the engine
path; with no hook, it declines with the same rows; with a `beforeFind`
hook on `sys_record_share`, it declines and the engine's seam refuses
500.
5. The dry-run echo, `ObjectQLStrategy.generateSql` through
`compileScopedFilterToSql`: **changed here.** Prints the subquery
(pinned). It does not consult the class recognition: it describes the
statement and runs nothing.
6. The explainer, `matchUnderDeclaredColumns`: **changed here.** Leaves
are resolved before the match, in `applyRecordAttribution`.
7. `driver-odata`: **changed here.** The refusal sits beside
`refuseReadableIds`.
8. `driver-mongodb` `translateFilter`: **out of scope** (frozen under
#5499). Its R1 is the step-2 seam:
`fixed-read-leaf-seam.ts#passFixedReadLeaves` refuses any leaf bound for
a driver whose `supports.fixedReadLeaves` does not list it, before the
driver is asked.
9. `driver-memory`: **out of scope** (#35873 on hold). The same seam
refusal applies.
10. `formula` `matchesFilterCondition` and objectql `applyHaving` /
`matchesHaving`: **already compliant.** The engine's having door refuses
a leaf before `applyHaving` runs (`engine-delete-and-aggregate.ts`:
`atFixedReadLeafDoor(this, () => assertListComparandShapes(object,
'aggregate', query.having, 'having'))`). `matchesFilterCondition` sees a
sharing read filter only through the explainer, which now resolves the
leaf first. The write tenant wall's call matches tenant checks only.
11. `driver-sql` / Turso: **already compliant** (step 3, 643fe3b).
## Acceptance notes
- **B1, split to #35946.** `service-analytics` has no runtime dependency
on `@objectstack/objectql`, and no route that adds no package edge lets
the plugin ask the seam. So this PR ships the hook
(`AnalyticsServiceConfig.fixedReadLeafClassOf`) and the fail-closed
decline, and leaves the hook unwired in `AnalyticsServicePlugin`. Until
#35946 wires it, the native path declines a scope carrying a leaf on
every host and the engine path answers, with the right rows. Nothing
writes the leaf before step 6, so nothing changes today. #35946 carries
the wiring fork, and it lands before step 6.
- **D4's admission, reconciled.** In analytics, D4's "brand-naming
callers" are the callers that pass `admitFixedReadLeaves` to
`assertReadScopeComparandsRunnable`: the two merges, the label read, and
`compileScopedFilterToSql`. The last is the native compiler's own door.
It admits only what it compiles, and Q2 ruled that the native compiler
compiles the leaf. This reads as Q2's ruled consequence, so no ADR
amendment is needed. The seat may file a pointer note.
- **B6, decided: moved to the envelope.** No producer can emit the array
form: step 6 writes the object form at the engine's seam, and JSON
cannot carry the brand. The 400 also printed the recipient's id. This
replaces step 2's pin `the authoring lowering refuses the leaf` in
`shared-record-ids.test.ts`, which pinned exactly that branch. The new
pin asserts 500, the message, no recipient, and that the unbranded copy
keeps 400. Neither the spec refusal nor the where-face refusal is
logged: neither function holds a logger (the same precedent as
`driver-odata`'s `refuseReadableIds`).
- **PostgreSQL non-`text` `id`** (#35906, open): mirrors step 3's
`driver-sql`. No cast on the subquery.
- **A residual:** the spec door still answers a branded leaf for every
other caller that names no brand with `INVALID_FILTER` / 400, and its
message prints the leaf's inputs. Step 2 pinned that as the refusal
those callers had before the leaf. No caller here can reach it with a
branded leaf.
## Tests
The five new or changed test files ran at 65d8e8f, the rebased head.
The service-analytics file and its typecheck ran again at 646a10e,
after the sink field was renamed. Other runs are marked.
<details><summary>New and changed test files</summary>
- `service-analytics
src/__tests__/fixed-read-leaf-native-compile.test.ts`: Test Files 1
passed, Tests 27 passed
- `plugin-security src/explain-fixed-read-leaf.test.ts`: Tests 7 passed
- `driver-odata src/odata-driver.test.ts`: Tests 34 passed
- `objectql src/engine-fixed-read-leaf-seam.test.ts`: Tests 68 passed
- `spec src/data/shared-record-ids.test.ts`: Tests 30 passed
</details>
<details><summary>Package suites and typecheck</summary>
- Suites at the pre-rebase tree: spec 865 files passed, 1 skipped (27797
tests); objectql 624 files, 14169 tests; service-analytics 221 files,
5643 tests (after the surface-2 change); plugin-security 285 files, 7075
tests; driver-odata 9 files, 1455 tests.
- `typecheck`: spec, objectql, service-analytics, plugin-security and
driver-odata all exit 0.
- After the rebase, at 65d8e8f: rebuilt the platform-objects and
objectql dists and reran the five files above. plugin-security
`typecheck` passed, objectql `typecheck` passed, and the plugin-security
suite passed: 285 files, 7079 tests (45 skipped).
</details>
<details><summary>Ablation (committed first, restored to the HEAD
blob)</summary>
- Mutation: `scripts/ablation-replace.mjs` replaced `find`'s
`refuseFixedReadLeaf(query.where);` in `odata-driver.ts` with a marker.
The anchor went from 1 to 0 and the blob from d5cb1fe to
a4af98f3458f. On-disk marker count 1; the refusal call count fell from 2
to 1.
- Predicted: every `find` case red, every `count` case green.
- Observed: 12 failed, 22 passed. `find` returned rows (`{ code:
undefined, status: undefined }`) instead of refusing, which is B5's
pre-change behaviour: the predicate was left out of the `$filter` sent.
- Restore: blob back to d5cb1fe, equal to HEAD; `git diff HEAD`
empty.
- A first attempt was refused by the tool before it ran any command: its
replacement text was a substring of the anchor, so the replacement count
could not rise.
</details>
## Verification
- `node scripts/pm/dispatch-gates.mjs --commands` over the diff at
646a10e: 112 derived families. 110 exit 0. 2 exit 3, PREREQUISITE NOT
MET, read as NOT MEASURED: `check:dual-build-cjs-loads` needs every
workspace package's `dist/`, and `check:i18n` needs the workspace CLI
built. `--ran` reconciliation: 112 derived, 110 run, 2 NOT-MEASURED, 0
UNRUN.
- At 65d8e8f two gates went red, both from this diff, and both are
fixed in 646a10e. `check:logger-receiver-detach` read the refusal
sink's `log` field as a log channel passed as a callback, so the field
is now `logger`. `check:engine-double-contract` needed `--write` to
record the new explainer test's `findOne` double in
`scripts/engine-double-contract.pinned.json`.
- Line budget: 1632 changed lines (+1611 / −21) in 22 files, under
3,000.
- `node scripts/pm/overlap-check.mjs`: clean against origin/main
17ed9e7 and #34817, which shares `objectql/src/index.ts`.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01L9qibyXo4gSy8DJPNz4ANz)_
<!-- signed: repo:objectstack#7 · os-tesla ·
session_01L9qibyXo4gSy8DJPNz4ANz -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 9972432 commit 0a3dd40
22 files changed
Lines changed: 1743 additions & 23 deletions
File tree
- .changeset
- packages
- drivers/driver-odata/src
- objectql/src
- plugins/plugin-security/src
- services/service-analytics/src
- __tests__
- strategies
- spec/src/data
- scripts
| 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 | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
272 | 273 | | |
273 | 274 | | |
274 | 275 | | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
| 53 | + | |
| 54 | + | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| |||
288 | 289 | | |
289 | 290 | | |
290 | 291 | | |
| 292 | + | |
291 | 293 | | |
292 | 294 | | |
293 | 295 | | |
| |||
327 | 329 | | |
328 | 330 | | |
329 | 331 | | |
| 332 | + | |
330 | 333 | | |
331 | 334 | | |
332 | 335 | | |
| |||
624 | 627 | | |
625 | 628 | | |
626 | 629 | | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
627 | 674 | | |
628 | 675 | | |
629 | 676 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
426 | 427 | | |
427 | 428 | | |
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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
60 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
| |||
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
| 76 | + | |
| 77 | + | |
75 | 78 | | |
76 | 79 | | |
77 | 80 | | |
| |||
139 | 142 | | |
140 | 143 | | |
141 | 144 | | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
142 | 160 | | |
143 | 161 | | |
144 | 162 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
269 | 269 | | |
270 | 270 | | |
271 | 271 | | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
272 | 278 | | |
273 | 279 | | |
274 | 280 | | |
| |||
0 commit comments