Description
The ValidateEmail function provided below utilizes the govalidator.IsExistingEmail() function to validate email addresses. However, using this function can cause issues with the validation of GitHub Primary email addresses, such as "[email protected]". This is because the govalidator.IsExistingEmail() function attempts to verify if the domain can be reached, by performing a check using the net.LookupMX(host) and net.LookupIP(host) functions.
This domain verification check can fail for certain email addresses that are valid, such as the aforementioned GitHub Primary email address. This is because the "users.noreply.github.com" domain cannot be resolved using the net.LookupIP(host) function. As a result, using the govalidator.IsExistingEmail() function in this context may lead to false negatives and result in valid email addresses being rejected.
func ValidateEmail(email string, modified bool) error {
if modified && email == "" {
return nil
}
if !govalidator.IsExistingEmail(email) {
return ErrInvalidEmail
}
return nil
}
func IsExistingEmail(email string) bool {
if len(email) < 6 || len(email) > 254 {
return false
}
at := strings.LastIndex(email, "@")
if at <= 0 || at > len(email)-3 {
return false
}
user := email[:at]
host := email[at+1:]
if len(user) > 64 {
return false
}
switch host {
case "localhost", "example.com":
return true
}
if userDotRegexp.MatchString(user) || !userRegexp.MatchString(user) || !hostRegexp.MatchString(host) {
return false
}
if _, err := net.LookupMX(host); err != nil {
if _, err := net.LookupIP(host); err != nil {
return false
}
}
return true
}