package redact import ( "strings" "testing" ) // a gateway's key, synthetic, as #195 has it const ocKey = "oc_sk_AAAABBBBccccDDDD1234eeeeFFFF5678" // every row of #195's table, and what must stay alone as key is let in func TestMaskGatewayKey(t *testing.T) { cases := []struct { in string gone bool kind string }{ {in: `"key": "` + ocKey + `"`, gone: true, kind: "API_KEY"}, {in: `\"key\": \"` + ocKey + `\"`, gone: true, kind: "API_KEY"}, {in: `"apiKey": "` + ocKey + `"`, gone: true, kind: "API_KEY"}, {in: `"token": "` + ocKey + `"`, gone: true, kind: "API_KEY"}, {in: "bare " + ocKey + " value", gone: true, kind: "API_KEY"}, {in: "or_sk_0123456789abcdefghijKLMNOP", gone: true, kind: "API_KEY"}, // key alone, a value that is no API key {in: `{"id": "relay", "key": "Zq81nVx0pL2mWe7RtY4u"}`, gone: true, kind: "SECRET"}, {in: `{\"key\": \"Zq81nVx0pL2mWe7RtY4u\"}`, gone: true, kind: "SECRET"}, {in: "key: Zq81nVx0pL2mWe7RtY4u", gone: true, kind: "SECRET"}, {in: `"api-key": "Zq81nVx0pL2mWe7RtY4u"`, gone: true, kind: "SECRET"}, {in: `"apikey": "Zq81nVx0pL2mWe7RtY4u"`, gone: true, kind: "SECRET"}, {in: `\"apiKey\": \"Zq81nVx0pL2mWe7RtY4u\"`, gone: true, kind: "SECRET"}, // and key as code says it {in: `{"key": "value", "sort_key": "xyz"}`}, {in: `key = "someIdentifier"`}, {in: `key = "someVeryLongIdentifierName"`}, {in: `sort_key: created_at_2024_rows`}, {in: `monkey: banana1234567890abc`}, {in: `obj.key = "abc123"`}, {in: `"key": "row-12"`}, {in: "the task_sk_something_long_name_here module"}, // no digits {in: "abcde_sk_0123456789abcdefghijKL"}, // five letters is a word {in: `"key": "${OPENCODE_API_KEY_0123456789}"`}, } for _, c := range cases { out, n := Mask(c.in, Options{Secrets: true}) if !c.gone { if n != 0 || out != c.in { t.Errorf("%q masked: %q", c.in, out) } continue } if n != 1 || !strings.Contains(out, "{{"+c.kind+"_") { t.Errorf("%q: %d masked, want one %s: %q", c.in, n, c.kind, out) } if back := Restore(out, false); back != c.in { t.Errorf("round trip: %q → %q → %q", c.in, out, back) } } // the same key, the same placeholder, whatever field it is in a, _ := Mask(`"key": "`+ocKey+`"`, Options{Secrets: true}) b, _ := Mask("OC_API_KEY="+ocKey, Options{Secrets: true}) c, _ := Mask(ocKey, Options{Secrets: true}) if pa, pb, pc := placeholderRe.FindString(a), placeholderRe.FindString(b), placeholderRe.FindString(c); pa == "" || pa != pb || pa != pc { t.Fatalf("placeholders differ: %q %q %q", a, b, c) } } // the markers of a rule's names, made from its list, are in any name it takes func TestSecretMarkers(t *testing.T) { for _, name := range []string{"DB_PASSWORD", "passwd", "client_secret", "Token", "apiKey", "API-KEY", "AccessKey", "private_key", "CREDENTIAL", "Credentials"} { if !containsAny(name, secretNames.markers()) { t.Errorf("%s: no marker", name) } } } func TestRuleKind(t *testing.T) { for in, want := range map[string]string{ "": "CUSTOM", "api key": "API_KEY", "oc-sk": "OC_SK", "{{x}}": "X", "9lives": "CUSTOM_9LIVES", "数据库": "CUSTOM", "a_very_long_kind_name_that_goes_on": "A_VERY_LONG_KIND_NAME_TH", } { if got := RuleKind(in); got != want { t.Errorf("RuleKind(%q) = %q, want %q", in, got, want) } } } func TestCheckRules(t *testing.T) { got, err := CheckRules([]Rule{{Kind: " gw key ", Prefix: " acme_ "}, {Regex: " "}, {Kind: "GW_KEY", Prefix: "acme_"}, {Kind: "ns", Regex: `ns-([0-9a-f]{12})`}}) if err != nil || len(got) != 2 || got[0] != (Rule{Kind: "GW_KEY", Prefix: "acme_"}) || got[1].Kind != "NS" { t.Fatalf("%+v %v", got, err) } for _, bad := range [][]Rule{ {{Regex: `ns-([0-9a-f`}}, // doesn't compile {{Regex: `(?Pa)(?