Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

3.6 KB
Raw Blame
package redact

import (
	"regexp"
	"strings"
	"testing"
)

// A URL's user:password@host is no address. With Mask personal data on,
// postgres://app:[email protected]: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:[email protected]:5432/x", `^postgres://app:` + ph("PASSWORD") + `@db\.acme-corp\.cn:5432/x$`},
		{"mysql://root:[email protected]/shop", `^mysql://root:` + ph("PASSWORD") + `@mysql\.acme-corp\.cn/shop$`},
		{"amqp://guest:[email protected]:5672/", `^amqp://guest:` + ph("PASSWORD") + `@mq\.acme-corp\.cn:5672/$`},
		{"mongodb+srv://svc:[email protected]/app?retryWrites=true", `^mongodb\+srv://svc:` + ph("PASSWORD") + `@cluster0\.ab1cd\.mongodb\.net/app\?retryWrites=true$`},
		{"clone https://deploy:[email protected]/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://:[email protected]: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://[email protected]/yetone/magpie.git", `^git clone https://yetone@github\.com/yetone/magpie\.git$`},
		{"ssh://[email protected]: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 [email protected] today", `^mail ` + ph("EMAIL") + ` today$`},
		{"<a href=\"mailto:[email protected]\">", `^<a href="mailto:` + ph("EMAIL") + `">$`},
		{"https://lists.acme-corp.cn/archive/[email protected]", `^https://lists\.acme-corp\.cn/archive/` + ph("EMAIL") + `$`},
		{"https://acme-corp.cn/[email protected]", `^https://acme-corp\.cn/invite\?to=` + ph("EMAIL") + `$`},
		{"https://deploy:[email protected]/[email protected]", `^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://:[email protected]: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:[email protected]: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{"[email protected]": "private_email"}}
	in := "postgres://app:[email protected]:5432/x"
	if out, _ := Mask(in, Options{Personal: true, Model: f.find}); out != in {
		t.Errorf("model's: %q", out)
	}
}