这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pkg/files/redact_emails.go
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions pkg/files/redact_emails_test.go
Original file line number Diff line number Diff line change
@@ -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"))
})
}
}
File renamed without changes.
File renamed without changes.