Skip to content

Commit 4129e0e

Browse files
authored
Add a warning for disallowed email domains (#29658)
Resolve #29660 Follow #29522 and #29609 Add a warning for disallowed email domains when admins manually add/edit users. Thanks @yp05327 for the [comment](#29605 (comment)) ![image](https://github.com/go-gitea/gitea/assets/15528715/6737b221-a3a2-4180-9ef8-b846c10f96e0)
1 parent 8fc1a8f commit 4129e0e

File tree

6 files changed

+32
-16
lines changed

6 files changed

+32
-16
lines changed

models/user/email_address.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -539,17 +539,17 @@ func validateEmailBasic(email string) error {
539539

540540
// validateEmailDomain checks whether the email domain is allowed or blocked
541541
func validateEmailDomain(email string) error {
542-
// if there is no allow list, then check email against block list
543-
if len(setting.Service.EmailDomainAllowList) == 0 &&
544-
validation.IsEmailDomainListed(setting.Service.EmailDomainBlockList, email) {
542+
if !IsEmailDomainAllowed(email) {
545543
return ErrEmailInvalid{email}
546544
}
547545

548-
// if there is an allow list, then check email against allow list
549-
if len(setting.Service.EmailDomainAllowList) > 0 &&
550-
!validation.IsEmailDomainListed(setting.Service.EmailDomainAllowList, email) {
551-
return ErrEmailInvalid{email}
546+
return nil
547+
}
548+
549+
func IsEmailDomainAllowed(email string) bool {
550+
if len(setting.Service.EmailDomainAllowList) == 0 {
551+
return !validation.IsEmailDomainListed(setting.Service.EmailDomainBlockList, email)
552552
}
553553

554-
return nil
554+
return validation.IsEmailDomainListed(setting.Service.EmailDomainAllowList, email)
555555
}

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ team_name_been_taken = The team name is already taken.
573573
team_no_units_error = Allow access to at least one repository section.
574574
email_been_used = The email address is already used.
575575
email_invalid = The email address is invalid.
576+
email_domain_is_not_allowed = The domain of user email <b>%s</b> conflicts with EMAIL_DOMAIN_ALLOWLIST or EMAIL_DOMAIN_BLOCKLIST. Please ensure your operation is expected.
576577
openid_been_used = The OpenID address "%s" is already used.
577578
username_password_incorrect = Username or password is incorrect.
578579
password_complexity = Password does not pass complexity requirements:

routers/api/v1/admin/user.go

+9
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ func CreateUser(ctx *context.APIContext) {
147147
}
148148
return
149149
}
150+
151+
if !user_model.IsEmailDomainAllowed(u.Email) {
152+
ctx.Resp.Header().Add("X-Gitea-Warning", fmt.Sprintf("the domain of user email %s conflicts with EMAIL_DOMAIN_ALLOWLIST or EMAIL_DOMAIN_BLOCKLIST", u.Email))
153+
}
154+
150155
log.Trace("Account created by admin (%s): %s", ctx.Doer.Name, u.Name)
151156

152157
// Send email notification.
@@ -220,6 +225,10 @@ func EditUser(ctx *context.APIContext) {
220225
}
221226
return
222227
}
228+
229+
if !user_model.IsEmailDomainAllowed(*form.Email) {
230+
ctx.Resp.Header().Add("X-Gitea-Warning", fmt.Sprintf("the domain of user email %s conflicts with EMAIL_DOMAIN_ALLOWLIST or EMAIL_DOMAIN_BLOCKLIST", *form.Email))
231+
}
223232
}
224233

225234
opts := &user_service.UpdateOptions{

routers/web/admin/users.go

+8
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ func NewUserPost(ctx *context.Context) {
202202
}
203203
return
204204
}
205+
206+
if !user_model.IsEmailDomainAllowed(u.Email) {
207+
ctx.Flash.Warning(ctx.Tr("form.email_domain_is_not_allowed", u.Email))
208+
}
209+
205210
log.Trace("Account created by admin (%s): %s", ctx.Doer.Name, u.Name)
206211

207212
// Send email notification.
@@ -425,6 +430,9 @@ func EditUserPost(ctx *context.Context) {
425430
}
426431
return
427432
}
433+
if !user_model.IsEmailDomainAllowed(form.Email) {
434+
ctx.Flash.Warning(ctx.Tr("form.email_domain_is_not_allowed", form.Email))
435+
}
428436
}
429437

430438
opts := &user_service.UpdateOptions{

services/forms/user_form.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"strings"
1111

1212
auth_model "code.gitea.io/gitea/models/auth"
13+
user_model "code.gitea.io/gitea/models/user"
1314
"code.gitea.io/gitea/modules/setting"
1415
"code.gitea.io/gitea/modules/structs"
15-
"code.gitea.io/gitea/modules/validation"
1616
"code.gitea.io/gitea/modules/web/middleware"
1717
"code.gitea.io/gitea/services/context"
1818

@@ -109,11 +109,7 @@ func (f *RegisterForm) Validate(req *http.Request, errs binding.Errors) binding.
109109
// domains in the whitelist or if it doesn't match any of
110110
// domains in the blocklist, if any such list is not empty.
111111
func (f *RegisterForm) IsEmailDomainAllowed() bool {
112-
if len(setting.Service.EmailDomainAllowList) == 0 {
113-
return !validation.IsEmailDomainListed(setting.Service.EmailDomainBlockList, f.Email)
114-
}
115-
116-
return validation.IsEmailDomainListed(setting.Service.EmailDomainAllowList, f.Email)
112+
return user_model.IsEmailDomainAllowed(f.Email)
117113
}
118114

119115
// MustChangePasswordForm form for updating your password after account creation

tests/integration/api_admin_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ func TestAPICreateUser_NotAllowedEmailDomain(t *testing.T) {
354354
"password": "allowedUser1_pass",
355355
"must_change_password": "true",
356356
}).AddTokenAuth(token)
357-
MakeRequest(t, req, http.StatusCreated)
357+
resp := MakeRequest(t, req, http.StatusCreated)
358+
assert.Equal(t, "the domain of user email allowedUser1@example1.org conflicts with EMAIL_DOMAIN_ALLOWLIST or EMAIL_DOMAIN_BLOCKLIST", resp.Header().Get("X-Gitea-Warning"))
358359

359360
req = NewRequest(t, "DELETE", "/api/v1/admin/users/allowedUser1").AddTokenAuth(token)
360361
MakeRequest(t, req, http.StatusNoContent)
@@ -378,7 +379,8 @@ func TestAPIEditUser_NotAllowedEmailDomain(t *testing.T) {
378379
SourceID: 0,
379380
Email: &newEmail,
380381
}).AddTokenAuth(token)
381-
MakeRequest(t, req, http.StatusOK)
382+
resp := MakeRequest(t, req, http.StatusOK)
383+
assert.Equal(t, "the domain of user email user2@example1.com conflicts with EMAIL_DOMAIN_ALLOWLIST or EMAIL_DOMAIN_BLOCKLIST", resp.Header().Get("X-Gitea-Warning"))
382384

383385
originalEmail := "user2@example.com"
384386
req = NewRequestWithJSON(t, "PATCH", urlStr, api.EditUserOption{

0 commit comments

Comments
 (0)