背景(2026-09-10 mc P0 事故):某轮蒸馏把含真 Tailscale 预授权 key 的原始对话
当事实入库 → 知识图谱按其文本建实体 → ObsidianSyncer 用实体标题当文件名/正文,
把 tskey-auth-… 落成可读镜像文件,又被 git 跟踪并推送 Gitea。
本提交在三个写入边界统一加脱敏。
新增 internal/redact(唯一真源):
- SecretPattern 覆盖 sk- / tskey- / github_pat_ / ghp_ / nvapi- / AKIA
- Go RE2 不支持 lookbehind,故"前一字符是否属于更长标识符"手工判定(isTokenByte)
- sk- 段只吃 [A-Za-z0-9],避免误伤 URL 里的 task-driven 片段(事故中的假阳性来源)
- RedactSecrets 幂等;ContainsSecret 供"直接丢弃"场景用
四处接入:
- distill/quality.go IsQualityFact 判定前脱敏 + FilterQualityFacts 返回值脱敏
- governance/graph_auto.go cleanEntityName 命中→空串;UpdateFromDistill 只对有效实体
建节点/建边;extractEntitiesFromText 过滤;含凭证的 fact 不建节点
- api/routes/obsidian.go sanitizeFilename 入参脱敏 + PushToObsidian 镜像正文脱敏
- api/server.go 蒸馏直写记忆库路径(绕过 FilterQualityFacts 的那条)同步脱敏
测试:13 条新用例(redact 5 / distill 3 / governance 4 / routes 1),
含 URL 假阳性回归与幂等性断言。全部使用构造的假密钥,不含任何真实凭证。
证据:go build ./... OK;
go test -count=1 ./internal/redact/ ./internal/distill/ ./internal/governance/ ./internal/api/routes/ 全绿。
顺带修既有 test 腐化(否则 governance 包根本无法编译测试,非本任务引入):
governance_test.go isContradiction→IsContradiction、Navigate 补 relFilter 参数;
bench_test.go 同。仅测试文件,无生产行为改动。
81 lines
2.7 KiB
Go
81 lines
2.7 KiB
Go
// 织忆 MemoryWeave — 凭证脱敏单元测试
|
|
// 2026-09-10 新增(mc P0 事故):锁死「密钥不得进记忆/图谱/Obsidian 镜像」这个回归。
|
|
package redact
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// 测试用假密钥(非真实凭证,按各平台形状构造)
|
|
const (
|
|
fakeTailscale = "tskey-auth-kTESTONLY0000000000000000000000000000000000000000000000000" // 61 位
|
|
fakeOpenAI = "sk-TESTONLY000000000000000000000000000" // 48 位
|
|
fakeGithubPAT = "github_pat_11TESTONLY0000000000000000000000000000000000000000000000000000000000000000"
|
|
fakeNvapi = "nvapi-TESTONLY0000000000000000000000000000"
|
|
)
|
|
|
|
func TestRedactSecrets_KnownShapes(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
in string
|
|
}{
|
|
{"tailscale", "配置 tailscale up --authkey " + fakeTailscale + " 完成"},
|
|
{"openai", "OPENAI_API_KEY=" + fakeOpenAI},
|
|
{"github_pat", "GITHUB_PERSONAL_ACCESS_TOKEN: " + fakeGithubPAT},
|
|
{"nvapi", "key=" + fakeNvapi},
|
|
{"aws", "AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE"},
|
|
}
|
|
for _, tc := range cases {
|
|
got := RedactSecrets(tc.in)
|
|
if strings.Contains(got, "<REDACTED-secret>") != true {
|
|
t.Errorf("%s: 未脱敏: %q", tc.name, got)
|
|
}
|
|
}
|
|
// 脱敏后不得再残留任何形状
|
|
for _, tc := range cases {
|
|
if ContainsSecret(RedactSecrets(tc.in)) {
|
|
t.Errorf("%s: 脱敏后仍能匹配到凭证形状", tc.name)
|
|
}
|
|
}
|
|
}
|
|
|
|
// URL / 普通连字符词里的 "sk-" 片段不得误伤(事故里 false positive 的来源)
|
|
func TestRedactSecrets_NoFalsePositiveOnURL(t *testing.T) {
|
|
in := "参考 https://yoheinakajima.com/task-driven-autonomous-agents 与 babybeeagi-task-xxx 的说明"
|
|
if got := RedactSecrets(in); got != in {
|
|
t.Errorf("URL 片段被误脱敏:\n got=%q\nwant=%q", got, in)
|
|
}
|
|
if ContainsSecret(in) {
|
|
t.Error("ContainsSecret 对 URL 片段误报")
|
|
}
|
|
}
|
|
|
|
// 嵌在更长标识符里的假命中不替换(前一位是单词字符)
|
|
func TestRedactSecrets_EmbeddedTokenIgnored(t *testing.T) {
|
|
in := "字段名 xxxkey_sk-TESTONLY0000000000000000000000000001234 不是凭证"
|
|
if got := RedactSecrets(in); got != in {
|
|
t.Errorf("嵌在标识符里的 token 被误脱敏: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestRedactSecrets_IdempotentAndPlainTextUntouched(t *testing.T) {
|
|
plain := "牧尘喜欢结论先行,不要废话科普。"
|
|
if got := RedactSecrets(plain); got != plain {
|
|
t.Errorf("纯文本被改动: %q", got)
|
|
}
|
|
once := RedactSecrets("key=" + fakeOpenAI)
|
|
if twice := RedactSecrets(once); twice != once {
|
|
t.Errorf("脱敏不幂等: %q → %q", once, twice)
|
|
}
|
|
}
|
|
|
|
func TestContainsSecret(t *testing.T) {
|
|
if !ContainsSecret("authkey " + fakeTailscale) {
|
|
t.Error("真实形状应被判为含凭证")
|
|
}
|
|
if ContainsSecret("普通的记忆内容,没有凭证") {
|
|
t.Error("普通内容误报")
|
|
}
|
|
}
|