OpenAI's Privacy Filter gives common Chinese names (张伟, 李娜, 陈建国,
黄晓明) almost no PERSON weight. Lowering the O label's emission by 2 or
4 in a patched pf-serve didn't bring them back. It also returns spans
that run past the name (「王芳,她下周一」, 「张伟先生」).
zhname.go adds a rule layer behind the model, only when personal data
is covered:
- a surname (single, compound, or 小/老 + surname) with a 1–2
character given name is masked when something around it says it is
a person:
- a role before it (客户, 联系人:, 我叫, 作者:, @);
- a verb before it (给, 告诉, 抄送) or after it (说, 反馈, 先生);
- a title or 总 (孙经理, 邓总);
- a name joined by 和/与/跟/及/、 to a name already found counts too;
- words that start with a surname (张量, 高度, 王国, 周五, 总是) are
never taken;
- the model's PERSON spans with Han in them are cut at punctuation or a
title, and only when at least two characters remain.
Run:
- Real model, end to end through MaskJSON (pf-serve q8, Metal):
- masked 张伟, 王芳 (cut), 李娜, 刘洋, 陈建国, 赵丽颖, 欧阳娜娜, 司马光,
黄晓明, 周杰伦, 小王, 老李, 马云, 王老板, 邓总, 孙经理, 刘总监,
吴敏, 郑浩, 何静, 唐宁, 许文强, 罗志祥, 林老师 and 郭靖;
- Japanese, Korean and English names are unchanged.
- Missed, with nothing around them that says person: 「模块是谢磊写的」
and 「和黄蓉团队」.
- False positives: none over 2237 lines of the repo's Chinese docs and
i18n, or 61 developer sentences.
- Cost: ~19ms CPU over 880KB.
- Broken by hand:
- with the layer off, TestChineseNames has 8 names unmasked;
- with the two-character guard off before a title, 感谢李老师 goes red.
- Checks:
- gofmt -l is clean;
- go vet is clean on darwin, GOOS=linux -tags nogui and GOOS=windows.
- go test ./... -timeout 45m at load ~750: 63 packages ok, redact
among them. internal/agent (TestReconnectKeepsTheLastPick) and
internal/gateway (TestLargeRequestEndpoints) hit the 45m timeout with
no failed assertion, and are being rerun alone.
151 lines
4.1 KiB
Go
151 lines
4.1 KiB
Go
package redact
|
|
|
|
import (
|
|
"strings"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// A local model (internal/pii, Settings → Privacy's "Find with a local
|
|
// model") finds what no rule can: a name or an address in free text, a
|
|
// password with no password= before it. Its spans are masked beside the
|
|
// rules', under the same switches: a name, an address, a phone number, an
|
|
// email or an account number is personal data, a secret is a secret.
|
|
|
|
// Found is a span the model found in a text: byte offsets, and its label.
|
|
type Found struct {
|
|
Start, End int
|
|
Label string
|
|
}
|
|
|
|
// Model finds spans in each of texts, in order. It never fails: one that
|
|
// can't find (the model didn't start) panics, so that the request is held
|
|
// back rather than sent unmasked (gwmw.Safely).
|
|
type Model func(texts []string) [][]Found
|
|
|
|
// modelKinds are the placeholders' kinds for the model's labels. Its
|
|
// private_date and private_url are left out: a date in a request is
|
|
// nearly always a log's or a version's, and a URL the agent's own.
|
|
var modelKinds = map[string]string{
|
|
"private_person": "PERSON",
|
|
"private_address": "ADDRESS",
|
|
"private_phone": "PHONE",
|
|
"private_email": "EMAIL",
|
|
"account_number": "ACCOUNT",
|
|
"secret": "SECRET",
|
|
}
|
|
|
|
// modelCat is the category (Options.covers) of each kind the model finds.
|
|
func modelCat(kind string) string {
|
|
if kind == "SECRET" {
|
|
return ""
|
|
}
|
|
return catPersonal
|
|
}
|
|
|
|
// modelSpans are o.found's spans for s that o covers, trimmed of the
|
|
// spaces the model takes in with a word (and a Chinese name of what it
|
|
// ran on into, zhTrim), and with what it gets wrong often left out
|
|
// (modelTakes); and the Chinese names it missed (zhNameSpans).
|
|
func modelSpans(s string, o Options) []span {
|
|
if o.Model == nil || !o.covers(catPersonal) && len(o.found[s]) == 0 {
|
|
return nil
|
|
}
|
|
var out []span
|
|
for _, f := range o.found[s] {
|
|
kind := modelKinds[f.Label]
|
|
if kind == "" || !o.covers(modelCat(kind)) || f.Start < 0 || f.End > len(s) || f.Start >= f.End {
|
|
continue
|
|
}
|
|
a, b := f.Start, f.End
|
|
for a < b && isSpace(s[a]) {
|
|
a++
|
|
}
|
|
for b > a && isSpace(s[b-1]) {
|
|
b--
|
|
}
|
|
if kind == "PERSON" {
|
|
a, b = zhTrim(s, a, b)
|
|
}
|
|
if a < b && utf8.ValidString(s[a:b]) && modelTakes(kind, s[a:b]) {
|
|
out = append(out, span{a, b, kind})
|
|
}
|
|
}
|
|
if o.covers(catPersonal) {
|
|
var person []span
|
|
for _, f := range out {
|
|
if f.kind == "PERSON" {
|
|
person = append(person, f)
|
|
}
|
|
}
|
|
out = append(out, zhNameSpans(s, person)...)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func isSpace(c byte) bool { return c == ' ' || c == '\t' || c == '\n' || c == '\r' }
|
|
|
|
// modelTakes says v is masked as the model's kind: a secret as long as the
|
|
// rules ask of one, never an id a vendor or a tool made (req_…, toolu_…,
|
|
// call_…), which the agent and the vendor match against another; a name
|
|
// of two letters at least.
|
|
func modelTakes(kind, v string) bool {
|
|
switch kind {
|
|
case "SECRET":
|
|
return len(v) >= 8 && !madeID(v)
|
|
case "PERSON":
|
|
return utf8.RuneCountInString(v) >= 2 && strings.IndexFunc(v, unicode.IsLetter) >= 0
|
|
case "ACCOUNT":
|
|
return !madeID(v)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// madeID says v is an id of the shape vendors and tools give theirs: a
|
|
// short lower-case prefix, an underscore, then letters and digits
|
|
// (req_011CS…, toolu_01A…, call_x9…, msg_…).
|
|
func madeID(v string) bool {
|
|
p, rest, ok := strings.Cut(v, "_")
|
|
if !ok || len(p) < 2 || len(p) > 6 || len(rest) < 8 {
|
|
return false
|
|
}
|
|
for _, c := range p {
|
|
if c < 'a' || c > 'z' {
|
|
return false
|
|
}
|
|
}
|
|
for _, c := range rest {
|
|
if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9') {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// withModel is o with what o.Model finds in texts, the texts that may be
|
|
// masked: each once, the short ones left out.
|
|
func withModel(o Options, texts []string) Options {
|
|
if o.Model == nil || !o.Secrets && !o.Personal {
|
|
return o
|
|
}
|
|
var ask []string
|
|
seen := map[string]bool{}
|
|
for _, t := range texts {
|
|
if len(t) >= 3 && !seen[t] {
|
|
seen[t] = true
|
|
ask = append(ask, t)
|
|
}
|
|
}
|
|
if len(ask) == 0 {
|
|
return o
|
|
}
|
|
got := o.Model(ask)
|
|
o.found = make(map[string][]Found, len(ask))
|
|
for i, t := range ask {
|
|
if i < len(got) && len(got[i]) > 0 {
|
|
o.found[t] = got[i]
|
|
}
|
|
}
|
|
return o
|
|
}
|