Skip to content

Commit d025d84

Browse files
chrisshyizeripathlunnytechknowlogick
authored
Add email validity check (#13475)
* Improve error feedback for duplicate deploy keys Instead of a generic HTTP 500 error page, a flash message is rendered with the deploy key page template so inform the user that a key with the intended title already exists. * API returns 422 error when key with name exists * Add email validity checking Add email validity checking for the following routes: [Web interface] 1. User registration 2. User creation by admin 3. Adding an email through user settings [API] 1. POST /admin/users 2. PATCH /admin/users/:username 3. POST /user/emails * Add further tests * Add signup email tests * Add email validity check for linking existing account * Address PR comments * Remove unneeded DB session * Move email check to updateUser Co-authored-by: zeripath <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 7d2700c commit d025d84

File tree

13 files changed

+163
-2
lines changed

13 files changed

+163
-2
lines changed

integrations/api_admin_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,22 @@ func TestAPIListUsersNonAdmin(t *testing.T) {
144144
req := NewRequestf(t, "GET", "/api/v1/admin/users?token=%s", token)
145145
session.MakeRequest(t, req, http.StatusForbidden)
146146
}
147+
148+
func TestAPICreateUserInvalidEmail(t *testing.T) {
149+
defer prepareTestEnv(t)()
150+
adminUsername := "user1"
151+
session := loginUser(t, adminUsername)
152+
token := getTokenForLoggedInUser(t, session)
153+
urlStr := fmt.Sprintf("/api/v1/admin/users?token=%s", token)
154+
req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
155+
"email": "[email protected]\r\n",
156+
"full_name": "invalid user",
157+
"login_name": "invalidUser",
158+
"must_change_password": "true",
159+
"password": "password",
160+
"send_notify": "true",
161+
"source_id": "0",
162+
"username": "invalidUser",
163+
})
164+
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
165+
}

integrations/signup_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
package integrations
66

77
import (
8+
"fmt"
89
"net/http"
10+
"strings"
911
"testing"
1012

1113
"code.gitea.io/gitea/modules/setting"
14+
"github.com/stretchr/testify/assert"
15+
"github.com/unknwon/i18n"
1216
)
1317

1418
func TestSignup(t *testing.T) {
@@ -28,3 +32,37 @@ func TestSignup(t *testing.T) {
2832
req = NewRequest(t, "GET", "/exampleUser")
2933
MakeRequest(t, req, http.StatusOK)
3034
}
35+
36+
func TestSignupEmail(t *testing.T) {
37+
defer prepareTestEnv(t)()
38+
39+
setting.Service.EnableCaptcha = false
40+
41+
tests := []struct {
42+
email string
43+
wantStatus int
44+
wantMsg string
45+
}{
46+
{"[email protected]\r\n", http.StatusOK, i18n.Tr("en", "form.email_invalid", nil)},
47+
{"[email protected]\r", http.StatusOK, i18n.Tr("en", "form.email_invalid", nil)},
48+
{"[email protected]\n", http.StatusOK, i18n.Tr("en", "form.email_invalid", nil)},
49+
{"[email protected]", http.StatusFound, ""},
50+
}
51+
52+
for i, test := range tests {
53+
req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
54+
"user_name": fmt.Sprintf("exampleUser%d", i),
55+
"email": test.email,
56+
"password": "examplePassword!1",
57+
"retype": "examplePassword!1",
58+
})
59+
resp := MakeRequest(t, req, test.wantStatus)
60+
if test.wantMsg != "" {
61+
htmlDoc := NewHTMLParser(t, resp.Body)
62+
assert.Equal(t,
63+
test.wantMsg,
64+
strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
65+
)
66+
}
67+
}
68+
}

models/error.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,21 @@ func (err ErrEmailAlreadyUsed) Error() string {
193193
return fmt.Sprintf("e-mail already in use [email: %s]", err.Email)
194194
}
195195

