Skip to content

Commit 06a1dd2

Browse files
authored
Merge pull request #465 from slow-stack/fix/secret-scan-alert
test(scan): 语料里的 OpenAI key 改拼接,并加字面量守卫(secret scanning alert #1)
2 parents a543aee + aa0c265 commit 06a1dd2

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

‎dsh-mneme/test/helpers/write-admission-samples.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ export const SAMPLE_VALUES = {
4747
".eyJzdWIiOiIxMjM0NTY3ODkwIn0",
4848
".dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U"
4949
),
50-
stripeKey: assemble("sk_", "live_", "51H8Qw2KZvN3mPqRsTuVwXyZ0123456789abcdef")
50+
stripeKey: assemble("sk_", "live_", "51H8Qw2KZvN3mPqRsTuVwXyZ0123456789abcdef"),
51+
// 这条曾以整串字面量留在下面的 content 里,被 GitHub secret scanning 报成 openai_api_key
52+
// (alert #1)。它是形状样本不是真凭据(正文就是 a-z、0-9 顺序串),但扫描器只看形状,
53+
// 所以按上面第 32-34 行的口径统一走拼接,别让它再当例外。
54+
openaiKey: assemble("sk-proj-", "abcdefghijklmnopqrstuvwxyz0123456789ABCD")
5155
};
5256

5357
/** 密钥凭据类正样本。kind 与 label 是判据该报回来的东西。 */
@@ -73,7 +77,7 @@ export const SECRET_SAMPLES = [
7377
kind: "openai_key",
7478
label: "OpenAI API key",
7579
title: "调试记录",
76-
content: "临时用 sk-proj-abcdefghijklmnopqrstuvwxyz0123456789ABCD 调了一次",
80+
content: `临时用 ${SAMPLE_VALUES.openaiKey} 调了一次`,
7781
why: "sk-/sk-proj- 前缀 + 长串"
7882
},
7983
{

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,47 @@ test("长文本上的回溯有上界:写阻塞类回归的计时护栏", () =>
205205
assert.ok(ms < 2000, `${text.length} 字符耗时 ${ms.toFixed(1)}ms,超 2000ms 预算(回溯上界被破坏?)`);
206206
}
207207
});
208+
209+
// 仓库文本守卫:语料里的凭据值必须以拼接形式存在,仓库里不得留下完整形状。
210+
// 触发这件事的是 GitHub secret scanning 的 alert #1——write-admission-samples.js 里
211+
// openai-key 那条曾经是整串字面量(同文件其它四条都已是 assemble 拼接),被报成
212+
// openai_api_key 的 open=true 告警。它本身不是泄露(正文是 a-z、0-9 的顺序串),
213+
// 但扫描器只看形状;而这条漏网说明「逐条记得改」靠不住,所以用一条测试把它钉住:
214+
// 以后谁再往语料里加字面量,这里先红,不用等 GitHub 来通知。
215+
test("语料里不留完整形状的凭据字面量(secret scanning alert #1 的回归锁)", async () => {
216+
const { readFile, readdir } = await import("node:fs/promises");
217+
const helpersDir = new URL("./helpers/", import.meta.url);
218+
const files = (await readdir(helpersDir)).filter((f) => f.endsWith(".js"));
219+
// 前缀集合与长度都对齐 src/sensitive-scan.js 的 github_token / slack_token /
220+
// openai_key 三条规则——只有形状一致才谈得上「扫描器会认」,前缀写窄了等于给
221+
// gho_ / ghs_ / xoxp- 这些同族凭据开后门。改那三条规则时这里要跟着改。
222+
// 唯一放宽的是 sk- 的下限:生产规则是 24,这里用 20,宁可多报——漏报的代价是
223+
// GitHub 再报一次告警,多报的代价只是有人来把这行改成拼接。
224+
const rules = [
225+
// kind 与生产判据对齐,下面的断言拿它反查 scanSensitive 是不是真认这个形状。
226+
{ re: /\bsk-(?:proj-)?[A-Za-z0-9_-]{20,}\b/, what: "OpenAI key", kind: "openai_key", probe: `sk-${"a1".repeat(13)}` },
227+
{ re: /\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36}\b/, what: "GitHub token", kind: "github_token", probe: `ghs_${"a1".repeat(18)}` },
228+
{ re: /\bxox[abprs]-[A-Za-z0-9-]{10,}\b/, what: "Slack token", kind: "slack_token", probe: `xoxp-${"a1".repeat(8)}` }
229+
];
230+
// 「与生产规则同源」不能只是注释:CodeRabbit 在 #465 指出前一版把前缀写窄了
231+
// (只认 ghp_ / xoxb-,漏了 gho_/ghs_/xoxp-),注释却写着同源。所以这里反查
232+
// scanSensitive——守卫的每条规则必须真的被生产判据认成同一个 kind,否则这条红。
233+
// 生产规则收窄前缀时,这条测试会先于「GitHub 又报一次告警」发现不一致。
234+
// 两段合起来才叫同源:探针要同时被**守卫的规则**和**生产判据**认下,且 kind 一致。
235+
// 只查一边的话,任一侧收窄前缀都不会红——那正是 CodeRabbit 指出的失效模式。
236+
for (const { re, what, kind, probe } of rules) {
237+
assert.match(probe, re, `守卫自己的 ${what} 规则认不出 ${probe},前缀写窄了?`);
238+
assert.equal(scanSensitive(probe)?.kind, kind, `生产判据不认 ${probe} 为 ${kind},守卫与它不同源`);
239+
}
240+
for (const file of files) {
241+
const text = await readFile(new URL(file, helpersDir), "utf8");
242+
for (const { re, what } of rules) {
243+
const hit = text.match(re);
244+
assert.equal(
245+
hit,
246+
null,
247+
`${file} 里出现了完整的 ${what} 字面量(${hit?.[0]?.slice(0, 12)}…):按 assemble 拼接,别整串写`
248+
);
249+
}
250+
}
251+
});

0 commit comments

Comments
 (0)