Skip to content

Commit 41bae29

Browse files
check blocklist for emails when adding them to account (#26812) (#26831)
Backport #26812 by @techknowlogick Co-authored-by: techknowlogick <[email protected]>
1 parent c72f606 commit 41bae29

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

models/user/email_address.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/log"
1717
"code.gitea.io/gitea/modules/setting"
1818
"code.gitea.io/gitea/modules/util"
19+
"code.gitea.io/gitea/modules/validation"
1920

2021
"xorm.io/builder"
2122
)
@@ -161,7 +162,17 @@ func ValidateEmail(email string) error {
161162
return ErrEmailInvalid{email}
162163
}
163164

164-
// TODO: add an email allow/block list
165+
// if there is no allow list, then check email against block list
166+
if len(setting.Service.EmailDomainAllowList) == 0 &&
167+
validation.IsEmailDomainListed(setting.Service.EmailDomainBlockList, email) {
168+
return ErrEmailInvalid{email}
169+
}
170+
171+
// if there is an allow list, then check email against allow list
172+
if len(setting.Service.EmailDomainAllowList) > 0 &&
173+
!validation.IsEmailDomainListed(setting.Service.EmailDomainAllowList, email) {
174+
return ErrEmailInvalid{email}
175+
}
165176

166177
return nil
167178
}

modules/validation/helpers.go

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

1212
"code.gitea.io/gitea/modules/setting"
13+
14+
"github.com/gobwas/glob"
1315
)
1416

1517
var externalTrackerRegex = regexp.MustCompile(`({?)(?:user|repo|index)+?(}?)`)
@@ -48,6 +50,29 @@ func IsValidSiteURL(uri string) bool {
4850
return false
4951
}
5052

53+
// IsEmailDomainListed checks whether the domain of an email address
54+
// matches a list of domains
55+
func IsEmailDomainListed(globs []glob.Glob, email string) bool {
56+
if len(globs) == 0 {
57+
return false
58+
}
59+
60+
n := strings.LastIndex(email, "@")
61+
if n <= 0 {
62+
return false
63+
}
64+
65+
domain := strings.ToLower(email[n+1:])
66+
67+
for _, g := range globs {
68+
if g.Match(domain) {
69+
return true
70+
}
71+
}
72+
73+
return false
74+
}
75+
5176
// IsAPIURL checks if URL is current Gitea instance API URL
5277
func IsAPIURL(uri string) bool {
5378
return strings.HasPrefix(strings.ToLower(uri), strings.ToLower(setting.AppURL+"api"))

services/forms/user_form.go

+3-26
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
"code.gitea.io/gitea/modules/context"
1414
"code.gitea.io/gitea/modules/setting"
1515
"code.gitea.io/gitea/modules/structs"
16+
"code.gitea.io/gitea/modules/validation"
1617
"code.gitea.io/gitea/modules/web/middleware"
1718

1819
"gitea.com/go-chi/binding"
19-
"github.com/gobwas/glob"
2020
)
2121

2222
// InstallForm form for installation page
@@ -103,40 +103,17 @@ func (f *RegisterForm) Validate(req *http.Request, errs binding.Errors) binding.
103103
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
104104
}
105105

106-
// IsEmailDomainListed checks whether the domain of an email address
107-
// matches a list of domains
108-
func IsEmailDomainListed(globs []glob.Glob, email string) bool {
109-
if len(globs) == 0 {
110-
return false
111-
}
112-
113-
n := strings.LastIndex(email, "@")
114-
if n <= 0 {
115-
return false
116-
}
117-
118-
domain := strings.ToLower(email[n+1:])
119-
120-
for _, g := range globs {
121-
if g.Match(domain) {
122-
return true
123-
}
124-
}
125-
126-
return false
127-
}
128-
129106
// IsEmailDomainAllowed validates that the email address
130107
// provided by the user matches what has been configured .
131108
// The email is marked as allowed if it matches any of the
132109
// domains in the whitelist or if it doesn't match any of
133110
// domains in the blocklist, if any such list is not empty.
134111
func (f *RegisterForm) IsEmailDomainAllowed() bool {
135112
if len(setting.Service.EmailDomainAllowList) == 0 {
136-
return !IsEmailDomainListed(setting.Service.EmailDomainBlockList, f.Email)
113+
return !validation.IsEmailDomainListed(setting.Service.EmailDomainBlockList, f.Email)
137114
}
138115

139-
return IsEmailDomainListed(setting.Service.EmailDomainAllowList, f.Email)
116+
return validation.IsEmailDomainListed(setting.Service.EmailDomainAllowList, f.Email)
140117
}
141118

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

0 commit comments

Comments
 (0)