diff --git a/SECURITY.md b/SECURITY.md index 8cbfb92e..d09dd397 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -151,7 +151,7 @@ We follow the [CVSS v3.1](https://www.first.org/cvss/v3.1/specification-document The token is a lock on the **write** surface, not a wall around the whole API. -- **The real boundary is loopback + the DSH host listener.** The host-side routes (`/api/dsh-mneme/*`) are mounted on the DSH port, which binds `127.0.0.1` and is fenced by the host against DNS rebinding and cross-site browser requests. That fence does **not** establish identity: any local process, and any page running in the host's own origin, is inside the perimeter. +- **The real boundary is loopback + the DSH host listener.** The host-side routes (`/api/dsh-mneme/*`) are mounted on the DSH port, which binds `127.0.0.1`. Every one of them asks the host for a verdict before doing anything else — `ctx.connection.requestRejection(req)` — so the host's own fence against DNS rebinding and cross-site browser requests applies to this plugin's routes too: a page open in your browser cannot call them, and gets `403` (fence) or `401` (not signed in to the host) instead of memory data. That fence is not identity: any process already running locally, and any page in the host's own origin, is inside the perimeter. When the host provides no `connection` service (headless, an older host, tests), the check is skipped and behaviour falls back to what it was before this gate existed — a missing host service must not 401 the panel. - **Authentication is opt-in and fail-open.** `apiToken` defaults to empty; while it is empty, `isAuthorized()` returns true for every request — reads *and* writes. Setting a token gates the write routes and the sensitive reads; it does not gate plain reads. - **Open reads (no token even when one is configured)**: `GET /`, `/info`, `/version-check`, `/list`, `/recall-stats`, `/search`, `/semantic`, `/semantic/llm-audit`, `/semantic/llm-audit/stats`, `/semantic/consolidation-stats`, `/semantic/graph/*`, `/entities`, `/memories/entities`, `/dream-status`, `/inject-status`, `/inject-preview`, `/pipeline`, `/candidates`, `/diary`, `/diary/entry`, `/diary/library`, `/diary/library/entry`, `/llm-providers`, `/config-keys`, plus the `GET`/`HEAD` branches of `/profile`, `/rules`, `/persona`, `/persona/session`, `/profile-facts`, `/mode`, `/features`, `/commands`, `/nudge`, `/diary/compose`, `/diary/export`, of the two share-receive routes `/receive` and `/receive/undo`, of `/profile-facts/apply`, of the six chorography read routes `/choro/repos`, `/choro/modules`, `/choro/page`, `/choro/search`, `/choro/claims`, `/choro/run` (M1/finale: they read repo-local wiki pages and `choro_*` index tables — not memory rows; with `chorographyEnabled` off they uniformly answer `409 disabled`, the #497 mount-anyway-gate-in-logic pattern), of the two T1 job routes `/choro/t1` and `/choro/t1/status` (same `token-write` shape as `/choro/run`: the only token-exempt branch is a `405`, and the POST itself answers `409 disabled` unless both `chorographyEnabled` and `chorographyT1FromPanel` are on), and of the three `token-write` knowledge-base routes `/kb/collections`, `/kb/ingest` and `/kb/search` (again only a `405` is genuinely open on them, because `needsToken()` exempts `GET`/`HEAD` under `token-write`; the two plain-`token` reads under `/kb/*` stay gated even on `GET`). `/choro/run` is the same shape as `/receive`: a `token-write` route whose GET branch is token-exempt, so the only genuinely open thing on that path is a `405` — the POST write itself stays gated. Treat them as readable by anything local. `/receive` and `/receive/undo` belong on this list even though they write: under the `token-write` policy, `needsToken()` exempts `GET`/`HEAD` from the token, so the only thing genuinely open on those two paths is a `405` — no read branch exists. `/profile-facts/apply` is POST-only for the same reason (its only open branch is a `405`). The `GET` branch of `/profile-facts` stays open even while `profileFactsEnabled` is off: facts survive a switch-off and the panel must be able to show them (same reasoning as `GET /profile`). This section mirrors the policy declarations rather than anyone's intent, which is why a write route shows up under "open reads"; `dsh-mneme/test/security-doc.test.js` compares both lists against the routes actually mounted, in both directions. - **Gated routes (when a token is configured)**: every write — `/create`, `/update`, `/delete`, `/import`, `/bootstrap`, `/vector-config`, `/vector-reindex`, `/runtime/provision`, `/maintenance/reclaim`, `/test-model`, `/conflicts/resolve`, `/profile/extract` (POST-only: one host-LLM call that returns a profile **draft**; other methods get 405 before any side effect), `/profile-facts/apply` (POST-only: per-fact manual corrections from the panel; writes the `profile_facts` tables, never `user_settings`), `/semantic/graph/affective/apply` (POST-only: per-group manual corrections on `affective_context` edges — rename/archive/restore/promote/demote; writes edge metadata plus the `affective_journal` ledger, never `user_settings`; its GET branch is open under the wildcard above — no gated read exists there), `/profile-facts/run` (POST-only, `token` policy like `/profile/extract`: one host-LLM maintenance pass with no read branch; 409 while `profileFactsEnabled` is off), `/scope/promote` (POST-only, plain `token`: re-stamps the `workspace_scope` of selected rows, one at a time, with a `scope_changes` audit row per success — it moves memories out of a hard-walled import area, so an unauthenticated caller must never reach it), `/choro/run` (POST-only: the panel "rebuild skeleton" write path, re-running T0 into the repo's `.chorography/**`; with `chorographyEnabled` off it uniformly answers 409 disabled), `/choro/t1` (POST-only: spawns a real T1 run through the choro CLI — a host-LLM pass that costs money, so it carries its own `chorographyT1FromPanel` switch on top of `chorographyEnabled`), `/choro/t1/status` (the job poll: its `logTail` carries CLI/model output, so it is not a public-repo read like the rest of `/choro/*`), `/kb/collections`, `/kb/search` (POST-only writes; the only token-exempt branch is the `405`, same shape as `/choro/run`), `/kb/ingest` (POST-only: pulls a configured local source into the knowledge base; source-level failures still answer `200` with `ok:false`, which is a result, not a permission), `/kb/docs`, `/kb/doc` (plain `token`: they hand back whatever collection content the user ingested — that is private material, unlike the repo-documentation `/choro/*` reads), `/receive`, `/receive/undo` (the two share-receive routes write into *this* machine's library, which is why they are `token-write`), `/diary/compose` (POST-only: one host-LLM call that writes `/diaries//YYYY-MM-DD.md`; 409 while `diaryEnabled` is off), `/diary/export` (POST-only: copies one entry into the *user's* bound project directory, which is outside every path the plugin otherwise owns — 409 on an existing target unless `overwrite` is explicit), and the non-`GET` branches of `/profile`, `/rules`, `/persona`, `/persona/session`, `/profile-facts`, `/mode`, `/features`, `/commands`, `/nudge` — plus seven reads: `/export`, `/external-api`, `/conflicts`, `/conflicts/ledger` (read-only triage ledger — tier/status/detection timestamps and title-level info, same sensitivity as `/conflicts`), `/health`, `/share`, `/memory/provenance`. `/share` is gated because the share pack is a whole-library read delivered over POST, and it answers 405 to GET — no open read path exists. `/memory/provenance` is gated because it returns the **verbatim raw text** of distilled conversation windows (opt-in via `distillRecords.enabled`); chained with the open list route it would otherwise let any local process enumerate ids and then pull that text back, which reaches the same result as `/export` in a different shape. @@ -549,7 +549,7 @@ This project is licensed under the **MIT License**. See [LICENSE](https://github Token 是**写**面上的锁,不是把整个 API 围起来的墙。 -- **真正的边界是回环 + 宿主监听器。** 宿主侧路由(`/api/dsh-mneme/*`)挂在 DSH 端口上,该端口绑 `127.0.0.1`,并由宿主挡掉 DNS rebinding 与跨站浏览器请求。这道围栏**不建立身份**:任何本地进程、以及任何跑在宿主同源页面里的脚本,都在这道围栏之内。 +- **真正的边界是回环 + 宿主监听器。** 宿主侧路由(`/api/dsh-mneme/*`)挂在 DSH 端口上,该端口绑 `127.0.0.1`。**每一条路由在动手之前都先问宿主一句 `ctx.connection.requestRejection(req)`**,所以宿主挡 DNS rebinding 与跨站浏览器请求的那道围栏,对插件的路由同样生效:浏览器里开着的网页调不动它们,拿到的是 `403`(围栏不过)或 `401`(没登录宿主),而不是记忆数据。这道围栏**不建立身份**:任何本地进程、以及任何跑在宿主同源页面里的脚本,都在这道围栏之内。宿主没提供 `connection` 服务时(headless、旧宿主、测试)这道检查跳过,行为退回加它之前的样子——缺一个宿主服务不能让整个面板 401。 - **鉴权是 opt-in 且 fail-open。** `apiToken` 默认为空;为空期间 `isAuthorized()` 对每个请求都返回 true——读和写都放行。配了 token 之后,它守的是写路由与敏感读,不守普通读。 - **开放读(即使配了 token 也不需要)**:`GET /`、`/info`、`/version-check`、`/list`、`/recall-stats`、`/search`、`/semantic`、`/semantic/llm-audit`、`/semantic/llm-audit/stats`、`/semantic/consolidation-stats`、`/semantic/graph/*`、`/entities`、`/memories/entities`、`/dream-status`、`/inject-status`、`/inject-preview`、`/pipeline`、`/candidates`、`/diary`、`/diary/entry`、`/diary/library`、`/diary/library/entry`、`/llm-providers`、`/config-keys`,以及 `/profile`、`/rules`、`/persona`、`/persona/session`、`/profile-facts`、`/mode`、`/features`、`/commands`、`/nudge`、`/diary/compose`、`/diary/export`、`/receive`、`/receive/undo`、`/profile-facts/apply`、`/choro/repos`、`/choro/modules`、`/choro/page`、`/choro/search`、`/choro/claims`、`/choro/run`、`/choro/t1`、`/choro/t1/status`、`/kb/collections`、`/kb/ingest`、`/kb/search` 的 `GET`/`HEAD` 分支。`/choro/run` 与 `/receive` 同款:token-write 的 GET 分支免 token 后真正开放的只有一个 405(它只收 POST),出现在这里是 policy 声明的忠实投影。把它们当作「本地任何东西都能读」。`/receive`、`/receive/undo` 明明是写路由也出现在这一串里,这不是笔误:`token-write` 下 `needsToken()` 对 `GET`/`HEAD` 免 token,所以这两条路径上真正「开放」的只有一个 `405`,压根没有可读分支;`/profile-facts/apply` 同理只收 POST,开放的唯一分支是一个 `405`。`/profile-facts` 的 `GET` 分支在 `profileFactsEnabled` 关着时同样开放:开关关掉后存量事实还在库里,面板要能展示(与 `GET /profile` 同一理由)。`/choro/*` 六条(chorography 仓库地志读侧,M1 + 收尾档)读的是**仓库里的 MD 页与 choro_* 索引表**,不是记忆行——内容是代码仓库的公开文档面,`chorographyEnabled` 关着时统一回 409 disabled(路由挂着、逻辑层挡,#497 先例),不因为开放读而绕过这个闸。这一节照 policy 声明抄、不照意图写,`dsh-mneme/test/security-doc.test.js` 会拿真实装配出来的路由双向比对这两行。 - **受门控的路由(配了 token 之后)**:全部写操作——`/create`、`/update`、`/delete`、`/import`、`/bootstrap`、`/vector-config`、`/vector-reindex`、`/runtime/provision`、`/maintenance/reclaim`、`/test-model`、`/conflicts/resolve`、`/profile/extract`(只收 POST:花一次宿主 LLM 产一段画像**草稿**,其它方法在产生任何副作用之前就 405)、`/profile-facts/apply`(只收 POST:面板的逐条人工修正;写的是 profile_facts 三表,绝不碰 user_settings)、`/semantic/graph/affective/apply`(只收 POST:情绪的逐组人工修正——改词/归档/恢复/升格/降格,作用于 affective_context 边;写的是边的 metadata 加 affective_journal 账本,绝不碰 user_settings;其 GET 分支按上方通配开放,那里没有受门控的读分支)、`/profile-facts/run`(只收 POST,`token` 政策与 `/profile/extract` 同款:一次宿主 LLM 维护轮,无读分支;`profileFactsEnabled` 关着时 409)、`/scope/promote`(只收 POST,`token` 政策:把选中条目的 `workspace_scope` 逐条改写出去,每条成功都落一行 `scope_changes` 审计——它负责把记忆搬出硬墙导入区,绝不能给未鉴权的调用方)、`/choro/run`(只收 POST:面板「重建骨架」的写入口,重跑 T0 落仓库磁盘的 `.chorography/**`——chorographyEnabled 关着时统一 409 disabled)、`/choro/t1`(只收 POST:经 choro CLI 真起一次 T1——一次要花模型额度的宿主 LLM 作业,所以在 `chorographyEnabled` 之上另有一枚 `chorographyT1FromPanel` 闸)、`/choro/t1/status`(任务轮询:`logTail` 里带 CLI 与模型的原始输出,不属 `/choro/*` 其余几条那种「仓库公开文档面」)、`/kb/collections`、`/kb/search`(只收 POST 的写入口,token-write 下真正开放的只有一个 405,与 `/choro/run` 同款)、`/kb/ingest`(只收 POST:把用户配置的本地来源抽进知识库;来源级失败仍回 `200` + `ok:false`,那是结果不是权限)、`/kb/docs`、`/kb/doc`(纯 `token`:回的是用户导进来的集合内容,属私料,与 `/choro/*` 那类仓库文档读侧不同量级)、`/receive`、`/receive/undo`(这两条写的是**本机自己的库**,所以归 `token-write`)、`/diary/compose`(只收 POST:花一次宿主 LLM 写 `/diaries/<项目键>/YYYY-MM-DD.md`;`diaryEnabled` 关着时 409)、`/diary/export`(只收 POST:把某一篇**复制**进用户绑定的项目目录——那是插件本来不拥有的路径,目标已存在时默认 409,要覆盖必须显式传 `overwrite`),以及 `/profile`、`/rules`、`/persona`、`/persona/session`、`/profile-facts`、`/mode`、`/features`、`/commands`、`/nudge` 的非 `GET` 分支——外加七条读:`/export`、`/external-api`、`/conflicts`、`/conflicts/ledger`(矛盾分诊账本的只读面:tier/status/检出时间与标题级信息,与 `/conflicts` 同级——处置由 enforce 档自动完成,行不是待办)、`/health`、`/share`(分享包是「用 POST 递出来的全库读取」,GET 一律 405,没有开放的读分支)、`/memory/provenance`(它回的是蒸馏窗口的**逐字会话原文**,并自 #512 起按窗口带出 `distill_run_id` / `run_type` / `trigger_source` / `run_status` 的运行归因;`distillRecords.enabled` 打开才有;否则把它和上面那个开放的列表路由串起来,任何本地进程都能先枚举 id、再按 id 把那些原文捞回去——可达结果与 `/export`(一次拿全库)同量级,只是换了形状)。 diff --git a/dsh-mneme/CHANGELOG.md b/dsh-mneme/CHANGELOG.md index 4993fe5d..f69d1496 100644 --- a/dsh-mneme/CHANGELOG.md +++ b/dsh-mneme/CHANGELOG.md @@ -2,6 +2,14 @@ ## [Unreleased] +- **宿主 HTTP 路由从没问过宿主「这个请求该不该放行」:浏览器里打开的任意网页都能调记忆接口(awesome 市场的条目因此被 maintainer 补了一句安全说明)**。`createRouteRegistrar`(`dsh-mneme/src/api.js:166`)只做 apiToken 判定,而 `open` 策略的路由——`/api/dsh-mneme`、`/info`、`/version-check`、`/list`、`/recall-stats`、`/search`、`/profile` 等——按 `needsToken` 的定义**永不鉴权**,于是这两层之外再没有任何一处问过宿主「这个请求该不该放行」:同机任何进程、以及浏览器里打开的任意网页,都能直接读出这些路由返回的记忆数据。修法是在 handler 最开头、在 apiToken 判定**之前**先问一次 `ctx.connection.requestRejection(req)`——`403`(Host/Origin 围栏不过)或 `401`(未登录宿主)直接结束响应、一个字节记忆内容都不回,只有 `undefined` 才继续走原有的 policy 路径。门控收在 `createRouteRegistrar` 里一次(`gate(req, res)`),新增的每条路由自动带上,不靠各路由自己记得。 + +- **为什么 `inject` 不加 `connection`(这一处别改回去)**。cordis 的 `_refresh()`(`node_modules/@deepseek-ai/cordis/src/fiber.ts:618-626`)是**全或无**:`inject` 里任一登记服务没绑上,整个 fiber 就不激活。把 `connection` 列进去,等于让插件在 headless、旧版宿主、以及任何没 provide connection 的环境上**整个起不来**——用一次可选的安全增强换掉插件本体,这个交换比不成立。所以改成请求时惰性读:`ctx.reflect?.get` 存在就用它取 `connection`,否则退回 `ctx.connection`(`ReflectService.get` 对未 provide 的服务返回 undefined,不抛),取出后还要确认它有 `requestRejection` 方法才用。宿主没提供、方法不存在、读取抛错——三种情况一律按「没有这道检查」处理,行为逐字退回改动之前:**缺一个宿主服务不能让面板 401**,否则还没跟上这一步的宿主会看见一个打不开的记忆面板。 + +- **standalone 数据面与面板都不受影响**。`bin/cli.mjs`、`bin/dsh-mneme-mcp.mjs`、`bin/dsh-mneme-serve.mjs` 走的是 `src/api-standalone.js`(8790,Node `createServer` 自起,**不经** `createRouteRegistrar`),CLI 与 MCP 的用法一字未改;面板自己在宿主同源页面里跑,`apiFetch` 用相对路径 `fetch`、同源请求自带 cookie,本来就在围栏之内。 + + **测试** 3,650 → **3,657**(+7,`test/api.test.js`):connection 回 `403` 时连 `policy: "open"` 的路由也挡下、回 `401` 同样挡下且 body 为空、放行之后 policy 照旧(open 仍开放、token 仍要 token)、宿主没提供 connection 时行为与改动前逐字一致、connection 在但没有 `requestRejection` 方法按「没提供」处理、`createApi` 之后才挂上的 connection 对后续请求仍然生效(这条专门锁住惰性读取,避免有人图省事挪回启动时读一次)、读取 connection 抛错时按未提供处理而不是把请求打成 500。`SECURITY.md` 中英两份的安全模型表同步:§5 首条写明宿主侧路由先问访问检查、页面拿到 403/401 而不是记忆数据,并写清「宿主没提供 connection 时跳过」与「这道围栏**不建立身份**」两处边界。 + - **默认落盘路径把每个人钉在 `~/.dsh` 里:换掉宿主就得换记忆库(#744)**。库的默认目录原先写死成 `~/.dsh/memory`,字面量在源码与 `bin/` 里散着 **9 处**(`src/config.js`、`src/api.js` 三次、`src/candidates.js`、`src/pipeline-nudge.js`、`src/pipeline-observations.js` 两次、`src/diary/dir.js`),三份各自实现的 `expandHome`、`src/index.js` 与 `src/serve.js` 各写一遍的 `~` 展开,运行时与模型缓存也是 `~/.dsh/mneme/{runtime,models}`。后果不是「看着像绑定了」,而是真搬不动:把宿主换掉,人的库就留在老位置,插件到一个新家后看见的是一张空表。这一版把「家」抽成一层:新 `src/paths.js` 里 `MNEME_HOME` 是正式名、`DSH_MNEME_HOME` 降成别名,默认落在 `/.mneme`;`resolveMemoryDir()` 一条链决定最终目录——显式 `memoryDir` > `MNEME_MEMORY_DIR`(别名 `DSH_MNEME_MEMORY_DIR`)> 新家默认,且**判据是「那个目录里有没有 `memory.db`」而不是「目录在不在」**,空壳目录不该把主库挡掉。四个刻意的取舍:**存量必须继续能用**——新家里没有库、老 `~/.dsh/memory` 里有,就继续用老的并打一行 info 说明「可迁可不迁」,改默认值绝不能把现有用户变成一张全新空库(本机实测:`resolveMemoryDir("")` 返回 `D:\DSH\.dsh\memory`、`source: "legacy"`、`hasDb: true`,845.9MB 的库原样留着);**缓存与库用不同的兼容规则**——缓存丢了会重下(200MB 量级)不会丢内容,所以旧目录存在就继续用,避免白白重下一个;**旧目录的位置跟着 `DSH_HOME` 走**(用 `legacyHome()` 算,不再写死 `join(home, ".dsh")`):宿主把家指到别处时,那 200MB 缓存不该被当成不存在而重下。这一条第一版写反了(故意只看 `home` 以免测试被机器的 `DSH_HOME` 带偏),代价是别人的机器上缓存白丢;现在把 `env` 做成显式参数,测试传 `{}` 就能隔掉环境,不用担心被带偏;**两边都有库时按体积判一次,不静默搬家**——这是本版最要紧的一处修正(评审 Amarre 指出):原规则「两边都有库就用新家」分不清「已经迁完的人」和「不小心建了一个」——谁在 `MNEME_HOME` 下跑一次 standalone 或一次测试,新家就长出一个几 MB 的库,下次启动静默选新的,几百 MB 的主库再也读不到,且一条日志都没有。现在新库比旧库小到 **10%** 以下就判为误建,继续用旧库并 `warn` 出两个路径与各自体积(附逃生阀:真迁过去的人显式设 `MNEME_MEMORY_DIR` 即可),体积相当或更大才认作真迁完。这条告警文案与判定收在 `src/paths.js` 的 `explainResolution()`,**宿主 `src/index.js` 与 daemon `src/serve.js` 共用同一份**——误建恰恰常从 daemon/测试那边发生,只在宿主喊等于漏掉一半现场;**不删任何东西**,两个库都在,只是选哪本。**测试** 3,599 → **3,620**(+21,新 `test/paths.test.js`):默认家是中性名、新旧环境变量谁优先、空闲机器落到新家、**旧目录有库就继续用**、旧目录只是空壳仍用新家、**两边都有库且体积相当 → 认新家**、**新库只有 1KB 对着 100KB 主库 → 判误建仍用旧库并报 conflict**、**10% 边界(正好 10% 不算、9.9% 才算)**、**迁移后库更小但同量级(30%)仍认新家**、显式配置永远赢且不看库、运行时与模型缓存沿用旧目录、**旧缓存跟着 `DSH_HOME` 走**、告警文案三档(warn 带两路径与体积、info、正常不吭声)、`expandHome` 口径,最后一条是「**不依赖 DSH 也能跑起来**」的可执行验证——空环境 + 空 home 下走默认值建库、写入一条、按 id 读回,不是「代码里搜不到 dsh 字样」;另有 `test/serve.test.js` 新增一条走 daemon 真实入口的用例:摆出旧家 100KB 真库 + 新家 1KB 空库,断言 daemon 喊了、且写入真落进旧库(直接开旧库按 id 查,不是只看日志)。变异自证六条:把 `MNEME_HOME` 与 `DSH_MNEME_HOME` 的优先序调反 → 优先序用例红;把 `hasDb` 改成只看目录存在 → 空壳目录用例红(主库被空目录挡掉);把旧库兼容整段删掉 → `source: "legacy"` 那条红;把体积判据的阈值改成 0 → 误建与边界两条红;把 `legacyHome()` 换回写死的 `join(home, ".dsh")` → `DSH_HOME` 缓存用例红;把 daemon 的 `explainResolution` 调用删掉 → `serve` 那条红。`bin/` 四个入口同时收口:`cli.mjs` 认 `MNEME_URL`/`MNEME_TOKEN`/`MNEME_CONFIG_DIR`(`DSH_MNEME_*` 仍认、新名优先),`dsh-mneme-serve.mjs` 与 `dsh-mneme-mcp.mjs` 同口径,`dsh-mneme-choro.mjs` 的 `--db` 缺省值改从同一条解析取而不是自己拼字面量。`src/diary/dir.js` 的 `memoryBase` 与 `test/diary.test.js` 相应断言一起改掉——原先那条把 `~/.dsh/memory` 钉成期望值,机器上一有存量库它就会随机器变;现在只断言「与解析器同一答案」。**评审补齐十一处(#744 复审)**:① `dsh-mneme-choro.mjs` 的 `defaultDbPath()` 把 `resolveMemoryDir()` 的返回值直接当字符串传给 `path.join`,Node 24 会抛 TypeError——未传 `--db` 时默认命令根本跑不起来,改取 `.dir`;② `dsh-mneme-serve.mjs` 的 `firstNonEmpty` 返回未 trim 的原值,`MNEME_MEMORY_DIR` 前后带空格时下面 `startsWith("~")` 判据失效、被当相对路径 `resolve`,可能开出一个空库,改为返回 `v.trim()`;③ `paths.js` 新增 `legacyMemoryDirs()`:`DSH_HOME` 与 `~/.dsh` 不是同一个地方时,只认 `legacyHome()` 会宣布「没有旧库」而开新空库,现在把写死的老位置也留作兼容候选并写明优先级;④ `dbBytes()` 原来只量 `memory.db` 主文件,而 store 开了 WAL,已提交数据可能还挂在 `-wal` 里——旧库主文件小、WAL 大时会误判成「新库更大」而静默搬走主库,现在与 `store.js` 的占用统计同口径量主文件 + `-wal` + `-shm`(读不到算 0);⑤ `resolveRuntimeDir` / `resolveModelCacheDir` 原判据是「旧目录存在且新目录不存在才用旧的」,新目录只要是个空壳就赢、旧家那 200MB 缓存白扔重下,改成「旧目录存在就用旧的」;⑥ 默认库搬到 `/memory` 后,`qq-inbox` 从库目录退一级推出的收件箱位置与 onebot 实际写的 `/media/onebot` 错位、日记素材静默读不到,改为三档解析:显式 `diaryInboxDir` > 库旁的老位置(存在才用)> 宿主家下的正式位置;⑦ `src/index.js` 把启动时 `resolveMemoryDir()` 选定的目录钉进给 `createApi` 的配置——`cfg.memoryDir` 是原始配置(没配就是空串),而 `api.js` 的 `/pipeline`、`/candidates`、`/nudge` 每次请求都自己再调一次 `memoryBase(config.memoryDir)`,两边都有库时那个函数按体积重算,启动后旧库长过新库 10 倍就会翻到另一本,store 却还开着这本,旁挂数据写到别人家去;⑧ `test/serve.test.js` 的 `saved` 补齐 `MNEME_MEMORY_DIR` / `DSH_MNEME_MEMORY_DIR` 并 delete,否则开发者本机设了就会绕过双库判断、测试还可能写进真库;⑨ 同处 `rt.dispose()` 未 `await` 就删临时目录,Windows 上有文件锁会删失败并被 catch 吞掉、目录残留,改为 `await`;⑩ `test/diary.test.js` 里保存环境变量的键名与恢复的键名不一致(`MNEME_MEMORY_DIR: process.env.DSH_MNEME_MEMORY_DIR`),删了旧变量却恢复不回来,改成按原名保存;⑪ README 中英文卸载说明补上「用过的每个记忆库目录都要检查删除,包括自定义路径」,并说明新旧目录可能同时存库、只删当前选中的那个另一个还在、以后还可能被重新选中。新增测试 4 条:`paths.test.js` 两条(`DSH_HOME` 与 `~/.dsh` 不同时老位置仍作兼容候选;旧库体积算上 `-wal`/`-shm`)、`runtime-layout.test.js` 一条(旧缓存目录存在就用旧的,空壳新目录不参与判定)、`diary.test.js` 一条(收件箱三档)。变异自证:把 `legacyMemoryDirs()` 退回只认 `legacyHome()` → 兼容候选那条红;把 `dbBytes()` 退回只量主文件 → WAL 那条红;把缓存判据退回 `existsSync(old) && !existsSync(next)` → 旧缓存优先那条红;把收件箱三档退回只从库目录退一级 → 收件箱那条红。 - **想圈一块地单独放导入的会话,圈不住:`auto` 标注根本不是墙(硬墙工作区名单)**。`strictScope` 的硬过滤只对 `workspace_scope_source = 'explicit'` 的行生效(`src/service.js:143`、`:147`,`src/store.js:2008`/`:2011` 与 `:2638`/`:2641` 的 SQL 同口径),而三条写入路径——`memory_save`/`register_document`/`memory_organize` 的 `autoStamping` 与蒸馏的 `writeScope`——落的全是 `auto`。`auto` 只吃软加权(他 scope ×0.5,仍然可见),于是「把一个工作区当成封闭记忆区」这个想法在实现上不成立:实测在内存库上把 `strictScope` 与 `scopeEnabled` 都打开,`isVisibleInScope` 对 auto 跨区返回 `true`、`searchMemories` 从别的项目照样召回、`injectCandidates` 照样注入——**注入面也漏**,不只是检索面。这不是 bug 而是 #170 4.3 的裁决(「auto 不可信」),所以没有改 `isVisibleInScope`、没动 auto 语义,而是走它明说保留的那条通道:新增配置键 `scopeHardWallWorkspaces`(`z.array(z.string()).default([])`),**命中名单的工作区在写入时把 `workspace_scope_source` 落成 `explicit`**,硬过滤从此对它生效。两条落库路径都盖:工具侧三处收成一个 `stampScope` 辅助,蒸馏侧在 `writeScope` 之后单独盖一次——只改工具侧会留下一个由后台任务开着的门。四个刻意的取舍:**名单留空 = 行为逐字节不变**(没有「装了新版就自动圈地」);**比较前去掉末尾分隔符且不分大小写**(值可能来自 registry realpath 或会话 `cwd`,字面比会让墙时灵时不灵);**解析不到非空标签就退回 `auto`**(`normalizeExplicitScope` 会把 `global`/`*`/空白落成 `NULL`,而 `explicit` + `NULL` 是全局可见——那等于把墙拆了);**agent 维不套硬墙**(按工作区圈地,不能让同一 agent 在别的工作区的记忆一起被圈)。配套两条出口:`memory_list` / `/list` / standalone API 加 `workspace_scope` 过滤(**选料清单,不是可见性闸门**——只等值匹配、不看 scope source、不碰 `NULL` 行,`total` 与行同过滤);新增 `POST /scope/promote`(`token`,逐条搬、每条成功落一行 `scope_changes` 审计,非本区 id 回 `not-in-scope` 并报出实际 scope 而不是静默跳过)——「自己挑着搬」不能只删不改标注。**测试** 3,599 → **3,613**(+14:硬墙本体 11 条,评审修复 3 条):三面跨区皆不可见而同区可见(`isVisibleInScope`/`searchMemories`/`injectCandidates`)、名单判定容忍尾分隔符与大小写、`resolveScopeStamp` 四路(含「硬墙遇空标签不许盖章」)、`memory_save` 区内落 `explicit` 而区外落 `auto`、空名单逐字节不变、选料过滤只列那一格且不越过可见性闸门、出库逐条回执加审计、出库异常请求三条(缺 ids / 缺 to / 51 条)、蒸馏区内落 `explicit` 而区外落 `auto`。变异自证四条,每条只让一条用例红:`tools.js` 的 `hardWall` 恒 `false`、`summarize.js` 的 `hardWall` 恒 `false`、`scope.js` 的 `norm` 去掉归一、`store.js` 删掉选料子句。顺带补齐了两处会漂的平价:MCP(`bin/dsh-mneme-mcp.mjs`)与 standalone API 的 `workspace_scope` 入口,以及 `docs/CONFIGURATION.md` 键表、`SECURITY.md` 中英两份受门控清单里的 `/scope/promote`。 diff --git a/dsh-mneme/lib/api.js b/dsh-mneme/lib/api.js index ae34959d..22ed2189 100644 --- a/dsh-mneme/lib/api.js +++ b/dsh-mneme/lib/api.js @@ -163,8 +163,23 @@ function needsToken(req, policy, apiToken) { * 唯一的登记入口:校验 policy、拒绝重复路径、把 handler 包上鉴权。 * 返回的 count() 供 createApi 汇报真实路由数(此前的字面量 36 已经和现实脱节)。 */ -export function createRouteRegistrar(webServer, apiToken, disposers) { +export function createRouteRegistrar(webServer, apiToken, disposers, connectionOf = () => undefined) { const seen = new Set(); + // DSH 的访问检查(Host/Origin 围栏 + 宿主登录 cookie)。浏览器里随便一个网页 + // 都能向 127.0.0.1 发请求,所以「谁能问」不能只靠 apiToken——policy=open 的 + // 路由(信息、列表、检索)同样要过这道门。 + // + // connectionOf 是取值函数而不是取值结果:宿主服务的存续会变,每次请求都重问 + // 一次,才不会把 createApi 那一刻的快照(可能还是 undefined)冻死在这里。 + const gate = (req, res) => { + const connection = connectionOf(); + if (!connection || typeof connection.requestRejection !== "function") return false; + const rejection = connection.requestRejection(req); + if (rejection === undefined) return false; + res.statusCode = rejection; + res.end(); + return true; + }; const register = (route) => { if (!ROUTE_POLICIES.includes(route.policy)) { throw new Error( @@ -177,6 +192,7 @@ export function createRouteRegistrar(webServer, apiToken, disposers) { seen.add(key); const inner = route.handler; const handler = (req, res) => { + if (gate(req, res)) return undefined; if (!needsToken(req, route.policy, apiToken)) return inner(req, res); sendJson(res, 401, { error: "unauthorized" }); return undefined; @@ -284,6 +300,18 @@ export function createApi(ctx, service, settings, commands, embedder, semantic = // reflect,退回直接属性访问。createApi 只在有 webServer 时被调用(index.js // 守卫 + 测试 mock),故下方 register 用之非空。 const webServer = typeof ctx.reflect?.get === "function" ? ctx.reflect.get("webServer") : ctx.webServer; + // connection 同上(inject 声明的宿主服务),但**每次请求时才读**:它有可能是 + // 本插件 apply 之后才挂上的,读一次存下来就会永远是 undefined。反射读取本身 + // 也可能在宿主卸载过程中抛错,那种时候按「没提供」处理——缺一道访问检查, + // 好过让面板整体 500。 + const connectionOf = () => { + try { + if (typeof ctx.reflect?.get === "function") return ctx.reflect.get("connection"); + return ctx.connection; + } catch { + return undefined; + } + }; // feature flags 快照(GET/PUT 共用):overrides 是持久化的用户显式覆盖; // effective 是启动配置在白名单键上被 overrides 覆盖后的最终值。config 缺席 @@ -377,7 +405,7 @@ export function createApi(ctx, service, settings, commands, embedder, semantic = } // 所有路由都经这一处登记:policy 必填、鉴权在这里统一判定(见 createRouteRegistrar)。 - const { register, count: countRoutes } = createRouteRegistrar(webServer, apiToken, disposers); + const { register, count: countRoutes } = createRouteRegistrar(webServer, apiToken, disposers, connectionOf); // /api/dsh-mneme prefix fallback → 404 JSON for unknown sub-paths register({ diff --git a/dsh-mneme/lib/index.js b/dsh-mneme/lib/index.js index 73e86a31..bcb62375 100644 --- a/dsh-mneme/lib/index.js +++ b/dsh-mneme/lib/index.js @@ -70,6 +70,11 @@ export const name = "dsh-mneme"; // property without inject" 崩溃)。守卫用 ctx.reflect.get(免 inject 读取, // 未提供返回 undefined)而非 if (ctx.webServer):直接访问未注入属性在 cordis // Proxy 下会抛错而非返回 undefined。 +// 故意**不**把 connection 列进来:cordis 的 inject 是全或无的(_refresh 见到任一 +// 登记服务没有绑上就把整个 fiber 判 inactive),列进来会让 headless / 旧宿主上 +// 整个插件起不来——而访问检查是锦上添花,不是开机前提。改成请求时用 +// ctx.reflect.get("connection") 惰性读(见 api.js 的 connectionOf): +// 有就过 DSH 的 Host/Origin 围栏,没有就退回加这层之前的行为。 export const inject = ["tools", "systemPrompt", "llm", "agentDefaultModel", "commands", "webServer"]; export { Config }; diff --git a/dsh-mneme/src/api.js b/dsh-mneme/src/api.js index ae34959d..22ed2189 100644 --- a/dsh-mneme/src/api.js +++ b/dsh-mneme/src/api.js @@ -163,8 +163,23 @@ function needsToken(req, policy, apiToken) { * 唯一的登记入口:校验 policy、拒绝重复路径、把 handler 包上鉴权。 * 返回的 count() 供 createApi 汇报真实路由数(此前的字面量 36 已经和现实脱节)。 */ -export function createRouteRegistrar(webServer, apiToken, disposers) { +export function createRouteRegistrar(webServer, apiToken, disposers, connectionOf = () => undefined) { const seen = new Set(); + // DSH 的访问检查(Host/Origin 围栏 + 宿主登录 cookie)。浏览器里随便一个网页 + // 都能向 127.0.0.1 发请求,所以「谁能问」不能只靠 apiToken——policy=open 的 + // 路由(信息、列表、检索)同样要过这道门。 + // + // connectionOf 是取值函数而不是取值结果:宿主服务的存续会变,每次请求都重问 + // 一次,才不会把 createApi 那一刻的快照(可能还是 undefined)冻死在这里。 + const gate = (req, res) => { + const connection = connectionOf(); + if (!connection || typeof connection.requestRejection !== "function") return false; + const rejection = connection.requestRejection(req); + if (rejection === undefined) return false; + res.statusCode = rejection; + res.end(); + return true; + }; const register = (route) => { if (!ROUTE_POLICIES.includes(route.policy)) { throw new Error( @@ -177,6 +192,7 @@ export function createRouteRegistrar(webServer, apiToken, disposers) { seen.add(key); const inner = route.handler; const handler = (req, res) => { + if (gate(req, res)) return undefined; if (!needsToken(req, route.policy, apiToken)) return inner(req, res); sendJson(res, 401, { error: "unauthorized" }); return undefined; @@ -284,6 +300,18 @@ export function createApi(ctx, service, settings, commands, embedder, semantic = // reflect,退回直接属性访问。createApi 只在有 webServer 时被调用(index.js // 守卫 + 测试 mock),故下方 register 用之非空。 const webServer = typeof ctx.reflect?.get === "function" ? ctx.reflect.get("webServer") : ctx.webServer; + // connection 同上(inject 声明的宿主服务),但**每次请求时才读**:它有可能是 + // 本插件 apply 之后才挂上的,读一次存下来就会永远是 undefined。反射读取本身 + // 也可能在宿主卸载过程中抛错,那种时候按「没提供」处理——缺一道访问检查, + // 好过让面板整体 500。 + const connectionOf = () => { + try { + if (typeof ctx.reflect?.get === "function") return ctx.reflect.get("connection"); + return ctx.connection; + } catch { + return undefined; + } + }; // feature flags 快照(GET/PUT 共用):overrides 是持久化的用户显式覆盖; // effective 是启动配置在白名单键上被 overrides 覆盖后的最终值。config 缺席 @@ -377,7 +405,7 @@ export function createApi(ctx, service, settings, commands, embedder, semantic = } // 所有路由都经这一处登记:policy 必填、鉴权在这里统一判定(见 createRouteRegistrar)。 - const { register, count: countRoutes } = createRouteRegistrar(webServer, apiToken, disposers); + const { register, count: countRoutes } = createRouteRegistrar(webServer, apiToken, disposers, connectionOf); // /api/dsh-mneme prefix fallback → 404 JSON for unknown sub-paths register({ diff --git a/dsh-mneme/src/index.js b/dsh-mneme/src/index.js index 73e86a31..bcb62375 100644 --- a/dsh-mneme/src/index.js +++ b/dsh-mneme/src/index.js @@ -70,6 +70,11 @@ export const name = "dsh-mneme"; // property without inject" 崩溃)。守卫用 ctx.reflect.get(免 inject 读取, // 未提供返回 undefined)而非 if (ctx.webServer):直接访问未注入属性在 cordis // Proxy 下会抛错而非返回 undefined。 +// 故意**不**把 connection 列进来:cordis 的 inject 是全或无的(_refresh 见到任一 +// 登记服务没有绑上就把整个 fiber 判 inactive),列进来会让 headless / 旧宿主上 +// 整个插件起不来——而访问检查是锦上添花,不是开机前提。改成请求时用 +// ctx.reflect.get("connection") 惰性读(见 api.js 的 connectionOf): +// 有就过 DSH 的 Host/Origin 围栏,没有就退回加这层之前的行为。 export const inject = ["tools", "systemPrompt", "llm", "agentDefaultModel", "commands", "webServer"]; export { Config }; diff --git a/dsh-mneme/test/api.test.js b/dsh-mneme/test/api.test.js index 865790a2..a51086ea 100644 --- a/dsh-mneme/test/api.test.js +++ b/dsh-mneme/test/api.test.js @@ -3026,3 +3026,144 @@ test("POST /diary/compose 两个身份参数都没有时传 null,不假装自 assert.equal(res.statusCode, 200); assert.equal(calls[0].agentId, null, "不知道是谁 ≠ 以为是 default"); }); + +// ── DSH 访问检查(fkysly 在 awesome 条目里点出的那条:只有 apiToken 挡不住 +// 浏览器里任意网页对 127.0.0.1 的请求)。宿主给了 connection 就必须先过 +// requestRejection;没给(headless / 旧宿主 / 测试 mock)则行为退回加这层之前。 + +function setupWithConnection(connection, apiToken = FAKE_API_TOKEN) { + const store = createStore(":memory:"); + const service = createService({ store, mirror: null, config: testConfig({}) }); + const settings = createSettings(store.db); + const commands = { + add: (def) => settings.addCommand(def), + remove: (id) => settings.removeCommand(id), + list: () => settings.listCommands() + }; + const routes = []; + const ctx = { + webServer: { + register(route) { + routes.push(route); + return () => {}; + } + }, + connection + }; + createApi(ctx, service, settings, commands, null, undefined, apiToken, null); + return { routes, service, store }; +} + +const ALLOW_ALL = { requestRejection: () => undefined }; + +test("connection 说 403 时,连 policy=open 的路由也不放行(跨站网页调不动)", async () => { + const seen = []; + const { routes } = setupWithConnection({ + requestRejection(request) { + seen.push(request.url); + return 403; + } + }); + // /api/dsh-mneme/info 是 policy=open:加这层之前它对谁都不设防。 + const route = findHandler(routes, "/api/dsh-mneme/info"); + assert.equal(route.policy, "open", "这条测试的前提就是它 policy=open"); + + const res = new FakeRes(); + await route.handler(req("/api/dsh-mneme/info"), res); + assert.equal(res.statusCode, 403); + assert.equal(res.body, "", "被拒时不该回插件数据,也不该回 JSON 错误体"); + assert.equal(seen.length, 1, "requestRejection 恰好被问一次"); +}); + +test("connection 说 401 时同样挡下,且不改写状态码以外的响应", async () => { + const { routes } = setupWithConnection({ requestRejection: () => 401 }); + const res = new FakeRes(); + await findHandler(routes, "/api/dsh-mneme/info").handler(req("/api/dsh-mneme/info"), res); + assert.equal(res.statusCode, 401); + assert.equal(res.body, ""); +}); + +test("connection 放行后策略照旧:open 直接过,token 仍要 token", async () => { + const { routes } = setupWithConnection(ALLOW_ALL); + + const openRes = new FakeRes(); + await findHandler(routes, "/api/dsh-mneme/info").handler(req("/api/dsh-mneme/info"), openRes); + assert.equal(openRes.statusCode, 200, "放行后 open 路由行为不变"); + + const noAuth = new FakeRes(); + await findHandler(routes, "/api/dsh-mneme/memory/provenance") + .handler(req("/api/dsh-mneme/memory/provenance?id=x"), noAuth); + assert.equal(noAuth.statusCode, 401, "访问检查不等于鉴权:token 策略还要 token"); + + // 放行 token 的那一半必须在**要 token 的路由**上验:拿 policy=open 的 /info + // 带 token 是自证——就算受门控路由错误地拒掉所有 token,那条断言照样绿。 + const authed = new FakeRes(); + const r = req("/api/dsh-mneme/vector-config"); + r.headers = { authorization: `Bearer ${FAKE_API_TOKEN}` }; + await findHandler(routes, "/api/dsh-mneme/vector-config").handler(r, authed); + assert.equal(authed.statusCode, 200, "带正确 token 时受门控路由要放行"); +}); + +test("宿主没提供 connection 时不加检查,行为与加这层之前逐字一致", async () => { + const { routes } = setupWithConnection(undefined); + const res = new FakeRes(); + await findHandler(routes, "/api/dsh-mneme/info").handler(req("/api/dsh-mneme/info"), res); + assert.equal(res.statusCode, 200, "headless / 旧宿主不能因为拿不到 connection 就整体 401"); +}); + +test("connection 对象在手、但没有 requestRejection 方法时按「没提供」处理", async () => { + const { routes } = setupWithConnection({}); + const res = new FakeRes(); + await findHandler(routes, "/api/dsh-mneme/info").handler(req("/api/dsh-mneme/info"), res); + assert.equal(res.statusCode, 200); +}); + +test("connection 在 createApi 之后才挂上时,随后的请求仍然过检查(惰性读取)", async () => { + // cordis 里宿主服务的存续会变(重载、后到),createApi 那一刻的快照不能 + // 被冻死:否则先起的面板就永久裸奔。 + const store = { connection: undefined }; + const routes = []; + const ctx = { + reflect: { get: (name) => (name === "webServer" + ? { register(r) { routes.push(r); return () => {}; } } + : store[name]) }, + webServer: { register(r) { routes.push(r); return () => {}; } } + }; + const storeDb = createStore(":memory:"); + const service = createService({ store: storeDb, mirror: null, config: testConfig({}) }); + const settings = createSettings(storeDb.db); + createApi(ctx, service, settings, { add() {}, remove() {}, list: () => [] }, null, undefined, "", null); + + const route = findHandler(routes, "/api/dsh-mneme/info"); + + // 先没有 connection:放行。 + const before = new FakeRes(); + await route.handler(req("/api/dsh-mneme/info"), before); + assert.equal(before.statusCode, 200); + + // 之后宿主挂上 connection 并开始拒绝:同一个 handler 必须跟着变。 + store.connection = { requestRejection: () => 403 }; + const after = new FakeRes(); + await route.handler(req("/api/dsh-mneme/info"), after); + assert.equal(after.statusCode, 403, "后来挂上的 connection 必须被看到"); +}); + +test("读 connection 抛错时按未提供处理,不让面板 500", async () => { + const routes = []; + const ctx = { + reflect: { + get(name) { + if (name === "webServer") return { register(r) { routes.push(r); return () => {}; } }; + throw new Error("cannot get required service \"connection\" in inactive context"); + } + } + }; + const storeDb = createStore(":memory:"); + const service = createService({ store: storeDb, mirror: null, config: testConfig({}) }); + const settings = createSettings(storeDb.db); + createApi(ctx, service, settings, { add() {}, remove() {}, list: () => [] }, null, undefined, "", null); + + const res = new FakeRes(); + await findHandler(routes, "/api/dsh-mneme/info").handler(req("/api/dsh-mneme/info"), res); + assert.equal(res.statusCode, 200, "宿主卸载中的抛错不能变成面板 500"); +});