diff --git a/internal/redact/decimal_test.go b/internal/redact/decimal_test.go index eca9857ad..b6b9b2d5b 100644 --- a/internal/redact/decimal_test.go +++ b/internal/redact/decimal_test.go @@ -113,3 +113,53 @@ func TestKnownSecretInDecimalStillMasked(t *testing.T) { } func jsonQuote(s string) string { return `"` + jsonEscape(s) + `"` } + +// A number that stands bare as a JSON value is a count or a time, not a +// phone, a card or an ID number: a session file's line in a tool's output +// (id.null on Discord, "timestamp": 17929036837 masked as a PHONE). It is +// judged by where it sits, not by its key's name. The same digits in a +// JSON string, or in text, are still masked. +func TestBareJSONNumberNotMasked(t *testing.T) { + o := Options{Secrets: true, Personal: true} + for _, s := range []string{ + // the reporter's line, as the session file has it + `{"type":"user","timestamp": 17929036837,"uuid":"6316955a","isoTime":"2026-10-25T04:48:03.755Z"}`, + // 10 and 13 digits, seconds and milliseconds + `{"timestamp":1792903683,"id":"6316955a"}`, + `{"timestamp":1792903683755}`, + // 11 digits that start 1[3-9], at the object's end and spread out + `{"n": 13800138000}`, + "{\n \"count\" :\t13800138000\n}", + // in an array of numbers + `{"ids":[13800138000, 13900139000]}`, + `[1, 13800138000]`, + // a Luhn-valid card's digits and an ID card's, as numbers + `{"snowflake": 4111111111111111}`, + `{"seq": 110105194912310038}`, + } { + 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) + } + } + for _, c := range []struct{ in, kind, v string }{ + {`{"phone": "13800138000"}`, "PHONE", "13800138000"}, + {`{"timestamp": 1792903683, "phone": "13800138000"}`, "PHONE", "13800138000"}, + {`["13800138000"]`, "PHONE", "13800138000"}, + {"phone:13800138000", "PHONE", "13800138000"}, + {"call 13800138000, thanks", "PHONE", "13800138000"}, + {"我的号码 13800138000,谢谢", "PHONE", "13800138000"}, + {"numbers 13900139000, 13800138000]", "PHONE", "13800138000"}, + {`{"note": "call 13800138000"}`, "PHONE", "13800138000"}, + {`{"card": "4111111111111111"}`, "BANK_CARD", "4111111111111111"}, + } { + out, _ := Mask(c.in, o) + if strings.Contains(out, c.v) || !strings.Contains(out, "{{"+c.kind+"_") { + t.Errorf("Mask(%q) = %q, want %s masked as %s", c.in, out, c.v, c.kind) + } + } +} diff --git a/internal/redact/redact.go b/internal/redact/redact.go index 28a80348f..71fdfdaf6 100644 --- a/internal/redact/redact.go +++ b/internal/redact/redact.go @@ -761,7 +761,7 @@ func mask(raw string, o Options, put func(kind, v string) string, escaped bool) if escaped { found = outsideEscapes(raw, found) } - return apply(raw, noDecimals(s, noIDs(s, found)), put, o) + return apply(raw, noNumbers(s, noIDs(s, found)), put, o) } // unescapedView is s, JSON text, as long as s and read as the vendor reads @@ -860,21 +860,67 @@ func outsideEscapes(s string, found []span) []span { return out } -// noDecimals is found without what is a piece of a decimal number of s: +// noNumbers 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 { +// because its digits happen to check (willz on Discord). Nor is a number +// that stands bare as a JSON value: "timestamp": 17929036837 in a session +// file is a count or a time, and a phone, a card or an ID number in JSON +// is a string (id.null 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 noNumbers(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) + return personal && f.by != ByWord && f.by != ByYours && (inDecimal(s, f.start, f.end) || jsonNumber(s, f.start, f.end)) }) } +// jsonNumber says s[a:b] is all of a bare JSON number, by where it sits: +// digits only, an object's value ("key": …) or an element of an array of +// numbers ([…, …]), and nothing after it but the , } or ] that ends a +// value. "phone": "13800138000" is a string, and phone:13800138000 and +// "call 13800138000, thanks" are text, so each is still masked. +func jsonNumber(s string, a, b int) bool { + if a >= b || strings.Trim(s[a:b], digits) != "" { + return false + } + space := func(c byte) bool { return c == ' ' || c == '\t' || c == '\n' || c == '\r' } + k := b + for k < len(s) && space(s[k]) { + k++ + } + if k < len(s) && strings.IndexByte(",}]", s[k]) < 0 { + return false + } + i := a - 1 + for i >= 0 && space(s[i]) { + i-- + } + if i < 0 { + return false + } + switch s[i] { + case ':': + for i--; i >= 0 && space(s[i]); i-- { + } + return i >= 0 && s[i] == '"' + case '[', ',': + // back over the numbers before it to the array's [ + for ; i >= 0; i-- { + if s[i] == '[' { + return true + } + if strings.IndexByte(digits+",.-+eE", s[i]) < 0 && !space(s[i]) { + return false + } + } + } + return false +} + // 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).