Skip to content

Commit 780e656

Browse files
authored
Merge pull request #462 from slow-stack/fix/security-audit-p0
安全审计第一批:/export 与 GET /external-api 补鉴权、readBody 超限回 413、读面上界、SECURITY.md 与 CONFIGURATION.md 改成实况、osv 改为拦、npm provenance、禁止 Co-Authored-By。CI 十项全绿,CodeRabbit 意见已逐条处理。
2 parents bbd0abc + f32ed48 commit 780e656

20 files changed

Lines changed: 705 additions & 123 deletions

‎.github/workflows/ci.yml‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ name: CI
77
# 3. Codecov 只在 ubuntu+node24 一个分片跑一次(覆盖率无需每个组合都传)。
88
# 4. node_modules 缓存(cache-dependency-path 指向子目录的 lockfile)。
99
# 5. 宿主版本兼容腿 test-host-legacy:peer 声明的 0.1.x 两段各真跑一遍(见该作业注释)。
10-
# ⚠️ 需要 package.json 配合一处小改(跨平台通配符):
11-
# dsh-mneme/package.json 的 "test": "node --test test/*.test.js" 在 Windows 的 cmd 下
12-
# 不展开 glob。建议改为 "test": "node --test test/"(目录模式,全平台等价),
13-
# "test:coverage" 同理(c8 node --test test/)。改动只影响脚本调用方式,用例不变。
10+
# 关于 test 脚本的跨平台通配符(2026-10 实测,别按旧建议改):
11+
# dsh-mneme/package.json 的 "test": "node --test test/*.test.js" 在 Windows cmd 下
12+
# 不展开 glob,但引号会把字面量交给 node,由 node 自己的 glob 展开(Node 22/24 皆可,
13+
# 本仓 windows 腿一直绿)。反过来,改成 "node --test test/" 在 Windows 上实测报
14+
# MODULE_NOT_FOUND(Node v24.15.0),所以**不要**那样改。
1415

1516
on:
1617
push:

‎.github/workflows/release.yml‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ on:
2929
type: boolean
3030
default: false
3131

32+
# 权限按 job 发放,工作流级一律只读:
33+
# publish → id-token: write(npm provenance 走 OIDC,不需要长期 token)
34+
# release → contents: write(建/改 GitHub Release)
35+
# verify/test/notify 只读代码。工作流级给写权限等于把它们全变成可被依赖链
36+
# 利用的提权面(CWE-250)。
3237
permissions:
33-
contents: write
38+
contents: read
3439

3540
jobs:
3641
# ── 1. 版本号管理:tag ↔ package.json ↔ CHANGELOG 三方一致 ──────────────
@@ -106,6 +111,11 @@ jobs:
106111
needs: [verify, test]
107112
if: github.event_name == 'push' || inputs.dry_run != true
108113
runs-on: ubuntu-latest
114+
# npm provenance 用 OIDC 而不是长期 token 签名:--provenance 需要 id-token。
115+
# 挂在 job 上而不是 workflow 上,别的 job 拿不到这个能力。
116+
permissions:
117+
contents: read
118+
id-token: write
109119
steps:
110120
- uses: actions/checkout@v4
111121

@@ -128,7 +138,9 @@ jobs:
128138
if npm view "@modusensus/dsh-mneme@${V}" version >/dev/null 2>&1; then
129139
echo "v${V} already published on registry — skipping"
130140
else
131-
npm publish --access public
141+
# --provenance:把 tarball 与这次 commit / workflow 绑成可验证的 attestation,
142+
# 消费者能确认「这个版本确实从这条 commit 发出来」,而不是相信 registry 上的名字。
143+
npm publish --access public --provenance
132144
fi
133145
env:
134146
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -154,6 +166,9 @@ jobs:
154166
needs: [verify, test, publish]
155167
if: github.event_name == 'push' || inputs.dry_run != true
156168
runs-on: ubuntu-latest
169+
# 建/改 GitHub Release 要写权限,只有这个 job 需要。
170+
permissions:
171+
contents: write
157172
steps:
158173
- uses: actions/checkout@v4
159174

‎.github/workflows/security.yml‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
name: Security
22

