Files
magpie/internal/redact/phone.go
T
yetone 189db9ba5b redact: ids, digests and 毛利 go as written; the slider reaches known values; a user name goes as a made-up name
Reports on X under the masking demo (10-28):

_kaichen: "hash类型也会被过滤…长int,假如以1为开头的…会被当作电话被mask;
"毛利低"也会被当作人名被mask。即使把匹配拉到 0.95,也是一样行为".

- Hex digests: 16 to 31 hex digits in one case, with letters and digits
  both (a cache key, a short commit), are ids now (digestShaped in
  ids.go), like UUIDs and 32+ hex. The rules' SECRET keeps one only under
  a credential's field or a CJK/prose key with no field word (密钥为…);
  the model's SECRET is dropped under a field named for something else
  (cache_key, commit, object, blob, file, content, revision, partition,
  shard, bucket, dedup, lookup, row, sort) and on a JSON object's key
  (nameOfField). A bare `key` after api/access/secret/private/signing/
  license is still a credential (keyPhrase). The IBAN case kept its mask
  through the letter/digit minimums.
- 11-digit 1x numbers (phone.go, phoneAt): a phone with +86/86, as the
  value of a field whose name says phone (phone, mobile, tel, cell,
  contact, 手机, 电话, `number`), or in prose with no id word right
  before it (order, id, no., #, 订单号, 编号, 单号, 流水号…). Under
  order_id/uid, after 订单号, or with a letter or digit against it, it
  goes as written. PHONE and BANK_CARD are bounded by alnum, not digits,
  in the rule and in known values.
- 毛利: zhWords gained 毛利, 毛重, 毛坯, 毛巾, 毛衣, so 毛利低 (a margin)
  is no surname+name. The model still finds 毛利兰 (Guard). 毛利小五郎
  (indekkusu141209) is masked by neither model nor the rules now.
- The slider: a value masked once was masked again as known whatever the
  threshold. Each value now remembers the highest score it was found
  with (rules count as sure, ruleFound), and knownSpans skips one under
  Options.MinScore, which masking sets from pii.Threshold.

eerttreeeert: with mimo-v2.6-flash the home folder went as
C:\Users\{{USERNAME_…}}; the model took it for a temp folder and set out
to delete it. With the user name switch on, it now goes as a made-up name
of 8 lowercase letters (HMAC of the name, same every turn, standIn in
shape.go), restored in replies and tool calls like any placeholder;
Preview shows the same name.

Docs: docs/subsystems/gateway-routing.md masking bullets, and the site's
masking page (en, zh, ja) on mobile ids and the user name.

Run (mote, at this tree on base 6af83172d; rebased onto b01193ef9, which
changes nothing in redact, masking or pii, and redact, masking and
pii/... run again ok at the pushed head):
- gofmt -l clean; go vet ./... and GOOS=darwin/linux/windows go vet of
  redact, masking, pii: ok.
- go test -count=1 -timeout 45m redact, masking, pii/..., gateway, otlp,
  sessions, settings: all ok but internal/gateway's
  TestPluginStreamsPastTheBudgetWait (connection reset by peer, 183 of
  192), red the same alone at fresh origin/main b01193ef9 (169 of 192);
  #3946, commented.
- Real models on mote (AstrLink Guard and Privacy Filter, cached): the
  reporter's hashes, ids and every 毛利低 sentence kept; a prose phone
  masked; 毛利兰是谁 masked by Guard; 毛利小五郎姓什么 kept by both.

Broken by hand, each red: phoneAt off; the phone bound back to digits;
digestShaped off; "cache" out of the field list; nameOfField off; the
model's field check off; 毛利 out of zhWords; the slider check out of
knownSpans; standIn off.

Not tried: a real mimo-v2.6-flash session with the made-up name; the
Windows box; the GUI suite (no GUI file changed). 毛利小五郎 stays
unmasked by rules and both models, said in the doc.
2026-10-28 16:05:19 +08:00

170 lines
5.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package redact
import (
"strings"
"unicode/utf8"
)
// Eleven digits that start 13 to 19 are a mainland mobile number's form,
// and an id's: an order's, a user's, a row's (13800138000 as order_id,
// WHERE id = 18612345678, 订单号 13912345678). Every one went to the
// vendor as {{PHONE_…}} (_kaichen on X, 10-28: "长int,假如以1为开头的,
// 刚好匹配到长度,会被当作电话被mask"), and an agent can't look up an
// order it can't read. So the form alone is a phone only where nothing
// says it is something else:
//
// - with a phone's own marks it is one wherever it is: +86 or 86 before
// it (+86 138 0013 8000 reads as +86 and the rest);
// - as a field's value (JSON, k=v, YAML, SQL's =, an XML element) it is
// one only when the field's name has a phone's word in it: phone,
// mobile, tel, cell, contact, 手机, 电话 (phone_number, mobileNo,
// <tel>); under order_id, uid or id it is that id;
// - in prose it is one, unless an id's word is right before it: order,
// id, no., #, 订单号, 编号, 单号, 流水号, 工单 (我的手机号是 13800138000
// still masks, 订单号 13912345678 已发货 doesn't).
//
// A letter or a digit against it on either side makes it a piece of a
// longer word, never a phone (the rule's bound): 9f13800138000abc is a
// hash.
// phoneWords are the words of a field's name that make its value a phone.
var phoneWords = []string{"phone", "phones", "mobile", "mobiles", "tel", "telephone", "cell", "cellphone", "contact", "contacts", "whatsapp", "sms", "msisdn", "fax"}
// phoneMarks, in a field's name in Chinese, make its value a phone.
var phoneMarks = []string{"手机", "电话", "電話", "联系", "聯繫", "联络"}
// idWords, right before a number in prose, make it an id.
var idWords = []string{"order", "id", "uid", "no", "no.", "#", "number", "ticket", "invoice", "订单", "订单号", "单号", "编号", "流水号", "工单", "工单号", "运单号", "快递单号", "订单编号", "交易号", "序号", "账单号", "id号"}
// phoneAt says the PHONE rule's s[a:b] is a phone, not an id of the same
// form.
func phoneAt(s string, a, b int) bool {
if b-a > 11 {
return true // +86, 86
}
if name, prev, ok := fieldName(s, a); ok {
return phoneName(name) || prev != "" && phoneName(prev) || strings.EqualFold(name, "number")
}
if a > 0 && s[a-1] == '#' {
return false // #13912345678
}
before := strings.ToLower(strings.TrimRight(s[max(0, a-32):a], " \t(\"'“「"))
for _, c := range []string{"是", "为", "為"} {
before = strings.TrimRight(strings.TrimSuffix(before, c), " \t")
}
for _, w := range idWords {
if !strings.HasSuffix(before, w) {
continue
}
// a whole word: "order", not the end of "recorder"
rest := before[:len(before)-len(w)]
r, _ := utf8.DecodeLastRuneInString(rest)
if len(rest) > 0 && w[0] < 0x80 && (r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '_') {
continue
}
// but a phone's number: phone no. 13800138000
if w == "no" || w == "no." || w == "number" {
rest = strings.TrimRight(rest, " ")
if phoneName(rest[nameStart(rest, len(rest)):]) {
return true
}
}
return false
}
return true
}
// phoneName says a field's name is a phone's: one of its words is one of
// phoneWords (phone_number, mobileNo), or it has one of phoneMarks (手机号).
func phoneName(name string) bool {
for _, w := range nameWords(name) {
for _, p := range phoneWords {
if w == p {
return true
}
}
}
for _, m := range phoneMarks {
if strings.Contains(name, m) {
return true
}
}
return false
}
// fieldName is the name of the field whose value starts at s[a]: the
// word before :, =, :or an XML element's >, with quotes and spaces
// between, as JSON, k=v, YAML, SQL and XML have one, and prev the word
// before it with one space between (the phone of "phone no: …"). ok is
// false in prose, where no such separator is right before the value.
func fieldName(s string, a int) (name, prev string, ok bool) {
i := a
for i > 0 && strings.IndexByte(" \t\"'", s[i-1]) >= 0 {
i--
}
switch {
case i > 0 && s[i-1] == '>':
// <tel>13800138000</tel>: the element's name
k := strings.LastIndexByte(s[max(0, i-65):i-1], '<')
if k < 0 {
return "", "", false
}
tag := s[max(0, i-65)+k+1 : i-1]
if sp := strings.IndexAny(tag, " \t"); sp >= 0 {
tag = tag[:sp]
}
if tag == "" || strings.IndexFunc(tag, func(r rune) bool {
return !(r < 0x80 && strings.IndexByte(alnum+"_.:-", byte(r)) >= 0)
}) >= 0 {
return "", "", false
}
return tag, "", true
case i > 0 && (s[i-1] == ':' || s[i-1] == '='):
i--
case i >= len(":") && s[i-len(":"):i] == ":":
i -= len(":")
default:
return "", "", false
}
for i > 0 && strings.IndexByte(" \t\"'", s[i-1]) >= 0 {
i--
}
j := nameStart(s, i)
if j == i {
return "", "", false
}
name = s[j:i]
if n := utf8.RuneCountInString(name); n > 6 && hasCJK(name) {
// a long run of CJK before a colon is a sentence's: its last few
// characters name the value (我的手机号是:…)
name = string([]rune(name)[n-6:])
}
if j >= 2 && s[j-1] == ' ' && s[j-2] != ' ' {
prev = s[nameStart(s, j-1) : j-1]
}
return name, prev, true
}
// nameStart is where the name that ends at s[i] starts: letters, digits,
// _, . and - back from i, or Chinese characters.
func nameStart(s string, i int) int {
j := i
for j > 0 {
r, n := utf8.DecodeLastRuneInString(s[:j])
if !(r < 0x80 && strings.IndexByte(alnum+"_.-", byte(r)) >= 0 || r >= 0x4e00 && r <= 0x9fff) {
break
}
j -= n
}
return j
}
func hasCJK(s string) bool {
for _, r := range s {
if r >= 0x4e00 && r <= 0x9fff {
return true
}
}
return false
}