diff --git a/pkg/files/files.go b/pkg/files/files.go index 7144a7d..0090b7b 100644 --- a/pkg/files/files.go +++ b/pkg/files/files.go @@ -29,6 +29,7 @@ func Clean(files []FileData) []FileData { for i := range files { files[i].Content = RemoveComments(files[i].Name, files[i].Content) files[i].Content = ReplaceSecrets(files[i].Content, "[REDACTED]") + files[i].Content = ReplaceEmails(files[i].Content, "redacted@example.org") files[i].Cleaned = true } return files diff --git a/pkg/files/redact_emails.go b/pkg/files/redact_emails.go new file mode 100644 index 0000000..8e59eeb --- /dev/null +++ b/pkg/files/redact_emails.go @@ -0,0 +1,8 @@ +package files + +import ( + "regexp" +) + +// ReplaceEmails tries to detect email addresses in the subject string and then replaces them. +var ReplaceEmails = regexp.MustCompile(`[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}`).ReplaceAllString diff --git a/pkg/files/redact_emails_test.go b/pkg/files/redact_emails_test.go new file mode 100644 index 0000000..8e00e07 --- /dev/null +++ b/pkg/files/redact_emails_test.go @@ -0,0 +1,68 @@ +package files + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestReplaceEmails(t *testing.T) { + cases := []struct { + name string + input string + expected string + }{ + { + name: "Single email", + input: "Contact: john.doe@example.com for support", + expected: "Contact: redacted@example.org for support", + }, + { + name: "Multiple emails", + input: "Email admin@company.org or support@help.net", + expected: "Email redacted@example.org or redacted@example.org", + }, + { + name: "Email in code comment", + input: `# Contact maintainer at maintainer@project.dev + func main() {`, + expected: `# Contact maintainer at redacted@example.org + func main() {`, + }, + { + name: "Email in config file", + input: `smtp: + username: noreply@company.com + password: secret123`, + expected: `smtp: + username: redacted@example.org + password: secret123`, + }, + { + name: "Email with plus sign", + input: "test+tag@gmail.com is valid", + expected: "redacted@example.org is valid", + }, + { + name: "Email with numbers and dots", + input: "user.123@sub.domain.co.uk works fine", + expected: "redacted@example.org works fine", + }, + { + name: "No emails", + input: "This text has no email addresses", + expected: "This text has no email addresses", + }, + { + name: "Almost email but invalid", + input: "Not an email: @domain.com or user@ or .com", + expected: "Not an email: @domain.com or user@ or .com", + }, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + assert.Equal(t, c.expected, ReplaceEmails(c.input, "redacted@example.org")) + }) + } +} diff --git a/pkg/files/secrets.go b/pkg/files/redact_secrets.go similarity index 100% rename from pkg/files/secrets.go rename to pkg/files/redact_secrets.go diff --git a/pkg/files/secrets_test.go b/pkg/files/redact_secrets_test.go similarity index 100% rename from pkg/files/secrets_test.go rename to pkg/files/redact_secrets_test.go