Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make e-mail sanity check more precise #20991

Merged
merged 13 commits into from
Oct 12, 2022
5 changes: 2 additions & 3 deletions models/user/email_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (err ErrEmailCharIsNotSupported) Error() string {
}

// ErrEmailInvalid represents an error where the email address does not comply with RFC 5322
// or has a leading '-' character
balanceofcowards marked this conversation as resolved.
Show resolved Hide resolved
type ErrEmailInvalid struct {
Email string
}
Expand Down Expand Up @@ -134,9 +135,7 @@ func ValidateEmail(email string) error {
return ErrEmailCharIsNotSupported{email}
}

if !(email[0] >= 'a' && email[0] <= 'z') &&
!(email[0] >= 'A' && email[0] <= 'Z') &&
!(email[0] >= '0' && email[0] <= '9') {
if email[0] == '-' {
return ErrEmailInvalid{email}
}

Expand Down
36 changes: 19 additions & 17 deletions models/user/email_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,23 +281,25 @@ func TestEmailAddressValidate(t *testing.T) {
`first~last@iana.org`: nil,
`first;last@iana.org`: user_model.ErrEmailCharIsNotSupported{`first;last@iana.org`},
".233@qq.com": user_model.ErrEmailInvalid{".233@qq.com"},
"!233@qq.com": user_model.ErrEmailInvalid{"!233@qq.com"},
"#233@qq.com": user_model.ErrEmailInvalid{"#233@qq.com"},
"$233@qq.com": user_model.ErrEmailInvalid{"$233@qq.com"},
"%233@qq.com": user_model.ErrEmailInvalid{"%233@qq.com"},
"&233@qq.com": user_model.ErrEmailInvalid{"&233@qq.com"},
"'233@qq.com": user_model.ErrEmailInvalid{"'233@qq.com"},
"*233@qq.com": user_model.ErrEmailInvalid{"*233@qq.com"},
"+233@qq.com": user_model.ErrEmailInvalid{"+233@qq.com"},
"/233@qq.com": user_model.ErrEmailInvalid{"/233@qq.com"},
"=233@qq.com": user_model.ErrEmailInvalid{"=233@qq.com"},
"?233@qq.com": user_model.ErrEmailInvalid{"?233@qq.com"},
"^233@qq.com": user_model.ErrEmailInvalid{"^233@qq.com"},
"`233@qq.com": user_model.ErrEmailInvalid{"`233@qq.com"},
"{233@qq.com": user_model.ErrEmailInvalid{"{233@qq.com"},
"|233@qq.com": user_model.ErrEmailInvalid{"|233@qq.com"},
"}233@qq.com": user_model.ErrEmailInvalid{"}233@qq.com"},
"~233@qq.com": user_model.ErrEmailInvalid{"~233@qq.com"},
"!233@qq.com": nil,
"#233@qq.com": nil,
"$233@qq.com": nil,
"%233@qq.com": nil,
"&233@qq.com": nil,
"'233@qq.com": nil,
"*233@qq.com": nil,
"+233@qq.com": nil,
"-233@qq.com": user_model.ErrEmailInvalid{"-233@qq.com"},
"/233@qq.com": nil,
"=233@qq.com": nil,
"?233@qq.com": nil,
"^233@qq.com": nil,
"_233@qq.com": nil,
"`233@qq.com": nil,
"{233@qq.com": nil,
"|233@qq.com": nil,
"}233@qq.com": nil,
"~233@qq.com": nil,
";233@qq.com": user_model.ErrEmailCharIsNotSupported{";233@qq.com"},
"Foo <foo@bar.com>": user_model.ErrEmailCharIsNotSupported{"Foo <foo@bar.com>"},
string([]byte{0xE2, 0x84, 0xAA}): user_model.ErrEmailCharIsNotSupported{string([]byte{0xE2, 0x84, 0xAA})},
Expand Down