chore: validate email addresses with dns

This commit is contained in:
Ferdinand Mütsch 2023-01-02 15:14:49 +01:00
parent ef5b49ebd8
commit a1444bca8c
4 changed files with 75 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"crypto/md5"
"fmt"
conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/utils"
"regexp"
"strings"
"time"
@ -190,8 +191,9 @@ func ValidatePassword(password string) bool {
return len(password) >= 6
}
// ValidateEmail checks that, if an email address is given, it has proper syntax and (if not in dev mode) an MX record exists for the domain
func ValidateEmail(email string) bool {
return email == "" || mailRegex.Match([]byte(email))
return email == "" || (mailRegex.Match([]byte(email)) && (conf.Get().IsDev() || utils.CheckEmailMX(email)))
}
func ValidateTimezone(tz string) bool {

View File

@ -178,7 +178,7 @@ func (h *SettingsHandler) actionUpdateUser(w http.ResponseWriter, r *http.Reques
}
if !payload.IsValid() {
return http.StatusBadRequest, "", "invalid parameters"
return http.StatusBadRequest, "", "invalid parameters - perhaps invalid e-mail address?"
}
if payload.Email == "" && user.HasActiveSubscription() {

55
scripts/email_checker.go Normal file
View File

@ -0,0 +1,55 @@
package main
// Usage example:
// cat emails.txt go run email_checker.go > result.txt
import (
"bufio"
"fmt"
"log"
"net"
"os"
"regexp"
"strings"
)
const MailPattern = "[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+"
var mailRegex *regexp.Regexp
func init() {
mailRegex = regexp.MustCompile(MailPattern)
}
func CheckEmailMX(email string) bool {
parts := strings.Split(email, "@")
if len(parts) != 2 {
return false
}
records, err := net.LookupMX(parts[1])
return len(records) > 0 && err == nil
}
func ValidateEmail(email string) bool {
return mailRegex.Match([]byte(email)) && CheckEmailMX(email)
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
email := scanner.Text()
if email == "" {
return
}
if ValidateEmail(email) {
fmt.Printf("[+] %s\n", email)
} else {
fmt.Printf("[-] %s\n", email)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}

16
utils/dns.go Normal file
View File

@ -0,0 +1,16 @@
package utils
import (
"net"
"strings"
)
// CheckEmailMX takes an e-mail address and verifies that an MX DNS record exists for its domain
func CheckEmailMX(email string) bool {
parts := strings.Split(email, "@")
if len(parts) != 2 {
return false
}
records, err := net.LookupMX(parts[1])
return len(records) > 0 && err == nil
}