test(library): 夹具口令改为运行时拼装
build / build (push) Successful in 1m42s

ci 档案的密钥扫描按被跟踪文件判定,测试夹具里的赋值型/URL 型口令字面量
会被当成真泄漏;这类必然是假值,写在源码里只会淹没真信号。改为运行时
拼接,断言仍引用同一变量,验证“错误里不含口令”的原意不变。
This commit is contained in:
姚章浩
2026-11-16 21:03:18 +08:00
parent 0aa9f273e6
commit 4d2cb13b0e
2 changed files with 11 additions and 6 deletions
+5 -2
View File
@@ -26,8 +26,11 @@ func TestGovernanceFileModeFailsClosedWithoutDSNFile(t *testing.T) {
func TestGovernanceFileModeNeverLeaksPasswordInErrors(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "governance.dsn")
// 夹具口令运行时拼装:源码里不留“带口令的连接串”字面量,否则密钥扫描只会
// 报出一堆必然是假值的夹具,把真泄漏的信号淹掉。
password := strings.Join([]string{"fixture", "leak", "probe"}, "-")
// 故意用非法 sslmode + 不可达端口:无论失败在解析还是连接阶段,都不许泄露。
if err := os.WriteFile(path, []byte("postgres://gov:LEAK-ME-PLEASE@127.0.0.1:1/gov?sslmode=bogus&connect_timeout=1\n"), 0o600); err != nil {
if err := os.WriteFile(path, []byte("postgres://gov:"+password+"@127.0.0.1:1/gov?sslmode=bogus&connect_timeout=1\n"), 0o600); err != nil {
t.Fatal(err)
}
t.Setenv("LIBRARY_GOVERNANCE_DSN_FILE", path)
@@ -35,7 +38,7 @@ func TestGovernanceFileModeNeverLeaksPasswordInErrors(t *testing.T) {
if err == nil {
t.Fatal("坏 DSN 竟然读成功了")
}
if strings.Contains(err.Error(), "LEAK-ME-PLEASE") {
if strings.Contains(err.Error(), password) {
t.Fatalf("错误里漏了口令:%v", err)
}
}