redact: a URL's user:password@host is a PASSWORD and a host, not one EMAIL (#3538)

With Mask personal data on, postgres://app:hunter2pass@db.acme-corp.cn:5432/x
went as app:{{EMAIL_…}}:5432/x: the EMAIL rule took hunter2pass@db.acme-corp.cn,
which starts where the PASSWORD rule's value does and is longer, so it won
the overlap. The kind was wrong and the host the model may need was hidden.

Now an EMAIL match whose start is inside a URL's authority (after
scheme:// with no /, ?, #, space, quote or @ between) is turned away
(notUserinfo, the rule's `at`), and the local model's EMAIL the same way
(modelFits). The password is the PASSWORD rule's and the host goes as
written. An address in a URL's path or query, in mailto: and in prose is
an EMAIL still.

Sibling found on the way: the PASSWORD rule asked for a user before the
colon, so redis://:pass@host (Redis has a password and no user) was not
masked with Mask secrets alone, and was only covered by the EMAIL accident
with personal data on. The user part may now be empty; without that the
email fix would have sent a Redis password as written.

Behaviour change to note: a userinfo with no password
(https://yetone@github.com/..., ssh://git@gitlab...) was an EMAIL and now
goes as written; it is a user name in a URL, not an address. With Mask
personal data on and Mask secrets off, a URL's password now goes as
written, as Mask secrets off says (it went as an EMAIL by accident).

#3029's file names (…@2x.jpg) are untouched: realEmail is unchanged and
TestFileNameIsNoEmail/TestMoreFileNamesAreNoEmail pass. #3365 (rule SECRETs
and known values) is a different rule; this changes nothing it reports.

Test: TestURLUserinfoIsNoEmail (internal/redact/userinfo_test.go) with the
report's literal string, mysql://, amqp://, mongodb+srv://, https://user:pass@,
redis://:pass@, a user with no password, ssh://git@, and emails in prose,
mailto:, a URL path, a query, and a query after a userinfo; a request via
MaskJSON; and the local model's EMAIL.

Broken by hand:
- redact.go and model.go reverted: red, 9 table cases plus the request and
  model checks (the literal went as postgres://app:{{EMAIL_…}}:5432/x).
- model.go alone reverted: red on the model check only.
- the PASSWORD regex's user back to +: red on both Redis cases (with
  personal on it went fully unmasked).

Run at this head (rebased on origin/main d153f0259):
- go test -count=1 ./internal/redact/... ./internal/masking/... ./plugins/redact/...: ok
- go test -count=1 ./internal/gateway/ -run 'Mask|Redact|Privacy|Masked': ok
- gofmt -l internal/redact internal/masking plugins/redact: empty
- GOOS=linux, windows, darwin go vet on the three packages: ok
Not run: the full suite (no suite lock on this machine; owner: don't hold
the push for it). Not tried in the GUI's Try it preview by hand; it calls
the same redact.Preview path the tests exercise.
This commit is contained in:
yetone
2026-10-25 09:57:21 +08:00
parent d153f02593
commit 641b1217f2
3 changed files with 99 additions and 4 deletions
+2 -1
View File
@@ -254,7 +254,8 @@ func modelFits(s string, a, b int, kind string) bool {
}
return !hexRe.MatchString(v) || cued(s, a, secretCues)
case "EMAIL":
return wordByte(v[0]) && !modCacheRe.MatchString(v)
// a URL's user:password@host is no address, as for the rule's (#3538)
return wordByte(v[0]) && !modCacheRe.MatchString(v) && !inUserinfo(s, a)
}
return true
}
+38 -3
View File
@@ -141,8 +141,9 @@ var rules = append([]rule{
{kind: "API_KEY", re: regexp.MustCompile(`(?:hf_[A-Za-z0-9]{30,}|gsk_[A-Za-z0-9]{40,}|xai-[A-Za-z0-9]{40,}|npm_[A-Za-z0-9]{36}|pypi-[A-Za-z0-9_-]{50,}|dop_v1_[a-f0-9]{64}|SG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43})`),
markers: []string{"hf_", "gsk_", "xai-", "npm_", "pypi-", "dop_v1_", "SG."}, bound: tokenCh},
{kind: "TOKEN", re: regexp.MustCompile(`eyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}`), markers: []string{"eyJ"}, bound: tokenCh},
// the password of user:password@host
{kind: "PASSWORD", re: regexp.MustCompile(`[A-Za-z][A-Za-z0-9+.-]*://[^\s:@/?#"'<>]+:([^\s@/?#"'<>]{3,})@`), markers: []string{"://"}, ok: notAVariable},
// the password of user:password@host, or of :password@host as a Redis
// URL has it with no user
{kind: "PASSWORD", re: regexp.MustCompile(`[A-Za-z][A-Za-z0-9+.-]*://[^\s:@/?#"'<>]*:([^\s@/?#"'<>]{3,})@`), markers: []string{"://"}, ok: notAVariable},
// password = …, API_KEY: "…", as .env files and configs have them
{kind: "SECRET", re: regexp.MustCompile(`(?i)[A-Za-z0-9_.-]*(?:` + secretNames.pattern() + `)[A-Za-z0-9_.-]*` + quote + `[ \t]*[:=][ \t]*` + quote + `(` + secretCh + `{8,})`),
markers: secretNames.markers(), ok: secretValue, at: notCode},
@@ -152,7 +153,7 @@ var rules = append([]rule{
{kind: "SECRET", re: regexp.MustCompile(`(?i)(?:^|[^A-Za-z0-9_.-])` + quote + `key` + quote + `[ \t]*[:=][ \t]*` + quote + `(` + secretCh + `{16,})`),
markers: fieldNames{{"key", "key"}}.markers(), ok: secretValue, at: notCode},
{kind: "EMAIL", re: regexp.MustCompile(`[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}`), markers: []string{"@"}, bound: alnum + "._%+-", ok: realEmail, cat: catPersonal},
{kind: "EMAIL", re: regexp.MustCompile(`[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}`), markers: []string{"@"}, bound: alnum + "._%+-", ok: realEmail, at: notUserinfo, cat: catPersonal},
{kind: "ID_CARD", re: regexp.MustCompile(`[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dXx]`), bound: alnum, ok: chineseID, cat: catPersonal},
{kind: "PHONE", re: regexp.MustCompile(`(?:\+?86[- ]?)?1[3-9]\d{9}`), bound: digits, cat: catPersonal},
{kind: "BANK_CARD", re: regexp.MustCompile(`[3-6]\d{3}(?:[ -]?\d{4}){2,3}(?:[ -]?\d{1,3})?`), bound: digits, ok: luhn, cat: catPersonal},
@@ -247,6 +248,40 @@ func notCode(s string, a, b int) bool {
return rest == "" || !strings.ContainsRune(";,)}]|?&<>", rune(rest[0]))
}
// notUserinfo turns away what looks like an email at s[a:b] but is a
// URL's user:password@host or user@host: its @ ends the userinfo, so
// what is before it is a user name or a password and what is after it
// the host. postgres://app:hunter2pass@db.acme-corp.cn:5432/x went as
// app:{{EMAIL_…}}:5432, the password and the host one address, where the
// password is the PASSWORD rule's and the host stays (#3538). An address
// in a URL's path or query (?to=bob@acme.cn) and in mailto: is an
// email still.
func notUserinfo(s string, a, _ int) bool {
return !inUserinfo(s, a)
}
// inUserinfo says s[a] is inside a URL's authority, after its scheme://
// and before the host: nothing from there back to the // is a /, ?, #,
// space, quote, < or > or @.
func inUserinfo(s string, a int) bool {
i := a
for i > 0 && !strings.ContainsRune(" \t\r\n/?#\"'<>@`", rune(s[i-1])) {
i--
}
if i < 3 || s[i-1] != '/' || s[i-2] != '/' || s[i-3] != ':' {
return false
}
j := i - 3 // the scheme ends at s[j], its colon
k := j
for k > 0 && (s[k-1] >= 'a' && s[k-1] <= 'z' || s[k-1] >= 'A' && s[k-1] <= 'Z' || s[k-1] >= '0' && s[k-1] <= '9' || strings.IndexByte("+.-", s[k-1]) >= 0) {
k--
}
for k < j && !(s[k] >= 'a' && s[k] <= 'z' || s[k] >= 'A' && s[k] <= 'Z') {
k++ // a scheme starts with a letter
}
return k < j
}
func realEmail(v string) bool {
host := strings.ToLower(v[strings.LastIndexByte(v, '@')+1:])
if fileHost(host) {
+59
View File
@@ -0,0 +1,59 @@
package redact
import (
"regexp"
"strings"
"testing"
)
// A URL's user:password@host is no address. With Mask personal data on,
// postgres://app:hunter2pass@db.acme-corp.cn:5432/x went as
// app:{{EMAIL_…}}:5432, the password and the host one EMAIL (#3538). The
// password is a PASSWORD, and the host goes as written.
func TestURLUserinfoIsNoEmail(t *testing.T) {
o := Options{Secrets: true, Personal: true}
ph := func(kind string) string { return `\{\{` + kind + `_[a-z2-7]{8}\}\}` }
for _, c := range []struct{ in, want string }{
// the report's literal string
{"postgres://app:hunter2pass@db.acme-corp.cn:5432/x", `^postgres://app:` + ph("PASSWORD") + `@db\.acme-corp\.cn:5432/x$`},
{"mysql://root:Tr0ub4dor3@mysql.acme-corp.cn/shop", `^mysql://root:` + ph("PASSWORD") + `@mysql\.acme-corp\.cn/shop$`},
{"amqp://guest:rabbitPw9@mq.acme-corp.cn:5672/", `^amqp://guest:` + ph("PASSWORD") + `@mq\.acme-corp\.cn:5672/$`},
{"mongodb+srv://svc:m0ng0Secret@cluster0.ab1cd.mongodb.net/app?retryWrites=true", `^mongodb\+srv://svc:` + ph("PASSWORD") + `@cluster0\.ab1cd\.mongodb\.net/app\?retryWrites=true$`},
{"clone https://deploy:gitPa55word@git.acme-corp.cn/team/repo.git now", `^clone https://deploy:` + ph("PASSWORD") + `@git\.acme-corp\.cn/team/repo\.git now$`},
// Redis has a password and no user
{"REDIS_URL=redis://:r3disPass@cache.acme-corp.cn:6379/0", `^REDIS_URL=redis://:` + ph("PASSWORD") + `@cache\.acme-corp\.cn:6379/0$`},
// a user and no password: neither part is an address
{"git clone https://yetone@github.com/yetone/magpie.git", `^git clone https://yetone@github\.com/yetone/magpie\.git$`},
{"ssh://git@gitlab.acme-corp.cn:2222/team/repo.git", `^ssh://git@gitlab\.acme-corp\.cn:2222/team/repo\.git$`},
// an address is still one: in prose, in mailto:, in a URL's path or query
{"mail rina.k@fastmail.com today", `^mail ` + ph("EMAIL") + ` today$`},
{"<a href=\"mailto:rina.k@fastmail.com\">", `^<a href="mailto:` + ph("EMAIL") + `">$`},
{"https://lists.acme-corp.cn/archive/rina.k@fastmail.com", `^https://lists\.acme-corp\.cn/archive/` + ph("EMAIL") + `$`},
{"https://acme-corp.cn/invite?to=rina.k@fastmail.com", `^https://acme-corp\.cn/invite\?to=` + ph("EMAIL") + `$`},
{"https://deploy:gitPa55word@git.acme-corp.cn/?cc=rina.k@fastmail.com", `^https://deploy:` + ph("PASSWORD") + `@git\.acme-corp\.cn/\?cc=` + ph("EMAIL") + `$`},
} {
if out, _ := Mask(c.in, o); !regexp.MustCompile(c.want).MatchString(out) {
t.Errorf("%q went as %q", c.in, out)
}
}
// with only Mask secrets on, Redis's password is masked too: the rule
// asked for a user before it (a value of its own, not one known from above)
if out, _ := Mask("redis://:r3disOnly7@cache.acme-corp.cn:6379/0", Options{Secrets: true}); !regexp.MustCompile(`^redis://:` + ph("PASSWORD") + `@cache\.acme-corp\.cn:6379/0$`).MatchString(out) {
t.Errorf("secrets only: %q", out)
}
// as a request carries it
body := `{"messages":[{"role":"user","content":"connect to postgres://app:hunter2pass@db.acme-corp.cn:5432/x"}]}`
out, n := MaskJSON([]byte(body), o)
if n != 1 || strings.Contains(string(out), "hunter2pass") || strings.Contains(string(out), "EMAIL_") || !strings.Contains(string(out), "@db.acme-corp.cn:5432/x") {
t.Errorf("request: %d masked: %s", n, out)
}
// nor does the local model's EMAIL take the password and the host
f := &fakeModel{values: map[string]string{"hunter2pass@db.acme-corp.cn": "private_email"}}
in := "postgres://app:hunter2pass@db.acme-corp.cn:5432/x"
if out, _ := Mask(in, Options{Personal: true, Model: f.find}); out != in {
t.Errorf("model's: %q", out)
}
}