redact: a token count is not a secret (Discord, steven_4422; #3775)

steven_4422 in #feedback, with a screenshot of the Masking page: "total
number of tokens are masked as secret". Claude Code's system reminder
<total_tokens>1499872 tokens left</total_tokens> went upstream as
<total_tokens>{{SECRET_dkfgb3bf}} tokens left</total_tokens>, listed under
Secrets and found by the local model. #3775 had the same with AstrLink
Guard (<total_tokens>14902266).

Cause: the model labels the count `secret`; modelFits lets a hex run
(hexRe, which a run of decimal digits matches) through when secretCued
finds a key word right before it, and secretCued takes a cue anywhere in
the name before the value, with letters after it: the tag's name
total_tokens ends "token" + "s", so the count was "called a token". The
same held for "total_tokens": N, max_tokens: N and token_count=N.

Fix: a value of decimal digits alone that the model calls a secret is
masked only where the name right before it (then =, :, is, a tag's >,
quotes) ends with a key's, a token's, a password's or a PIN's word
(numberNamedSecret: api_key=, "token": , PIN:, user_pin=, 密码是,
<token>…). A name that runs on past the word counts what it names.
Rules are unchanged (numeric keys after key=/token: labels, phones,
ID numbers keep their own rules). The eval's token-count case now comes
out right for AstrLink Guard, so its #3775 marker is off.

Tests: TestTokenCountIsNoSecret uses the reporter's bytes in a Claude
Code user message's system reminder (the reporter's 7-digit counts, and
8-digit ones as Claude Code sends on a large window; the model's
len >= 8 secret check already let the 7-digit ones go, so the reporter's
exact values may have been 8 digits as read off the screenshot), plus a
usage object, max_tokens, token_count and "spin:". TestNamedNumber-
SecretStillMasked keeps api_key=, "token":, token is, PIN:, user_pin=,
password:, 密码是 and <token>…</token> masked.

Broken by hand: with model.go as on main, TestTokenCountIsNoSecret is
red for the 8-digit counts, total_tokens, max_tokens and token_count
(PIN:/user_pin= were not masked on main either; now they are).

Run (mote-run, base origin/main 93b31f5b7):
- gofmt -l clean; go vet ./internal/redact, GOOS=linux -tags nogui and
  GOOS=windows go vet ./internal/redact ./internal/masking ./internal/pii:
  clean.
