Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
redact: a key or token after 密钥, 令牌, 金鑰…, or apikey/token before 为/是/:, is masked (CyberCaius on Discord)
yetoneee715c2e0e17
diff --git a/internal/redact/redact.go b/internal/redact/redact.goindex a90178ead..c0d18010d 100644--- a/internal/redact/redact.go+++ b/internal/redact/redact.go@@ -157,8 +157,16 @@ var rules = append([]rule{// 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: "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},+ // 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 ++ `[ \t]*(?:[为為是][ \t]*[::]?|:)[ \t]*(?:\\?["'“「])?(` + secretCh + `{3,}[A-Za-z0-9_\-/+=~!@#%^&*])`),+ fold: cjkCopulas, back: 24, ahead: 260, ok: keyValue},{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},@@ -168,7 +176,34 @@ var rules = append([]rule{// cjkPasswordNames are what a password is called in Chinese, Japanese and// Korean.-var cjkPasswordNames = []string{"密码", "密碼", "口令", "パスワード", "비밀번호"}+var cjkPasswordNames = []string{"密码", "密碼", "口令", "パスワード", "비밀번호", "암호"}++// 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키",+}++// cjkCopulas are what stands between a name and its value in a Chinese+// message: 为, 為, 是, a full-width colon.+var cjkCopulas = []string{"为", "為", "是", ":"}++// lowerAll is names as fold has them, in lower case.+func lowerAll(names []string) []string {+ out := make([]string, len(names))+ for i, n := range names {+ out[i] = strings.ToLower(n)+ }+ return out+}++// 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_\-/+=~!@#%^&*])`)+}// 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@@ -226,6 +261,17 @@ func notAVariable(v string) bool {return strings.Trim(v, "*xX.") != ""}+// keyValue is a key's or a token's value, not a word or a number: letters+// and digits both, or 16 characters or more that aren't all digits. token 是+// expired, key 是 value and 密钥为 2048 are left.+func keyValue(v string) bool {+ if !notAVariable(v) {+ return false+ }+ letter := strings.IndexFunc(v, func(r rune) bool { return r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z' }) >= 0+ return letter && (strings.ContainsAny(v, digits) || len(v) >= 16)+}+// 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 {@@ -834,10 +880,14 @@ func (r rule) matches(s, lower string) [][]int {i = at + len(f)// from the line's start, or from where a word starts no further// back than back: what the pattern takes for the text's start is- // where a line or a word starts in s too- start := strings.LastIndexByte(s[:at], '\n') + 1- if start < at-r.back {- start = at - r.back+ // where a line or a word starts in s too. The line's start is+ // looked for no further back than that: a text of one long+ // line, as a request body is, isn't read back to its start for+ // each marker.+ start := max(0, at-r.back)+ if nl := strings.LastIndexByte(s[start:at], '\n'); nl >= 0 {+ start += nl + 1+ } else if start > 0 {for start < at && (strings.IndexByte(alnum+"_.-", s[start-1]) >= 0 || s[start]&0xc0 == 0x80) {start++}diff --git a/internal/redact/zhpassword_test.go b/internal/redact/zhpassword_test.goindex 6756bccbd..4d2e896ef 100644--- a/internal/redact/zhpassword_test.go+++ b/internal/redact/zhpassword_test.go@@ -48,3 +48,56 @@ func TestCJKPasswordMasked(t *testing.T) {}}}++// A key, a secret or a token written after its name as a Chinese message+// has it is masked, by a Chinese name or an English one before 为, 為, 是 or+// :: "密钥为 xxx" and "apikey 为 xxx" went to the vendor as written+// (CyberCaius on Discord). The name and the words around it stay.+func TestCJKSecretNamesMasked(t *testing.T) {+ cases := []struct{ in, gone, kept string }{+ {"密钥为 a8f3k2m9q1", "a8f3k2m9q1", "密钥为 "},+ {"apikey 为 AKdx93kfLq0z", "AKdx93kfLq0z", "apikey 为 "},+ {"密钥是 7f3e9c2ab41d", "7f3e9c2ab41d", "密钥是 "},+ {"密钥:Zt7qP2vX9wLm", "Zt7qP2vX9wLm", "密钥:"},+ {"秘钥 q8w7e6r5t4y3", "q8w7e6r5t4y3", "秘钥 "},+ {"私钥是 MIIEvQIBADANBgkq", "MIIEvQIBADANBgkq", "私钥是 "},+ {"访问令牌:tk_8d7f6g5h4j3k", "tk_8d7f6g5h4j3k", "访问令牌:"},+ {"token 是 a1b2c3d4e5f6,别外传", "a1b2c3d4e5f6", "token 是 "},+ {"我的 API Key 为 xk-2f9d8s7a6q5w。", "xk-2f9d8s7a6q5w", "API Key 为 "},+ {"API Key:xk2f9d8s7a6q5w", "xk2f9d8s7a6q5w", "API Key:"},+ {"账号密码 admin123", "admin123", "账号密码 "},+ {"口令是 Zx!2025q", "Zx!2025q", "口令是 "},+ {"密码为 hunter22", "hunter22", "密码为 "},+ {"金鑰為 9Kd8s7Fq6w5e", "9Kd8s7Fq6w5e", "金鑰為 "},+ {"密鑰:Pq9w8e7r6t5y", "Pq9w8e7r6t5y", "密鑰:"},+ {"access_token 是 ya29a0AfH6SMB", "ya29a0AfH6SMB", "access_token 是 "},+ {"Secret 為「s3cr3tValue9」", "s3cr3tValue9", "Secret 為「"},+ {"WiFi密码:20261025", "20261025", "WiFi密码:"},+ {"admin密钥是k9j8h7g6f5", "k9j8h7g6f5", "admin密钥是"},+ }+ 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":"apikey 为 AKdx93kfLq0z"}]}`+ if out, n := MaskJSON([]byte(body), Options{Secrets: true}); n != 1 || strings.Contains(string(out), "AKdx93kfLq0z") || string(RestoreJSON(out)) != body {+ t.Errorf("body: %d %s", n, out)+ }+ // what isn't a secret: no value, words about keys, a placeholder, a+ // number, a word, too short+ for _, in := range []string{+ "请输入密钥", "请输入密钥:", "密钥管理", "忘记密钥了怎么办", "令牌桶算法", "私钥文件放在哪",+ "密钥:${API_KEY}", "密钥:****", "token 是 ****", "apikey 为 ${OPENAI_API_KEY}",+ "RSA 密钥为 2048 位", "token 是 expired", "这个 key 是 value", "token 是 abc",+ "monkey 是 动物", "key 是什么", "API Key 为空",+ } {+ if out, n := Mask(in, Options{Secrets: true}); n != 0 {+ t.Errorf("%q masked: %q", in, out)+ }+ }+}