Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: update domain validator (#12420)
Danny Xu06dca2fe274d
diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.goindex e036bc7ea0..10cfb76e9a 100644--- a/backend/api/v1/setting_service.go+++ b/backend/api/v1/setting_service.go@@ -10,7 +10,6 @@ import ("strings""time"- "github.com/asaskevich/govalidator""github.com/google/go-cmp/cmp""github.com/pkg/errors""google.golang.org/grpc/codes"@@ -209,17 +208,11 @@ func (s *SettingService) UpdateSetting(ctx context.Context, request *v1pb.Updatepayload.MaximumRoleExpiration = nil}}- if payload.Domains != nil && len(payload.Domains) > 0 {- for _, domain := range payload.Domains {- if !govalidator.IsDNSName(domain) {- return nil, status.Errorf(codes.InvalidArgument, "invalid domain: %s", domain)- }- }+ if len(payload.Domains) == 0 && payload.EnforceIdentityDomain {+ return nil, status.Errorf(codes.InvalidArgument, "identity domain can be enforced only when workspace domains are set")}- if payload.EnforceIdentityDomain {- if payload.Domains == nil || len(payload.Domains) == 0 {- return nil, status.Errorf(codes.InvalidArgument, "enforce identity domain is enabled but no domain is set")- }+ if err := validateDomains(payload.Domains); err != nil {+ return nil, status.Errorf(codes.InvalidArgument, "invalid domains, error %v", err)}bytes, err := protojson.Marshal(payload)if err != nil {@@ -1254,3 +1247,53 @@ func checkSubstitution(substitution string) error {}return nil}++var domainRegexp = regexp.MustCompile(`^(?i:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}$`)+var disallowedDomains = map[string]bool{+ "gmail.com": true,+ "googlemail.com": true,+ "outlook.com": true,+ "hotmail.com": true,+ "live.com": true,+ "msn.com": true,+ "yahoo.com": true,+ "ymail.com": true,+ "rocketmail.com": true,+ "icloud.com": true,+ "me.com": true,+ "mac.com": true,+ "aol.com": true,+ "zoho.com": true,+ "protonmail.com": true,+ "gmx.com": true,+ "gmx.net": true,+ "mail.com": true,+ "yandex.com": true,+ "yandex.ru": true,+ "fastmail.com": true,+ "fastmail.fm": true,+ "tutanota.com": true,+ "163.com": true,+ "126.com": true,+ "sohu.com": true,+ "qq.com": true,+ "sina.com": true,+ "sina.cn": true,+ "aliyun.com": true,+ "aliyun.cn": true,+ "tom.com": true,+ "21cn.com": true,+ "yeah.net": true,+}++func validateDomains(domains []string) error {+ for _, domain := range domains {+ if !domainRegexp.MatchString(domain) {+ return errors.Errorf("invalid domain %q", domain)+ }+ if disallowedDomains[domain] {+ return errors.Errorf("domain %q is not allowed", domain)+ }+ }+ return nil+}diff --git a/backend/api/v1/setting_service_test.go b/backend/api/v1/setting_service_test.gonew file mode 100644index 0000000000..3a6903360d--- /dev/null+++ b/backend/api/v1/setting_service_test.go@@ -0,0 +1,54 @@+package v1++import (+ "testing"++ "github.com/stretchr/testify/require"+)++func TestValidateDomains(t *testing.T) {+ a := require.New(t)++ testCases := []struct {+ domain string+ wantErr bool+ }{+ {+ domain: "",+ wantErr: true,+ },+ {+ domain: "[email protected]",+ wantErr: true,+ },+ {+ domain: "BYTEBASE.COM",+ wantErr: true,+ },+ {+ domain: "bytebase.com",+ wantErr: false,+ },+ {+ domain: "x.y",+ wantErr: true,+ },+ {+ domain: "abc.xyz",+ wantErr: false,+ },+ {+ domain: "gmail.com",+ wantErr: true,+ },+ }++ for _, tc := range testCases {+ err := validateDomains([]string{tc.domain})+ if tc.wantErr {+ a.Error(err, tc.domain)+ } else {+ a.NoError(err, tc.domain)+ }+ }+}diff --git a/go.mod b/go.modindex 2cd55e9fe2..d31ba92965 100644--- a/go.mod+++ b/go.mod@@ -11,7 +11,6 @@ require (github.com/ClickHouse/clickhouse-go/v2 v2.25.0github.com/alexmullins/zip v0.0.0-20180717182244-4affb64b04d0github.com/antlr4-go/antlr/v4 v4.13.1- github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2github.com/aws/aws-sdk-go-v2 v1.27.0github.com/aws/aws-sdk-go-v2/config v1.27.16github.com/aws/aws-sdk-go-v2/credentials v1.17.16diff --git a/go.sum b/go.sumindex d6bdb37e7a..287b594ef7 100644--- a/go.sum+++ b/go.sum@@ -702,8 +702,6 @@ github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A=-github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=-github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU=github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=