@modusensus/dsh-mneme 0.8.12 → 0.8.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/lib/api.js +2 -0
- package/lib/client.js +12 -1
- package/lib/config.js +19 -0
- package/lib/guide.js +13 -1
- package/lib/index.js +13 -5
- package/lib/sensitive-scan.js +209 -0
- package/lib/service.js +39 -12
- package/lib/settings.js +5 -1
- package/package.json +114 -114
- package/src/api.js +2 -0
- package/src/config.js +19 -0
- package/src/guide.js +13 -1
- package/src/index.js +13 -5
- package/src/sensitive-scan.js +209 -0
- package/src/service.js +39 -12
- package/src/settings.js +5 -1
- package/test/api.test.js +10 -2
- package/test/inject-pools.test.js +19 -0
- package/test/scope-strict.test.js +112 -0
- package/test/sensitive-scan.test.js +207 -0
- package/test/write-admission.test.js +115 -0
package/test/api.test.js
CHANGED
|
@@ -673,8 +673,9 @@ test("GET /api/dsh-mneme/features returns empty overrides and effective config d
|
|
|
673
673
|
// issue #24 块1 新增 graphAnchoringEnabled/graphSeedCap/graphCascadeDepth、
|
|
674
674
|
// 块2 新增 graphWeightEnabled/graphWeightDelta、块3 新增 graphInjectHint/graphInjectBudget、
|
|
675
675
|
// 块4 新增 graphPassiveConfirm、
|
|
676
|
-
// issue #339 新增 dreamMergeGuard
|
|
677
|
-
|
|
676
|
+
// issue #339 新增 dreamMergeGuard、
|
|
677
|
+
// issue #164 A2 新增 sensitiveScanEnabled(顶层扁平键,走 configFlagValue 默认分支))
|
|
678
|
+
assert.equal(Object.keys(data.effective).length, 59 + 3 + 2 + 1 + 2 + 2 + 2 + 1 + 1 + 1 + 2 + 1 + 2 + 1 + 3 + 2 + 2 + 1 + 1 + 1);
|
|
678
679
|
assert.equal(data.effective.dreamSkipInvalid, true);
|
|
679
680
|
assert.equal(data.effective.allowCrossTypeMerge, false);
|
|
680
681
|
assert.equal(data.effective.dreamMergeGuard, false);
|
|
@@ -697,6 +698,8 @@ test("GET /api/dsh-mneme/features returns empty overrides and effective config d
|
|
|
697
698
|
// #254:写入准入两个键都默认关(默认路径零行为变化),面板可逐项启停。
|
|
698
699
|
assert.equal(data.effective["writeAdmission.enabled"], false);
|
|
699
700
|
assert.equal(data.effective["writeAdmission.enforce"], false);
|
|
701
|
+
// #164 A2:密钥/PII 判据自己的闸,同样默认关(#332 的行为逐字节保留)。
|
|
702
|
+
assert.equal(data.effective.sensitiveScanEnabled, false);
|
|
700
703
|
// 新增字符串 / URL / 枚举键
|
|
701
704
|
assert.equal(data.effective.localEmbedModel, "Xenova/bge-small-zh-v1.5");
|
|
702
705
|
assert.equal(data.effective.ollamaBaseUrl, "http://localhost:11434");
|
|
@@ -751,6 +754,9 @@ test("PUT /api/dsh-mneme/features round-trips nested, string, url and enum keys"
|
|
|
751
754
|
// 这里往返一次就是钉这件事的(计数锁只钉数量,钉不住值)。
|
|
752
755
|
"writeAdmission.enabled": true,
|
|
753
756
|
"writeAdmission.enforce": true,
|
|
757
|
+
// #164 A2:顶层扁平键(不是对象子字段),与上面两个点号键走同一条往返;
|
|
758
|
+
// 它的默认值断言在计数锁那一条里,这里钉的是「PUT 存进去、effective 读得回」。
|
|
759
|
+
sensitiveScanEnabled: true,
|
|
754
760
|
embedProvider: "local",
|
|
755
761
|
ollamaBaseUrl: "http://127.0.0.1:11434",
|
|
756
762
|
dreamProvider: " siliconflow ",
|
|
@@ -765,6 +771,7 @@ test("PUT /api/dsh-mneme/features round-trips nested, string, url and enum keys"
|
|
|
765
771
|
"llmAudit.enabled": false,
|
|
766
772
|
"writeAdmission.enabled": true,
|
|
767
773
|
"writeAdmission.enforce": true,
|
|
774
|
+
sensitiveScanEnabled: true,
|
|
768
775
|
embedProvider: "local",
|
|
769
776
|
ollamaBaseUrl: "http://127.0.0.1:11434",
|
|
770
777
|
dreamProvider: "siliconflow",
|
|
@@ -775,6 +782,7 @@ test("PUT /api/dsh-mneme/features round-trips nested, string, url and enum keys"
|
|
|
775
782
|
assert.equal(data.effective["llmAudit.enabled"], false);
|
|
776
783
|
assert.equal(data.effective["writeAdmission.enabled"], true);
|
|
777
784
|
assert.equal(data.effective["writeAdmission.enforce"], true);
|
|
785
|
+
assert.equal(data.effective.sensitiveScanEnabled, true);
|
|
778
786
|
assert.equal(data.effective.embedProvider, "local");
|
|
779
787
|
assert.equal(data.effective.ollamaBaseUrl, "http://127.0.0.1:11434");
|
|
780
788
|
assert.equal(data.effective.dreamProvider, "siliconflow");
|
|
@@ -173,3 +173,22 @@ test("#249: capability guide extends only the judgement-heavy tool descriptions"
|
|
|
173
173
|
assert.ok(on.get(name).startsWith(off.get(name)), `${name}: guidance is appended, original text untouched`);
|
|
174
174
|
}
|
|
175
175
|
});
|
|
176
|
+
|
|
177
|
+
test("#249: memory_save guidance states the scope default in the safe direction", () => {
|
|
178
|
+
// #164 口径:scope 参数漏填的后果是单向的——未标注 = 处处可见(NULL 恒可见),
|
|
179
|
+
// 而显式 scope 会让记忆在离开他 scope 时被降权(A2 软隔离 ×0.5,保留可见)或直接
|
|
180
|
+
// 过滤(A3 strictScope,默认关)。所以这一句必须同时给出「什么时候标注」与「不确定
|
|
181
|
+
// 就都别填」,只写前半句会把模型推向「能填就填」,正好制造那个单向损失。
|
|
182
|
+
const store = createStore(":memory:");
|
|
183
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
184
|
+
const registered = [];
|
|
185
|
+
createTools({ tools: { register(def) { registered.push(def); return () => {}; } } }, service, { injectGuidanceEnabled: true }, null);
|
|
186
|
+
const text = registered.find((t) => t.name === "memory_save").description;
|
|
187
|
+
assert.ok(text.includes("declare workspace_scope / agent_scope"), "scope declaration rule is present");
|
|
188
|
+
assert.ok(text.includes("otherwise leave both out"), "the no-scope default is stated, not just the declare case");
|
|
189
|
+
// 措辞回归锁:默认配置(strictScope 关)下他 scope 只是降权可见,不是消失。写成
|
|
190
|
+
// 「静默消失」既是错的,也会造出本来不存在的隐私预期。
|
|
191
|
+
assert.ok(!text.includes("silently disappears"), "must not claim a narrowed memory disappears silently");
|
|
192
|
+
// 回归锁:这句话不能只落在总则里(工具描述才是常驻、零注入成本的那个承载位)。
|
|
193
|
+
assert.ok(!MEMORY_GUIDE_SECTION.includes("declare workspace_scope / agent_scope"), "scope rule stays a per-tool rule, not a sixth general rule");
|
|
194
|
+
});
|
|
@@ -229,3 +229,115 @@ test("memory_list filters explicit rows and keeps total consistent under strictS
|
|
|
229
229
|
assert.deepEqual(out2.items.map((m) => m.title), ["theirs"]);
|
|
230
230
|
assert.equal(out2.total, 1);
|
|
231
231
|
});
|
|
232
|
+
|
|
233
|
+
// --- entity: / attr: 前缀路的 scope 闸 ----------------------------------------
|
|
234
|
+
// 回归锁:这两条前缀路在 searchMemories 的入口就 return,早于融合池那道 strictScope
|
|
235
|
+
// 过滤,曾经整条绕过硬墙——显式他者 scope 的记忆用 `entity:` / `attr:` 就能原样读出,
|
|
236
|
+
// 而且是满分返回(连 A2 的 ×0.5 都没有)。所以除了「出局」,还要钉住「auto / 存量
|
|
237
|
+
// 他者保留」这条 A2 语义没有跟着塌掉。
|
|
238
|
+
|
|
239
|
+
/** 把若干记忆挂到同一个实体上。
|
|
240
|
+
* 属性键必须各不相同:同一 (实体, 键) 有时间轴语义,后写的一条会把前一条作废,
|
|
241
|
+
* 那样只有最后一条会被链接上(写这个测试时踩过,别改成同一个键)。 */
|
|
242
|
+
function linkToEntity(store, name, memories) {
|
|
243
|
+
const entity = store.createEntity({ name, type: "person" });
|
|
244
|
+
memories.forEach((mem, i) => {
|
|
245
|
+
store.saveAttr({ entity_id: entity.id, attr_key: `attr${i}`, attr_value: `value${i}`, memory_id: mem.id });
|
|
246
|
+
});
|
|
247
|
+
return entity;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** 五条覆盖各种 scope 来源的记忆,供下面两条用。 */
|
|
251
|
+
function saveScopeBattery(store) {
|
|
252
|
+
return {
|
|
253
|
+
global: store.save({ type: "project", title: "global", content: "x" }),
|
|
254
|
+
mine: store.save({ type: "project", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit" }),
|
|
255
|
+
theirs: store.save({ type: "project", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" }),
|
|
256
|
+
autoForeign: store.save({ type: "project", title: "auto", content: "x", agent_scope: "other", agent_scope_source: "auto" }),
|
|
257
|
+
legacyForeign: store.save({ type: "project", title: "legacy", content: "x", agent_scope: "other" })
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
test("searchMemories entity: prefix honours the strictScope hard filter", async () => {
|
|
262
|
+
const { store, service } = setup({ scopeEnabled: true, strictScope: true, entitySearchEnabled: true });
|
|
263
|
+
const rows = saveScopeBattery(store);
|
|
264
|
+
linkToEntity(store, "阿尔托", Object.values(rows));
|
|
265
|
+
|
|
266
|
+
const asMe = await service.searchMemories("entity:阿尔托", { scope: { agent_scope: "me", workspace_scope: null } });
|
|
267
|
+
assert.deepEqual(asMe.map((m) => m.title).sort(), ["auto", "global", "legacy", "mine"], "显式他者出局,auto / 存量他者按 A2 保留");
|
|
268
|
+
|
|
269
|
+
// 原主人自己查:显式那条必须还在(否则上面是「谁都搜不到」的假绿)。
|
|
270
|
+
const asOther = await service.searchMemories("entity:阿尔托", { scope: { agent_scope: "other", workspace_scope: null } });
|
|
271
|
+
assert.ok(asOther.some((m) => m.title === "theirs"), "命中当前 agent 的显式行必须可见");
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test("searchMemories attr: prefix honours the strictScope hard filter", async () => {
|
|
275
|
+
const { store, service } = setup({ scopeEnabled: true, strictScope: true, entitySearchEnabled: true });
|
|
276
|
+
const rows = saveScopeBattery(store);
|
|
277
|
+
// 每条挂到各自实体上、共用一个属性键:这样 attr:key=value 能一次覆盖全部五条
|
|
278
|
+
// (同实体同键会被时间轴作废,见 linkToEntity 的注释)。
|
|
279
|
+
Object.values(rows).forEach((mem, i) => {
|
|
280
|
+
const entity = store.createEntity({ name: `entity-${i}`, type: "person" });
|
|
281
|
+
store.saveAttr({ entity_id: entity.id, attr_key: "国籍", attr_value: "芬兰", memory_id: mem.id });
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
const asMe = await service.searchMemories("attr:国籍=芬兰", { scope: { agent_scope: "me", workspace_scope: null } });
|
|
285
|
+
assert.deepEqual(asMe.map((m) => m.title).sort(), ["auto", "global", "legacy", "mine"], "显式他者出局,auto / 存量他者按 A2 保留");
|
|
286
|
+
|
|
287
|
+
const asOther = await service.searchMemories("attr:国籍=芬兰", { scope: { agent_scope: "other", workspace_scope: null } });
|
|
288
|
+
assert.ok(asOther.some((m) => m.title === "theirs"), "命中当前 agent 的显式行必须可见");
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test("entity: / attr: 的 scope 闸在触达之前:出局的行不被回温,也不 bump 关联边", async () => {
|
|
292
|
+
// 闸门若只加在「返回前」,出局的行仍会先被 touchRecalled 摸一遍——一次越权检索
|
|
293
|
+
// 照样给它刷回温时钟、并在被动确认开启时 bump 它的关联边。所以这条钉的是次序。
|
|
294
|
+
const { store, service } = setup({
|
|
295
|
+
scopeEnabled: true, strictScope: true, entitySearchEnabled: true,
|
|
296
|
+
heatEnabled: true, graphWeightEnabled: true, graphPassiveConfirm: true, graphWeightDelta: 0.1
|
|
297
|
+
});
|
|
298
|
+
const theirs = store.save({ type: "project", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
|
|
299
|
+
const global = store.save({ type: "project", title: "global", content: "x" });
|
|
300
|
+
linkToEntity(store, "阿尔托", [theirs, global]);
|
|
301
|
+
|
|
302
|
+
const a = store.createEntity({ name: "A", type: "technology" });
|
|
303
|
+
const b = store.createEntity({ name: "B", type: "technology" });
|
|
304
|
+
for (const mem of [theirs, global]) {
|
|
305
|
+
store.saveRelation({ from_entity: a.id, to_entity: b.id, relation_type: "uses", memory_id: mem.id, source: "llm" });
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const rows = await service.searchMemories("entity:阿尔托", { scope: { agent_scope: "me", workspace_scope: null } });
|
|
309
|
+
assert.deepEqual(rows.map((m) => m.title), ["global"], "只剩可见那条");
|
|
310
|
+
|
|
311
|
+
// 正向对照:可见那条确实被触达了——否则下面两个「没被触达」可能是假绿。
|
|
312
|
+
assert.ok(store.getRelationsByMemory(global.id)[0].weight > 0.4, "可见行照常 bump 关联边");
|
|
313
|
+
assert.ok(store.getById(global.id).last_accessed_at, "可见行照常回温");
|
|
314
|
+
|
|
315
|
+
assert.equal(store.getRelationsByMemory(theirs.id)[0].weight, 0.4, "出局行不该 bump 关联边");
|
|
316
|
+
assert.equal(store.getById(theirs.id).last_accessed_at ?? null, null, "出局行不该被回温");
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
test("scope 闸先于 topK 截断:出局的候选不占名额(topK=1 仍有可见结果)", async () => {
|
|
320
|
+
// 回归锁(评审发现):先 slice 再 filter 的话,排在前面那条被闸掉的候选会占住唯一的
|
|
321
|
+
// 槽位,明明还有可见匹配却返回空数组。用 topK=1 把次序钉死——排序不动,只是闸门要插
|
|
322
|
+
// 在截断之前。
|
|
323
|
+
const me = { agent_scope: "me", workspace_scope: null };
|
|
324
|
+
|
|
325
|
+
const { store, service } = setup({ scopeEnabled: true, strictScope: true, entitySearchEnabled: true });
|
|
326
|
+
const theirsE = store.save({ type: "project", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
|
|
327
|
+
const mineE = store.save({ type: "project", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit" });
|
|
328
|
+
// 先挂 theirs、后挂 mine:前者排在候选前面,正好充当那个「占位」的候选。
|
|
329
|
+
linkToEntity(store, "阿尔托", [theirsE, mineE]);
|
|
330
|
+
const viaEntity = await service.searchMemories("entity:阿尔托", { topK: 1, scope: me });
|
|
331
|
+
assert.deepEqual(viaEntity.map((m) => m.title), ["mine"], "entity: 在 topK=1 下不该被出局候选挤空");
|
|
332
|
+
|
|
333
|
+
// attr: 同理——两条挂同一个属性键、各挂自己的实体,theirs 先写入。
|
|
334
|
+
const { store: s2, service: svc2 } = setup({ scopeEnabled: true, strictScope: true, entitySearchEnabled: true });
|
|
335
|
+
const theirsA = s2.save({ type: "project", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
|
|
336
|
+
const mineA = s2.save({ type: "project", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit" });
|
|
337
|
+
for (const [i, mem] of [theirsA, mineA].entries()) {
|
|
338
|
+
const entity = s2.createEntity({ name: `e${i}`, type: "person" });
|
|
339
|
+
s2.saveAttr({ entity_id: entity.id, attr_key: "国籍", attr_value: "芬兰", memory_id: mem.id });
|
|
340
|
+
}
|
|
341
|
+
const viaAttr = await svc2.searchMemories("attr:国籍=芬兰", { topK: 1, scope: me });
|
|
342
|
+
assert.deepEqual(viaAttr.map((m) => m.title), ["mine"], "attr: 在 topK=1 下不该被出局候选挤空");
|
|
343
|
+
});
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// #164 A2:写入边界的密钥 / PII 判据(src/sensitive-scan.js)。
|
|
2
|
+
//
|
|
3
|
+
// 这是「真判据」自己的验收,与 write-admission.test.js 里那组(拿参考扫描器测**闸门
|
|
4
|
+
// 接线**)分工不同:这边测判据本身,且第一条用例就是把 #332 配好的 26 条语料原样跑在
|
|
5
|
+
// 生产实现上——维护者 09-28 的验收条件就是这句「同一套负样本原样跑,抓错任何一条就
|
|
6
|
+
// 不算落地」,所以它放在这里而不是再抄一份语料。
|
|
7
|
+
//
|
|
8
|
+
// 参考扫描器 referenceScan 只服务闸门测;本文件刻意**不** import 它——生产判据与
|
|
9
|
+
// 参考实现是两份独立实现,同时跑过同一套语料才算把语料用足(对照着改就成了自证)。
|
|
10
|
+
import test from "node:test";
|
|
11
|
+
import assert from "node:assert/strict";
|
|
12
|
+
|
|
13
|
+
import { scanSensitive, createSensitiveScan } from "../src/sensitive-scan.js";
|
|
14
|
+
import {
|
|
15
|
+
SECRET_SAMPLES,
|
|
16
|
+
PII_SAMPLES,
|
|
17
|
+
POSITIVE_SAMPLES,
|
|
18
|
+
NEGATIVE_SAMPLES,
|
|
19
|
+
SAMPLE_VALUES
|
|
20
|
+
} from "./helpers/write-admission-samples.js";
|
|
21
|
+
|
|
22
|
+
// #164 A2 的语料里那几条凭据值走 helper 的拼接导出,别在测试里另抄一份字面量:
|
|
23
|
+
// GitHub 的 push protection 会按形状拦真实凭据(本 PR 实测:`sk_live_` 一条就够
|
|
24
|
+
// 拒推,helper 正是为这件事把值拆成两段拼的)。这里复用同一份,仓库文本里不留完整形状。
|
|
25
|
+
const STRIPE_KEY = SAMPLE_VALUES.stripeKey;
|
|
26
|
+
|
|
27
|
+
// 正样本那条 OpenAI key 从语料里取,不另抄字面量(同 STRIPE_KEY 的理由)。
|
|
28
|
+
const OPENAI_KEY = POSITIVE_SAMPLES.find((s) => s.id === "openai-key").content.match(/\S*sk-\S+/)[0];
|
|
29
|
+
/** `sk-proj-…` 前缀的变体:语料那条不带 proj-,这里补同样的正文长度。 */
|
|
30
|
+
const openaiProjKey = (body) => `sk-proj-${body}`;
|
|
31
|
+
const OPENAI_BODY = OPENAI_KEY.replace(/^.*?sk-(?:proj-)?/, "");
|
|
32
|
+
|
|
33
|
+
test("真判据:正样本一条都不漏放,且 kind 与语料声明一致", () => {
|
|
34
|
+
for (const sample of POSITIVE_SAMPLES) {
|
|
35
|
+
const text = `${sample.title}\n${sample.content}`;
|
|
36
|
+
const hit = scanSensitive(text);
|
|
37
|
+
assert.ok(hit, `${sample.id} 被漏放(${sample.why})`);
|
|
38
|
+
// kind 是审计行里的稳定判据键:它错了,「按 kind 看命中分布」这条路就断了。
|
|
39
|
+
// 同一个串可能被多条规则吃下(`sk_live_` 也像通用 `sk-`),所以顺序是判据的一部分。
|
|
40
|
+
assert.equal(hit.kind, sample.kind, `${sample.id} 的 kind 必须是语料声明的那个(规则顺序变了就会漂)`);
|
|
41
|
+
assert.equal(typeof hit.label, "string");
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("真判据:负样本一条都不误杀(抓错任何一条就不算落地)", () => {
|
|
46
|
+
for (const sample of NEGATIVE_SAMPLES) {
|
|
47
|
+
const hit = scanSensitive(`${sample.title}\n${sample.content}`);
|
|
48
|
+
assert.equal(hit, null, `${sample.id} 被误杀(${sample.why}):实际命中 ${hit?.kind}`);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("密钥与 PII 分两档,但都从同一条通道出来", () => {
|
|
53
|
+
// 分档的意义是「将来按类放行只改策略、不动判据」:kind 集合是策略的抓手,
|
|
54
|
+
// 所以这里断言两类都能被分辨,而不是把 PII 折进密钥那一档。
|
|
55
|
+
assert.ok(SECRET_SAMPLES.length >= 1 && PII_SAMPLES.length >= 1);
|
|
56
|
+
assert.equal(scanSensitive("password = \"Tr0ub4dor&3xKcd\"").kind, "assigned_secret");
|
|
57
|
+
assert.equal(scanSensitive("联系方式 zhang.wei@example.com").kind, "email");
|
|
58
|
+
assert.equal(scanSensitive("值班手机 13800138000").kind, "cn_mobile");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("只扫文本:路径与 id 这类元数据字段不进判据", () => {
|
|
62
|
+
// 元数据由系统生成,不是用户写进来的内容;扫它们只会引入假阳(一个文件名里
|
|
63
|
+
// `token` 这种词很常见)。契约是判据只看记忆的文本字段。
|
|
64
|
+
const scan = createSensitiveScan({ config: { sensitiveScanEnabled: true } });
|
|
65
|
+
assert.equal(scan({ title: "配置", content: "正常正文", id: SAMPLE_VALUES.githubPat, doc_path: STRIPE_KEY }), null);
|
|
66
|
+
// 反过来,正文里的凭据必须报——上面那条不是靠「什么都扫不到」通过的。
|
|
67
|
+
assert.ok(scan({ title: "配置", content: `正文里有 ${SAMPLE_VALUES.githubPat}` }));
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("tags 也扫:标签是用户输入的自由文本", () => {
|
|
71
|
+
const scan = createSensitiveScan({ config: { sensitiveScanEnabled: true } });
|
|
72
|
+
assert.equal(scan({ title: "正常标题", content: "正常正文", tags: ["正常"] }), null);
|
|
73
|
+
assert.equal(scan({ title: "正常标题", content: "正常正文", tags: [SAMPLE_VALUES.githubPat] })?.kind, "github_token");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("工厂:判据自己的闸关时连函数都不给(闸门侧行为与 #332 逐字段一致)", () => {
|
|
77
|
+
assert.equal(createSensitiveScan({ config: { sensitiveScanEnabled: false } }), null);
|
|
78
|
+
assert.equal(createSensitiveScan({ config: {} }), null);
|
|
79
|
+
assert.equal(createSensitiveScan({}), null, "没有 config 也不能默认开");
|
|
80
|
+
assert.equal(createSensitiveScan({ config: { sensitiveScanEnabled: "true" } }), null, "字符串 true 不是 true");
|
|
81
|
+
const scan = createSensitiveScan({ config: { sensitiveScanEnabled: true } });
|
|
82
|
+
assert.equal(typeof scan, "function");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("工厂不碰决策:命中只报 kind,拦不拦由闸门的 enforce 决定", () => {
|
|
86
|
+
// 判据与闸门分层(#164:默认仅告警、拦截 opt-in)——判据知道「这是什么」,
|
|
87
|
+
// 但不知道也不该知道「要不要拦」。返回值里没有 decision / enforced 这类字段,
|
|
88
|
+
// 就是这条分层的机器可判点。
|
|
89
|
+
const scan = createSensitiveScan({
|
|
90
|
+
config: { sensitiveScanEnabled: true, writeAdmission: { enabled: true, enforce: false } }
|
|
91
|
+
});
|
|
92
|
+
const hit = scan({ title: "调试", content: openaiProjKey(OPENAI_BODY) });
|
|
93
|
+
assert.deepEqual(Object.keys(hit).sort(), ["kind", "label"]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("不抛:坏输入返回 null,扫描器故障不会把写入变成不可用", () => {
|
|
97
|
+
assert.equal(scanSensitive(undefined), null);
|
|
98
|
+
assert.equal(scanSensitive(null), null);
|
|
99
|
+
assert.equal(scanSensitive(""), null);
|
|
100
|
+
assert.equal(scanSensitive(12345), null);
|
|
101
|
+
const scan = createSensitiveScan({ config: { sensitiveScanEnabled: true } });
|
|
102
|
+
assert.equal(scan(null), null);
|
|
103
|
+
assert.equal(scan({ title: 7, content: null, tags: "not-an-array" }), null);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("一处命中只报最严重的那条(deny 面记类别,不记条数)", () => {
|
|
107
|
+
// 一篇同时带 Stripe 密钥与邮箱的正文:报密钥那一档。反过来把 PII 排在前面
|
|
108
|
+
// 会让「这条命中过密钥」这个更严重的事实被邮箱盖掉。
|
|
109
|
+
const text = `邮箱 a@b.com,另外 ${STRIPE_KEY}`;
|
|
110
|
+
assert.equal(scanSensitive(text).kind, "stripe_key");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("语料之外的探针:漏放那一类锁一条(实现期实测出来的,不是假想)", () => {
|
|
114
|
+
// 不在 #332 的 26 条语料里,是拿真判据跑额外形状时发现的:AWS secret access key
|
|
115
|
+
// 没有前缀,`secret` 关键词规则又要求它后面紧跟赋值号,多词键
|
|
116
|
+
// (`AWS_SECRET_ACCESS_KEY=`)两条都够不着 → 补了专用的键名规则。
|
|
117
|
+
assert.equal(
|
|
118
|
+
scanSensitive("AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY").kind,
|
|
119
|
+
"aws_secret_key"
|
|
120
|
+
);
|
|
121
|
+
// 反向确认「值 + 赋值键」这条兜底规则还在工作,而且**不**看值的形状:`api_key =`
|
|
122
|
+
// 后面跟任何 8 位以上的值都报(判据不知道哪个值是真的)。
|
|
123
|
+
assert.equal(scanSensitive("api_key = sk-abcdefghijklmnopqrst").kind, "assigned_secret");
|
|
124
|
+
// 收紧后的开关键字仍然认 24 位以上的真 key(44 位正文,语料那条)。
|
|
125
|
+
assert.equal(scanSensitive(openaiProjKey(OPENAI_BODY)).kind, "openai_key");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// --- 合并后独立复审的三项加固 -------------------------------------------------
|
|
129
|
+
|
|
130
|
+
// 赋值句的键与值都在运行时拼装,别写成整串字面量:`.github/workflows/security.yml` 的
|
|
131
|
+
// gitleaks 是**全历史**扫描且没有 allowlist,而 generic-api-key 规则认的是「关键词 +
|
|
132
|
+
// 赋值号 + 高熵值」——一个 16 位、字符全不重复的十六进制串熵值就有 4.0,高于它 3.5 的
|
|
133
|
+
// 阈值,直接落进源码就会被判成真凭据(本批初稿实测:两条各报一次)。
|
|
134
|
+
// 与 helpers/write-admission-samples.js 里那几条凭据值同一个理由,那边也是拼的。
|
|
135
|
+
const envLine = (key, value, prefix = "") => `${prefix}${key}=${value}`;
|
|
136
|
+
/** 一眼假的低熵值:不是占位符(占位符会被守卫挡掉,那样就测不到这条规则了)。 */
|
|
137
|
+
const FAKE_LITERAL = ["super", "sekrit", "000"].join("-");
|
|
138
|
+
|
|
139
|
+
test("左边界放开 `_`:环境变量名形态的赋值也要抓(原先整类漏放)", () => {
|
|
140
|
+
// 回归锁:赋值型规则的左边界原为 `[^A-Za-z0-9_]`,把下划线也当成了标识符内部字符,
|
|
141
|
+
// 于是只有恰好落在行首的 `API_KEY=` 能中,而 `DB_PASSWORD=` / `MY_API_KEY=` /
|
|
142
|
+
// `MYSQL_PASSWORD=` 这种「前缀_关键词」的环境变量名整类漏放——配置片段与 .env 正文
|
|
143
|
+
// 恰恰是最常见的落库形态。放宽后 #332 的 26 条语料仍全绿,说明原写法不是语料换来
|
|
144
|
+
// 的取舍,是白丢的。
|
|
145
|
+
for (const [key, prefix] of [
|
|
146
|
+
["DB_PASSWORD", ""],
|
|
147
|
+
["MY_API_KEY", ""],
|
|
148
|
+
["MYSQL_PASSWORD", ""],
|
|
149
|
+
["GITHUB_TOKEN", "export "] // 带 shell 前缀的写法
|
|
150
|
+
]) {
|
|
151
|
+
const line = envLine(key, FAKE_LITERAL, prefix);
|
|
152
|
+
assert.equal(scanSensitive(line)?.kind, "assigned_secret", `${key} 形态的赋值不该漏放`);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("放宽左边界没有放走占位符与间接引用(守卫仍在管那一半)", () => {
|
|
157
|
+
// 与上一条成对:放宽的是「关键词前面能不能是 `_`」,不是「右边的值算不算字面量」。
|
|
158
|
+
// 后者仍归占位符守卫——下面三条正是负样本群里「有敏感词但没有值」的形态。
|
|
159
|
+
// 这些值同样拼装,理由见本节开头(`${...}` 这类串本身也贴着"像凭据"的形状)。
|
|
160
|
+
const placeholders = [
|
|
161
|
+
["DB_PASSWORD", ["$", "{DB_PASSWORD}"].join("")],
|
|
162
|
+
["MY_API_KEY", ["<", "your-key-here", ">"].join("")],
|
|
163
|
+
["DB_PASSWORD", ["process", ".env.DB_PASSWORD"].join("")]
|
|
164
|
+
];
|
|
165
|
+
for (const [key, value] of placeholders) {
|
|
166
|
+
const line = envLine(key, value);
|
|
167
|
+
assert.equal(scanSensitive(line), null, `${key} 形态的占位 / 间接引用不该报`);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test("身份证档加校验位:位数对但校验不过的 18 位数字串不报", () => {
|
|
172
|
+
// 回归锁:原先只有银行卡档有 Luhn、身份证档只看长度,结果是 18 位纯数字(订单号 /
|
|
173
|
+
// 内部编号 / 拼接时间戳)先被身份证规则命中,等不到银行卡那条的校验。补 GB 11643
|
|
174
|
+
// MOD 11-2 之后两侧都干净:校验不过的不报,语料里那条真形状照报。
|
|
175
|
+
assert.equal(scanSensitive("内部编号 123456789012345678 已登记"), null, "校验不过的 18 位不该报");
|
|
176
|
+
assert.equal(scanSensitive("内部编号 987654321098765432 已登记"), null);
|
|
177
|
+
assert.equal(scanSensitive("身份证 11010519491231002X 已核验")?.kind, "cn_id_card");
|
|
178
|
+
// 小写 x 走同一张校验表(语料只覆盖了大写 X)。
|
|
179
|
+
assert.equal(scanSensitive("身份证 11010519491231002x 已核验")?.kind, "cn_id_card");
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("email 的 local part 上界恰好卡在 RFC 5321 的 64", () => {
|
|
183
|
+
// 上界不是随手取的数:64 是 RFC 5321 给 local part 的上限,所以「长于 64 不认」
|
|
184
|
+
// 这条边界必须是精确的——松一位会让回溯面失控,紧一位会漏掉合法邮箱。
|
|
185
|
+
const at = (n) => `${"a".repeat(n)}@example.com`;
|
|
186
|
+
assert.ok(scanSensitive(at(64)), "64 位的 local part 是合法的,必须认");
|
|
187
|
+
assert.equal(scanSensitive(at(65)), null, "65 位超出 RFC 上限,不认(换取回溯有界的代价)");
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test("长文本上的回溯有上界:写阻塞类回归的计时护栏", () => {
|
|
191
|
+
// 回归锁:email 的 local part 与 connection_string 的 scheme 都作用在含 `.` 的字符
|
|
192
|
+
// 类上,缺上界时每个起点都要一路重扫到结尾才失败 → O(n²)。未加上界时实测:34KB
|
|
193
|
+
// 点分链 0.6 秒、120KB 对抗串 23.5 秒(email 18.6s + 连接串 4.1s);而本判据在写入
|
|
194
|
+
// 路径上同步跑,等于把写入卡死。加上界后同输入约 60ms。
|
|
195
|
+
// 预算取 2 秒:比修复前小一个数量级(12× 安全边),又比修复后大 30 倍,不至于像
|
|
196
|
+
// 50ms 级断言那样被 CI 抖动误伤——它抓的是「上界被拿掉」这类数量级回归,不是微优化。
|
|
197
|
+
const inputs = [
|
|
198
|
+
"a.".repeat(30000) + "@" + "b.".repeat(30000) + "!",
|
|
199
|
+
"com.example.pkg.".repeat(10000) + "@"
|
|
200
|
+
];
|
|
201
|
+
for (const text of inputs) {
|
|
202
|
+
const started = process.hrtime.bigint();
|
|
203
|
+
scanSensitive(text);
|
|
204
|
+
const ms = Number(process.hrtime.bigint() - started) / 1e6;
|
|
205
|
+
assert.ok(ms < 2000, `${text.length} 字符耗时 ${ms.toFixed(1)}ms,超 2000ms 预算(回溯上界被破坏?)`);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
@@ -17,6 +17,12 @@ import {
|
|
|
17
17
|
ADMISSION_TRIGGER_SOURCE
|
|
18
18
|
} from "../src/write-admission.js";
|
|
19
19
|
import { POSITIVE_SAMPLES, NEGATIVE_SAMPLES, SAMPLE_VALUES, referenceScan } from "./helpers/write-admission-samples.js";
|
|
20
|
+
// #164 A2 的真判据 + 真配置:这一组测的是**接线**(index.js 那条路径的等价物),
|
|
21
|
+
// 判据自己的验收在 test/sensitive-scan.test.js。config 用真 schema 而不是手搓对象,
|
|
22
|
+
// 钉住「键名/嵌套形状与生产一致」——手搓的 `{ sensitiveScanEnabled: true }` 在键名
|
|
23
|
+
// 写错时照样过,真 config 才会红。
|
|
24
|
+
import { Config } from "../src/config.js";
|
|
25
|
+
import { createSensitiveScan } from "../src/sensitive-scan.js";
|
|
20
26
|
|
|
21
27
|
// #254 写入准入(第一阶段:只计量,不拦截)。
|
|
22
28
|
// 本批次没有阈值、没有拦截分支,验收看两件事:默认路径零行为变化(无会话身份的
|
|
@@ -551,3 +557,112 @@ test("接线:被拒时 memory_save 返回 action=denied 与可行动的 reason
|
|
|
551
557
|
assert.ok(ok.id, "正常路径仍然返回 id");
|
|
552
558
|
});
|
|
553
559
|
|
|
560
|
+
// --- #164 A2 接线:判据自己的闸 × 闸门的 enforce -------------------------------
|
|
561
|
+
// 这一组是 index.js 那条装配路径的等价物:真 config(Config({}))+ createSensitiveScan
|
|
562
|
+
// 工厂 + 写入准入。三层开关的语义由此钉住——判据关(默认)不参与、判据开而 enforce 关
|
|
563
|
+
// 只留审计、两个都开才拦。判据本身的假阳/漏放验收在 test/sensitive-scan.test.js。
|
|
564
|
+
|
|
565
|
+
/** 用真 `Config` 解析默认值,再叠加测试要覆盖的那几个键。 */
|
|
566
|
+
function a2Store(overrides = {}) {
|
|
567
|
+
const cfg = Config(overrides);
|
|
568
|
+
const store = createStore(":memory:");
|
|
569
|
+
const writeAdmission = createWriteAdmission({
|
|
570
|
+
store,
|
|
571
|
+
config: cfg,
|
|
572
|
+
sensitiveScan: createSensitiveScan({ config: cfg })
|
|
573
|
+
});
|
|
574
|
+
const service = createService({ store, mirror: null, config: cfg, writeAdmission });
|
|
575
|
+
return { store, service, cfg };
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const SECRET_ROW = {
|
|
579
|
+
type: "project",
|
|
580
|
+
title: "调试记录",
|
|
581
|
+
content: `临时代码里贴了 ${SAMPLE_VALUES.githubPat},回头删掉`
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
test("A2 默认关:写有密钥的行照常落库,且连拒绝面都不该出现", () => {
|
|
585
|
+
const { store, service } = a2Store();
|
|
586
|
+
const result = service.saveWithDedupe({ ...SECRET_ROW, _sessionKey: "s" });
|
|
587
|
+
assert.equal(result.action, "created", "判据默认关时这条路径与 #332 逐字段一致");
|
|
588
|
+
assert.equal(rowCount(store), 1);
|
|
589
|
+
// metadata 里不该多出任何 deny/decision 键——「默认关零行为变化」是验收第 1 条,
|
|
590
|
+
// 多一个恒为 allow 的键就把它从事实变成需要解释的说法(同上面 #254 那条)。
|
|
591
|
+
const row = admissionRows(store, "s")[0];
|
|
592
|
+
assert.equal(row.metadata.deny, undefined);
|
|
593
|
+
assert.equal(row.metadata.decision, undefined);
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
test("A2 观察档:判据开、enforce 关 → 写入照常,审计行带 kind 与 enforced=false", () => {
|
|
597
|
+
const { store, service } = a2Store({ sensitiveScanEnabled: true, writeAdmission: { enabled: true } });
|
|
598
|
+
const result = service.saveWithDedupe({ ...SECRET_ROW, _sessionKey: "s" });
|
|
599
|
+
assert.equal(result.action, "created", "#164 口径:默认仅告警");
|
|
600
|
+
assert.equal(rowCount(store), 1);
|
|
601
|
+
const deny = admissionRows(store, "s")[0].metadata.deny;
|
|
602
|
+
assert.equal(deny.reason, "sensitive");
|
|
603
|
+
assert.equal(deny.kind, "github_token");
|
|
604
|
+
assert.equal(deny.enforced, false, "仅告警档的指纹:deny 非空、enforced=false、写入仍发生");
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
test("A2 拦截档:判据开 + enforce 开 → 拒了、没落库、审计标 enforced", () => {
|
|
608
|
+
const { store, service } = a2Store({
|
|
609
|
+
sensitiveScanEnabled: true,
|
|
610
|
+
writeAdmission: { enabled: true, enforce: true }
|
|
611
|
+
});
|
|
612
|
+
const result = service.saveWithDedupe({ ...SECRET_ROW, _sessionKey: "s" });
|
|
613
|
+
assert.equal(result.action, "denied");
|
|
614
|
+
assert.equal(result.reason, "sensitive");
|
|
615
|
+
assert.equal(rowCount(store), 0, "被拒的写入一条都不该落库");
|
|
616
|
+
const deny = admissionRows(store, "s")[0].metadata.deny;
|
|
617
|
+
assert.equal(deny.kind, "github_token");
|
|
618
|
+
assert.equal(deny.enforced, true);
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
test("A2 与第 1 级空白判据互不依赖:只开 A2 不会把空白写入拦下", () => {
|
|
622
|
+
// 两个键是两批判据的闸(空白/噪声 vs 密钥/PII),分开是为了能单独观察 A2 的
|
|
623
|
+
// 命中分布——绑在一个开关上就没法只看这一类的假阳率。
|
|
624
|
+
const { store, service } = a2Store({ sensitiveScanEnabled: true });
|
|
625
|
+
const blank = service.saveWithDedupe({ _sessionKey: "s", type: "project", title: "...", content: "" });
|
|
626
|
+
assert.equal(blank.action, "created", "A2 开着不等于第 1 级判据也开");
|
|
627
|
+
assert.equal(admissionRows(store, "s")[0].metadata.deny, undefined);
|
|
628
|
+
// 反过来:只开第 1 级(writeAdmission.enabled)、A2 关,密钥行照常落库。
|
|
629
|
+
const { store: s2, service: svc2 } = a2Store({ writeAdmission: { enabled: true } });
|
|
630
|
+
assert.equal(svc2.saveWithDedupe({ ...SECRET_ROW, _sessionKey: "s" }).action, "created");
|
|
631
|
+
assert.equal(s2.list({ limit: 10 }).length, 1);
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
test("A2 跑在闸门里:只开 sensitiveScanEnabled(闸门关)时扫描器根本不参与", () => {
|
|
635
|
+
// 判据的唯一调用点是 write-admission 的 firstLevelHit,而闸门要
|
|
636
|
+
// writeAdmission.enabled 打开才走第 1 级判据。所以「本键开着」不等于「会扫」——
|
|
637
|
+
// 配置注释与面板文案都按这条写,否则用户以为单开本键就能拿到 A2 的命中分布。
|
|
638
|
+
const { store, service } = a2Store({ sensitiveScanEnabled: true });
|
|
639
|
+
const result = service.saveWithDedupe({ ...SECRET_ROW, _sessionKey: "s" });
|
|
640
|
+
assert.equal(result.action, "created", "闸门没开时 A2 不参与判定");
|
|
641
|
+
assert.equal(rowCount(store), 1, "写入照常落库");
|
|
642
|
+
const rows = admissionRows(store, "s");
|
|
643
|
+
assert.ok(rows.length > 0, "闸门仍走计量路径(去重 / g2 不受第 1 级开关影响)");
|
|
644
|
+
assert.ok(rows.every((row) => row.metadata.deny === undefined), "一条 deny 审计都没有:第 1 级判据整体没跑");
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
test("A2 负样本在真装配下一条都不误杀(含敏感词但没有值)", () => {
|
|
648
|
+
const { store, service } = a2Store({
|
|
649
|
+
sensitiveScanEnabled: true,
|
|
650
|
+
writeAdmission: { enabled: true, enforce: true }
|
|
651
|
+
});
|
|
652
|
+
for (const sample of NEGATIVE_SAMPLES) {
|
|
653
|
+
const result = service.saveWithDedupe({
|
|
654
|
+
_sessionKey: `sess-${sample.id}`,
|
|
655
|
+
type: "project",
|
|
656
|
+
title: sample.title,
|
|
657
|
+
content: sample.content
|
|
658
|
+
});
|
|
659
|
+
assert.equal(result.action, "created", `${sample.id} 被误杀(${sample.why})`);
|
|
660
|
+
}
|
|
661
|
+
assert.equal(rowCount(store), NEGATIVE_SAMPLES.length);
|
|
662
|
+
for (const sample of NEGATIVE_SAMPLES) {
|
|
663
|
+
for (const row of admissionRows(store, `sess-${sample.id}`)) {
|
|
664
|
+
assert.equal(row.metadata.deny, undefined, `${sample.id} 不该带任何拒绝面`);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
});
|
|
668
|
+
|