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

check blocklist for emails when adding them to account #26812

Merged
merged 4 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion models/user/email_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/validation"

"xorm.io/builder"
)
Expand Down Expand Up @@ -161,7 +162,17 @@ func ValidateEmail(email string) error {
return ErrEmailInvalid{email}
}

// TODO: add an email allow/block list
// if there is no allow list, then check email against block list
if len(setting.Service.EmailDomainAllowList) == 0 &&
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
validation.IsEmailDomainListed(setting.Service.EmailDomainBlockList, email) {
return ErrEmailInvalid{email}
}

// if there is an allow list, then check email against allow list
if len(setting.Service.EmailDomainAllowList) > 0 &&
!validation.IsEmailDomainListed(setting.Service.EmailDomainAllowList, email) {
return ErrEmailInvalid{email}
}

return nil
}
Expand Down
25 changes: 25 additions & 0 deletions modules/validation/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"

"code.gitea.io/gitea/modules/setting"

"github.com/gobwas/glob"
)

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

// IsEmailDomainListed checks whether the domain of an email address
// matches a list of domains
func IsEmailDomainListed(globs []glob.Glob, email string) bool {
if len(globs) == 0 {
return false
}

n := strings.LastIndex(email, "@")
if n <= 0 {
return false
}

domain := strings.ToLower(email[n+1:])

for _, g := range globs {
if g.Match(domain) {
return true
}
}

return false
}

// IsAPIURL checks if URL is current Gitea instance API URL
func IsAPIURL(uri string) bool {
return strings.HasPrefix(strings.ToLower(uri), strings.ToLower(setting.AppURL+"api"))
Expand Down
29 changes: 3 additions & 26 deletions services/forms/user_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/validation"
"code.gitea.io/gitea/modules/web/middleware"

"gitea.com/go-chi/binding"
"github.com/gobwas/glob"
)

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

// IsEmailDomainListed checks whether the domain of an email address
// matches a list of domains
func IsEmailDomainListed(globs []glob.Glob, email string) bool {
if len(globs) == 0 {
return false
}

n := strings.LastIndex(email, "@")
if n <= 0 {
return false
}

domain := strings.ToLower(email[n+1:])

for _, g := range globs {
if g.Match(domain) {
return true
}
}

return false
}

// IsEmailDomainAllowed validates that the email address
// provided by the user matches what has been configured .
// The email is marked as allowed if it matches any of the
// domains in the whitelist or if it doesn't match any of
// domains in the blocklist, if any such list is not empty.
func (f *RegisterForm) IsEmailDomainAllowed() bool {
if len(setting.Service.EmailDomainAllowList) == 0 {
return !IsEmailDomainListed(setting.Service.EmailDomainBlockList, f.Email)
return !validation.IsEmailDomainListed(setting.Service.EmailDomainBlockList, f.Email)
}

return IsEmailDomainListed(setting.Service.EmailDomainAllowList, f.Email)
return validation.IsEmailDomainListed(setting.Service.EmailDomainAllowList, f.Email)
}

// MustChangePasswordForm form for updating your password after account creation
Expand Down