- go test -timeout 45m ./internal/redact/... ./internal/masking
  ./internal/pii ./internal/otlp ./internal/sessions ./internal/settings
  ./internal/gui ./internal/gateway: all ok except
  - redact TestMaskEval: the token-count case's known issue was now
    fixed (marker removed, green after);
  - gui: TestCoreScriptBudget (green at 19594cc1c), TestSettingsSave-
    KeepsWhatItDoesNotSend (#4514, red on main, gui untouched here) and
    ten AppKit real-window tests killed at their deadlines on mote
    (#2645, #4125).
- Rebased on origin/main 19594cc1c: go test ./internal/redact/... ok,
  gofmt clean, TestCoreScriptBudget ok, #4514 still red there.

Not tried: the real local model (AstrLink Guard / Privacy Filter) on the
reporter's request; the gateway package was not run again after the
rebase (the commits in between touch usage, provider and the GUI).
This commit is contained in:
yetone
2026-10-31 14:19:02 +08:00
parent 19594cc1cb
commit 03f609e3fd
4 changed files with 104 additions and 2 deletions
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -90,7 +90,7 @@ var evalCases = []evalCase{
{
ID: "token-count", From: "611b9e173",
Text: "<system-reminder><total_tokens>14902266 tokens left</total_tokens></system-reminder>",
Keep: []evalValue{{V: "14902266", Kind: "ACCOUNT", GuardKnown: "#3775"}},
Keep: []evalValue{{V: "14902266", Kind: "ACCOUNT"}},
},
{
ID: "assistant-names", From: "611b9e173",
+42
View File
@@ -360,6 +360,9 @@ func modelFits(s string, a, b int, kind string) bool {
if pinnedAfter(s, b) {
return false
}
if strings.Trim(v, digits) == "" {
return numberNamedSecret(s, a)
}
if wordsOnly(v) && !assignedSecret(s, a) {
return false
}
@@ -936,6 +939,45 @@ func secretCued(s string, at int) bool {
return false
}
// numberNamedSecret says the run of decimal digits at s[a:], which the
// model took for a secret, is given a secret's name: the name right
// before it, then =, :, is or a tag's > (and quotes), ends with a key's,
// a token's, a password's or a PIN's word (api_key=, "token": , PIN:,
// 密码是). Digits alone are a count, a time or an id everywhere else.
// secretCued, which a hex run asks, takes a cue anywhere in the name and
// read Claude Code's <total_tokens>14998720 tokens left</total_tokens>
// as a token: every turn's count went as {{SECRET_…}} (steven_4422 on
// Discord, 10-31). A name that ends past the word counts what it names
// (total_tokens, max_tokens, token_count, keys).
func numberNamedSecret(s string, a int) bool {
before := strings.ToLower(s[max(0, a-40):a])
for {
t := strings.TrimRight(before, " \t\"'`=::>“「")
for _, sep := range []string{" is", " was", "是", "为", "為"} {
t = strings.TrimSuffix(t, sep)
}
if t == before {
break
}
before = t
}
if before == "" {
return false
}
for _, c := range numberSecretNames {
// a PIN's word stands alone (pin, user_pin), not the end of
// another's (spin, mapping)
if strings.HasSuffix(before, c) && (c != "pin" || len(before) == 3 || !unicode.IsLetter(rune(before[len(before)-4]))) {
return true
}
}
return false
}
// numberSecretNames are what a name that gives digits a secret's place
// ends with.
var numberSecretNames = []string{"key", "token", "secret", "password", "passwd", "pass", "pwd", "passcode", "pin", "密钥", "密码", "令牌", "口令"}
// patternWords says v is three or more words joined by |, as a grep's or
// a regexp's alternatives are.
func patternWords(v string) bool {
+60
View File
@@ -0,0 +1,60 @@
package redact
import (
"strings"
"testing"
)
// The count Claude Code puts in a system reminder each turn is no secret,
// though the local model calls it one: steven_4422 on Discord (10-31) saw
// <total_tokens>{{SECRET_dkfgb3bf}} tokens left</total_tokens> on the
// Masking page, found by the local model. The tag's name has "token" in
// it, which secretCued took for the digits' name. The reporter's counts
// are as written, and the same counts at eight digits, which Claude Code
// sends on a large window (14902266) and the model's length check lets in.
func TestTokenCountIsNoSecret(t *testing.T) {
for _, n := range []string{"1499872", "1493809", "14998720", "14938090"} {
reminder := "<system-reminder>\n<total_tokens>" + n + " tokens left</total_tokens>\n</system-reminder>"
f := &fakeModel{values: map[string]string{n: "secret"}}
o := Options{Secrets: true, Personal: true, Model: f.find}
body := `{"model":"claude-opus-4-1","system":[{"type":"text","text":"You are Claude Code."}],"messages":[{"role":"user","content":[{"type":"text","text":` + jsonQuote(reminder) + `},{"type":"text","text":"go on"}]}]}`
if got, k := MaskJSON([]byte(body), o); k != 0 || string(got) != body {
t.Errorf("%s: %d masked: %s", n, k, got)
}
}
// a count in a usage object or a setting is no secret either (each
// its own number: one masked is known, and masked wherever it is)
for _, c := range []struct{ text, v string }{
{`{"usage":{"total_tokens": 14998721}}`, "14998721"},
{`max_tokens: 32000000`, "32000000"},
{`token_count=14998722`, "14998722"},
{`spin: 14998723`, "14998723"},
} {
f := &fakeModel{values: map[string]string{c.v: "secret"}}
o := Options{Secrets: true, Model: f.find}
if out, k := Mask(c.text, o); k != 0 {
t.Errorf("%q: %d masked: %q", c.text, k, out)
}
}
}
// Digits the model calls a secret are masked where a secret's name gives
// them to it: a key's, a token's, a password's or a PIN's.
func TestNamedNumberSecretStillMasked(t *testing.T) {
for _, c := range []struct{ text, v string }{
{"api_key=48213097715", "48213097715"},
{`{"token": "73914682055"}`, "73914682055"},
{"the access token is 73914682055", "73914682055"},
{"PIN: 73914682", "73914682"},
{"user_pin=73914682", "73914682"},
{"wifi password: 83920175", "83920175"},
{"密码是83920175", "83920175"},
{"<token>73914682055</token>", "73914682055"},
} {
f := &fakeModel{values: map[string]string{c.v: "secret"}}
o := Options{Secrets: true, Model: f.find}
if out, _ := Mask(c.text, o); strings.Contains(out, c.v) {
t.Errorf("%q: not masked: %q", c.text, out)
}
}
}