3-
# 安全扫描:只做「注释级提醒」不拦合并(跑几周没误报再收紧为 fail)。
3+
# 安全扫描的姿态(2026-10 审计后改口径):gitleaks 与 dependency-review 一红,
4+
# job 就是红的;但「红」≠「拦合并」——只有进了 main 的 required status checks
5+
# 才会真的挡住 PR。这条区别写在注释里而不是只靠印象:本轮把两者加进 required
6+
# checks 之前,任何一次安全告警都只是标签页里的一条记录。
7+
# osv-scanner 现在也 fail-on-vuln=true(lockfile 有已知漏洞即红),不再只当提醒。
48
# 组成:
59
# 1. gitleaks —— 密钥泄露扫描(NPM_TOKEN 等一旦进历史就完了,这是最便宜的保险)
610
# 2. dependency-review —— PR 依赖 diff:新引入依赖的已知漏洞 / 投毒包举报(OSV)
@@ -40,7 +44,8 @@ jobs:
4044
with:
4145
fail-on-severity: moderate
4246

43-
# 官方 reusable workflow:扫 lockfile、SARIF 传 Security 标签页;fail-on-vuln=false = 纯提醒
47+
# 官方 reusable workflow:扫 lockfile、SARIF 传 Security 标签页;fail-on-vuln=true = 有已知漏洞即红
48+
# (2026-10 审计前它是 false,只上传 SARIF 不拦;lockfile 当时 0 漏洞,所以打开没有存量噪音)
4449
osv-scanner:
4550
if: github.event_name == 'push'
4651
permissions:
@@ -49,6 +54,6 @@ jobs:
4954
security-events: write
5055
uses: google/osv-scanner-action/.github/workflows/[email protected]
5156
with:
52-
fail-on-vuln: false
57+
fail-on-vuln: true
5358
scan-args: |-
5459
-r ./dsh-mneme

‎AGENTS.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ dsh-mneme 是 DSH 宿主的记忆插件(蒸馏 / 注入 / 检索 / 巩固 / sc
2424
- `lib/` 是 sync 产物(见上):各自树内同步,`npm run sync` **只在准备提交前跑一次**,别当保存键用;
2525
- 共享资源同一时刻只有一个写入者:`npm install`、`npm publish`(且只在 `dsh-mneme/` 内执行;发布走 GitHub Actions 的 release.yml,不在本机跑)、插件 relink——**被 link 的那棵树才是「活」的**;
2626
- 不 `amend` / force-push 别人的提交,不把别人未提交的改动 `stash` 走;
27-
- **提交不得有 AI 署名**(无 `Co-Authored-By`、无 "Generated with")——见 [CONTRIBUTING](CONTRIBUTING.md);
27+
- **提交不得有 AI 署名**,且**任何 `Co-Authored-By` trailer 都不许**(不管它指向人、bot 还是 AI 工具),也不许 "Generated with" 之类的工具署名。规则自 2026-09-19 生效,此前的历史提交不追溯、不改写——见 [CONTRIBUTING](CONTRIBUTING.md);
2828
- **本机私有信息不进公开文件**:车道分配、端口占用、记忆库路径这类只对某台机器成立的东西,放不进库的本地板(`.worktrees/coordination.md`)或 `.git/info/exclude`;本文件只放对所有人都成立的纪律(定位见开头:薄入口);
2929
- 两个宿主都加载本插件时:`memoryDir` 可共用(`src/store.js` 的 `busy_timeout` + WAL 已为多进程就绪),但 `externalApiEnabled`(默认端口 8790)与 `autoDream` 后台任务**只能一边开**,否则 EADDRINUSE + 重复做梦(镜像/审计双写)。独立服务(`dsh-mneme-serve`)同理:与 DSH 外部访问抢同一端口、**二选一**;它第一期无 LLM,巩固结构性只在宿主侧,不构成第二个做梦者。
3030

@@ -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;第一阶段只计量不拦截,测量点落 `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

‎CONTRIBUTING.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ Common scripts (all run under `dsh-mneme/`):
109109
- Commits whose author email resolves to **someone else's GitHub account**, or that cannot be linked to you at all, **will not be merged**: unattributed code means nobody owns the responsibility when review questions or regressions arrive.
110110
- You are expected to participate in review and answer questions about your own code — attribution is what makes that possible.
111111

112+
**No co-author trailers.** A commit message must not carry `Co-Authored-By:` at all — not for a person, not for a bot, not for an AI tool — and no `Generated with …` tool signature either. The author line is the only attribution mechanism here.
113+
114+
> Effective 2026-09-19. Earlier history is **not** rewritten: a handful of pre-rule commits carry `Co-Authored-By: Claude` trailers, and they stay as they are — rewriting public history to satisfy a later rule is worse than the trailers themselves.
115+
112116
---
113117

114118
## Contributor Workflow: Issue → Claim → PR

0 commit comments

Comments
 (0)