Skip to content

Commit 82faa1b

Browse files
authored
Merge pull request #356 from slow-stack/fix/a2-sensitive-scan-hardening
fix(admission): A2 判据三处加固——回溯上界 / 环境变量名漏放 / 身份证校验位
2 parents 68aec84 + 398d49a commit 82faa1b

6 files changed

Lines changed: 161 additions & 14 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-3E63DD?style=flat-square" alt="license"></a>
1111
<a href="https://github.com/slow-stack/mneme/actions"><img src="https://img.shields.io/github/actions/workflow/status/slow-stack/mneme/ci.yml?style=flat-square&label=CI" alt="CI"></a>
1212
<a href="https://nodejs.org"><img src="https://img.shields.io/badge/node-22%2B-3E63DD?style=flat-square&logo=nodedotjs&logoColor=white" alt="node"></a>
13-
<a href="https://github.com/slow-stack/mneme"><img src="https://img.shields.io/badge/tests-1497%20passed-3E63DD?style=flat-square" alt="tests"></a>
13+
<a href="https://github.com/slow-stack/mneme"><img src="https://img.shields.io/badge/tests-1519%20passed-3E63DD?style=flat-square" alt="tests"></a>
1414
<a href="https://codecov.io/gh/slow-stack/mneme"><img src="https://img.shields.io/codecov/c/github/slow-stack/mneme/main?style=flat-square" alt="coverage"></a>
1515
<a href="https://github.com/awesome-dsh-plugin/awesome-dsh-plugin"><img src="https://awesome-dsh-plugin.com/badge.svg" alt="Awesome"></a>
1616
</p>
@@ -183,7 +183,7 @@ dsh web
183183

