diff --git a/internal/redact/decimal_test.go b/internal/redact/decimal_test.go new file mode 100644 index 000000000..eca9857ad --- /dev/null +++ b/internal/redact/decimal_test.go @@ -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) + `"` } diff --git a/internal/redact/redact.go b/internal/redact/redact.go index 436b95bf7..a129a8a78 100644 --- a/internal/redact/redact.go +++ b/internal/redact/redact.go @@ -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.