Repository navigation
Commit 73ec085
fix(analytics): an empty or blank where key answers 400 INVALID_FIELD on all three analytics doors (#38218)
Fixes #35543
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:通过接口或 AI
助手做统计查询时,筛选条件里写了空的或只有空格的字段名,现在会直接得到「字段名无效」的明确提示,不再报服务器内部错误或无权限。
- 为什么做:让统计查询对这种写错的请求和普通数据查询给出同一种答复,调用方能看懂错在哪里。
- 出错会怎样:最坏情况是某个本来能用的统计筛选被误拒,调用方会看到明确的拒绝提示;改动只有一处检查,可以直接撤回。
- 要你做什么:无。
## What changes
An empty filter key (`{"": "z"}`) on `POST /api/v1/analytics/query`, as
the dataset `runtimeFilter` on `POST /api/v1/analytics/dataset/query`,
and through MCP `query_dataset` reached SQLite as `WHERE = ?` and
answered `500 DATABASE_ERROR`. A blank key (`" "`) answered `403
PERMISSION_DENIED` from the field-read gate. Both now answer `400
INVALID_FIELD` (`field`, `param: 'where'`), as `/data` answers the same
body. The engine is never reached.
The mechanism: every analytics gate passes the empty key by.
`namedQueryFields` skips `member === ''` outright, and
`resolveMemberSource` answers `source: null` (a stand-down) for any
spelling that is not a bare identifier. So nothing judged it before the
strategy compiled it. The fix is one check,
`assertWhereMemberIsFieldName`, at the top of the `where` source-field
gate (`assertWhereFields`, #5669). That gate runs on every path out of
`ensureCube` and on the dataset draft-preview path, so all three doors
share it. The check needs no field probe. It runs before the gate's
three probe stand-downs, because no object has a field with an empty
name.
`/data`'s validator (`assertFilterFieldsExist`,
`@objectstack/metadata-protocol`) is not reused. It answers "is this a
field of the object". The analytics question is "does this member
resolve to a column", and the existing `where` gate already asks it in
the analytics vocabulary (cube members, the cube qualifier, relationship
paths). Taking a dependency on the protocol package for an emptiness
check would add a second reader of the filter tree.
Deliberately narrow: only whitespace-only keys. A key that is text but
no column reference (`amount + 1`, `foo bar`) keeps the #21156
member-shape gate's `PERMISSION_DENIED` / 403. That verdict is pinned
"in every tier" by `caller-member-column-reference-gate.test.ts`, which
this PR's first, wider draft turned red (8 cases). Changing that verdict
is a contract question and is out of scope here.
## Tests
`src/__tests__/where-member-field-name-35543.test.ts` uses the
plugin-wired service (`AnalyticsServicePlugin` over an engine double),
with all three doors called as their callers call them: `service.query`,
`service.queryDataset` and `datasetTools().queryDataset`.
- An empty key, a blank key and a tab-only key on each door: `code:
'INVALID_FIELD'`, `status: 400`, `field`, `param: 'where'`. The engine
saw no statement.
- An empty key nested under `$or`: refused the same way.
- `amount + 1` keeps `PERMISSION_DENIED` / 403. This is the #21156
boundary.
- CONTROL: `{status: 'paid'}` answers on all three doors.
Ablation through `scripts/ablation-replace.mjs`, which deleted the
gate's call line (anchor 1 to 0, restored blob == HEAD, `git diff HEAD`
empty). 10 failed, 1 passed (the control). The red set was every refusal
case on all three doors.
Live re-measure on the stock showcase (`--fresh`, admin), before at
`7605e0c9c` and after at `c79abcb26`:
| body | before | after |
|:--|:--|:--|
| `/analytics/query` `where {"": "z"}` | 500 DATABASE_ERROR | 400
INVALID_FIELD |
| `/analytics/dataset/query` `runtimeFilter {"": "z"}` | 500
DATABASE_ERROR | 400 INVALID_FIELD |
| MCP `query_dataset` `runtimeFilter {"": "z"}` | isError DATABASE_ERROR
/ 500 | isError INVALID_FIELD / 400 |
| the same three with `{" ": "z"}` | 403 PERMISSION_DENIED | 400
INVALID_FIELD |
| CONTROL `{"status": "paid"}` on all three | 200 | 200 |
| `/data/showcase_invoice/query` `where {"": "z"}` | 400 INVALID_FIELD |
400 INVALID_FIELD |
<details><summary>Package run at c79abcb</summary>
`pnpm --filter @objectstack/service-analytics typecheck && … build && …
test` under the verify lock: `VERDICT command-exit 0`, `Test Files 228
passed (228)`, `Tests 5914 passed | 345 skipped (6259)`. `tsc
--listFiles` includes the new test file.
</details>
## Acceptance notes
- The card's second "also seen" row is a separate mechanism and is not
fixed here. `where {"account.a.b.c.d": "z"}` still answers `403
PERMISSION_DENIED` naming the synthesized alias `account__a`. The
two-hop `account.nope` answers `400 INVALID_FIELD` naming
`showcase_account`. It is a relationship-path resolution question, not
an empty key, and is reported for the seat to file.
- A key with a space in it (`foo bar`) still answers 403 under #21156,
where `/data` answers 400. See the narrowing above.
- `/data`'s empty-key envelope omits `field`, where its blank-key
envelope carries `field: " "`. This is an observation only.
## Verification
Read first: `assertWhereMemberIsFieldName` and its call at the top of
`assertWhereFields`
(`packages/services/service-analytics/src/analytics-service.ts`). The
risk hunk is the move of the three probe stand-downs below the member
collection. The second file is the new test.
Two-minute check: `pnpm --filter @objectstack/service-analytics exec
vitest run --maxWorkers=2
src/__tests__/where-member-field-name-35543.test.ts`. Expect 12 passed.
Gates at `c79abcb26`: `dispatch-gates --ran` accounts for 71 derived
families. 69 ran with exit 0. 2 are NOT MEASURED (exit 3, prerequisite
not met): `check:dual-build-cjs-loads` needs a full workspace build, and
`check:type-check-debt` could not get a turn on the verify lock. CI runs
both.
---
_Generated by [Claude
Code](https://claude.ai/code/session_017xRBcSAaBEfQpU2yJexSJ8)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 0e65fb3 commit 73ec085
3 files changed
Lines changed: 186 additions & 8 deletions
File tree
- .changeset
- packages/services/service-analytics/src
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
448 | 448 | | |
449 | 449 | | |
450 | 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 | + | |
451 | 480 | | |
452 | 481 | | |
453 | 482 | | |
| |||
5045 | 5074 | | |
5046 | 5075 | | |
5047 | 5076 | | |
5048 | | - | |
5049 | | - | |
5050 | 5077 | | |
5051 | 5078 | | |
5052 | 5079 | | |
5053 | | - | |
5054 | | - | |
5055 | | - | |
5056 | | - | |
5057 | | - | |
5058 | | - | |
5059 | 5080 | | |
5060 | 5081 | | |
5061 | 5082 | | |
| |||
5071 | 5092 | | |
5072 | 5093 | | |
5073 | 5094 | | |
| 5095 | + | |
| 5096 | + | |
| 5097 | + | |
| 5098 | + | |
| 5099 | + | |
| 5100 | + | |
| 5101 | + | |
| 5102 | + | |
| 5103 | + | |
| 5104 | + | |
| 5105 | + | |
| 5106 | + | |
| 5107 | + | |
| 5108 | + | |
| 5109 | + | |
5074 | 5110 | | |
5075 | 5111 | | |
5076 | 5112 | | |
| |||
0 commit comments