diff --git a/internal/redact/eval_cases_test.go b/internal/redact/eval_cases_test.go index c938db85c..2306cab21 100644 --- a/internal/redact/eval_cases_test.go +++ b/internal/redact/eval_cases_test.go @@ -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"}}, }, } diff --git a/internal/redact/model.go b/internal/redact/model.go index ae0624ce0..d56d53835 100644 --- a/internal/redact/model.go +++ b/internal/redact/model.go @@ -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 diff --git a/internal/redact/model_test.go b/internal/redact/model_test.go index 916e07cd1..4da54aaf4 100644 --- a/internal/redact/model_test.go +++ b/internal/redact/model_test.go @@ -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.