Files
magpie/internal/redact/eval_cases_test.go
T
yetone 552cdf8071 redact: a value the model spans from the delimiter before it is masked (#3591)
The model's span may start at the token before a value: the `":"` of
a JSON field, a CSV row's comma. A phone number in
{"phone":"+86 139 1234 5678"} came as `":"+86 139 1234 5678`, and in a
CSV row as `,+81 90-1234-5678`, which the PHONE check took for no
number, and it went as written. A name came as `"Wei Zhang"` and was
masked with its quotes, so the JSON lost them. modelSpans now trims
spaces and `"',;:` from both ends of a name's, a phone's, an
address's, an account's and an email's span; a secret's only spaces,
since a password may start or end with a quote.

Found by TestMaskEval: its 3 #3591 phones are no longer Known, and
"both" reads 33/34 masked, 0/40 wrongly (from 30/34).

Run: go test ./internal/redact and ./internal/pii ok; -run
'Mask|Redact|Privacy' ./internal/gui ./internal/gateway ok; go vet
darwin, linux, windows; gofmt -l clean.
Broken by hand: edge = isSpace for every kind: TestModelSpanLeavesDelimiters
red (both phones as written, the name's quotes gone) and TestMaskEval
red on the 3 phones.
Not tried: the full suite (internal/proc's #3567 is red on main), a
real request through the gateway with the model on.
2026-10-25 17:44:52 +08:00

229 lines
10 KiB
Go

package redact_test
// evalCase is one text of the evaluation (eval_test.go): where it comes
// from (an issue or a commit, or "true" for one of the values masking is
// for), the values in it to mask and those to keep as written.
type evalCase struct {
ID, From, Text string
Mask, Keep []evalValue
}
// evalValue is a value in a case's text. Kind is what it is, or for one
// to keep what it was or could be taken for. Known, where set, is the
// issue of a value that comes out wrong with the rules and the model.
type evalValue struct{ V, Kind, Known string }
// The keys are made up, in the vendors' shapes; the people, places and
// numbers are made up or the ones vendors and standards give as examples.
var (
evalOpenAIKey = "sk-proj-" + "Qm7vT2xLk9RbW4nZpE6sJc8YhD3fGa5uKiN1oVr0"
evalGitHub = "ghp_" + "0123456789abcdefghijABCDEFGHIJ0123"
evalAWSSecret = "wJalrXUtnFEMI/K7MDENG/" + "bPxRfiCYEXAMPLEKEY"
evalGoogleKey = "AIzaSy" + "B3xQ9mLk2Vr7TpW4nZs8YhC1dFgJ6uKe0oA"
evalJWT = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." + "eyJzdWIiOiI0MjEiLCJuYW1lIjoiTWFyaWEifQ." + "x7Hq2LmZ9pVb4WnT8sRc1YeKd6gJ3uFa0oNi5vQwE"
)
var evalCases = []evalCase{
// ---- reported: masked and should not have been ----------------------
{
ID: "snake-arg", From: "#3144",
Text: "parser.add_argument(\"--sequential_cutoff\", type=int, default=3,\n help=\"stop after this many misses\")",
Keep: []evalValue{{V: "sequential_cutoff", Kind: "PERSON"}},
},
{
ID: "snake-words", From: "#3144",
Text: "def build_index(max_retries, warmup_steps):\n return Index(stream_options=None, include_usage=True)",
Keep: []evalValue{{V: "max_retries", Kind: "PERSON"}, {V: "warmup_steps", Kind: "PERSON"}, {V: "stream_options", Kind: "PERSON"}, {V: "include_usage", Kind: "PERSON"}},
},
{
ID: "tool-servers", From: "#3143",
Text: "Use mcp__claude-in-chrome__navigate to open the page, then mcp__linear__save_issue to file it.",
Keep: []evalValue{{V: "mcp__claude-in-chrome__navigate", Kind: "PERSON"}, {V: "mcp__linear__save_issue", Kind: "PERSON"}},
},
{
ID: "retina-file", From: "#3029",
Text: "Here is the screenshot: ![shot](/Users/dev/Desktop/261020_14-16-11@2x.jpg) — the button is cut off.",
Keep: []evalValue{{V: "261020_14-16-11@2x.jpg", Kind: "EMAIL"}},
},
{
ID: "asset-files", From: "#3029",
Text: "import logo from './assets/logo@3x.png';\nimport intro from './media/intro@2x.mp4';",
Keep: []evalValue{{V: "logo@3x.png", Kind: "EMAIL"}, {V: "intro@2x.mp4", Kind: "EMAIL"}},
},
{
ID: "url-userinfo", From: "#3538",
Text: "DATABASE_URL=postgres://app:hunter2pass@db.acme-corp.cn:5432/orders",
Mask: []evalValue{{V: "hunter2pass", Kind: "SECRET"}},
Keep: []evalValue{{V: "db.acme-corp.cn", Kind: "EMAIL"}},
},
{
ID: "git-user-url", From: "#3538",
Text: "git clone https://git@gitlab.acme-corp.cn/team/repo.git && cd repo",
Keep: []evalValue{{V: "git@gitlab.acme-corp.cn", Kind: "EMAIL"}},
},
{
ID: "unix-time", From: "#3577",
Text: `{"type":"tool_call","id":"call_7","ts":1761379200,"name":"read_file","args":{"path":"main.go"}}`,
Keep: []evalValue{{V: "1761379200", Kind: "PHONE"}},
},
{
ID: "unix-time-ms", From: "#3577",
Text: "tool_call finished at 1761379200123 (took 812 ms)",
Keep: []evalValue{{V: "1761379200123", Kind: "PHONE"}},
},
{
ID: "session-uuid", From: "d3db461bb",
Text: "Read ~/.claude/projects/-Users-dev-app/3f2b6c1e-8a4d-4f7e-9b1a-2c3d4e5f6a7b.jsonl for the transcript.",
Keep: []evalValue{{V: "3f2b6c1e-8a4d-4f7e-9b1a-2c3d4e5f6a7b", Kind: "SECRET"}},
},
{
ID: "digests", From: "d3db461bb",
Text: "sha256: 80efc1803eda7c095a79741d2008c07e2e0a57b01bac8825fbeb448fd097998c\ncommit b855cfee9a1c3d4e5f60718293a4b5c6d7e8f901",
Keep: []evalValue{{V: "80efc1803eda7c095a79741d2008c07e2e0a57b01bac8825fbeb448fd097998c", Kind: "SECRET"}, {V: "b855cfee9a1c3d4e5f60718293a4b5c6d7e8f901", Kind: "ACCOUNT"}},
},
{
ID: "tool-ids", From: "611b9e173",
Text: `{"type":"tool_result","tool_use_id":"toolu_01XkP9qLm2VbT7wZ4nR8sYc3","content":"ok"} request req_011CUq7vHn3kLp9WmT2xZb4R`,
Keep: []evalValue{{V: "toolu_01XkP9qLm2VbT7wZ4nR8sYc3", Kind: "SECRET"}, {V: "req_011CUq7vHn3kLp9WmT2xZb4R", Kind: "SECRET"}},
},
{
ID: "token-count", From: "611b9e173",
Text: "<system-reminder><total_tokens>14902266 tokens left</total_tokens></system-reminder>",
Keep: []evalValue{{V: "14902266", Kind: "ACCOUNT"}},
},
{
ID: "assistant-names", From: "611b9e173",
Text: "You are Claude, powered by Opus 5.5. Codex and Haiku 4.5 may also answer in this session.",
Keep: []evalValue{{V: "Claude", Kind: "PERSON"}, {V: "Opus 5.5", Kind: "PERSON"}, {V: "Codex", Kind: "PERSON"}, {V: "Haiku 4.5", Kind: "PERSON"}},
},
{
ID: "home-path", From: "611b9e173",
Text: "cd /Users/jdoe/workspace/projects/magpie && go test ./internal/redact",
Keep: []evalValue{{V: "jdoe", Kind: "PERSON"}, {V: "magpie", Kind: "PERSON"}},
},
{
ID: "grep-pattern", From: "611b9e173",
Text: "rg -n 'password|passwd|secret_key|api_token' --glob '*.go'",
Keep: []evalValue{{V: "password|passwd|secret_key|api_token", Kind: "SECRET"}},
},
{
ID: "sizes-versions", From: "611b9e173",
Text: "Screenshot 1624x1487 at 2x; magpie v0.1.1513 built with go1.26.8 on darwin/arm64.",
Keep: []evalValue{{V: "1624x1487", Kind: "PHONE"}, {V: "v0.1.1513", Kind: "ACCOUNT"}, {V: "1.26.8", Kind: "PHONE"}},
},
{
ID: "noreply", From: "true",
Text: "Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>",
Keep: []evalValue{{V: "49699333+dependabot[bot]@users.noreply.github.com", Kind: "EMAIL"}},
},
{
ID: "example-mail", From: "true",
Text: "// e.g. send(\"user@example.com\", subject)",
Keep: []evalValue{{V: "user@example.com", Kind: "EMAIL"}},
},
{
ID: "code-locations", From: "true",
Text: "internal/redact/model.go:212:9: undefined: epochNumber (pid 48213, exit status 2)",
Keep: []evalValue{{V: "48213", Kind: "PHONE"}, {V: "model.go:212", Kind: "ACCOUNT"}},
},
{
ID: "colors-ports", From: "true",
Text: ".btn { color: #1a2b3c; } server listening on 127.0.0.1:3425, retry in 30000ms",
Keep: []evalValue{{V: "#1a2b3c", Kind: "SECRET"}, {V: "30000", Kind: "PHONE"}},
},
{
ID: "zh-words", From: "#1977",
Text: "请帮我把这个函数改成异步的,并且加上错误处理和单元测试。",
Keep: []evalValue{{V: "函数", Kind: "PERSON"}, {V: "错误处理", Kind: "PERSON"}},
},
// ---- what masking is for --------------------------------------------
{
ID: "prose-person", From: "true",
Text: "Please email Maria Gonzalez about the overdue invoice before Friday.",
Mask: []evalValue{{V: "Maria Gonzalez", Kind: "PERSON"}},
},
{
ID: "git-author", From: "true",
Text: "commit 4f1c2d9\nAuthor: Jane Smith <jane.smith@gmail.com>\nDate: Tue Oct 21 09:14:03 2026 +0800",
Mask: []evalValue{{V: "Jane Smith", Kind: "PERSON"}, {V: "jane.smith@gmail.com", Kind: "EMAIL"}},
},
{
ID: "json-contact", From: "true",
Text: `{"customer":{"name":"Wei Zhang","email":"wei.zhang@outlook.com","phone":"+86 139 1234 5678"}}`,
Mask: []evalValue{{V: "Wei Zhang", Kind: "PERSON"}, {V: "wei.zhang@outlook.com", Kind: "EMAIL"}, {V: "139 1234 5678", Kind: "PHONE"}},
},
{
ID: "us-address", From: "true",
Text: "Ship the replacement to 742 Evergreen Terrace, Springfield, OR 97477 and call (212) 555-0199 on arrival.",
Mask: []evalValue{{V: "742 Evergreen Terrace", Kind: "ADDRESS"}, {V: "555-0199", Kind: "PHONE"}},
},
{
ID: "zh-contact", From: "#1977",
Text: "联系人:张伟,电话 13800138000,地址:北京市朝阳区建国路88号院3号楼",
Mask: []evalValue{{V: "张伟", Kind: "PERSON"}, {V: "13800138000", Kind: "PHONE"}, {V: "建国路88号", Kind: "ADDRESS"}},
},
{
ID: "zh-prose", From: "#1977",
Text: "王芳下周一去上海出差,酒店那边请联系她的助理刘洋。",
Mask: []evalValue{{V: "王芳", Kind: "PERSON"}, {V: "刘洋", Kind: "PERSON", Known: "#3592"}},
},
{
ID: "log-user", From: "true",
Text: "2026-10-21T09:14:03Z INFO login ok user=li.na@qq.com tenant=acme ip=10.2.3.4",
Mask: []evalValue{{V: "li.na@qq.com", Kind: "EMAIL"}},
},
{
ID: "phone-intl", From: "true",
Text: "If the build breaks overnight, page Tom on +1 415-555-0132.",
Mask: []evalValue{{V: "415-555-0132", Kind: "PHONE"}, {V: "Tom", Kind: "PERSON"}},
},
{
ID: "card", From: "true",
Text: "The customer's card number is 4111 1111 1111 1111, expiry 12/29.",
Mask: []evalValue{{V: "4111 1111 1111 1111", Kind: "ACCOUNT"}},
},
{
ID: "iban", From: "true",
Text: "Wire the refund to IBAN DE89 3704 0044 0532 0130 00 (Commerzbank).",
Mask: []evalValue{{V: "DE89 3704 0044 0532 0130 00", Kind: "ACCOUNT"}},
},
{
ID: "zh-id", From: "true",
Text: "身份证号 11010519491231002X,开户行 招商银行",
Mask: []evalValue{{V: "11010519491231002X", Kind: "ACCOUNT"}},
},
{
ID: "env-file", From: "true",
Text: "OPENAI_API_KEY=" + evalOpenAIKey + "\nDB_PASSWORD=Tr0ub4dor&3xyz\nDEBUG=true",
Mask: []evalValue{{V: evalOpenAIKey, Kind: "SECRET"}, {V: "Tr0ub4dor&3xyz", Kind: "SECRET"}},
Keep: []evalValue{{V: "DEBUG=true", Kind: "SECRET"}},
},
{
ID: "prose-password", From: "true",
Text: "the staging db password is hunter2Xq9z, don't commit it",
Mask: []evalValue{{V: "hunter2Xq9z", Kind: "SECRET"}},
},
{
ID: "zh-password", From: "true",
Text: "服务器密码是 Qw8!zLp3mN,用完记得改掉",
Mask: []evalValue{{V: "Qw8!zLp3mN", Kind: "SECRET"}},
},
{
ID: "bearer", From: "true",
Text: "curl -H 'Authorization: Bearer " + evalJWT + "' https://api.acme-corp.cn/v1/me",
Mask: []evalValue{{V: evalJWT, Kind: "SECRET"}},
},
{
ID: "code-keys", From: "true",
Text: "const gh = \"" + evalGitHub + "\";\nconst maps = '" + evalGoogleKey + "';\naws_secret_access_key = " + evalAWSSecret,
Mask: []evalValue{{V: evalGitHub, Kind: "SECRET"}, {V: evalGoogleKey, Kind: "SECRET"}, {V: evalAWSSecret, Kind: "SECRET"}},
},
{
ID: "tool-result-mixed", From: "true",
Text: "$ cat contacts.csv\nname,email,phone\nRina Kobayashi,rina.k@fastmail.com,+81 90-1234-5678\nOmar Haddad,omar.haddad@proton.me,+44 7700 900123",
Mask: []evalValue{{V: "Rina Kobayashi", Kind: "PERSON"}, {V: "rina.k@fastmail.com", Kind: "EMAIL"}, {V: "90-1234-5678", Kind: "PHONE"}, {V: "Omar Haddad", Kind: "PERSON"}, {V: "omar.haddad@proton.me", Kind: "EMAIL"}, {V: "7700 900123", Kind: "PHONE"}},
Keep: []evalValue{{V: "contacts.csv", Kind: "EMAIL"}},
},
}