196+
// ErrEmailInvalid represents an error where the email address does not comply with RFC 5322
197+
type ErrEmailInvalid struct {
198+
Email string
199+
}
200+
201+
// IsErrEmailInvalid checks if an error is an ErrEmailInvalid
202+
func IsErrEmailInvalid(err error) bool {
203+
_, ok := err.(ErrEmailInvalid)
204+
return ok
205+
}
206+
207+
func (err ErrEmailInvalid) Error() string {
208+
return fmt.Sprintf("e-mail invalid [email: %s]", err.Email)
209+
}
210+
196211
// ErrOpenIDAlreadyUsed represents a "OpenIDAlreadyUsed" kind of error.
197212
type ErrOpenIDAlreadyUsed struct {
198213
OpenID string

models/user.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"errors"
1515
"fmt"
1616
_ "image/jpeg" // Needed for jpeg support
17+
"net/mail"
1718
"os"
1819
"path/filepath"
1920
"regexp"
@@ -808,6 +809,11 @@ func CreateUser(u *User) (err error) {
808809
return ErrEmailAlreadyUsed{u.Email}
809810
}
810811

812+
_, err = mail.ParseAddress(u.Email)
813+
if err != nil {
814+
return ErrEmailInvalid{u.Email}
815+
}
816+
811817
isExist, err = isEmailUsed(sess, u.Email)
812818
if err != nil {
813819
return err
@@ -951,7 +957,12 @@ func checkDupEmail(e Engine, u *User) error {
951957
}
952958

953959
func updateUser(e Engine, u *User) error {
954-
_, err := e.ID(u.ID).AllCols().Update(u)
960+
u.Email = strings.ToLower(u.Email)
961+
_, err := mail.ParseAddress(u.Email)
962+
if err != nil {
963+
return ErrEmailInvalid{u.Email}
964+
}
965+
_, err = e.ID(u.ID).AllCols().Update(u)
955966
return err
956967
}
957968

models/user_mail.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package models
88
import (
99
"errors"
1010
"fmt"
11+
"net/mail"
1112
"strings"
1213

1314
"code.gitea.io/gitea/modules/log"
@@ -143,6 +144,11 @@ func addEmailAddress(e Engine, email *EmailAddress) error {
143144
return ErrEmailAlreadyUsed{email.Email}
144145
}
145146

147+
_, err = mail.ParseAddress(email.Email)
148+
if err != nil {
149+
return ErrEmailInvalid{email.Email}
150+
}
151+
146152
_, err = e.Insert(email)
147153
return err
148154
}
@@ -167,6 +173,10 @@ func AddEmailAddresses(emails []*EmailAddress) error {
167173
} else if used {
168174
return ErrEmailAlreadyUsed{emails[i].Email}
169175
}
176+
_, err = mail.ParseAddress(emails[i].Email)
177+
if err != nil {
178+
return ErrEmailInvalid{emails[i].Email}
179+
}
170180
}
171181

172182
if _, err := x.Insert(emails); err != nil {

models/user_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,21 @@ func TestCreateUser(t *testing.T) {
329329
assert.NoError(t, DeleteUser(user))
330330
}
331331

332+
func TestCreateUserInvalidEmail(t *testing.T) {
333+
user := &User{
334+
Name: "GiteaBot",
335+
Email: "[email protected]\r\n",
336+
Passwd: ";p['////..-++']",
337+
IsAdmin: false,
338+
Theme: setting.UI.DefaultTheme,
339+
MustChangePassword: false,
340+
}
341+
342+
err := CreateUser(user)
343+
assert.Error(t, err)
344+
assert.True(t, IsErrEmailInvalid(err))
345+
}
346+
332347
func TestCreateUser_Issue5882(t *testing.T) {
333348

334349
// Init settings

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ org_name_been_taken = The organization name is already taken.
366366
team_name_been_taken = The team name is already taken.
367367
team_no_units_error = Allow access to at least one repository section.
368368
email_been_used = The email address is already used.
369+
email_invalid = The email address is invalid.
369370
openid_been_used = The OpenID address '%s' is already used.
370371
username_password_incorrect = Username or password is incorrect.
371372
password_complexity = Password does not pass complexity requirements:

routers/admin/users.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ func NewUserPost(ctx *context.Context, form auth.AdminCreateUserForm) {
129129
case models.IsErrEmailAlreadyUsed(err):
130130
ctx.Data["Err_Email"] = true
131131
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplUserNew, &form)
132+
case models.IsErrEmailInvalid(err):
133+
ctx.Data["Err_Email"] = true
134+
ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tplUserNew, &form)
132135
case models.IsErrNameReserved(err):
133136
ctx.Data["Err_UserName"] = true
134137
ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplUserNew, &form)
@@ -277,6 +280,9 @@ func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) {
277280
if models.IsErrEmailAlreadyUsed(err) {
278281
ctx.Data["Err_Email"] = true
279282
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplUserEdit, &form)
283+
} else if models.IsErrEmailInvalid(err) {
284+
ctx.Data["Err_Email"] = true
285+
ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tplUserEdit, &form)
280286
} else {
281287
ctx.ServerError("UpdateUser", err)
282288
}

