redact: a decimal's digits are not masked as a number of their own (willz, Discord)
willz on Discord: a tool's JSON with "height": 5.13393848347348 went to
the vendor with part of the number as a placeholder. A run of digits
right after the point of a decimal, or right before it, was judged on
its own: 0.13800138000 went as 0.{{PHONE_…}}, 6.4111111111111111e-3 as
6.{{BANK_CARD_…}}e-3, 1.110105194912310038 as 1.{{ID_CARD_…}}, and
13800138000.5 as {{PHONE_…}}.5. The rules' bound only kept digits off
either side, not a point with a digit beyond it.
mask now drops, after noIDs, any span of a personal-data kind (kindCats:
PHONE, BANK_CARD, ID_CARD, SSN, IP, the model's ACCOUNT and PHONE, …)
that starts with a digit after "<digit>." or ends with a digit before
".<digit>" (noDecimals, inDecimal). It is one choke point for every
finder: the rules, the local model's spans and values known from
before. Not changed: secrets (a key or password masked once is masked
wherever it is, a decimal's digits too), the user's words and rules,
and the signed-thinking path, which re-masks only secrets. A phone
number or card standing alone, in text, after a colon, in a JSON
string, or at a sentence's end ("13800138000.") is masked as before.
A number list written "1.13800138000" with no space now goes as
written; a comma decimal (5,13800138000) is not treated as one.
Reporter's literal: at 86254d404 the rules alone leave
5.13393848347348 as written (the 14 fractional digits are too long for
a phone or a card); it is masked when the local model labels the
fraction (TestDecimalDigitsNotMaskedByModel, 5.{{ACCOUNT_…}} on the old
code) or once its digits are a known value, which is how it leaked
into another test's body on the old code. Which finder masked it for
willz is not known; the fix covers all three.
Tests (decimal_test.go): TestDecimalDigitsNotMasked (the reporter's
bytes and siblings: negative, exponent, ID card, whole part, text, many
digits; through Mask and MaskJSON in a tool_result),
TestDecimalDigitsNotMaskedByModel, TestKnownNumberNotInDecimal (plain
and in a signed thinking block), TestStandaloneNumbersStillMasked,
TestKnownSecretInDecimalStillMasked.
Run at 28d191fbe (on origin/main 86254d404):
- old redact.go with the new tests: all four decimal tests FAIL as above
(TestStandaloneNumbersStillMasked too, through the known ACCOUNT).
- broken by hand: without the fraction-side check 12 failing lines;
without the whole-part-side check, {"total": 13800138000.5} and
"13800138000.25" fail; without the personal-kind scope,
TestKnownSecretInDecimalStillMasked fails.
- gofmt -l internal/redact: empty. go vet ./..., GOOS=linux go vet
-tags nogui ./..., GOOS=windows go vet ./...: ok.
- go test -timeout 45m ./... (one suite on the machine, checked with
ps): 68 packages ok, no FAIL; internal/redact, internal/gateway and
internal/agent ok. An earlier full run during the edit also passed.
- after rebasing on fresh origin/main: go test ./internal/redact/... ok.
Not tried: the real local model (internal/pii) on willz's text, a real
gateway request, Windows or Linux runs of the package. No docs page
describes the rules' boundaries, so none changed.
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
package redact
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A decimal number is judged whole: the digits after its point, or before
|
||||
// it, are not a phone, a card or an ID number of their own because they
|
||||
// happen to check (willz on Discord: "height": 5.13393848347348).
|
||||
func TestDecimalDigitsNotMasked(t *testing.T) {
|
||||
o := Options{Secrets: true, Personal: true}
|
||||
for _, s := range []string{
|
||||
// the reporter's bytes, as a tool's output has them
|
||||
`{"height": 5.13393848347348}`,
|
||||
// a fraction a phone number's rule takes
|
||||
`{"lat": 0.13800138000, "lng": -3.13800138000}`,
|
||||
// a bank card's (Luhn checks), with an exponent after it
|
||||
`{"ratio": 6.4111111111111111e-3}`,
|
||||
// an ID card's
|
||||
`{"score": 1.110105194912310038}`,
|
||||
// the whole part, with a fraction after it
|
||||
`{"total": 13800138000.5}`,
|
||||
// in text, and many digits
|
||||
`the mean was 2.13800138000 over 12.41111111111111113333 runs`,
|
||||
} {
|
||||
out, n := Mask(s, o)
|
||||
if n != 0 || out != s {
|
||||
t.Errorf("Mask(%q) = %q (%d masked), want it as written", s, out, n)
|
||||
}
|
||||
body := `{"messages":[{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_1","content":` + jsonQuote(s) + `}]}]}`
|
||||
if got, n := MaskJSON([]byte(body), o); n != 0 || string(got) != body {
|
||||
t.Errorf("MaskJSON: %d masked: %s", n, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// What the local model takes for a phone or an account number inside a
|
||||
// decimal is a piece of it too.
|
||||
func TestDecimalDigitsNotMaskedByModel(t *testing.T) {
|
||||
const frac = "13393848347348"
|
||||
f := &fakeModel{values: map[string]string{frac: "account_number"}}
|
||||
o := Options{Secrets: true, Personal: true, Model: f.find}
|
||||
body := `{"messages":[{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_1","content":"{\"height\": 5.` + frac + `}"}]}]}`
|
||||
if got, n := MaskJSON([]byte(body), o); n != 0 || string(got) != body {
|
||||
t.Fatalf("%d masked: %s", n, got)
|
||||
}
|
||||
// the same digits on their own are still the model's to mask
|
||||
body = `{"messages":[{"role":"user","content":"my account is ` + frac + `"}]}`
|
||||
if got, n := MaskJSON([]byte(body), o); n != 1 || strings.Contains(string(got), frac) {
|
||||
t.Fatalf("%d masked: %s", n, got)
|
||||
}
|
||||
}
|
||||
|
||||
// A phone number masked before is not found again in a decimal's digits,
|
||||
// in a thinking block the vendor signed either.
|
||||
func TestKnownNumberNotInDecimal(t *testing.T) {
|
||||
o := Options{Secrets: true, Personal: true}
|
||||
const phone = "13800138000"
|
||||
if out, _ := Mask("call "+phone, o); strings.Contains(out, phone) {
|
||||
t.Fatalf("not masked to begin with: %q", out)
|
||||
}
|
||||
for _, s := range []string{"0." + phone, phone + ".25", "x=-7." + phone + "e2"} {
|
||||
if out, n := Mask("the value "+s+" is right", o); n != 0 {
|
||||
t.Errorf("%q: %d masked: %q", s, n, out)
|
||||
}
|
||||
}
|
||||
body := `{"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"lat is 0.` + phone + `","signature":"Eqk1"}]}]}`
|
||||
if got, n := MaskJSON([]byte(body), o); n != 0 || string(got) != body {
|
||||
t.Fatalf("%d masked: %s", n, got)
|
||||
}
|
||||
}
|
||||
|
||||
// Numbers that stand alone are still masked: on their own, in text,
|
||||
// after a colon, at a sentence's end, beside a decimal that is not them.
|
||||
func TestStandaloneNumbersStillMasked(t *testing.T) {
|
||||
o := Options{Secrets: true, Personal: true}
|
||||
for _, c := range []struct{ in, kind, v string }{
|
||||
{"13800138000", "PHONE", "13800138000"},
|
||||
{"call 13800138000 now", "PHONE", "13800138000"},
|
||||
{"phone:13800138000", "PHONE", "13800138000"},
|
||||
{`{"phone": "13800138000"}`, "PHONE", "13800138000"},
|
||||
{"my number is 13800138000.", "PHONE", "13800138000"},
|
||||
{"价格 5.0,电话 13800138000", "PHONE", "13800138000"},
|
||||
{"v1.2 +86 13800138000", "PHONE", "13800138000"},
|
||||
{"card 4111 1111 1111 1111", "BANK_CARD", "4111 1111 1111 1111"},
|
||||
{"card 4111111111111111.", "BANK_CARD", "4111111111111111"},
|
||||
{"id 110105194912310038", "ID_CARD", "110105194912310038"},
|
||||
} {
|
||||
out, n := Mask(c.in, o)
|
||||
if n != 1 || strings.Contains(out, c.v) || !strings.Contains(out, "{{"+c.kind+"_") {
|
||||
t.Errorf("Mask(%q) = %q (%d masked), want %s masked as %s", c.in, out, n, c.v, c.kind)
|
||||
}
|
||||
}
|
||||
body := `{"messages":[{"role":"user","content":"{\"height\": 5.13393848347348, \"phone\": \"13800138000\"}"}]}`
|
||||
got, n := MaskJSON([]byte(body), o)
|
||||
if n != 1 || strings.Contains(string(got), "13800138000") || !strings.Contains(string(got), "5.13393848347348") {
|
||||
t.Fatalf("%d masked: %s", n, got)
|
||||
}
|
||||
}
|
||||
|
||||
// A secret masked before is masked wherever it is, in a decimal's digits
|
||||
// too: masking a number costs less than sending a password.
|
||||
func TestKnownSecretInDecimalStillMasked(t *testing.T) {
|
||||
o := Options{Secrets: true, Personal: true}
|
||||
const secret = "4417hunter2Pass"
|
||||
if out, _ := Mask("DB_PASSWORD="+secret, o); strings.Contains(out, secret) {
|
||||
t.Fatalf("not masked to begin with: %q", out)
|
||||
}
|
||||
if out, n := Mask("the ratio is 0."+secret, o); n != 1 || strings.Contains(out, secret) {
|
||||
t.Fatalf("%d masked: %q", n, out)
|
||||
}
|
||||
}
|
||||
|
||||
func jsonQuote(s string) string { return `"` + jsonEscape(s) + `"` }
|
||||
@@ -585,7 +585,31 @@ func mask(s string, o Options, put func(kind, v string) string) (string, int) {
|
||||
// kind it has always had
|
||||
found = append(found, modelSpans(s, o)...)
|
||||
found = append(found, knownSpans(s, o)...)
|
||||
return apply(s, noIDs(s, found), put, o)
|
||||
return apply(s, noDecimals(s, noIDs(s, found)), put, o)
|
||||
}
|
||||
|
||||
// noDecimals is found without what is a piece of a decimal number of s:
|
||||
// digits right after the point of 5.13800138000, or right before the
|
||||
// point of 13800138000.5. A number is judged whole, and the fraction of
|
||||
// a height, a price or a coordinate is not a phone or a card number
|
||||
// because its digits happen to check (willz on Discord). Only personal
|
||||
// data's kinds, whoever found them (a rule, the model, a value known
|
||||
// before): a secret is masked wherever it is, and what the user chose
|
||||
// (their words, their rules) stays theirs.
|
||||
func noDecimals(s string, found []span) []span {
|
||||
return slices.DeleteFunc(found, func(f span) bool {
|
||||
_, personal := kindCats[f.kind]
|
||||
return personal && f.by != ByWord && f.by != ByYours && inDecimal(s, f.start, f.end)
|
||||
})
|
||||
}
|
||||
|
||||
// inDecimal says s[a:b] starts with a digit that is a decimal's fraction
|
||||
// (a digit and a point before it) or ends with a digit that is its whole
|
||||
// part (a point and a digit after it).
|
||||
func inDecimal(s string, a, b int) bool {
|
||||
isDigit := func(i int) bool { return i >= 0 && i < len(s) && s[i] >= '0' && s[i] <= '9' }
|
||||
return isDigit(a) && a >= 2 && s[a-1] == '.' && isDigit(a-2) ||
|
||||
isDigit(b-1) && b+1 < len(s) && s[b] == '.' && isDigit(b+1)
|
||||
}
|
||||
|
||||
// by is the finder r is, for Hit.By.
|
||||
|
||||
Reference in New Issue
Block a user