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.
This commit is contained in:
yetone
2026-10-25 17:44:52 +08:00
parent 033990e642
commit 552cdf8071
3 changed files with 39 additions and 4 deletions
+2 -2
View File
@@ -151,7 +151,7 @@ var evalCases = []evalCase{
{
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", Known: "#3591"}},
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",
@@ -222,7 +222,7 @@ var evalCases = []evalCase{
{
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", Known: "#3591"}, {V: "Omar Haddad", Kind: "PERSON"}, {V: "omar.haddad@proton.me", Kind: "EMAIL"}, {V: "7700 900123", Kind: "PHONE", Known: "#3591"}},
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"}},
},
}
+15 -2
View File
@@ -59,10 +59,14 @@ func modelSpans(s string, o Options) []span {
continue
}
a, b := f.Start, f.End
for a < b && isSpace(s[a]) {
edge := isSpace
if kind != "SECRET" {
edge = delimiter
}
for a < b && edge(s[a]) {
a++
}
for b > a && isSpace(s[b-1]) {
for b > a && edge(s[b-1]) {
b--
}
if kind == "PERSON" {
@@ -86,6 +90,15 @@ func modelSpans(s string, o Options) []span {
func isSpace(c byte) bool { return c == ' ' || c == '\t' || c == '\n' || c == '\r' }
// delimiter says c may end a span of the model's and is no part of a
// name, a phone number, an address or an account: a space, or what is
// between values in JSON or a CSV row. The model's span may start at the
// token before the value, and a phone number in {"phone":"+86 139 …"}
// came as `":"+86 139 …`, or in a CSV row as `,+81 90-…`, and was no
// number to the PHONE check: it went as written (#3591). A secret's
// edges are its own (a password may start with a quote).
func delimiter(c byte) bool { return isSpace(c) || strings.IndexByte(`"',;:`, c) >= 0 }
// modelTakes says v is masked as the model's kind: a secret as long as the
// rules ask of one, never an id a vendor or a tool made (req_…, toolu_…,
// call_…), which the agent and the vendor match against another; a name
+22
View File
@@ -1,6 +1,7 @@
package redact
import (
"regexp"
"slices"
"strings"
"testing"
@@ -110,6 +111,27 @@ func TestModelSpansFiltered(t *testing.T) {
}
}
// A span the model starts at the delimiter before a value is the value's:
// a phone number in JSON or a CSV row is masked, and the delimiter goes as
// written (#3591, from the evaluation's json-contact and tool-result-mixed).
func TestModelSpanLeavesDelimiters(t *testing.T) {
f := &fakeModel{values: map[string]string{
`":"+86 139 1234 5678`: "private_phone",
`,+81 90-1234-5678`: "private_phone",
`,rina.k@fastmail.com`: "private_email",
`"Wei Zhang"`: "private_person",
}}
o := Options{Personal: true, Model: f.find}
for _, c := range []struct{ in, want string }{
{`{"name":"Wei Zhang","phone":"+86 139 1234 5678"}`, `^\{"name":"\{\{PERSON_[a-z2-7]{8}\}\}","phone":"\{\{PHONE_[a-z2-7]{8}\}\}"\}$`},
{"Rina,rina.k@fastmail.com,+81 90-1234-5678\n", `^Rina,\{\{EMAIL_[a-z2-7]{8}\}\},\{\{PHONE_[a-z2-7]{8}\}\}\n$`},
} {
if out, _ := Mask(c.in, o); !regexp.MustCompile(c.want).MatchString(out) {
t.Errorf("%q went as %q", c.in, out)
}
}
}
// A request's texts go to the model once, together, each once: never its
// ids, kinds or tools' definitions, which are never masked, nor what a
// vendor sealed with a signature.