diff options
Diffstat (limited to 'model')
-rw-r--r-- | model/config.go | 45 | ||||
-rw-r--r-- | model/config_test.go | 90 |
2 files changed, 128 insertions, 7 deletions
diff --git a/model/config.go b/model/config.go index ba3a02d33..074632a67 100644 --- a/model/config.go +++ b/model/config.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "net/url" + "regexp" "strings" "time" ) @@ -1772,10 +1773,16 @@ func (s *MessageExportSettings) SetDefaults() { } type DisplaySettings struct { + CustomUrlSchemes *[]string ExperimentalTimezone *bool } func (s *DisplaySettings) SetDefaults() { + if s.CustomUrlSchemes == nil { + customUrlSchemes := []string{} + s.CustomUrlSchemes = &customUrlSchemes + } + if s.ExperimentalTimezone == nil { s.ExperimentalTimezone = NewBool(false) } @@ -1900,10 +1907,18 @@ func (o *Config) SetDefaults() { } func (o *Config) IsValid() *AppError { + if len(*o.ServiceSettings.SiteURL) == 0 && *o.EmailSettings.EnableEmailBatching { + return NewAppError("Config.IsValid", "model.config.is_valid.site_url_email_batching.app_error", nil, "", http.StatusBadRequest) + } + if *o.ClusterSettings.Enable && *o.EmailSettings.EnableEmailBatching { return NewAppError("Config.IsValid", "model.config.is_valid.cluster_email_batching.app_error", nil, "", http.StatusBadRequest) } + if len(*o.ServiceSettings.SiteURL) == 0 && *o.ServiceSettings.AllowCookiesForSubdomains { + return NewAppError("Config.IsValid", "Allowing cookies for subdomains requires SiteURL to be set.", nil, "", http.StatusBadRequest) + } + if err := o.TeamSettings.isValid(); err != nil { return err } @@ -1960,6 +1975,10 @@ func (o *Config) IsValid() *AppError { return err } + if err := o.DisplaySettings.isValid(); err != nil { + return err + } + return nil } @@ -2209,6 +2228,12 @@ func (ss *ServiceSettings) isValid() *AppError { return NewAppError("Config.IsValid", "model.config.is_valid.login_attempts.app_error", nil, "", http.StatusBadRequest) } + if len(*ss.SiteURL) != 0 { + if _, err := url.ParseRequestURI(*ss.SiteURL); err != nil { + return NewAppError("Config.IsValid", "model.config.is_valid.site_url.app_error", nil, "", http.StatusBadRequest) + } + } + if len(*ss.WebsocketURL) != 0 { if _, err := url.ParseRequestURI(*ss.WebsocketURL); err != nil { return NewAppError("Config.IsValid", "model.config.is_valid.websocket_url.app_error", nil, "", http.StatusBadRequest) @@ -2334,6 +2359,26 @@ func (mes *MessageExportSettings) isValid(fs FileSettings) *AppError { return nil } +func (ds *DisplaySettings) isValid() *AppError { + if len(*ds.CustomUrlSchemes) != 0 { + validProtocolPattern := regexp.MustCompile(`(?i)^\s*[a-z][a-z0-9+.-]*\s*$`) + + for _, scheme := range *ds.CustomUrlSchemes { + if !validProtocolPattern.MatchString(scheme) { + return NewAppError( + "Config.IsValid", + "model.config.is_valid.display.custom_url_schemes.app_error", + map[string]interface{}{"Protocol": scheme}, + "", + http.StatusBadRequest, + ) + } + } + } + + return nil +} + func (o *Config) GetSanitizeOptions() map[string]bool { options := map[string]bool{} options["fullname"] = o.PrivacySettings.ShowFullName diff --git a/model/config_test.go b/model/config_test.go index e39ecef3b..5406d680d 100644 --- a/model/config_test.go +++ b/model/config_test.go @@ -82,13 +82,6 @@ func TestConfigDefaultFileSettingsS3SSE(t *testing.T) { } } -func TestConfigDefaultSiteURL(t *testing.T) { - c1 := Config{} - c1.SetDefaults() - - assert.Equal(t, "", *c1.ServiceSettings.SiteURL, "SiteURL should be empty by default.") -} - func TestConfigDefaultServiceSettingsExperimentalGroupUnreadChannels(t *testing.T) { c1 := Config{} c1.SetDefaults() @@ -443,3 +436,86 @@ func TestMessageExportSetDefaultsExportDisabledExportFromTimestampNonZero(t *tes require.Equal(t, int64(0), *mes.ExportFromTimestamp) require.Equal(t, 10000, *mes.BatchSize) } + +func TestDisplaySettingsIsValidCustomUrlSchemes(t *testing.T) { + tests := []struct { + name string + value []string + valid bool + }{ + { + name: "empty", + value: []string{}, + valid: true, + }, + { + name: "custom protocol", + value: []string{"steam"}, + valid: true, + }, + { + name: "multiple custom protocols", + value: []string{"bitcoin", "rss", "redis"}, + valid: true, + }, + { + name: "containing numbers", + value: []string{"ut2004", "ts3server", "h323"}, + valid: true, + }, + { + name: "containing period", + value: []string{"iris.beep"}, + valid: true, + }, + { + name: "containing hyphen", + value: []string{"ms-excel"}, + valid: true, + }, + { + name: "containing plus", + value: []string{"coap+tcp", "coap+ws"}, + valid: true, + }, + { + name: "starting with number", + value: []string{"4four"}, + valid: false, + }, + { + name: "starting with period", + value: []string{"data", ".dot"}, + valid: false, + }, + { + name: "starting with hyphen", + value: []string{"-hyphen", "dns"}, + valid: false, + }, + { + name: "invalid symbols", + value: []string{"!!fun!!"}, + valid: false, + }, + { + name: "invalid letters", + value: []string{"école"}, + valid: false, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + ds := &DisplaySettings{} + ds.SetDefaults() + + ds.CustomUrlSchemes = &test.value + + if err := ds.isValid(); err != nil && test.valid { + t.Error("Expected CustomUrlSchemes to be valid but got error:", err) + } else if err == nil && !test.valid { + t.Error("Expected CustomUrlSchemes to be invalid but got no error") + } + }) + } +} |