redact: a bare JSON number ("timestamp": 17929036837) is not masked as a phone (id.null on Discord)
The 脱敏 before/after view showed a tool result holding a session file's
JSONL line, and its "timestamp": 17929036837 went as {{PHONE_…}}. The
PHONE rule is 1[3-9]\d{9} bounded by digits, so any 11-digit number
starting 13-19 is a CN mobile wherever it sits. A phone, a card or an ID
number in JSON is a string; a bare number there is a count, an id or a
time.
Now a personal kind's value that is all of a bare JSON number is left,
judged by where it sits and its form, not by the key's name: digits only,
after "key": (an object's value) or inside an array of numbers, with only
, } ] or the text's end after it. It sits beside the decimal check
(noDecimals, now noNumbers), so it holds for what the rules, the model
and values known before find alike. A BANK_CARD (Luhn-valid snowflake)
and an ID_CARD's digits as JSON numbers are left the same way.
Still masked (tested): "phone": "13800138000", ["13800138000"],
phone:13800138000, "call 13800138000, thanks", 我的号码 13800138000,谢谢,
"numbers 13900139000, 13800138000]" (no [ before), a phone inside a JSON
string's text, "card": "4111111111111111", and every case of
TestStandaloneNumbersStillMasked. "phone": 13800138000 as a number is
left too; that is the cost of not going by the key's name.
Run at this head:
- go test ./internal/redact ./internal/pii ./internal/settings
./internal/masking ./internal/otlp ./internal/sessions ./internal/gui
-count=1: ok
- go test ./internal/gateway -run 'CJK|Mask|Redact|Privacy|PII|Secret': ok
- gofmt -l, go vet, GOOS=windows go vet, GOOS=linux go vet -tags nogui
./internal/redact: clean
- Broken by hand: with origin/main's redact.go, TestBareJSONNumberNotMasked
fails 7 cases (the reporter's line, {"n": 13800138000}, a spread-out
object, two arrays, the card and the ID number). The 10- and 13-digit
epochs were not masked before either; they are in the test to stay so.
Not tried: the GUI before/after view in a running magpie, and the full suite.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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).
|
||||
|
||||
Reference in New Issue
Block a user