Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

redact: a key of digits only after 密钥为, token 是, apikey:… is masked; PIN码, 安全码, 授权码 join the names (CyberCaius on Discord)

yetonee4809a150a4e
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)
+ }
+ }
+}