Verify / verify (push) Canceled after 0s
- 将凭据片段列表提升为全局默认列表,新增 DefaultSensitiveList、NewSensitiveList、Extend 与 SafeWith,支持整体替换或在默认列表之上扩展 - 新增 SetSensitiveDetection 与 SensitiveDetectionEnabled,缺省开启敏感检测,关闭后日志副本不再按名称隐藏凭据字段 - 显式敏感列表、访问日志固定凭据头与 RedactKeys、SQL config_value 与 Redis 认证命令等固定策略不受开关影响 - 同步接入使用指南的能力清单与使用说明
147 lines
4.7 KiB
Go
147 lines
4.7 KiB
Go
package storagevalue
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestCredentialCopiesPreserveOriginalAndNumbers 验证卡密、验证码及不同命名的密钥字段脱敏,不改动原始值或大整数。
|
|
func TestCredentialCopiesPreserveOriginalAndNumbers(t *testing.T) {
|
|
var (
|
|
original = map[string]any{
|
|
"card_key": "private-card",
|
|
"verification_code": "private-code",
|
|
"apiKey": "private-api",
|
|
"nested": `{"accessKey":"private-access","id":18446744073709551615}`,
|
|
}
|
|
result = Safe(original)
|
|
body, err = json.Marshal(result)
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(body), "private-") {
|
|
t.Fatal("credential appeared in log copy")
|
|
}
|
|
if !strings.Contains(string(body), "18446744073709551615") {
|
|
t.Fatal("log copy rounded an identifier")
|
|
}
|
|
if original["card_key"] != "private-card" {
|
|
t.Fatal("redaction modified execution data")
|
|
}
|
|
}
|
|
|
|
// TestDefaultSensitiveListMatchesCredentialKeys 验证默认列表覆盖凭据字段名并归一连字符形式。
|
|
func TestDefaultSensitiveListMatchesCredentialKeys(t *testing.T) {
|
|
for _, key := range []string{"password", "User-Password", "apiKey", "api_key", "verification_code", "private-key"} {
|
|
if !Sensitive(key) {
|
|
t.Fatalf("default list missed credential key %s", key)
|
|
}
|
|
}
|
|
if Sensitive("user_name") {
|
|
t.Fatal("default list flagged a plain field")
|
|
}
|
|
}
|
|
|
|
// TestNewSensitiveListReplacesDefault 验证自定义列表只按传入片段判定,不继承默认列表。
|
|
func TestNewSensitiveListReplacesDefault(t *testing.T) {
|
|
var (
|
|
list = NewSensitiveList([]string{"employee_no"})
|
|
)
|
|
if !list.Sensitive("Employee-No") {
|
|
t.Fatal("custom list missed its own part")
|
|
}
|
|
if list.Sensitive("password") {
|
|
t.Fatal("custom list inherited the default parts")
|
|
}
|
|
if NewSensitiveList(nil).Sensitive("password") != Sensitive("password") {
|
|
t.Fatal("empty custom list differs from the default list")
|
|
}
|
|
}
|
|
|
|
// TestExtendAddsPartsOnTopOfCurrentList 验证扩展在当前列表之上追加片段且不修改原列表。
|
|
func TestExtendAddsPartsOnTopOfCurrentList(t *testing.T) {
|
|
var (
|
|
base = DefaultSensitiveList()
|
|
extended = base.Extend("employee_no")
|
|
)
|
|
if !extended.Sensitive("employee_no") {
|
|
t.Fatal("extended list missed the appended part")
|
|
}
|
|
if !extended.Sensitive("password") {
|
|
t.Fatal("extended list lost the default parts")
|
|
}
|
|
if base.Sensitive("employee_no") {
|
|
t.Fatal("extend modified the original list")
|
|
}
|
|
}
|
|
|
|
// TestSafeWithAppliesCustomList 验证自定义列表参与结构副本脱敏且不改写原始数据。
|
|
func TestSafeWithAppliesCustomList(t *testing.T) {
|
|
var (
|
|
original = map[string]any{"employee_no": "private-no", "password": "private-password"}
|
|
list = NewSensitiveList([]string{"employee_no"})
|
|
result = SafeWith(original, list)
|
|
body, err = json.Marshal(result)
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(body), "[REDACTED]") {
|
|
t.Fatal("custom list did not redact the field")
|
|
}
|
|
if strings.Contains(string(body), "private-no") {
|
|
t.Fatal("custom sensitive value appeared in log copy")
|
|
}
|
|
if !strings.Contains(string(body), "private-password") {
|
|
t.Fatal("replaced list still redacted the default sensitive key")
|
|
}
|
|
if original["employee_no"] != "private-no" {
|
|
t.Fatal("redaction modified execution data")
|
|
}
|
|
}
|
|
|
|
// TestSensitiveDetectionSwitch 验证敏感检测缺省开启,可手动关闭后不再按名称隐藏凭据字段,显式列表仍然生效。
|
|
func TestSensitiveDetectionSwitch(t *testing.T) {
|
|
var (
|
|
original = map[string]any{"password": "private-password"}
|
|
)
|
|
defer SetSensitiveDetection(true)
|
|
if !SensitiveDetectionEnabled() {
|
|
t.Fatal("sensitive detection must be enabled by default")
|
|
}
|
|
if !Sensitive("password") {
|
|
t.Fatal("default detection missed credential key")
|
|
}
|
|
if body, _ := json.Marshal(Safe(original)); !strings.Contains(string(body), "[REDACTED]") {
|
|
t.Fatal("default detection did not hide credential field")
|
|
}
|
|
SetSensitiveDetection(false)
|
|
if SensitiveDetectionEnabled() {
|
|
t.Fatal("switch state was not closed")
|
|
}
|
|
if Sensitive("password") {
|
|
t.Fatal("closed detection still flagged credential key")
|
|
}
|
|
var (
|
|
body, err = json.Marshal(Safe(original))
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(body), "private-password") {
|
|
t.Fatal("closed detection still hid credential field")
|
|
}
|
|
if original["password"] != "private-password" {
|
|
t.Fatal("closed detection modified execution data")
|
|
}
|
|
if explicit, err := json.Marshal(SafeWith(original, DefaultSensitiveList())); err != nil || !strings.Contains(string(explicit), "[REDACTED]") {
|
|
t.Fatal("explicit sensitive list must stay effective")
|
|
}
|
|
SetSensitiveDetection(true)
|
|
if !Sensitive("password") {
|
|
t.Fatal("re-enabled detection did not resume")
|
|
}
|
|
}
|