Skip to content

Commit 7b1a772

Browse files
committed
fix(api): 超限请求体回 413,而不是把画像/规则清空
1 parent 01a3365 commit 7b1a772

12 files changed

Lines changed: 245 additions & 92 deletions

File tree

‎.github/workflows/release.yml‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ on:
2929
type: boolean
3030
default: false
3131

32+
# 权限按 job 发放:只有 publish 需要 contents: write(建 Release 前的 tag 校验)
33+
# 与 id-token: write(npm provenance 走 OIDC)。verify/test/notify 只读代码,
34+
# 工作流级给写权限等于把它们全变成可被依赖链利用的提权面(CWE-250)。
3235
permissions:
33-
contents: write
34-
# npm provenance 用 OIDC 而不是长期 token 签名:publish 步骤要能取 id-token,
35-
# 否则 --provenance 会直接报错(2026-10 审计的供应链项)。
36-
id-token: write
36+
contents: read
3737

3838
jobs:
3939
# ── 1. 版本号管理:tag ↔ package.json ↔ CHANGELOG 三方一致 ──────────────
@@ -109,6 +109,11 @@ jobs:
109109
needs: [verify, test]
110110
if: github.event_name == 'push' || inputs.dry_run != true
111111
runs-on: ubuntu-latest
112+
# npm provenance 用 OIDC 而不是长期 token 签名:--provenance 需要 id-token。
113+
# 挂在 job 上而不是 workflow 上,别的 job 拿不到这个能力。
114+
permissions:
115+
contents: read
116+
id-token: write
112117
steps:
113118
- uses: actions/checkout@v4
114119

@@ -159,6 +164,9 @@ jobs:
159164
needs: [verify, test, publish]
160165
if: github.event_name == 'push' || inputs.dry_run != true
161166
runs-on: ubuntu-latest
167+
# 建/改 GitHub Release 要写权限,只有这个 job 需要。
168+
permissions:
169+
contents: write
162170
steps:
163171
- uses: actions/checkout@v4
164172

