diff --git a/internal/redact/model.go b/internal/redact/model.go index 3ff337737..e8b7d14f7 100644 --- a/internal/redact/model.go +++ b/internal/redact/model.go @@ -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 } diff --git a/internal/redact/redact.go b/internal/redact/redact.go index 7fcff4079..546747ff5 100644 --- a/internal/redact/redact.go +++ b/internal/redact/redact.go @@ -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) { diff --git a/internal/redact/userinfo_test.go b/internal/redact/userinfo_test.go new file mode 100644 index 000000000..20ca2cdbf --- /dev/null +++ b/internal/redact/userinfo_test.go @@ -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$`}, + {"", `^$`}, + {"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) + } +}