Repository navigation
Commit 7abf25f
fix(service-knowledge): knowledge search asks the object's API exposure and the caller's field-level read rules per source (#36850)
Fixes #36839
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:受限用户(以及代其提问的
AI)检索知识库时,若某个知识源包含他无权查看的字段,或其对象未对接口开放、所属包已停用、是未声明 AI
可读的系统对象,该知识源对他不再返回任何结果;有权限的用户与系统任务不受影响。
- 为什么做:属于「MCP 问数」主线,在知识检索对 MCP 客户开放之前,先堵住低权限用户借 AI 读到其本无权看到的字段内容这一安全缺口。
- 出错会怎样:最坏情况是受限用户检索不到本该能看到的知识源(只会更严,不会更松);改动可直接回滚。
- 要你做什么:无。
## What changes
Option D of the ruling recorded on #36650 (comment 7121194365, quoted in
#36839's body): in the knowledge service, at source granularity, fail
closed, no new spec key. Governing text: ADR-0090 D10 rule 1, ADR-0049.
- **Defect class / location / function.** Missing field-level and
API-exposure enforcement on a row-serving door:
`KnowledgeService.search`
(`packages/services/service-knowledge/src/knowledge-service.ts`)
re-checked an object-source hit only with a row read of its id under the
caller's context, while the passage it returns is built at index time,
under a system context, from every `contentFields` member, the record
`title` and the source's `metadataFields`.
- **New `knowledge-source-admission.ts`**, asked by a new private
`admitSources` step before any adapter is searched, for a non-system
caller with a data engine bound, once per (caller, object) per search:
1. disabled package: core's `registryObjectPackageDisabled` over the
engine's registry (the predicate the data route and the analytics door
ask; no injected hook needed, so no new dependency on
metadata-protocol);
2. API exposure: `canServeApiOperation(enable, 'search')`
(`@objectstack/spec/data`), the decision and operation the cross-object
search asks;
3. `sys_*` objects: served only when `isAiReadExposed` answers true
(`enable.aiReadable: true`), the MCP record tools' posture;
4. field gate: every field the source serves (`contentFields`, `'*'` =
every declared field except `id`; `metadataFields`; `title`), less the
credential-class and `internal` fields `recordToDocument` never indexes,
must be in the `security` service's `getQueryableFields` answer; absent
it or `undefined`, `getReadableFields` less every `maskingRule` field;
with no `security` service, every field except the `maskingRule` ones
(the contract's fallback, `ISecurityService.getQueryableFields`).
A source failing any gate is not searched for that caller. Queryable
rather than readable: a passage carries the unmasked value and the
search matches on it.
- **Fail closed.** A schema read, package probe or field answer that
throws refuses every source over that object (logged at `error`, `warn`
as fallback); a `security` service answering neither field question
refuses too. The system context bypasses, as it bypasses the row
re-check; the no-context and no-engine paths are unchanged (they already
drop every object-source hit).
- **Wiring.** `KnowledgeServicePlugin` passes a new optional
`resolveSecurity` option resolving `security` per search (plugin order
free). Only a missing answer or the kernel's not-registered refusal
(`isServiceNotRegisteredError`) reads as "no security service"; any
other throw from that lookup refuses the object's sources, reported at
`error` once per object per search. `KnowledgeFieldSecurity` is exported
as a type.
- **Objects nobody can judge, and hit origin (review round 1).** A
caller with no execution context, or a service with no data engine
bound, is searched no `object` source at all (before, an object-source
hit with no `sourceRecordId` reached it). An admitted object source's
hit with no `sourceRecordId` cannot be re-checked and is dropped.
`KnowledgeService.search` drops a hit whose reported `sourceId` names
another source than the one searched. Object admissions are asked
concurrently, and the `sys_*` posture also reads the schema's own name.
- **Not in this PR: `@objectstack/knowledge-ragflow`.** A RAGFlow-side
chunk filter added in review round 1 was removed again under the
maintainer's ruling recorded on #36839 (comment 7134290286, option B):
the documented RAGFlow API returns no chunk metadata, so the filter
could not act, and the package is retired whole on its own card. This PR
changes nothing in that package.
- **Pins and claims brought true.**
`row-serving-door-exposure.pin.test.ts` now holds knowledge retrieval BY
NAME (`HELD_BY_NAME`: the door imports its decision site, which asks the
exposure decision and `getQueryableFields`, and the behaviour pin
exists) instead of naming it unconstrained. The `mcp-http-identity` note
in `authz-conformance.matrix.ts` states the knowledge-service
enforcement its "RLS / FLS / tenant apply exactly as on REST /data"
claim relies on once a tool serves knowledge search.
`content/docs/protocol/knowledge.mdx` §5 (new "Source admission"
subsection), §7.1 and §9 state the gates and the narrowing.
`content/docs/permissions/system-context.mdx` row 46 now names
`admitSources` (its new system-context bypass read), with the generated
register regenerated by `check-system-context-census.mjs --write`.
## Tests
- New
`packages/services/service-knowledge/src/__tests__/knowledge-source-admission.test.ts`
(22), through the real `KnowledgeService`, refusal and preservation per
gate: a member who reads the row but not `salary_note` gets no hit from
the source naming it and the adapter is never asked to search it, while
a manager and the system context get it; `title` and `metadataFields`
hidden each refuse; `'*'` refuses on one hidden field and does not ask
about credential / `internal` fields; a masked field via the
`getReadableFields` fallback; no security service ⇒ `maskingRule`
fallback; a throwing field answer and an unanswering service fail
closed; the answer is asked once per object; `apiEnabled: false` dropped
for an end-user context and kept for the system context; an `apiMethods`
whitelist without `search` refused and one with `list` admitted; a
disabled package's object dropped and kept for system; a `sys_*` object
without `aiReadable` dropped, with it kept; a throwing schema read fails
closed; an http source is untouched. Two more drive
`KnowledgeServicePlugin` on a real `LiteKernel` with a registered
`security` service (refusal + preservation) and without one (fallback
admits).
- **Red before / green after.** With `knowledge-service.ts` and
`knowledge-service-plugin.ts` restored to the base `f77d078d` (blob
match confirmed), the new file ran 14 failed / 2 passed (the 2 are pure
preservation legs); restored to HEAD (blob match, `git diff HEAD` empty)
it runs 16 / 16.
- Review round 1 added 6 pins to that file: no context and no engine
each search no object source; an admitted object source's hit without
`sourceRecordId` is dropped; a hit naming another source is dropped; a
`security` lookup throwing anything but the not-registered miss fails
closed with one `error` log (system preserved); and
`isServiceNotRegisteredError`'s discrimination. Red before: with the
service and plugin restored to round-1 head `774652cd63` (the lookup
back to swallowing every throw), the file ran 5 failed / 17 passed (the
5 behaviour pins); restores matched HEAD. Three fixtures in
`knowledge-service.test.ts` that searched an object source with no
context now pass the system context or register the http source their
hit names.
- `row-serving-door-exposure.pin.test.ts` (core `repo` project) 4 / 4;
with `knowledge-service.ts` restored to base, the new held-by-name test
is red (1 failed / 3 passed), restored blob matches HEAD.
<details><summary>Local runs on head 5cc0e97 (after merging
origin/main at 305fe60)</summary>
- Under `os-verify-lock`, VERDICT command-exit 0: `pnpm --filter
'@objectstack/service-knowledge^...' --filter '@objectstack/lint...'
build`, then `@objectstack/service-knowledge` test (5 files / 71 tests),
typecheck (`tsc --noEmit` + `check:test-typecheck` OK) and build
(`check-dts-emitted` 2/2); core `repo` project
`row-serving-door-exposure.pin.test.ts` 4 / 4.
- `node scripts/pm/dispatch-gates.mjs --commands` on 5cc0e97: 105
derived; 103 exit 0. NOT MEASURED, PREREQUISITE NOT MET (exit 3; they
read dists of packages outside this diff, not built in this worktree):
`pnpm --filter @objectstack/spec run check:skill-examples`, `pnpm
check:dual-build-cjs-loads`. `--ran` with exit codes: 105 derived, 103
run, 2 NOT-MEASURED, 0 UNRUN.
- The merge conflicted on `content/docs/permissions/system-context.mdx`
rows 45/46 (main changed row 45, this PR row 46); resolved by keeping
both changes, and `check-system-context-census.mjs --write` reported the
register already current.
- `node scripts/check-platform-checklist.mjs --changed-from
origin/main`: exit 0, no anchored file in the change set.
</details>
## Acceptance notes
- `metadataFields` is gated beside `contentFields` and `title`: a hit's
`metadata` serves those values, so leaving them out would keep the same
leak; the card's Expected 2 names `contentFields` and `title`.
- `title` is required only when the object declares a `title` field (or
its schema is unknown), because `recordToDocument` serves the record's
`title` whatever the source names.
- A `security` service whose field answer is `undefined` on both methods
refuses the source (fail closed) rather than falling to the object's own
projection.
- No published accept set narrows: no spec key, value or schema changes.
What narrows is runtime results for restricted callers, stated in the
changeset.
## Verification
Read first, by risk:
`packages/services/service-knowledge/src/knowledge-source-admission.ts`
(`admitObject`, `servedFieldsOf`, `admitSourceFields`), the
`admitSources` hunk in `knowledge-service.ts`, then the plugin's
`resolveSecurity` wiring.
Two-minute check: `pnpm --filter @objectstack/service-knowledge exec
vitest run src/__tests__/knowledge-source-admission.test.ts` — expect 22
passed.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01QSeKYoxFNWhVjbZhqgBjKQ)_
<!-- signed: repo:objectstack#4 · os-justin ·
session_01QSeKYoxFNWhVjbZhqgBjKQ -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 9c57bd2 commit 7abf25f
12 files changed
Lines changed: 952 additions & 32 deletions
File tree
- .changeset
- content/docs
- ai
- permissions
- protocol
- packages
- core/src/security
- qa/dogfood/test
- services/service-knowledge/src
- __tests__
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
189 | 189 | | |
190 | 190 | | |
191 | 191 | | |
192 | | - | |
| 192 | + | |
193 | 193 | | |
194 | 194 | | |
195 | 195 | | |
| |||
700 | 700 | | |
701 | 701 | | |
702 | 702 | | |
| 703 | + | |
703 | 704 | | |
704 | 705 | | |
705 | 706 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
267 | 267 | | |
268 | 268 | | |
269 | 269 | | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
270 | 273 | | |
271 | 274 | | |
272 | 275 | | |
| |||
286 | 289 | | |
287 | 290 | | |
288 | 291 | | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
289 | 336 | | |
290 | 337 | | |
291 | 338 | | |
| |||
383 | 430 | | |
384 | 431 | | |
385 | 432 | | |
386 | | - | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
387 | 436 | | |
388 | 437 | | |
389 | 438 | | |
| |||
404 | 453 | | |
405 | 454 | | |
406 | 455 | | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
411 | 463 | | |
412 | 464 | | |
413 | 465 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
36 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
37 | 41 | | |
38 | 42 | | |
39 | 43 | | |
| |||
136 | 140 | | |
137 | 141 | | |
138 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
139 | 163 | | |
140 | 164 | | |
141 | 165 | | |
| |||
229 | 253 | | |
230 | 254 | | |
231 | 255 | | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
232 | 282 | | |
233 | 283 | | |
234 | 284 | | |
| |||
0 commit comments