routers/admin/users_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,33 @@ func TestNewUserPost_MustChangePasswordFalse(t *testing.T) {
8787
assert.Equal(t, email, u.Email)
8888
assert.False(t, u.MustChangePassword)
8989
}
90+
91+
func TestNewUserPost_InvalidEmail(t *testing.T) {
92+
93+
models.PrepareTestEnv(t)
94+
ctx := test.MockContext(t, "admin/users/new")
95+
96+
u := models.AssertExistsAndLoadBean(t, &models.User{
97+
IsAdmin: true,
98+
ID: 2,
99+
}).(*models.User)
100+
101+
ctx.User = u
102+
103+
username := "gitea"
104+
email := "[email protected]\r\n"
105+
106+
form := auth.AdminCreateUserForm{
107+
LoginType: "local",
108+
LoginName: "local",
109+
UserName: username,
110+
Email: email,
111+
Password: "abc123ABC!=$",
112+
SendNotify: false,
113+
MustChangePassword: false,
114+
}
115+
116+
NewUserPost(ctx, form)
117+
118+
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
119+
}

routers/api/v1/admin/user.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
101101
models.IsErrEmailAlreadyUsed(err) ||
102102
models.IsErrNameReserved(err) ||
103103
models.IsErrNameCharsNotAllowed(err) ||
104+
models.IsErrEmailInvalid(err) ||
104105
models.IsErrNamePatternNotAllowed(err) {
105106
ctx.Error(http.StatusUnprocessableEntity, "", err)
106107
} else {
@@ -208,7 +209,7 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) {
208209
}
209210

210211
if err := models.UpdateUser(u); err != nil {
211-
if models.IsErrEmailAlreadyUsed(err) {
212+
if models.IsErrEmailAlreadyUsed(err) || models.IsErrEmailInvalid(err) {
212213
ctx.Error(http.StatusUnprocessableEntity, "", err)
213214
} else {
214215
ctx.Error(http.StatusInternalServerError, "UpdateUser", err)

routers/api/v1/user/email.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package user
66

77
import (
8+
"fmt"
89
"net/http"
910

1011
"code.gitea.io/gitea/models"
@@ -78,6 +79,9 @@ func AddEmail(ctx *context.APIContext, form api.CreateEmailOption) {
7879
if err := models.AddEmailAddresses(emails); err != nil {
7980
if models.IsErrEmailAlreadyUsed(err) {
8081
ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(models.ErrEmailAlreadyUsed).Email)
82+
} else if models.IsErrEmailInvalid(err) {
83+
errMsg := fmt.Sprintf("Email address %s invalid", err.(models.ErrEmailInvalid).Email)
84+
ctx.Error(http.StatusUnprocessableEntity, "", errMsg)
8185
} else {
8286
ctx.Error(http.StatusInternalServerError, "AddEmailAddresses", err)
8387
}

routers/user/auth.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,9 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
964964
case models.IsErrEmailAlreadyUsed(err):
965965
ctx.Data["Err_Email"] = true
966966
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplLinkAccount, &form)
967+
case models.IsErrEmailInvalid(err):
968+
ctx.Data["Err_Email"] = true
969+
ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tplSignUp, &form)
967970
case models.IsErrNameReserved(err):
968971
ctx.Data["Err_UserName"] = true
969972
ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplLinkAccount, &form)
@@ -1151,6 +1154,9 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
11511154
case models.IsErrEmailAlreadyUsed(err):
11521155
ctx.Data["Err_Email"] = true
11531156
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignUp, &form)
1157+
case models.IsErrEmailInvalid(err):
1158+
ctx.Data["Err_Email"] = true
1159+
ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tplSignUp, &form)
11541160
case models.IsErrNameReserved(err):
11551161
ctx.Data["Err_UserName"] = true
11561162
ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplSignUp, &form)

routers/user/setting/account.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ func EmailPost(ctx *context.Context, form auth.AddEmailForm) {
179179

180180
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSettingsAccount, &form)
181181
return
182+
} else if models.IsErrEmailInvalid(err) {
183+
loadAccountData(ctx)
184+
185+
ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tplSettingsAccount, &form)
186+
return
182187
}
183188
ctx.ServerError("AddEmailAddress", err)
184189
return

0 commit comments

Comments
 (0)