From 86c618981fdaa89736bcf3ce5c2cf8920bd766d8 Mon Sep 17 00:00:00 2001 From: Vivek R Date: Sun, 11 Oct 2020 12:30:16 +0530 Subject: [PATCH] fix: use mail.ParseAddress to validate email instead of custom regex According to [RFC6532](https://tools.ietf.org/html/rfc6532) unicode characters are supported in email address but the in-built custom regex was only validating ASCII characters. --- internal/subimporter/importer.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/subimporter/importer.go b/internal/subimporter/importer.go index 0099efd..05a99f5 100644 --- a/internal/subimporter/importer.go +++ b/internal/subimporter/importer.go @@ -17,6 +17,7 @@ import ( "io" "io/ioutil" "log" + "net/mail" "os" "regexp" "strings" @@ -109,9 +110,6 @@ var ( "name": true, "attributes": true} - // https://www.alexedwards.net/blog/validation-snippets-for-go#email-validation - regexEmail = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") - regexCleanStr = regexp.MustCompile("[[:^ascii:]]") ) @@ -619,5 +617,11 @@ func countLines(r io.Reader) (int, error) { // IsEmail checks whether the given string is a valid e-mail address. func IsEmail(email string) bool { - return regexEmail.MatchString(email) + // Since `mail.ParseAddress` parses an email address which can also contain optional name component + // here we check if incoming email string is same as the parsed email.Address. So this eliminates + // any valid email address with name and also valid address with empty name like ``. + if em, err := mail.ParseAddress(email); err != nil || em.Address != email { + return false + } + return true }