redact: an address needs a mail domain (xxx@Agents.md, x@pytest.fixture)
ydzh on Discord: masking took xxx@Agents.md and xxx@pytest.xxx for email addresses. realEmail now also refuses a host that is a known agent doc file (agents.md, claude.md, readme.md, ...) or whose last label is not an ICANN TLD (publicsuffix) nor a local mail suffix (local, internal, lan, corp, home, intranet, localdomain). So decorator and module paths like pytest.fixture or dataclasses.dataclass go as written, while me@notes.md, j@Gmail.COM, admin@build.local and ops@mail.corp are still masked. Run: go test ./internal/redact (all, evals included) ok; go test ./internal/gateway -run 'Redact|Mask|Email|Privacy' ok; gofmt -l clean; GOOS=linux and windows go vet ./internal/redact ok. Broken by hand: with the new checks removed, TestEmailNeedsAMailDomain fails 10 cases. Not tried: the real app; full suite (other sessions' suites on the machine).
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package redact
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// What follows the @ of a decorator or a module's attribute (pytest.fixture)
|
||||
// ends in a word no registry has, and an agent's own files (AGENTS.md)
|
||||
// end in .md: neither was an address, and both were masked (ydzh on
|
||||
// Discord). Addresses on real domains, a company's own (.local, .corp)
|
||||
// and the .md of a domain that isn't such a file are still masked.
|
||||
func TestEmailNeedsAMailDomain(t *testing.T) {
|
||||
o := Options{Secrets: true, Personal: true}
|
||||
for _, s := range []string{
|
||||
"xxx@Agents.md", "follow docs@AGENTS.md", "see repo@CLAUDE.md and x@README.md",
|
||||
"x@pytest.fixture", "def@pytest.mark.parametrize", "conftest@pytest.mark.asyncio",
|
||||
"cls@dataclasses.dataclass", "obj@functools.cache",
|
||||
} {
|
||||
in := "look at " + s + " now"
|
||||
if out, n := Mask(in, o); n != 0 || out != in {
|
||||
t.Errorf("%q: %d masked: %q", s, n, out)
|
||||
}
|
||||
}
|
||||
for _, s := range []string{
|
||||
"rina.k@fastmail.com", "dev@startup.io", "me@notes.md", "ops@corp.example.co.uk",
|
||||
"x@company.ai", "li.wei@qq.com", "admin@build.local", "ops@mail.corp", "j@Gmail.COM",
|
||||
} {
|
||||
in := "mail " + s + " today"
|
||||
if out, n := Mask(in, o); n != 1 || strings.Contains(out, s) {
|
||||
t.Errorf("%q: %d masked: %q", s, n, out)
|
||||
}
|
||||
}
|
||||
f := &fakeModel{values: map[string]string{"x@pytest.fixture": "private_email", "xxx@Agents.md": "private_email"}}
|
||||
for _, s := range []string{"x@pytest.fixture", "xxx@Agents.md"} {
|
||||
if out, n := Mask(s, Options{Personal: true, Model: f.find}); n != 0 {
|
||||
t.Errorf("model's %q: %d masked: %q", s, n, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"unicode/utf8"
|
||||
|
||||
"golang.org/x/net/publicsuffix"
|
||||
)
|
||||
|
||||
// Options says what to mask.
|
||||
@@ -380,7 +382,7 @@ func realEmail(v string) bool {
|
||||
return false
|
||||
}
|
||||
host := strings.ToLower(v[at+1:])
|
||||
if fileHost(host) {
|
||||
if fileHost(host) || docFiles[host] || !mailTLD(host[strings.LastIndexByte(host, '.')+1:]) {
|
||||
return false
|
||||
}
|
||||
for _, fake := range []string{"example.com", "example.org", "example.net", "localhost", ".test", ".invalid", ".example"} {
|
||||
@@ -391,6 +393,31 @@ func realEmail(v string) bool {
|
||||
return !strings.HasPrefix(host, "noreply") && !strings.Contains(v, "noreply@")
|
||||
}
|
||||
|
||||
// docFiles are the files agents read by name, on a domain's ending
|
||||
// (.md): "@AGENTS.md" after a word, as a prompt names one, is no address
|
||||
// (ydzh on Discord: xxx@Agents.md was masked).
|
||||
var docFiles = map[string]bool{
|
||||
"agents.md": true, "claude.md": true, "gemini.md": true, "readme.md": true, "skill.md": true,
|
||||
"changelog.md": true, "contributing.md": true, "license.md": true, "lessons.md": true, "todo.md": true,
|
||||
"copilot-instructions.md": true, "crush.md": true, "qwen.md": true, "iflow.md": true, "warp.md": true,
|
||||
}
|
||||
|
||||
// localTLDs are names an organisation's own mail can end in that no
|
||||
// registry has: an address there is someone's all the same.
|
||||
var localTLDs = map[string]bool{"local": true, "internal": true, "lan": true, "corp": true, "home": true, "intranet": true, "localdomain": true}
|
||||
|
||||
// mailTLD is whether tld, an address's last label, is one mail can be
|
||||
// sent to: in the public suffix list's ICANN part, or an organisation's
|
||||
// own. A decorator's or a module's attribute after a name
|
||||
// (x@pytest.fixture, ydzh on Discord) ends in a word no registry has.
|
||||
func mailTLD(tld string) bool {
|
||||
if localTLDs[tld] {
|
||||
return true
|
||||
}
|
||||
_, icann := publicsuffix.PublicSuffix(tld)
|
||||
return icann
|
||||
}
|
||||
|
||||
// fileExt are file name extensions that are no top-level domain, so
|
||||
// what ends in one is a file's name and not an address: an image a Mac
|
||||
// saved at twice its size, 261020_14-16-11@2x.jpg, went to the model as
|
||||
|
||||
Reference in New Issue
Block a user