diff --git a/internal/redact/model.go b/internal/redact/model.go index 3791da248..3ff337737 100644 --- a/internal/redact/model.go +++ b/internal/redact/model.go @@ -68,7 +68,7 @@ func modelSpans(s string, o Options) []span { if kind == "PERSON" { a, b = zhTrim(s, a, b) } - if a < b && utf8.ValidString(s[a:b]) && modelTakes(kind, s[a:b]) { + if a < b && utf8.ValidString(s[a:b]) && modelTakes(kind, s[a:b]) && modelFits(s, a, b, kind) { out = append(out, span{a, b, kind, ByModel}) } } @@ -150,7 +150,7 @@ var ( // (req_011CS…, toolu_01A…, call_x9…, msg_…). func madeID(v string) bool { p, rest, ok := strings.Cut(v, "_") - if !ok || len(p) < 2 || len(p) > 6 || len(rest) < 8 { + if !ok || len(p) < 2 || len(p) > 8 || len(rest) < 8 { return false } for _, c := range p { @@ -192,3 +192,200 @@ func withModel(o Options, texts []string) Options { } return o } + +// modelFits says the model's value at s[a:b] is what its kind says by +// where it is, as modelTakes does by what it is. Run on what agents send +// (3 MB of Claude Code's requests, 10-25), the model masked ~1,800 +// values where the rules masked 17, most of them wrongly: the count in +// each turn's 14983666 tokens left as an +// account, a run's id as a phone, "Claude" and "Opus 5.5" in the system +// prompt as people, /Users/yetone's folder as a person, the tail of +// toolu_01… and req_011C… as a name or a secret, a grep pattern as a +// secret. Each goes as written now: +// - a piece cut out of a longer word, whatever its kind; +// - a name with a digit, a slash, an @ or a file's dot in it, all in +// lower case (magpie, claude-in-chrome), a path's part, or an +// assistant's or a model's own (Claude, Codex, Haiku 4.5); +// - an account that is a commit's hash, a version, or a number with +// nothing before it to say it is an account's; a phone number that is +// digits only with nothing to say it is one, and no 0 or + to start; +// - a secret that is a pattern's words, a|b|c, holds a UUID (a session's +// file) or is a piece of JSON; +// - an account or a phone number that is a size (1624x1487), drawing +// data (an SVG path's M16 0H44…) or a diff's index line; a phone with +// fewer than 7 digits, an address with no letter. +// +// A name's words each start with a capital, but for the particles names +// have (van, de, bin): "local LLM", "User yetone" and claudeKeychain are +// a program's or a log's words. +func modelFits(s string, a, b int, kind string) bool { + if a > 0 && wordByte(s[a-1]) && wordByte(s[a]) || b < len(s) && wordByte(s[b-1]) && wordByte(s[b]) { + return false + } + v := s[a:b] + switch kind { + case "PERSON": + return personFits(s, a, b, v) + case "ACCOUNT": + if hexRe.MatchString(v) && strings.ContainsAny(v, "abcdef") || versionRe.MatchString(v) || !numberLike(v) { + return false + } + return strings.Trim(v, digits) != "" || cued(s, a, accountCues) + case "PHONE": + if !numberLike(v) || countDigits(v) < 7 { + return false + } + return strings.Trim(v, digits) != "" || v[0] == '0' || cued(s, a, phoneCues) + case "ADDRESS": + return strings.IndexFunc(v, unicode.IsLetter) >= 0 + case "SECRET": + if !wordByte(v[0]) || patternWords(v) || uuidIn.MatchString(v) || strings.Contains(v, `": `) || strings.Contains(v, `", "`) || linkRe.MatchString(v) { + return false + } + // a piece of a longer base64 run (an image, a signature) is cut + // from it at a '+' or '/' + if a > 1 && (s[a-1] == '+' || s[a-1] == '/') && wordByte(s[a-2]) || b+1 < len(s) && (s[b] == '+' || s[b] == '/') && wordByte(s[b+1]) { + return false + } + // a bare hex run is a hash or an id unless something calls it a key; + // one with words after it is a hash in a tool's output + if f := strings.Fields(v); len(f) > 1 && hexRe.MatchString(f[0]) || modCacheRe.MatchString(v) { + return false + } + return !hexRe.MatchString(v) || cued(s, a, secretCues) + case "EMAIL": + return wordByte(v[0]) && !modCacheRe.MatchString(v) + } + return true +} + +func wordByte(c byte) bool { + return c == '_' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' +} + +func personFits(s string, a, b int, v string) bool { + if strings.ContainsAny(v, "0123456789/\\@<>|=_") || strings.IndexFunc(v, unicode.IsUpper) < 0 && isASCII(v) { + return false + } + // a file's name: LESSONS.md, a dot with a letter right after it + for i := 0; i+1 < len(v); i++ { + if v[i] == '.' && (v[i+1] >= 'a' && v[i+1] <= 'z' || v[i+1] >= 'A' && v[i+1] <= 'Z') { + return false + } + } + // a path's part: /Users/yetone, ~/.orca/ + if a > 0 && (s[a-1] == '/' || s[a-1] == '\\' || s[a-1] == '.' && a > 1 && (s[a-2] == '/' || s[a-2] == '\\')) || b < len(s) && (s[b] == '/' || s[b] == '\\') { + return false + } + if strings.ContainsRune(v, ':') { + return false + } + for i, w := range strings.Fields(v) { + if c := w[0]; c >= 'a' && c <= 'z' && (i == 0 || !nameParticles[w]) { + return false + } + if assistantNames[strings.ToLower(w)] { + return false + } + } + return true +} + +// nameParticles are the words in lower case a name may have between its +// others: Ludwig van Beethoven, Ibn al Haytham. +var nameParticles = map[string]bool{ + "van": true, "von": true, "der": true, "den": true, "de": true, "da": true, "del": true, "della": true, + "di": true, "du": true, "la": true, "le": true, "bin": true, "binti": true, "al": true, "el": true, + "y": true, "dos": true, "das": true, "do": true, "ter": true, "ten": true, "zu": true, +} + +// numberLike says v is written as a number is: digits, and the spaces, +// dashes, dots, brackets and + that group them, with a letter or two at +// most (an IBAN's, ext.). A size is no number (1624x1487). +func numberLike(v string) bool { + if sizeRe.MatchString(v) || strings.Contains(v, "..") { + return false + } + letters := 0 + for _, c := range v { + switch { + case c >= '0' && c <= '9', strings.ContainsRune(" -.()+/", c): + case unicode.IsLetter(c): + letters++ + default: + return false + } + } + return letters <= 4 && countDigits(v)*2 > len(v) +} + +func countDigits(v string) int { + n := 0 + for i := 0; i < len(v); i++ { + if v[i] >= '0' && v[i] <= '9' { + n++ + } + } + return n +} + +func isASCII(v string) bool { + for i := 0; i < len(v); i++ { + if v[i] >= utf8.RuneSelf { + return false + } + } + return true +} + +// assistantNames are what agents and models call themselves, in every +// system prompt: the model takes each for a person's name. +var assistantNames = map[string]bool{ + "claude": true, "chatgpt": true, "gpt": true, "codex": true, "gemini": true, "copilot": true, + "grok": true, "kimi": true, "qwen": true, "deepseek": true, "cursor": true, "devin": true, + "kiro": true, "opencode": true, "opus": true, "sonnet": true, "haiku": true, "fable": true, + "glm": true, "doubao": true, "magpie": true, "jev": true, +} + +var ( + hexRe = regexp.MustCompile(`^[0-9a-f]{7,64}$`) + versionRe = regexp.MustCompile(`^v?\d+(?:\.\d+)+$`) + sizeRe = regexp.MustCompile(`^\d+ ?[x×] ?\d+$`) + uuidIn = regexp.MustCompile(`(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`) + // a name in Go's module cache (d6426ecaaf0a28de@v31) + modCacheRe = regexp.MustCompile(`^[0-9a-f]{16}@v\d+$`) + // a link, or a host's name with a path (discord.gg/…) + linkRe = regexp.MustCompile(`^(?:[a-z][a-z0-9+.-]*://|[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+/)`) +) + +var ( + accountCues = []string{"account", "acct", "a/c", "iban", "routing", "sort code", "card", "账号", "帐号", "账户", "帐户", "卡号", "户名"} + secretCues = []string{"key", "token", "secret", "password", "passwd", "密钥", "密码", "令牌"} + phoneCues = []string{"phone", "tel", "mobile", "cell", "call", "whatsapp", "sms", "电话", "手机", "联系", "致电"} +) + +// cued says one of cues is in the 40 bytes before at, in any case. +func cued(s string, at int, cues []string) bool { + before := strings.ToLower(s[max(0, at-40):at]) + for _, c := range cues { + if strings.Contains(before, c) { + return true + } + } + return false +} + +// 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 { + parts := strings.Split(v, "|") + if len(parts) < 3 { + return false + } + for _, p := range parts { + if p == "" || strings.IndexFunc(p, unicode.IsLetter) < 0 || strings.Trim(p, alnum+"_.-") != "" { + return false + } + } + return true +} diff --git a/internal/redact/modelfits_test.go b/internal/redact/modelfits_test.go new file mode 100644 index 000000000..7bd450ca0 --- /dev/null +++ b/internal/redact/modelfits_test.go @@ -0,0 +1,70 @@ +package redact + +import ( + "strings" + "testing" +) + +// What the local model took for personal data or a secret in real agent +// requests, and isn't: each text is as a Claude Code or Codex request had +// it on 10-25, with the piece the model reported. Measured over 3 MB of +// them, the model alone masked ~1,800 such pieces; the rules 17. +func TestModelMisreadsNotMasked(t *testing.T) { + for _, c := range []struct{ text, value, label string }{ + // the count each turn carries + {"14881209 tokens left", "14881209", "account_number"}, + // run ids, hashes and versions + {"| 6123456789 | ci.yml | success |", "6123456789", "account_number"}, + {"ba319a758 gateway: the lone tool_choice none", "ba319a758", "account_number"}, + {"Update to v0.1.1442 now", "0.1.1442", "account_number"}, + {"a screenshot of 1624x1487 pixels", "1624x1487", "private_phone"}, + // the model's and the assistant's own names + {"You are powered by the model named Opus 5.5.", "Opus", "private_person"}, + {"Claude publishes it as an Artifact.", "Claude", "private_person"}, + {"Another Claude session holds the port.", "Another Claude", "private_person"}, + // a user name inside a path, a skill's name + {"read /Users/yetone/workspace/projects/magpie/AGENTS.md", "yetone", "private_person"}, + {"- artifact-design: Design guidance", "artifact-design", "private_person"}, + // ids cut out of longer words + {`"tool_use_id":"toolu_01XyZabcDEF123456"`, "XyZabcDEF123456", "secret"}, + {`"id":"srvtoolu_01AbCdEfGhIjKlMnOp"`, "srvtoolu_01AbCdEfGhIjKlMnOp", "secret"}, + {"a97c9bc67cc1f1e27", ">a97c9bc67cc1f1e27", "secret"}, + {"hex40 ok? 1 2f1d0c9b8a7e6f5d4c3b2a1908f7e6d5c4b3a291", "2f1d0c9b8a7e6f5d4c3b2a1908f7e6d5c4b3a291", "secret"}, + {"919585d1700e515ac38da49e31bc60a527fc15c2 ok? 1", "919585d1700e515ac38da49e31bc60a527fc15c2 ok? 1", "secret"}, + // names in Go's module cache, a path's piece + {"/Users/x/Library/Caches/go-build/63700b7ab65fd1d3@v63", "63700b7ab65fd1d3@v63", "secret"}, + {"d6426ecaaf0a28de@v31", "d6426ecaaf0a28de@v31", "private_email"}, + {"P=/Users/yetone/.npm/_npx/1e7f6d9597241db0", "/yetone/.", "private_email"}, + // a piece of a base64 image + {"iVBORw0KGgoAAAANSUhEUgAA+k5zM7Md7tM9ehxd51xF/isvM9", "k5zM7Md7tM9ehxd51xF", "secret"}, + // a grep alternation, a session's file, a link + {"rg 'modelTakes|modelFits|personFits' internal", "modelTakes|modelFits|personFits", "secret"}, + {"wrote 31a4f944-e7cf-4f6f-acc7-eef50d9911dd.jsonl", "31a4f944-e7cf-4f6f-acc7-eef50d9911dd.jsonl", "secret"}, + {"join at discord.gg/Ab3dEf9h today", "discord.gg/Ab3dEf9h", "secret"}, + } { + f := &fakeModel{values: map[string]string{c.value: c.label}} + if out, n := Mask(c.text, Options{Secrets: true, Personal: true, Model: f.find}); n != 0 { + t.Errorf("%q as %s: masked %q", c.value, c.label, out) + } + } +} + +// What the checks above leave out is still masked. +func TestModelFindsStillMasked(t *testing.T) { + for _, c := range []struct{ text, value, label string }{ + {"my account is 13393848347348", "13393848347348", "account_number"}, + {"Ask Rina Kowalczyk about it", "Rina Kowalczyk", "private_person"}, + {"Ludwig van Beethoven wrote it", "Ludwig van Beethoven", "private_person"}, + {"call me on 13800138000 tonight", "13800138000", "private_phone"}, + {"ring 020 7946 0958 after six", "020 7946 0958", "private_phone"}, + {"send it to 42 Wallaby Way, Sydney", "42 Wallaby Way, Sydney", "private_address"}, + {"the vault phrase is Zq8vR2mT6wLp", "Zq8vR2mT6wLp", "secret"}, + {"api key: 9f2c4e1a7b3d5c8e9f2c4e1a7b3d5c8e", "9f2c4e1a7b3d5c8e9f2c4e1a7b3d5c8e", "secret"}, + {"write to rina.k@kowalczyk.pl", "rina.k@kowalczyk.pl", "private_email"}, + } { + f := &fakeModel{values: map[string]string{c.value: c.label}} + if out, _ := Mask(c.text, Options{Secrets: true, Personal: true, Model: f.find}); strings.Contains(out, c.value) { + t.Errorf("%q as %s: left in %q", c.value, c.label, out) + } + } +}