Files
magpie/internal/redact/dateword.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

184 lines
6.2 KiB
Go

package redact
import (
"regexp"
"strconv"
"strings"
"sync"
"time"
"unicode"
"unicode/utf8"
)
// A masked word that is a date matches that calendar date however it is
// written (JackyCcao on X: with birthdays off, the user's own birthday as a
// masked word, 1990-05-12, let 1990/5/12, 1990年5月12日 and 19900512 go as
// written). The word is a date when it is all of one: year, month and day
// with - / . 、 or a space between (1990-05-12, 1990.5.12); 1990年5月12日 or
// 1990년 5월 12일; day and month before the year where only one reading is
// a date (13/05/1990, 05/13/1990, 05/05/1990, never 05/12/1990); or a month's
// English name (May 12, 1990; 12 May 1990). Eight digits alone are not
// taken for a date word: they are as likely an id.
//
// It then matches the same date, and only it, in each of those forms, with
// or without leading zeros, and as eight digits (19900512); day-first and
// month-first only where the day is over 12 or equals the month, so no
// form can read as another date. A match never has a digit beside it, nor
// a dot and a digit (1990.5.12.1 is a version, 11990-05-12 another number),
// whatever Whole words only says: a date is matched as a whole date.
// dateSep is what stands between a date's numbers.
const dateSep = `(?:[ \t]*[-/.、/-][ \t]*|[ \t])`
var (
dateYMD = regexp.MustCompile(`^(\d{4})` + dateSep + `(\d{1,2})` + dateSep + `(\d{1,2})\.?$`)
dateCJK = regexp.MustCompile(`^(\d{4})[ \t]*[年년][ \t]*(\d{1,2})[ \t]*[月월][ \t]*(\d{1,2})[ \t]*[日号號일]?$`)
dateDMY = regexp.MustCompile(`^(\d{1,2})` + dateSep + `(\d{1,2})` + dateSep + `(\d{4})$`)
dateNameF = regexp.MustCompile(`^([A-Za-z]+)\.?[ \t]+(\d{1,2})(?:st|nd|rd|th)?,?[ \t]+(\d{4})$`)
dateNameB = regexp.MustCompile(`^(\d{1,2})(?:st|nd|rd|th)?(?:[ \t]+|-)([A-Za-z]+)\.?,?(?:[ \t]+|-)(\d{4})$`)
)
var monthNames = []string{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
// monthOf is the month a name or its first three letters say, 0 for none.
func monthOf(name string) int {
l := strings.ToLower(name)
if l == "sept" {
return 9
}
for i, m := range monthNames {
ml := strings.ToLower(m)
if l == ml || len(l) == 3 && strings.HasPrefix(ml, l) {
return i + 1
}
}
return 0
}
// realDate says y-m-d is a day of the calendar.
func realDate(y, m, d int) bool {
if y < 1000 || y > 2999 || m < 1 || m > 12 || d < 1 {
return false
}
t := time.Date(y, time.Month(m), d, 0, 0, 0, 0, time.UTC)
return t.Day() == d && int(t.Month()) == m
}
// parseDateWord is the date the word w is, where it is one.
func parseDateWord(w string) (y, m, d int, ok bool) {
n := func(s string) int { v, _ := strconv.Atoi(s); return v }
if g := dateYMD.FindStringSubmatch(w); g != nil {
y, m, d = n(g[1]), n(g[2]), n(g[3])
} else if g := dateCJK.FindStringSubmatch(w); g != nil {
y, m, d = n(g[1]), n(g[2]), n(g[3])
} else if g := dateDMY.FindStringSubmatch(w); g != nil {
p, q := n(g[1]), n(g[2])
y = n(g[3])
switch {
case p == q:
m, d = p, q
case p > 12 && q <= 12:
d, m = p, q
case q > 12 && p <= 12:
m, d = p, q
default: // 05/12/1990: May 12th or December 5th
return 0, 0, 0, false
}
} else if g := dateNameF.FindStringSubmatch(w); g != nil {
m, d, y = monthOf(g[1]), n(g[2]), n(g[3])
} else if g := dateNameB.FindStringSubmatch(w); g != nil {
d, m, y = n(g[1]), monthOf(g[2]), n(g[3])
} else {
return 0, 0, 0, false
}
return y, m, d, realDate(y, m, d)
}
// dateForms is a pattern for the date y-m-d in each form a date word
// matches, and the word itself as written.
func dateForms(y, m, d int, w string) string {
num := func(v int) string { // 5 or 05; 12
if v < 10 {
return "0?" + strconv.Itoa(v)
}
return strconv.Itoa(v)
}
Y, M, D := strconv.Itoa(y), num(m), num(d)
full, abbr := monthNames[m-1], monthNames[m-1][:3]
if m == 9 {
abbr = "Sept|Sep"
}
name := `(?i:` + full + `|(?:` + abbr + `)\.?)`
ord := `(?:st|nd|rd|th)?`
forms := []string{
Y + dateSep + M + dateSep + D,
Y + `[ \t]*年[ \t]*` + M + `[ \t]*月[ \t]*` + D + `(?:[ \t]*[日号號])?`,
Y + `[ \t]*년[ \t]*` + M + `[ \t]*월[ \t]*` + D + `(?:[ \t]*일)?`,
Y + strconv.Itoa(100 + m)[1:] + strconv.Itoa(100 + d)[1:],
name + `[ \t]+` + D + ord + `,?[ \t]+` + Y,
D + ord + `(?:[ \t]+|-)` + name + `,?(?:[ \t]+|-)` + Y,
}
// day-first and month-first only where no other date reads the same
if d > 12 || d == m {
forms = append(forms, D+dateSep+M+dateSep+Y)
if d != m {
forms = append(forms, M+dateSep+D+dateSep+Y)
}
}
return `(?:` + strings.Join(forms, "|") + `|` + regexp.QuoteMeta(w) + `)`
}
var dateWords sync.Map // word → *regexp.Regexp, or nil for one that is no date
// dateWord is the pattern for the date the masked word w is, nil where it
// is no date.
func dateWord(w string) *regexp.Regexp {
if re, ok := dateWords.Load(w); ok {
return re.(*regexp.Regexp)
}
var re *regexp.Regexp
if y, m, d, ok := parseDateWord(w); ok {
re = regexp.MustCompile(dateForms(y, m, d, w))
}
dateWords.Store(w, re)
return re
}
// dateWhole says the date at s[a:b] is no part of a longer number: no
// digit beside it, no dot and a digit (a version's 1990.5.12.1, 3.1990.5.12),
// and no letter before a month's name (xMay), save in a script that puts
// no spaces between words (生日是May 12, 1990).
func dateWhole(s string, a, b int) bool {
isDigit := func(c byte) bool { return c >= '0' && c <= '9' }
if a > 0 {
if isDigit(s[a-1]) || s[a-1] == '.' && a > 1 && isDigit(s[a-2]) {
return false
}
if r, _ := utf8.DecodeLastRuneInString(s[:a]); unicode.IsLetter(rune(s[a])) && unicode.IsLetter(r) && !unicode.In(r, unicode.Han, unicode.Hiragana, unicode.Katakana, unicode.Hangul) {
return false
}
}
if b < len(s) {
if isDigit(s[b]) || s[b] == '.' && b+1 < len(s) && isDigit(s[b+1]) {
return false
}
}
return true
}
// dateSpans are where s has the date the masked word w is, nil and false
// where w is no date.
func dateSpans(s, w string) ([]span, bool) {
re := dateWord(w)
if re == nil {
return nil, false
}
var out []span
for _, m := range re.FindAllStringIndex(s, -1) {
if dateWhole(s, m[0], m[1]) {
out = append(out, span{m[0], m[1], "TERM", ByWord, 0})
}
}
return out, true
}