‎AGENTS.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ dsh-mneme 是 DSH 宿主的记忆插件(蒸馏 / 注入 / 检索 / 巩固 / sc
3737
| 存储生命周期 | `src/maintenance.js` | 无损回收维护入口(#275 第一批;`dsh-mneme reclaim`,默认 dry-run,VACUUM 单独指定) |
3838
| 回收 HTTP 口径 | `src/reclaim-http.js` | 面板路由与 Bearer 数据面共用的唯一一份解析与答复(#411);两张皮只许剩鉴权与路径不同 |
3939
| 服务层 | `src/service.js` | saveWithDedupe(去重键 type+title+scope 三元组)、fuseRecall 检索融合(keyword/vector/bm25/entity)、注入候选、镜像同步、冲突队列 |
40-
| 写入准入 | `src/write-admission.js` | 写入前的会话写入预算与同话题冷却(#254);第 1 级确定性判据(空白 / 噪声 / 密钥 PII)在 `writeAdmission.enforce` 打开时**会真拒绝**(`ADMISSION_DECISION_DENY`),默认只计量,测量点落 `llm_audit_logs` |
40+
| 写入准入 | `src/write-admission.js` | 写入前的会话写入预算与同话题冷却(#254)。第 1 级确定性判据(空白 / 噪声 / 密钥 PII)只在三个条件同时成立时才跑:有 `sessionKey`、`llmAudit.enabled` 未被显式关、`writeAdmission.enabled === true`;都成立且**命中**时,再由 `writeAdmission.enforce` 决定是否真拒绝(`ADMISSION_DECISION_DENY`)。只开 `enforce` 不跑判据、也不拦写入。默认只计量,测量点落 `llm_audit_logs` |
4141
| 注入 | `src/inject.js` | 注入位构造、内容截断、跨轮轮换 |
4242
| 蒸馏 | `src/summarize.js` + `src/quality-filter.js` | 会话 → 记忆;质量打分与处置(归档/降权) |
4343
| 巩固与睡眠 | `src/dream.js` + `src/dream/{decisions,clustering,sleep}.js` | LLM 巩固决策、dream_runs 审计回执 |
@@ -65,7 +65,8 @@ dsh-mneme 是 DSH 宿主的记忆插件(蒸馏 / 注入 / 检索 / 巩固 / sc
6565
6. 提交署名可归属与 AI 内容核验义务见 [CONTRIBUTING](CONTRIBUTING.md)(硬性要求);
6666
7. **发布/打包只在 `dsh-mneme/` 包目录内执行**——根目录历史上发出过坏包(0.6.9、0.7.0–0.7.10),此条为防复发红线;
6767
8. **注释写「为什么」,不写「是什么」**:非常规实现、防御段、行为开关的动机(issue 编号、消融数据、基准数字)必须落注释,代码含义靠命名与测试名表达;测试同样适用——锁形状/锁字面量的用例要注释写清它在防哪类回归(例:`test/recall-evals.test.js` 的 signals 形状锁)。
68-
9. **面板配色走 `--mneme-*` 语义色层**:宿主 `label-tertiary` / `label-dimmed` 是分隔线/占位符用的低对比层,**不是正文色**,直接拿来当 `color` 会在浅色或深色主题下掉到 AA 以下(#178 实测 70 条)。正文与图形一律用 `.mneme-x,.mneme-overlay,.mneme-triggerwrap,.mneme-topentry` 上定义的语义变量;新增或改动颜色后 `test/contrast.test.js` 会自动重算全部 `color` 声明的对比度(解析不出的颜色记失败,不静默跳过),并锁死「引用的 `--dsw-*` 必须真实存在」「四个渲染根一个不少」「图形色按 3:1」「虚线边不得靠 `opacity` 区分」。
68+
9. **新路由必须声明 `policy`**(`open` / `token` / `token-write`):`src/api.js` 的 `createRouteRegistrar` 对漏声明与重复路径直接抛错,插件起不来。归完类同步 `SECURITY.md` 的安全模型表——那张表是代码的投影,不是愿望清单;
69+
10. **面板配色走 `--mneme-*` 语义色层**:宿主 `label-tertiary` / `label-dimmed` 是分隔线/占位符用的低对比层,**不是正文色**,直接拿来当 `color` 会在浅色或深色主题下掉到 AA 以下(#178 实测 70 条)。正文与图形一律用 `.mneme-x,.mneme-overlay,.mneme-triggerwrap,.mneme-topentry` 上定义的语义变量;新增或改动颜色后 `test/contrast.test.js` 会自动重算全部 `color` 声明的对比度(解析不出的颜色记失败,不静默跳过),并锁死「引用的 `--dsw-*` 必须真实存在」「四个渲染根一个不少」「图形色按 3:1」「虚线边不得靠 `opacity` 区分」。
6970

7071
## 闸门清单
7172

‎SECURITY.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,12 @@ We follow the [CVSS v3.1](https://www.first.org/cvss/v3.1/specification-document
122122

123123
## Security Design Principles
124124

125-
### 1. Local-First, Zero-Network
125+
### 1. Local-First (not zero-network)
126126

127-
- All embeddings, reranking, and memory consolidation run locally
128-
- Memory text **never** leaves the device
129-
- No telemetry, no analytics, no remote logging
127+
- Storage, keyword/BM25 search, mirroring and the injection pipeline are entirely local
128+
- Embeddings and consolidation **run locally only when configured to**: `embedProvider` defaults to `openai`, which POSTs memory text to the endpoint you configure; `autoDream` / `autoSummarize` send text through the host LLM. Set `embedProvider: local` (plus the local runtime) if text must not leave the machine
129+
- Other outbound calls: the npm registry version check, and local model downloads when the runtime is provisioned
130+
- No telemetry, no analytics, no remote logging of memory content
130131

131132
### 2. Fail-Safe by Default
132133

@@ -156,6 +157,7 @@ The token is a lock on the **write** surface, not a wall around the whole API.
156157
- **Gated routes (when a token is configured)**: all writes (`/create`, `/update`, `/delete`, `/import`, `/mode`, `/features`, `/commands`, `/conflicts/resolve`, `PUT /profile`, `PUT /rules`, `/vector-config`, `/vector-reindex`, `/runtime/provision`, `/maintenance/reclaim`, `/test-model`, `/health`) and the sensitive reads `/export` and `/external-api`.
157158
- **The standalone data plane** (`127.0.0.1:8790`) is the opposite shape: every route except `GET /health` requires its own Bearer token. Pointing `externalApiHost` at a non-loopback address therefore turns it into a plaintext, token-only HTTP surface — an explicit operator decision.
158159
- **Consequence**: if you expose the plugin beyond loopback (reverse proxy, non-loopback bind), set `apiToken` *and* check that the plain reads above are acceptable to expose — they are not covered by it.
160+
- **How this list stays true**: every route declares its own `policy` (`open` / `token` / `token-write`) at registration, and `createRouteRegistrar` throws when a route has none or reuses a path. A new route that forgets its classification **fails to boot** instead of silently opening — the matrix above is derived from code, not from anyone's memory.
159161

160162
---
161163

‎dsh-mneme/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ dsh web
477477
| `GET` | `/search?q&mode=keyword\|vector\|hybrid\|auto&topK&occurred_from&occurred_to&agent_scope&workspace_scope` | 搜索(关键词 / 向量 / hybrid=向量领位关键词补位 / 自动)。scope 两参声明调用方身份(不传 = 不解析,行为与旧版一致;任一给出则 strictScope 硬过滤与 A2 软加权随之生效) |
478478
| `GET` | `/context?q&topK&threshold&agent_scope&workspace_scope` | 注入候选一站式聚合(issue #370,#363 承诺):宿主注入管线 `injectCandidates` 全语义(优先级分层 / 编码门控 / heat / scope 软加权 / hybrid 语义路 / pin 前置)+ 画像 + 规则,返回 `{profile, rules, items, pinnedCount}` 结构化条目,不含渲染文本。`threshold` 是 **importance 阈值**(默认 3,与 `/search` 的相似度阈值同名不同义);`topK` 默认 5(对齐宿主 `maxInjectedItems`);scope 语义与 `/search` 同口径;跨轮轮换 / 热记忆 / 时间前缀不含(调用方自管);recall_runs 注入分账与宿主注入同口径(#217 `mode='inject'`) |
479479
| `POST` | `/bootstrap` | 冷启动(v0.8.4):从仓库目录反向构建初始记忆,body `{dir}` 必填;幂等((type,title,scope) 去重 + `_overwrite` 原地刷新),零 LLM、必有产出 |
480-
| `GET`/`POST` | `/maintenance/reclaim` | 无损回收(#275 / #411,与面板同口径):默认 dry-run,body 或 query 带 `confirm=true` 才真删;`vacuum` 只允许显式指定 |
480+
| `POST` | `/maintenance/reclaim` | 无损回收(#275 / #411,与面板同口径):**只有 POST**(独立面没有 GET 预估分支)。字段只从 JSON body 读;`dryRun` 默认 `true`,只有 `dryRun:false` 且 `confirm:true` 才真删;`vacuum` 只允许显式指定 |
481481

482482
### curl 示例
483483

‎dsh-mneme/docs/CONFIGURATION.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@
293293
`autoReindexOnBoot` · `hybridInject` · `injectGuidanceEnabled` · `searchSemanticDedup` ·
294294
`selectiveInjectEnabled` · `bm25SearchEnabled` · `entityRecallEnabled` ·
295295
`graphAnchoringEnabled` · `graphWeightEnabled` · `graphInjectHint` · `graphPassiveConfirm` ·
296+
`actionRunEnabled` ·
296297
`dreamNarrativeEnabled` · `documentMemoryEnabled` · `heatEnabled` · `continuityRescueEnabled` ·
297298
`preInjectGate.enabled`(#380) · `distillRecords.enabled`(#399)
298299

‎dsh-mneme/lib/api-standalone.js‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,31 @@ function isAuthorized(req, apiToken) {
9393
}
9494

9595
/**
96-
* Collect the request body as text (tolerant of transport errors).
96+
* Collect the request body as text, or null when it exceeds the cap.
9797
*
98-
* 超过 MAX_REQUEST_BODY_BYTES 后停止累积、继续排空连接:解析空串得到 {},
99-
* 调用方的字段校验会把它变成 invalid-* 400,内存不随 Content-Length 增长。
98+
* 超限时**必须回 null 而不是空串**:空串经 parseBody 变成 {},而 /profile、
99+
* /rules、/persona 的 PUT 会把「字段缺失」当成「用户要清空」——一个 5 MiB 的
100+
* 请求就能把画像/规则/人格静默清掉。返回 null 让调用点在碰任何 store 之前
101+
* 直接 413(2026-10 评审指出的数据损坏路径)。
102+
* 连接不 destroy:调用方要能收到明确的 413,而不是被掐断。
100103
*/
101-
function readBody(req) {
104+
function readBody(req, res) {
102105
return new Promise((resolve) => {
103106
let body = "";
104107
let bytes = 0;
105108
let overflow = false;
106109
req.on("data", (chunk) => {
107110
if (overflow) return;
108111
bytes += chunk.length;
109-
if (bytes > MAX_REQUEST_BODY_BYTES) { overflow = true; body = ""; return; }
112+
if (bytes > MAX_REQUEST_BODY_BYTES) {
113+
overflow = true;
114+
body = "";
115+
sendJson(res, 413, { error: "payload-too-large", limit: MAX_REQUEST_BODY_BYTES });
116+
return;
117+
}
110118
body += chunk;
111119
});
112-
req.on("end", () => resolve(body));
120+
req.on("end", () => resolve(overflow ? null : body));
113121
req.on("error", () => resolve(""));
114122
});
115123
}
@@ -351,7 +359,8 @@ export function createStandaloneApi({ service, store, config = {}, logger, setti
351359
// 形状判定在 setPersona 内(settings.js 双形兼容:单对象=合并 default、
352360
// 多卡=全量替换);坏 JSON 回 400(与 /memories 写路径同口径)。
353361
if (req.method === "PUT" && pathname === "/persona") {
354-
void readBody(req).then((text) => {
362+
void readBody(req, res).then((text) => {
363+
if (text === null) return;
355364
let body;
356365
try {
357366
body = JSON.parse(text || "{}");
@@ -434,7 +443,8 @@ export function createStandaloneApi({ service, store, config = {}, logger, setti
434443
// 整个异步回调套 try/catch:service 抛错不能变成未处理拒绝(Node 默认
435444
// 策略下会终止宿主进程)或悬着不回的请求(外层同步 try 捕不到这里)。
436445
if (req.method === "PUT") {
437-
void readBody(req).then(async (text) => {
446+
void readBody(req, res).then(async (text) => {
447+
if (text === null) return;
438448
try {
439449
await handlePutBody(res, service, logger, id, text);
440450
} catch (error) {
@@ -450,7 +460,8 @@ export function createStandaloneApi({ service, store, config = {}, logger, setti
450460

451461
// --- POST /memories: save with title-dedupe (safer than raw save) ------
452462
if (req.method === "POST" && pathname === "/memories") {
453-
void readBody(req).then((text) => {
463+
void readBody(req, res).then((text) => {
464+
if (text === null) return;
454465
let body;
455466
try {
456467
body = JSON.parse(text || "{}");
@@ -538,7 +549,8 @@ export function createStandaloneApi({ service, store, config = {}, logger, setti
538549
// /api/dsh-mneme/maintenance/reclaim 共用 src/reclaim-http.js,两张皮只有鉴权
539550
// 与路径不同。这条曾经是整段复制的,于是面板侧修好之后这边还停在旧形态(#411)。
540551
if (req.method === "POST" && pathname === "/maintenance/reclaim") {
541-
void readBody(req).then((text) => {
552+
void readBody(req, res).then((text) => {
553+
if (text === null) return;
542554
const parsed = parseReclaimJson(text);
543555
if (!parsed.ok) {
544556
sendJson(res, 400, { error: parsed.error });
@@ -571,7 +583,8 @@ export function createStandaloneApi({ service, store, config = {}, logger, setti
571583
// 显式收 dir(插件没有工作区根目录概念,不猜路径);确定性解析零 LLM;
572584
// 幂等骑 saveWithDedupe 的 (type, title, scope) 去重 + _overwrite 刷新。
573585
if (req.method === "POST" && pathname === "/bootstrap") {
574-
void readBody(req).then((text) => {
586+
void readBody(req, res).then((text) => {
587+
if (text === null) return;
575588
let body;
576589
try {
577590
body = JSON.parse(text || "{}");

0 commit comments

Comments
 (0)