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

Support wildcards in email domain whitelisting #20257

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion services/forms/user_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/http"
"strings"

"github.com/gobwas/glob"

"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
Expand Down Expand Up @@ -118,7 +120,8 @@ func IsEmailDomainListed(list []string, email string) bool {
domain := strings.ToLower(email[n+1:])

for _, v := range list {
if strings.ToLower(v) == domain {
g := glob.MustCompile(strings.ToLower(v))
KingBain marked this conversation as resolved.
Show resolved Hide resolved
if g.Match(domain) {
return true
}
}
Expand Down
6 changes: 4 additions & 2 deletions services/forms/user_form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestRegisterForm_IsDomainAllowed_InvalidEmail(t *testing.T) {
func TestRegisterForm_IsDomainAllowed_WhitelistedEmail(t *testing.T) {
_ = setting.Service

setting.Service.EmailDomainWhitelist = []string{"gitea.io"}
setting.Service.EmailDomainWhitelist = []string{"gitea.io", "*.gov"}

tt := []struct {
email string
Expand All @@ -54,6 +54,7 @@ func TestRegisterForm_IsDomainAllowed_WhitelistedEmail(t *testing.T) {
{"security@gITea.io", true},
{"hdudhdd", false},
{"seee@example.com", false},
{"security@fishsauce.gov", true},
}

for _, v := range tt {
Expand All @@ -67,7 +68,7 @@ func TestRegisterForm_IsDomainAllowed_BlocklistedEmail(t *testing.T) {
_ = setting.Service

setting.Service.EmailDomainWhitelist = []string{}
setting.Service.EmailDomainBlocklist = []string{"gitea.io"}
setting.Service.EmailDomainBlocklist = []string{"gitea.io", "*.gov"}
KingBain marked this conversation as resolved.
Show resolved Hide resolved

tt := []struct {
email string
Expand All @@ -76,6 +77,7 @@ func TestRegisterForm_IsDomainAllowed_BlocklistedEmail(t *testing.T) {
{"security@gitea.io", false},
{"security@gitea.example", true},
{"hdudhdd", true},
{"security@fishsauce.gov", false},
}

for _, v := range tt {
Expand Down