184184
```bash
185185
cd dsh-mneme && npm install
186-
npm test # 1497 个测试
186+
npm test # 1519 个测试
187187
npm run stress # 三轴线压测
188188
npm run sync # src → lib 同步
189189
```
@@ -366,7 +366,7 @@ The plugin ships a zero-dependency stdio MCP server (standalone npm package **`m
366366

367367
```bash
368368
cd dsh-mneme && npm install
369-
npm test # 1497 tests
369+
npm test # 1519 tests
370370
npm run stress # three-axis stress test
371371
npm run sync # src → lib sync
372372
```

‎dsh-mneme/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
## 🐛 修复
6+
7+
- **密钥 / PII 判据三处加固(`src/sensitive-scan.js`,均为合并后独立复审发现)**:① **回溯有上界**——`email` 的 local part 与 `connection_string` 的 scheme 都作用在含 `.` 的字符类上,缺上界时每个起点都要一路重扫到结尾才失败(O(n²)),而判据在写入路径上同步跑:实测 34KB 点分链 0.6 秒、120KB 对抗串 23.5 秒(email 18.6s + 连接串 4.1s),等于把写入卡死;加上界后同输入约 60ms,边界取 RFC 5321 给 local part 的 64。② **赋值型规则左边界不再排除 `_`**——环境变量名正是拿 `_` 当分隔符,原写法让 `DB_PASSWORD=` / `MY_API_KEY=` / `MYSQL_PASSWORD=` 这类「前缀_关键词」整类漏放(只有恰好落在行首的 `API_KEY=` 能中);放宽后 #332 的 26 条语料仍全绿,挡误杀的仍是那道占位符守卫。③ **身份证档补校验位**(GB 11643 / ISO 7064 MOD 11-2)——原先只有银行卡档有 Luhn,18 位纯数字(订单号 / 内部编号)先被身份证规则命中,等不到银行卡那条的校验。三处都配了回归测试,并做过变异检验(改回原写法各自变红)。判据仍在 `sensitiveScanEnabled` 默认关之后,线上行为不变。
8+
59
## 🆕 新增
610

711
- **写入边界的密钥 / PII 判据(`sensitiveScanEnabled`,默认关)**:写入准入(#254 第 1 级)此前只跑空白 / 噪声两类判据,密钥 / PII 那一档按设计留了注入点而没实现。现在补上 `src/sensitive-scan.js`——纯确定性、零 LLM,先认形状再认关键词(赋值型规则带占位符守卫,所以「把 API key 放进环境变量」这类讨论句不报)。命中落审计 `metadata.deny.reason='sensitive'` + `kind`(密钥 / PII 分档,便于先看分布再决定放行策略),审计位与 #332 定的形状一致。开关分层:本键只决定「这类判据参不参与」,命中之后是仅告警还是真拦仍由 `writeAdmission.enforce` 决定(默认仅告警、拦截 opt-in)。回归样本集(10 条密钥 + 4 条 PII 正样本、12 条负样本)原样跑真判据:正样本不漏、负样本不误杀。

‎dsh-mneme/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![npm version](https://img.shields.io/npm/v/@modusensus/dsh-mneme?color=blue&label=npm)](https://www.npmjs.com/package/@modusensus/dsh-mneme)
66
[![license](https://img.shields.io/badge/license-MIT-green)](LICENSE)
77
[![Awesome](https://awesome-dsh-plugin.com/badge.svg)](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin)
8-
[![tests](https://img.shields.io/badge/tests-1497%20passed-success)](https://github.com/slow-stack/mneme)
8+
[![tests](https://img.shields.io/badge/tests-1519%20passed-success)](https://github.com/slow-stack/mneme)
99
[![CI](https://img.shields.io/github/actions/workflow/status/slow-stack/mneme/ci.yml)](https://github.com/slow-stack/mneme/actions)
1010
[![node](https://img.shields.io/badge/node-22%2B-blue)](https://nodejs.org)
1111
[![npm downloads](https://img.shields.io/npm/d18m/@modusensus/dsh-mneme.svg?color=blue&label=downloads)](https://www.npmjs.com/package/@modusensus/dsh-mneme)
@@ -584,7 +584,7 @@ src/
584584
├── api.js # HTTP 路由(Web 面板数据通道,含 /conflicts 冲突队列)
585585
└── index.js # 插件接线
586586
lib/ # src 的同步分发产物(npm run sync;发布前由 root prepack 的 check-sync.js 校验一致性;唯一手写例外 lib/client.js——Web 面板 bundle,sync 不覆盖)
587-
test/ # 1497 个 node:test 测试(审计与三轴线压测不变量;src↔lib 一致性由 scripts/check-sync.js 发布闸门校验)
587+
test/ # 1519 个 node:test 测试(审计与三轴线压测不变量;src↔lib 一致性由 scripts/check-sync.js 发布闸门校验)
588588
scripts/ # e2e-dsh.js 端到端演示 · stress-dsh.js 三轴线压测 · sync-lib.js 同步 · check-sync.js 发布闸门 · benchmark-recall.js / benchmark-embed.js / benchmark-rerank.js 基准 · sync-test-badge.mjs 测试徽章 · build-runtime-manifest.mjs 运行时清单
589589
```
590590
@@ -593,7 +593,7 @@ scripts/ # e2e-dsh.js 端到端演示 · stress-dsh.js 三轴线压
593593
```bash
594594
cd dsh-mneme
595595
npm install # 安装 peer 依赖(以 devDependencies 形式,用于本地测试)
596-
npm test # 运行 1497 个测试
596+
npm test # 运行 1519 个测试
597597
npm run stress # 三轴线压测:长会话检索 / 冲突仲裁 / 多 Agent 并发(离线 mock LLM)
598598
npm run sync # 把 src/ 同步到 lib/(发布时由 prepack 钩子自动执行)
599599
```

‎dsh-mneme/lib/sensitive-scan.js‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
// 就会把长度相近的订单号 / 内部编号吃进来,而 PII 这一档的误杀面已经比密钥大一档。
3636
// - 银行卡号加 Luhn 校验:`0000000000000000`、`4111111111111112` 这类形状对但校验
3737
// 不过的串不报。少了这道校验,任何 16 位数字串(订单号、时间戳拼接)都会命中。
38+
// - 身份证同样加校验位(GB 11643 / ISO 7064 MOD 11-2):位数对但校验不过的 18 位
39+
// 数字串不报。原先只有银行卡有校验、身份证没有,结果是 18 位纯数字先被身份证规则
40+
// 命中,反倒绕过银行卡那条的 Luhn。
3841
//
3942
// 归一化:这里**不**用 content-hash.js 的 normalizeForHash。那套口径(NFKC → 小写 →
4043
// 去标点)是为「只差格式的两条写入是否同一件事」定的,判据要的是原串的形状——大小写
@@ -64,7 +67,12 @@ const SECRET_RULES = [
6467
// 可能有 `RSA` / `EC` / `OPENSSH`,所以中间那段是 [A-Z ]*。
6568
{ kind: "private_key", label: "PEM private key", re: /-----BEGIN [A-Z ]*PRIVATE KEY-----/ },
6669
{ kind: "jwt", label: "JSON Web Token", re: /\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/ },
67-
{ kind: "connection_string", label: "credentials in URL", re: /\b[a-z][a-z0-9+.-]*:\/\/[^\s/:@]+:[^\s/:@]{6,}@/ },
70+
// scheme 与 userinfo 两段都加上界。scheme 那段的 `*` 作用在含 `.` 的字符类上,
71+
// 一条长点分串(包名 / 路径 / 版本链)里每个起点都要一路回溯到结尾才发现没有
72+
// `://` → 与 email 同源的 O(n²)(实测 120KB 对抗串里这条占 4.1 秒)。
73+
// URL scheme 名本就短、`user:password` 也不会长到 64,上界只钉住回溯面,
74+
// 真实连接串一条不少。
75+
{ kind: "connection_string", label: "credentials in URL", re: /\b[a-z][a-z0-9+.-]{0,31}:\/\/[^\s/:@]{1,64}:[^\s/:@]{6,}@/ },
6876
// Stripe 排在通用 `sk-` 之前:`sk_live_…` 两条规则都吃,先到的那条决定 kind。
6977
// 只认 sk_live_(生产密钥):sk_test_ 是公开测试密钥,报它是纯误杀。
7078
{ kind: "stripe_key", label: "Stripe secret key", re: /\bsk_live_[A-Za-z0-9]{16,}\b/ },
@@ -79,7 +87,11 @@ const SECRET_RULES = [
7987
// alphabet——凭据值没有通用形状,能通用的只有「它不像占位符」。
8088
kind: "assigned_secret",
8189
label: "assigned credential literal",
82-
re: /(?:^|[^A-Za-z0-9_])(?:password|passwd|pwd|secret|api[_-]?key|token)\b\s*[:=]\s*["']?([^\s"']{8,})/i,
90+
// 左边界不能把 `_` 排除在外:环境变量名正是拿 `_` 当分隔符,排除它会让
91+
// `DB_PASSWORD=` / `MY_API_KEY=` / `MYSQL_PASSWORD=` 整类漏放(只有恰好落在
92+
// 行首的 `API_KEY=` 能中)。放宽后 #332 那套 26 条语料(含 12 条负样本)全绿,
93+
// 说明原写法不是语料换来的取舍。挡误杀的是下面那道占位符守卫,不是这个边界。
94+
re: /(?:^|[^A-Za-z0-9])(?:password|passwd|pwd|secret|api[_-]?key|token)\b\s*[:=]\s*["']?([^\s"']{8,})/i,
8395
// 占位符守卫:右边是尖括号占位、shell / 模板变量、环境变量读取、或一串 x / * / …
8496
// 时不算命中。少这道守卫,`password: <redacted>` 与 `token: ${TOKEN}` 都会被报,
8597
// 而它们正是「配置里该怎么写」的示例文本。
@@ -93,10 +105,15 @@ const PII_RULES = [
93105
label: "email address",
94106
// 先看 TLD 再看 `@`:反过来的 `(?:[A-Za-z]{2,}\.)+[A-Za-z]{2,}` 对
95107
// `[email protected]` 这类可以回溯出指数条路径。
96-
re: /\b[A-Za-z0-9._%+-]+@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,}\b/
108+
// local part 的 `+` 必须加上界。该字符类含 `.`,所以一条长点分串(包名 / 路径 /
109+
// 版本链)后跟一个 `@` 时,每个起点都要重扫到那个 `@` 才失败 → 整体 O(n²)。
110+
// 实测 120KB 对抗串 23.5 秒里这条占 18.6 秒,34KB 点分链要 0.6 秒;本判据在写入
111+
// 路径上同步跑,等于把写入阻塞住。64 是 RFC 5321 给 local part 的上限,加上界
112+
// 不缩检测面——放弃的只是长于 64 的非法形状。
113+
re: /\b[A-Za-z0-9._%+-]{1,64}@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,}\b/
97114
},
98115
{ kind: "cn_mobile", label: "mainland mobile number", re: /(?<!\d)1[3-9]\d{9}(?!\d)/ },
99-
{ kind: "cn_id_card", label: "mainland ID number", re: /(?<![0-9A-Za-z])\d{17}[\dXx](?![0-9A-Za-z])/ },
116+
{ kind: "cn_id_card", label: "mainland ID number", re: /(?<![0-9A-Za-z])\d{17}[\dXx](?![0-9A-Za-z])/, cnId: true },
100117
{ kind: "bank_card", label: "payment card number", re: /(?<!\d)(?:\d{13,19})(?!\d)/, luhn: true }
101118
];
102119

@@ -124,6 +141,19 @@ function passesLuhn(value) {
124141
return sum % 10 === 0;
125142
}
126143

144+
// 大陆身份证校验位(GB 11643 / ISO 7064 MOD 11-2)。与银行卡的 Luhn 同理:18 位
145+
// 数字串在项目记忆里很常见(订单号、内部编号、拼接时间戳),位数这个形状拦不住它们,
146+
// 而校验位是身份证自带的、零成本的第二道形状。权重序列与余数映射都是标准值,别改。
147+
const CN_ID_WEIGHTS = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
148+
const CN_ID_CODES = "10X98765432";
149+
/** @param {string} value 命中串(17 位数字 + 校验位,校验位可为 X) */
150+
function passesCnId(value) {
151+
if (!/^\d{17}[\dXx]$/.test(value)) return false;
152+
let sum = 0;
153+
for (let i = 0; i < 17; i++) sum += Number(value[i]) * CN_ID_WEIGHTS[i];
154+
return CN_ID_CODES[sum % 11] === value[17].toUpperCase();
155+
}
156+
127157
/**
128158
* 扫一段文本里的密钥 / PII。命中返回 `{kind, label}`、未命中返回 null,不抛。
129159
*
@@ -141,6 +171,7 @@ export function scanSensitive(value) {
141171
// 守卫只看捕获组:没有捕获组的规则天然没有守卫。
142172
if (rule.guard && !rule.guard(match[1] ?? "")) continue;
143173
if (rule.luhn && !passesLuhn(match[0])) continue;
174+
if (rule.cnId && !passesCnId(match[0])) continue;
144175
return { kind: rule.kind, label: rule.label };
145176
}
146177
return null;

‎dsh-mneme/src/sensitive-scan.js‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
// 就会把长度相近的订单号 / 内部编号吃进来,而 PII 这一档的误杀面已经比密钥大一档。
3636
// - 银行卡号加 Luhn 校验:`0000000000000000`、`4111111111111112` 这类形状对但校验
3737
// 不过的串不报。少了这道校验,任何 16 位数字串(订单号、时间戳拼接)都会命中。
38+
// - 身份证同样加校验位(GB 11643 / ISO 7064 MOD 11-2):位数对但校验不过的 18 位
39+
// 数字串不报。原先只有银行卡有校验、身份证没有,结果是 18 位纯数字先被身份证规则
40+
// 命中,反倒绕过银行卡那条的 Luhn。
3841
//
3942
// 归一化:这里**不**用 content-hash.js 的 normalizeForHash。那套口径(NFKC → 小写 →
4043
// 去标点)是为「只差格式的两条写入是否同一件事」定的,判据要的是原串的形状——大小写
@@ -64,7 +67,12 @@ const SECRET_RULES = [
6467
// 可能有 `RSA` / `EC` / `OPENSSH`,所以中间那段是 [A-Z ]*。
6568
{ kind: "private_key", label: "PEM private key", re: /-----BEGIN [A-Z ]*PRIVATE KEY-----/ },
6669
{ kind: "jwt", label: "JSON Web Token", re: /\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/ },
67-
{ kind: "connection_string", label: "credentials in URL", re: /\b[a-z][a-z0-9+.-]*:\/\/[^\s/:@]+:[^\s/:@]{6,}@/ },
70+
// scheme 与 userinfo 两段都加上界。scheme 那段的 `*` 作用在含 `.` 的字符类上,
71+
// 一条长点分串(包名 / 路径 / 版本链)里每个起点都要一路回溯到结尾才发现没有
72+
// `://` → 与 email 同源的 O(n²)(实测 120KB 对抗串里这条占 4.1 秒)。
73+
// URL scheme 名本就短、`user:password` 也不会长到 64,上界只钉住回溯面,
74+
// 真实连接串一条不少。
75+
{ kind: "connection_string", label: "credentials in URL", re: /\b[a-z][a-z0-9+.-]{0,31}:\/\/[^\s/:@]{1,64}:[^\s/:@]{6,}@/ },
6876
// Stripe 排在通用 `sk-` 之前:`sk_live_…` 两条规则都吃,先到的那条决定 kind。
6977
// 只认 sk_live_(生产密钥):sk_test_ 是公开测试密钥,报它是纯误杀。
7078
{ kind: "stripe_key", label: "Stripe secret key", re: /\bsk_live_[A-Za-z0-9]{16,}\b/ },
@@ -79,7 +87,11 @@ const SECRET_RULES = [
7987
// alphabet——凭据值没有通用形状,能通用的只有「它不像占位符」。
8088
kind: "assigned_secret",
8189
label: "assigned credential literal",
82-
re: /(?:^|[^A-Za-z0-9_])(?:password|passwd|pwd|secret|api[_-]?key|token)\b\s*[:=]\s*["']?([^\s"']{8,})/i,
90+
// 左边界不能把 `_` 排除在外:环境变量名正是拿 `_` 当分隔符,排除它会让
91+
// `DB_PASSWORD=` / `MY_API_KEY=` / `MYSQL_PASSWORD=` 整类漏放(只有恰好落在
92+
// 行首的 `API_KEY=` 能中)。放宽后 #332 那套 26 条语料(含 12 条负样本)全绿,
93+
// 说明原写法不是语料换来的取舍。挡误杀的是下面那道占位符守卫,不是这个边界。
94+
re: /(?:^|[^A-Za-z0-9])(?:password|passwd|pwd|secret|api[_-]?key|token)\b\s*[:=]\s*["']?([^\s"']{8,})/i,
8395
// 占位符守卫:右边是尖括号占位、shell / 模板变量、环境变量读取、或一串 x / * / …
8496
// 时不算命中。少这道守卫,`password: <redacted>` 与 `token: ${TOKEN}` 都会被报,
8597
// 而它们正是「配置里该怎么写」的示例文本。
@@ -93,10 +105,15 @@ const PII_RULES = [
93105
label: "email address",
94106
// 先看 TLD 再看 `@`:反过来的 `(?:[A-Za-z]{2,}\.)+[A-Za-z]{2,}` 对
95107
// `[email protected]` 这类可以回溯出指数条路径。
96-
re: /\b[A-Za-z0-9._%+-]+@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,}\b/
108+
// local part 的 `+` 必须加上界。该字符类含 `.`,所以一条长点分串(包名 / 路径 /
109+
// 版本链)后跟一个 `@` 时,每个起点都要重扫到那个 `@` 才失败 → 整体 O(n²)。
110+
// 实测 120KB 对抗串 23.5 秒里这条占 18.6 秒,34KB 点分链要 0.6 秒;本判据在写入
111+
// 路径上同步跑,等于把写入阻塞住。64 是 RFC 5321 给 local part 的上限,加上界
112+
// 不缩检测面——放弃的只是长于 64 的非法形状。
113+
re: /\b[A-Za-z0-9._%+-]{1,64}@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,}\b/
97114
},
98115
{ kind: "cn_mobile", label: "mainland mobile number", re: /(?<!\d)1[3-9]\d{9}(?!\d)/ },
99-
{ kind: "cn_id_card", label: "mainland ID number", re: /(?<![0-9A-Za-z])\d{17}[\dXx](?![0-9A-Za-z])/ },
116+
{ kind: "cn_id_card", label: "mainland ID number", re: /(?<![0-9A-Za-z])\d{17}[\dXx](?![0-9A-Za-z])/, cnId: true },
100117
{ kind: "bank_card", label: "payment card number", re: /(?<!\d)(?:\d{13,19})(?!\d)/, luhn: true }
101118
];
102119

@@ -124,6 +141,19 @@ function passesLuhn(value) {
124141
return sum % 10 === 0;
125142
}
126143

144+
// 大陆身份证校验位(GB 11643 / ISO 7064 MOD 11-2)。与银行卡的 Luhn 同理:18 位
145+
// 数字串在项目记忆里很常见(订单号、内部编号、拼接时间戳),位数这个形状拦不住它们,
146+
// 而校验位是身份证自带的、零成本的第二道形状。权重序列与余数映射都是标准值,别改。
147+
const CN_ID_WEIGHTS = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
148+
const CN_ID_CODES = "10X98765432";
149+
/** @param {string} value 命中串(17 位数字 + 校验位,校验位可为 X) */
150+
function passesCnId(value) {
151+
if (!/^\d{17}[\dXx]$/.test(value)) return false;
152+
let sum = 0;
153+
for (let i = 0; i < 17; i++) sum += Number(value[i]) * CN_ID_WEIGHTS[i];
154+
return CN_ID_CODES[sum % 11] === value[17].toUpperCase();
155+
}
156+
127157
/**
128158
* 扫一段文本里的密钥 / PII。命中返回 `{kind, label}`、未命中返回 null,不抛。
129159
*
@@ -141,6 +171,7 @@ export function scanSensitive(value) {
141171
// 守卫只看捕获组:没有捕获组的规则天然没有守卫。
142172
if (rule.guard && !rule.guard(match[1] ?? "")) continue;
143173
if (rule.luhn && !passesLuhn(match[0])) continue;
174+
if (rule.cnId && !passesCnId(match[0])) continue;
144175
return { kind: rule.kind, label: rule.label };
145176
}
146177
return null;

0 commit comments

Comments
 (0)