diff --git a/internal/redact/redact.go b/internal/redact/redact.go index 546747ff5..a90178ead 100644 --- a/internal/redact/redact.go +++ b/internal/redact/redact.go @@ -152,6 +152,13 @@ var rules = append([]rule{ // code calls anything a key: a map's "key": "value", sort_key, keyboard. {kind: "SECRET", re: regexp.MustCompile(`(?i)(?:^|[^A-Za-z0-9_.-])` + quote + `key` + quote + `[ \t]*[:=][ \t]*` + quote + `(` + secretCh + `{16,})`), markers: fieldNames{{"key", "key"}}.markers(), ok: secretValue, at: notCode}, + // a password after its name in Chinese, Japanese or Korean, as people + // write one into a message: 密码:786kih, 密碼是 hunter2, パスワード: … . + // Unlike password = in a config, nothing in code is called 密码, so + // any value of four or more is taken, digits only too (CyberCaius on + // Discord); a sentence's closing stop is no part of it. + {kind: "PASSWORD", re: regexp.MustCompile(`(?:` + strings.Join(cjkPasswordNames, "|") + `)[ \t]*(?:[::=][ \t]*|是[ \t]*|は[ \t]*|는[ \t]*)?(?:\\?["'“「])?(` + secretCh + `{3,63}[A-Za-z0-9_\-/+=~!@#%^&*])`), + markers: cjkPasswordNames, ok: notAVariable}, {kind: "EMAIL", re: regexp.MustCompile(`[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}`), markers: []string{"@"}, bound: alnum + "._%+-", ok: realEmail, at: notUserinfo, cat: catPersonal}, {kind: "ID_CARD", re: regexp.MustCompile(`[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dXx]`), bound: alnum, ok: chineseID, cat: catPersonal}, @@ -159,6 +166,10 @@ var rules = append([]rule{ {kind: "BANK_CARD", re: regexp.MustCompile(`[3-6]\d{3}(?:[ -]?\d{4}){2,3}(?:[ -]?\d{1,3})?`), bound: digits, ok: luhn, cat: catPersonal}, }, moreRules...) +// cjkPasswordNames are what a password is called in Chinese, Japanese and +// Korean. +var cjkPasswordNames = []string{"密码", "密碼", "口令", "パスワード", "비밀번호"} + // fieldNames are what a field that holds a secret is called: a pattern for // each name, and text that any match of it has in it, so a rule's markers // and the names its pattern takes come from one list and can't drift apart. diff --git a/internal/redact/zhpassword_test.go b/internal/redact/zhpassword_test.go new file mode 100644 index 000000000..6756bccbd --- /dev/null +++ b/internal/redact/zhpassword_test.go @@ -0,0 +1,50 @@ +package redact + +import ( + "strings" + "testing" +) + +// A password written after its name in Chinese, Japanese or Korean is +// masked, a short one and one of digits only too: 密码:786kih went to the +// vendor as written (CyberCaius on Discord). The name, the words around it +// and a sentence's closing stop stay. +func TestCJKPasswordMasked(t *testing.T) { + cases := []struct { + in, gone string + kept []string + }{ + {"密码:786kih", "786kih", []string{"密码:"}}, + {"服务器密码是 hunter2,记得改", "hunter2", []string{"服务器密码是 ", ",记得改"}}, + {"WiFi 密碼: 12345678。", "12345678", []string{"密碼: ", "。"}}, + {"口令=Abc!2024.", "Abc!2024", []string{"口令=", "."}}, + {"パスワードは pa55word です", "pa55word", []string{"パスワードは ", " です"}}, + {"비밀번호: qwer1234", "qwer1234", []string{"비밀번호: "}}, + {`{"text":"账号 admin 密码 \"s3cr3t\""}`, "s3cr3t", []string{"账号 admin 密码 "}}, + } + for _, c := range cases { + out, n := Mask(c.in, Options{Secrets: true}) + if n != 1 || strings.Contains(unmarked(out), c.gone) { + t.Errorf("%q: %d masked → %q", c.in, n, out) + } + for _, k := range c.kept { + if !strings.Contains(out, k) { + t.Errorf("%q: %q gone from %q", c.in, k, out) + } + } + if back := Restore(out, false); back != c.in { + t.Errorf("round trip: %q → %q → %q", c.in, out, back) + } + } + // in a request body, as the gateway masks one + body := `{"messages":[{"role":"user","content":"密码:786kih"}]}` + if out, n := MaskJSON([]byte(body), Options{Secrets: true}); n != 1 || strings.Contains(string(out), "786kih") || string(RestoreJSON(out)) != body { + t.Errorf("body: %d %s", n, out) + } + // what isn't a password: no value, a placeholder, words, too short + for _, in := range []string{"请输入密码:", "密码:${DB_PASS}", "密码:****", "忘记密码了怎么办", "修改密码功能", "密码:abc"} { + if out, n := Mask(in, Options{Secrets: true}); n != 0 { + t.Errorf("%q masked: %q", in, out) + } + } +}