From e4809a150a4e4e48d6f4705666ac83af4f7d75d1 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 16:55:01 +0800 Subject: [PATCH] =?UTF-8?q?redact:=20a=20key=20of=20digits=20only=20after?= =?UTF-8?q?=20=E5=AF=86=E9=92=A5=E4=B8=BA,=20token=20=E6=98=AF,=20apikey?= =?UTF-8?q?=EF=BC=9A=E2=80=A6=20is=20masked;=20PIN=E7=A0=81,=20=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E7=A0=81,=20=E6=8E=88=E6=9D=83=E7=A0=81=20join=20the?= =?UTF-8?q?=20names=20(CyberCaius=20on=20Discord)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "密钥为12231241" in Settings → Masking's 试一试 came back "这里没有需要脱敏的 内容". ee715c2e0's key rules (a CJK name for a key or token, or an English one before 为/為/是/:) asked for letters in the value, so that "RSA 密钥为 2048 位" stays; an all-digit key went as written. 密码/口令 took digits already (714ab8e8b). Now: - After a key's name and 为, 為, 是, :, :, =, は or 는, a value of six digits or more is masked as SECRET, by the CJK names (密钥, 秘钥, 令牌, 金鑰, トークン, 토큰…) and the English ones (token 为, apikey:, API Key 是, secret 為). Not when a unit follows it (位, 字节, 个, 次, 秒, 毫秒, 天, ms, byte…), with no 为/是/: between (密钥 12345678), or under six digits. - New names: PIN码, PIN碼, PIN 码, 安全码, 安全碼, 暗証番号, 비번 for a password (any value of four or more, digits too, as 密码 has); 授权码, 授權碼 for a key; pin and pin code before 为/是/: in English (six digits or more, a word boundary first, so "spin 是 …" stays). The CJK names are matched in any case (pin码). Left as they were (tested): RSA 密钥为 2048 位, 密钥为 12345, 令牌有效期是 86400, 令牌是 300000 毫秒, 密钥长度为 4096000 位, token 是 3600000 ms, key 是 1048576 byte, 令牌桶容量为 100000, 密钥 55661237, pin 为 1934, const token = 44102939;, {"token": 44102939}, maxTokens: 128000, and every case of TestCJKPasswordMasked, TestCJKSecretNamesMasked and 511c4a765's code tests (code_test.go). Run at this head: - go test ./internal/redact ./internal/pii ./internal/settings ./internal/otlp ./internal/sessions ./internal/gui -count=1: ok - go test ./internal/gateway -run 'CJK|Mask|Redact|Privacy|PII|Secret': ok - go test ./internal/redact -run 'CJK|Code' -count=3 -shuffle=on: ok - gofmt -l, go vet, GOOS=windows go vet, GOOS=linux go vet -tags nogui ./internal/redact: clean - Broken by hand: with the parent's redact.go, TestCJKDigitKeysMasked fails 19 of its 21 masked cases (密码为/口令: already masked). - With Shape on, 密钥为12231241 goes as a made-up 8-hex key and comes back. Not tried: the GUI try box in a running magpie (no GUI file changed), and the full suite. --- internal/redact/redact.go | 42 ++++++++++++++++++---- internal/redact/zhpassword_test.go | 58 ++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 7 deletions(-) diff --git a/internal/redact/redact.go b/internal/redact/redact.go index 71fdfdaf6..7f3b535a2 100644 --- a/internal/redact/redact.go +++ b/internal/redact/redact.go @@ -162,14 +162,16 @@ var rules = append([]rule{ // Discord); a sentence's closing stop is no part of it. {kind: "PASSWORD", re: cjkNamed(cjkPasswordNames), fold: lowerAll(cjkPasswordNames), back: 64, ahead: 100, ok: notAVariable}, // a key, a secret or a token after its name the same way: 密钥为 …, - // 令牌是 …, 金鑰: … . A key is no number, so 密钥为 2048 位 is left. - {kind: "SECRET", re: cjkNamed(cjkKeyNames), fold: lowerAll(cjkKeyNames), back: 64, ahead: 100, ok: keyValue}, + // 令牌是 …, 金鑰: … . A key of digits only is taken after 为, 是, : + // or = when it has six or more and counts nothing, so 密钥为12231241 + // is masked and 密钥为 2048 位 is left. + {kind: "SECRET", re: cjkNamed(cjkKeyNames), fold: lowerAll(cjkKeyNames), back: 64, ahead: 100, at: keyAt}, // an English name for one before 为, 為, 是 or :, as a Chinese message // has it: apikey 为 …, token 是 …, API Key:… (CyberCaius on Discord). // password = … and API_KEY: … in a config are the SECRET rule's above. - {kind: "SECRET", re: regexp.MustCompile(`(?i)(?:` + secretNames.pattern() + `|pwd|api[ ]key|access[ ]key|secret[ ]key|key)` + quote + + {kind: "SECRET", re: regexp.MustCompile(`(?i)(?:` + secretNames.pattern() + `|pwd|api[ ]key|access[ ]key|secret[ ]key|key|\bpin(?:[ ]?code)?)` + quote + `[ \t]*(?:[为為是][ \t]*[::]?|:)[ \t]*(?:\\?["'“「])?(` + secretCh + `{3,}[A-Za-z0-9_\-/+=~!@#%^&*])`), - fold: cjkCopulas, back: 24, ahead: 260, ok: keyValue}, + fold: cjkCopulas, back: 24, ahead: 260, at: keyAt}, {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}, @@ -179,12 +181,12 @@ var rules = append([]rule{ // cjkPasswordNames are what a password is called in Chinese, Japanese and // Korean. -var cjkPasswordNames = []string{"密码", "密碼", "口令", "パスワード", "비밀번호", "암호"} +var cjkPasswordNames = []string{"密码", "密碼", "口令", "PIN码", "PIN碼", "PIN 码", "安全码", "安全碼", "パスワード", "暗証番号", "비밀번호", "암호", "비번"} // cjkKeyNames are what a key, a secret or a token is called in Chinese, // Simplified and Traditional, Japanese and Korean. var cjkKeyNames = []string{ - "密钥", "密鑰", "秘钥", "秘鑰", "私钥", "私鑰", "金钥", "金鑰", "令牌", "权杖", "權杖", + "密钥", "密鑰", "秘钥", "秘鑰", "私钥", "私鑰", "金钥", "金鑰", "令牌", "权杖", "權杖", "授权码", "授權碼", "秘密鍵", "トークン", "シークレット", "APIキー", "アクセスキー", "토큰", "비밀키", "API 키", "API키", } @@ -205,7 +207,7 @@ func lowerAll(names []string) []string { // cjkNamed is a value after one of names: after :, :, =, 为, 為, 是, は // or 는, quoted or not, or after nothing but a space. func cjkNamed(names []string) *regexp.Regexp { - return regexp.MustCompile(`(?:` + strings.Join(names, "|") + `)[ \t]*(?:[::=][ \t]*|[为為是][ \t]*[::]?[ \t]*|は[ \t]*|는[ \t]*)?(?:\\?["'“「])?(` + secretCh + `{3,63}[A-Za-z0-9_\-/+=~!@#%^&*])`) + return regexp.MustCompile(`(?i)(?:` + strings.Join(names, "|") + `)[ \t]*(?:[::=][ \t]*|[为為是][ \t]*[::]?[ \t]*|は[ \t]*|는[ \t]*)?(?:\\?["'“「])?(` + secretCh + `{3,63}[A-Za-z0-9_\-/+=~!@#%^&*])`) } // fieldNames are what a field that holds a secret is called: a pattern for @@ -275,6 +277,32 @@ func keyValue(v string) bool { return letter && (strings.ContainsAny(v, digits) || len(v) >= 16) } +// keyAt is keyValue for s[a:b], or six digits or more right after 为, 為, +// 是, :, :, =, は or 는 that count nothing: a key said in a message is +// often a number (密钥为12231241, token 是 20261025, CyberCaius on +// Discord), while 密钥为 2048 位 and 令牌 300000 毫秒 are a size and a +// time. +func keyAt(s string, a, b int) bool { + v := s[a:b] + if keyValue(v) { + return true + } + if len(v) < 6 || strings.Trim(v, digits) != "" { + return false + } + before := strings.TrimRight(s[:a], " \t\"'“「\\") + after := strings.TrimLeft(s[b:], " \t") + return slices.ContainsFunc(keySeparators, func(c string) bool { return strings.HasSuffix(before, c) }) && + !slices.ContainsFunc(quantityUnits, func(u string) bool { return strings.HasPrefix(after, u) }) +} + +// keySeparators are what stands between a name and a key's value. +var keySeparators = []string{"为", "為", "是", ":", ":", "=", "は", "는"} + +// quantityUnits are what a number counts when one follows it: bits, bytes, +// a time, a count. +var quantityUnits = []string{"位", "比特", "字节", "字節", "个", "個", "次", "秒", "毫秒", "分钟", "分鐘", "小时", "小時", "天", "年", "bit", "byte", "ms", "sec", "min", "ビット", "バイト", "비트", "바이트"} + // secretValue is a value that looks like a secret, not code: letters and // digits both, and not a call, a reference or a placeholder of its own. func secretValue(v string) bool { diff --git a/internal/redact/zhpassword_test.go b/internal/redact/zhpassword_test.go index 4d2e896ef..4393758f5 100644 --- a/internal/redact/zhpassword_test.go +++ b/internal/redact/zhpassword_test.go @@ -101,3 +101,61 @@ func TestCJKSecretNamesMasked(t *testing.T) { } } } + +// A key of digits only after its name and 为, 是, :, : or = is masked: +// "密钥为12231241" came back "nothing to mask" in the try box (CyberCaius +// on Discord). It takes six digits or more, and a number that counts +// something (2048 位, 300000 毫秒) is left. The names added with it (PIN码, +// 安全码, 授权码, 暗証番号, 비번, pin) are masked too. +func TestCJKDigitKeysMasked(t *testing.T) { + cases := []struct{ in, gone, kept string }{ + {"密钥为12231241", "12231241", "密钥为"}, + {"密钥是 12231241,记一下", "12231241", "密钥是 "}, + {"密钥:12231241", "12231241", "密钥:"}, + {"秘钥=12231241", "12231241", "秘钥="}, + {"令牌为「20261025」", "20261025", "令牌为「"}, + {"金鑰為 12231241。", "12231241", "金鑰為 "}, + {"トークンは 12231241", "12231241", "トークンは "}, + {"토큰: 12231241", "12231241", "토큰: "}, + {"token 为 12231241", "12231241", "token 为 "}, + {"apikey:12231241", "12231241", "apikey:"}, + {"API Key 是 9988776655", "9988776655", "API Key 是 "}, + {"secret 為 123456", "123456", "secret 為 "}, + {"pin 为 123456", "123456", "pin 为 "}, + {"PIN码:1234", "1234", "PIN码:"}, + {"pin码是 8520", "8520", "pin码是 "}, + {"信用卡安全码:8765", "8765", "安全码:"}, + {"邮箱授权码为 abcdefghijklmnop", "abcdefghijklmnop", "授权码为 "}, + {"暗証番号は 4321", "4321", "暗証番号は "}, + {"비번: 1q2w3e4r", "1q2w3e4r", "비번: "}, + // the password names took digits only already + {"密码为12231241", "12231241", "密码为"}, + {"口令:12231241", "12231241", "口令:"}, + } + for _, c := range cases { + out, n := Mask(c.in, Options{Secrets: true}) + if n != 1 || strings.Contains(unmarked(out), c.gone) || !strings.Contains(out, c.kept) { + t.Errorf("%q: %d masked → %q", c.in, n, out) + } + if back := Restore(out, false); back != c.in { + t.Errorf("round trip: %q → %q → %q", c.in, out, back) + } + } + body := `{"messages":[{"role":"user","content":"密钥为12231241"}]}` + if out, n := MaskJSON([]byte(body), Options{Secrets: true}); n != 1 || strings.Contains(string(out), "12231241") || string(RestoreJSON(out)) != body { + t.Errorf("body: %d %s", n, out) + } + // a number that is no key (each one masked nowhere above, as a value + // masked once is masked wherever it is again): too short, a size, a time, a count, no + // 为/是/: between the name and it, code + for _, in := range []string{ + "RSA 密钥为 2048 位", "密钥为 12345", "令牌有效期是 86400", "令牌是 300000 毫秒", + "密钥长度为 4096000 位", "token 是 3600000 ms", "key 是 1048576 byte", "令牌桶容量为 100000", + "密钥 55661237", "spin 是 778812", "pin 为 1934", + "const token = 44102939;", `{"token": 44102939}`, "maxTokens: 128000", + } { + if out, n := Mask(in, Options{Secrets: true}); n != 0 { + t.Errorf("%q masked: %q", in, out) + } + } +}