From bda2ef0ca023e79bddb941629043dec592968d0b Mon Sep 17 00:00:00 2001 From: Zach Wasserman Date: Fri, 31 Dec 2021 09:16:25 -0800 Subject: [PATCH] Fix regex matching email in host search (#3539) Fixes #3528 --- server/datastore/mysql/mysql.go | 2 +- server/datastore/mysql/mysql_test.go | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/server/datastore/mysql/mysql.go b/server/datastore/mysql/mysql.go index 6bcc2b5de2..f4b146235f 100644 --- a/server/datastore/mysql/mysql.go +++ b/server/datastore/mysql/mysql.go @@ -802,7 +802,7 @@ func searchLike(sql string, params []interface{}, match string, columns ...strin // definitely not cut out any valid address is to just check for // the presence of @, which is arguably the most important check // in this. -var rxLooseEmail = regexp.MustCompile(`^[^\s@]+@[^\s@\.]\..+$`) +var rxLooseEmail = regexp.MustCompile(`^[^\s@]+@[^\s@\.]+\..+$`) func hostSearchLike(sql string, params []interface{}, match string, columns ...string) (string, []interface{}) { base, args := searchLike(sql, params, match, columns...) diff --git a/server/datastore/mysql/mysql_test.go b/server/datastore/mysql/mysql_test.go index 80573a88b6..d01c039f58 100644 --- a/server/datastore/mysql/mysql_test.go +++ b/server/datastore/mysql/mysql_test.go @@ -781,7 +781,7 @@ func TestNewUsesRegisterTLS(t *testing.T) { require.Equal(t, "x509: certificate is not valid for any names, but wanted to match localhost", err.Error()) } -func TestWhereFilterTeas(t *testing.T) { +func TestWhereFilterTeams(t *testing.T) { t.Parallel() testCases := []struct { @@ -939,3 +939,24 @@ func TestCompareVersions(t *testing.T) { }) } } + +func TestRxLooseEmail(t *testing.T) { + testCases := []struct { + str string + match bool + }{ + {"foo", false}, + {"", false}, + {"foo@example", false}, + {"foo@example.com", true}, + {"foo+bar@example.com", true}, + {"foo.bar@example.com", true}, + {"foo.bar@baz.example.com", true}, + } + + for _, tc := range testCases { + t.Run(tc.str, func(t *testing.T) { + assert.Equal(t, tc.match, rxLooseEmail.MatchString(tc.str)) + }) + } +}