Skip to content

Commit

Permalink
Merge pull request knadh#205 from knadh/fix-email-validation
Browse files Browse the repository at this point in the history
fix: use mail.ParseAddress to validate email instead of custom regex
  • Loading branch information
knadh authored Oct 17, 2020
2 parents 8dbe30c + 86c6189 commit 669cb09
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions internal/subimporter/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"io"
"io/ioutil"
"log"
"net/mail"
"os"
"regexp"
"strings"
Expand Down Expand Up @@ -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:]]")
)

Expand Down Expand Up @@ -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 `<[email protected]>`.
if em, err := mail.ParseAddress(email); err != nil || em.Address != email {
return false
}
return true
}

0 comments on commit 669cb09

Please sign in to comment.