fix(user): unify email validation for registration and settings (#39304)

Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Abhay Pratap Singh
2026-09-16 17:34:41 +00:00
committed by GitHub
co-authored by silverwind wxiaoguang
parent c6c671e113
commit 7ebb2caa9e
21 changed files with 138 additions and 209 deletions
+23
View File
@@ -4,14 +4,18 @@
package validation
import (
"net/mail"
"net/url"
"regexp"
"slices"
"strings"
"sync"
"unicode/utf8"
"gitea.dev/modules/glob"
"gitea.dev/modules/setting"
"golang.org/x/net/idna"
)
type globalVarsStruct struct {
@@ -108,3 +112,22 @@ func IsValidBadgeSlug(slug string) bool {
vars := globalVars()
return vars.validBadgeSlugPattern.MatchString(slug) && !vars.invalidBadgeSlugPattern.MatchString(slug)
}
func IsEmailAddressValid(email string) bool {
if strings.ContainsFunc(email, func(r rune) bool { return r >= utf8.RuneSelf }) {
// At the moment, we don't support UTF8 email address. To support it, need to correctly handle IDN/punycode
return false
}
addr, err := mail.ParseAddress(email)
if err != nil || addr.Address != email {
// email must be parseable, and the "email" string must be the address, no other parts
return false
}
_, domain, _ := strings.Cut(email, "@")
if strings.HasPrefix(domain, "[") {
// address like "foo@[192.168.1.2]"
return true
}
_, err = idna.Registration.ToASCII(domain)
return err == nil
}