Commit Graph
3 Commits
Author SHA1 Message Date
yetone e4809a150a redact: a key of digits only after 密钥为, token 是, apikey:… is masked; PIN码, 安全码, 授权码 join the names (CyberCaius on Discord)
"密钥为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.
2026-10-25 16:55:06 +08:00
yetone ee715c2e0e redact: a key or token after 密钥, 令牌, 金鑰…, or apikey/token before 为/是/:, is masked (CyberCaius on Discord)
714ab8e8b masked a password after 密码/密碼/口令/パスワード/비밀번호. The reporter
came back with the rest of how a secret is sent in a Chinese chat: "密钥为 xxx",
"apikey 为 xxx", 密钥是, 秘钥, 私钥, 令牌, "token 是 xxx", "API Key 为 xxx". These
went to the vendor as written.

Now:
- A value after a CJK name for a key, secret or token is masked as SECRET:
  密钥, 密鑰, 秘钥, 秘鑰, 私钥, 私鑰, 金钥, 金鑰, 令牌, 权杖, 權杖, 秘密鍵, トークン,
  シークレット, APIキー, アクセスキー, 토큰, 비밀키, API 키. The value has letters and
  digits both, or is 16+ characters with a letter, so "RSA 密钥为 2048 位" stays.
- An English name (password, passwd, pwd, secret, token, api key, access key,
  private key, credential, key; DB_PASSWORD and access_token too) right before
  为, 為, 是 or a full-width colon is masked the same way. "token: x" with an
  ASCII colon stays the config rule's, as before.
- 为 and 為 join :, :, =, 是, は, 는 after any CJK name, so "密码为 hunter22" is
  masked too; 암호 (Korean) joins the password names.

Left as they were (tested): 请输入密钥, 请输入密钥:, 密钥管理, 忘记密钥了怎么办,
令牌桶算法, 私钥文件放在哪, 密钥:${API_KEY}, 密钥:****, token 是 ****,
apikey 为 ${OPENAI_API_KEY}, RSA 密钥为 2048 位, token 是 expired, 这个 key 是 value,
token 是 abc, key 是什么, API Key 为空, and every case of TestCJKPasswordMasked.

Cost: the English rule is run only near 为/為/是/: (fold, 24 bytes back,
260 ahead), and the CJK rules only near their names. Doing that showed that
matches() looked back for the line's start from every fold marker through
the whole text, which is quadratic on a one-line request body: with the
English rule folded on key/token, Mask over 1.5MB of dense text took 10.4s
(0.12s before). The line's start is now looked for no further back than
`back`, which is what the window used anyway. Benchmarked Mask on 156KB of
dense Chinese, of dense code+JSON, and on redact.go itself, against
origin/main: within noise of it on a box at load ~200 (zh 31-36ms vs 13-37ms,
code 72-84ms vs 55-72ms, src 12-14ms vs 24-36ms); benchmark not committed.

Run at this head:
- go test ./internal/redact ./internal/masking ./internal/otlp ./internal/pii
  ./internal/settings ./internal/sessions -count=1: ok
- go test ./internal/gateway -run 'CJK|Mask|Redact|Privacy' -count=1: ok
- gofmt -l, go vet ./internal/redact, GOOS=windows go vet ./internal/redact: clean
- Broken by hand: with origin/main's redact.go, TestCJKSecretNamesMasked fails
  16 of 19 masked cases and the JSON body case (账号密码 admin123 and 口令是 …
  were already masked). With the CJK rules' window back set to 0,
  "WiFi密码:20261025" and "admin密钥是k9j8h7g6f5" go unmasked (fixed with back 64).

Not tried: the GUI try box in a running magpie, and the full suite.
2026-10-25 15:27:39 +08:00
yetone 714ab8e8bf redact: a password after 密码, 密碼, 口令, パスワード or 비밀번호 is masked (CyberCaius on Discord)
"密码: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.
2026-10-25 14:50:13 +08:00