Skip to content

Find, verify, and analyze leaked credentials

3.9 KB
Raw Blame
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)

}