From 714ab8e8bf7a99ca3e1e88e778dfba057c07aea0 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 14:50:13 +0800 Subject: [PATCH] =?UTF-8?q?redact:=20a=20password=20after=20=E5=AF=86?= =?UTF-8?q?=E7=A0=81,=20=E5=AF=86=E7=A2=BC,=20=E5=8F=A3=E4=BB=A4,=20?= =?UTF-8?q?=E3=83=91=E3=82=B9=E3=83=AF=E3=83=BC=E3=83=89=20or=20=EB=B9=84?= =?UTF-8?q?=EB=B0=80=EB=B2=88=ED=98=B8=20is=20masked=20(CyberCaius=20on=20?= =?UTF-8?q?Discord)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "密码:786kih" in Settings → Masking's try box came back "nothing to mask", and so went to the vendor as written. The password rules only knew English field names (password = …, as configs have them) and asked for 8+ characters of letters and digits both, since code calls many things a password. Nothing in code is called 密码, so after a CJK name for a password any value of 4 or more is taken, digits only too, after :, :, =, 是, は or 는, quoted or not. A sentence's closing stop stays outside the value. Left as they were: "请输入密码:", 密码:${DB_PASS}, 密码:****, 忘记密码了怎么办, 修改密码功能, and a value of 3 (密码:abc). English "password: abc12" with a short value is unchanged; issue filed for it. Run at this head: - go test ./internal/redact ./internal/masking ./internal/otlp ./internal/pii ./internal/settings ./internal/sessions -count=1: ok (every package that imports redact but gateway and gui) - go test ./internal/gateway -run 'CJK|Mask|Redact|Privacy': ok - gofmt -l, go vet ./internal/redact: clean - Broken by hand: with origin/main's redact.go, TestCJKPasswordMasked fails 7 of 7 masked cases; green with the rule. Not tried: the GUI try box in a running magpie, and the full suite. --- internal/redact/redact.go | 11 +++++++ internal/redact/zhpassword_test.go | 50 ++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 internal/redact/zhpassword_test.go 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) + } + } +}