Files
trufflehog/pkg/common/patterns_test.go
Brad Larsen 2bffafb280 Clean up use of wasilibs/go-re2 (#5273)
* perf: switch nearly all remaining uses of stdlib regexp to wasilibs/go-re2
* perf: upgrade github.com/wasilibs/go-re2 from v1.9.0 to v1.12.0
* gofmt modified files
* snowflake detector: hoist constant regex compilation to package-level variables
* add golangci lint to steer folks to go-re2 instead of regexp
2026-09-09 17:25:38 -04:00

127 lines
3.9 KiB
Go

package common
import (
regexp "github.com/wasilibs/go-re2"
"testing"
"github.com/stretchr/testify/assert"
)
const (
usernamePattern = `?()/\+=\s\n`
passwordPattern = `^<>;.*&|£\n\s`
usernameRegex = `(?im)(?:user|usr)\S{0,40}?[:=\s]{1,3}[ '"=]{0,1}([^:?()/\+=\s\n]{4,40})\b`
passwordRegex = `(?im)(?:pass|password)\S{0,40}?[:=\s]{1,3}[ '"=]{0,1}([^:^<>;.*&|£\n\s]{4,40})`
)
func TestEmailRegexCheck(t *testing.T) {
testEmails := `
// positive cases
standard email = [email protected]
subdomain email = [email protected]
organization email = [email protected]
test email = [email protected]
with tag email = [email protected]
hyphen domain = [email protected]
service email = [email protected]
underscore email = [email protected]
department email = [email protected]
alphanumeric email = [email protected]
local server email = [email protected]
dot email = [email protected]
special char email = [email protected]
support email = [email protected]
insenstive email = [email protected]
insenstive domain = [email protected]
mix email = [email protected]
// negative cases
not an email = abc.123@z
looks like email = test@user <- no domain
random text = here's some information about local-user@edu user
`
expectedStr := []string{
"[email protected]", "[email protected]",
"[email protected]", "[email protected]", "[email protected]",
"[email protected]", "[email protected]", "[email protected]",
"[email protected]", "[email protected]", "[email protected]",
"[email protected]", "[email protected]", "[email protected]",
"[email protected]", "[email protected]", "[email protected]",
}
emailRegex := regexp.MustCompile(EmailPattern)
emailMatches := emailRegex.FindAllString(testEmails, -1)
assert.Exactly(t, emailMatches, expectedStr)
}
func TestUsernameRegexCheck(t *testing.T) {
usernameRegexPat := UsernameRegexCheck(usernamePattern)
expectedRegexPattern := regexp.MustCompile(usernameRegex)
if usernameRegexPat.compiledRegex.String() != expectedRegexPattern.String() {
t.Errorf("\n got %v \n want %v", usernameRegexPat.compiledRegex, expectedRegexPattern)
}
testString := `username = "johnsmith123"
username='johnsmith123'
username:="johnsmith123"
username:="johnsmith123",
username:="johnsmith123";
username = johnsmith123
username=johnsmith123`
expectedStr := []string{
"johnsmith123",
"johnsmith123",
"johnsmith123",
"johnsmith123",
"johnsmith123",
"johnsmith123",
"johnsmith123",
}
usernameRegexMatches := usernameRegexPat.Matches([]byte(testString))
assert.Exactly(t, usernameRegexMatches, expectedStr)
}
func TestPasswordRegexCheck(t *testing.T) {
passwordRegexPat := PasswordRegexCheck(passwordPattern)
expectedRegexPattern := regexp.MustCompile(passwordRegex)
assert.Equal(t, expectedRegexPattern.String(), passwordRegexPat.compiledRegex.String())
testString := `password = "johnsmith123$!"
password='johnsmith123$!'
password:="johnsmith123$!"
password:="johnsmith123$!",
password:="johnsmith123$!";
password:="johnsmi',th123$!";
password = johnsmith123$!
password=johnsmith123$!
PasswordAuthenticator(username, "johnsmith123$!")`
expectedStr := []string{
"johnsmith123$!",
"johnsmith123$!",
"johnsmith123$!",
"johnsmith123$!",
"johnsmith123$!",
"johnsmi',th123$!",
"johnsmith123$!",
"johnsmith123$!",
"johnsmith123$!",
}
passwordRegexMatches := passwordRegexPat.Matches([]byte(testString))
assert.Exactly(t, passwordRegexMatches, expectedStr)
}