[MM-15490] Rework default password requirements (#10844)

* Rework default password requirements

* Update API Test Lib Default User PW

* Remove unused function; Disable password reqs in dev mode

* Disable strict password requirements for unit tests

* Update unit tests
Этот коммит содержится в:
Daniel Schalla
2019-05-21 20:03:36 +02:00
коммит произвёл Christopher Speller
родитель 0b6acaa9ba
Коммит d269891476
9 изменённых файлов: 89 добавлений и 57 удалений

Просмотреть файл

@@ -108,6 +108,15 @@ func setupTestHelper(enterprise bool, updateConfig func(*model.Config)) *TestHel
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = true }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = true })
// Disable strict password requirements for test
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.PasswordSettings.MinimumLength = 5
*cfg.PasswordSettings.Lowercase = false
*cfg.PasswordSettings.Uppercase = false
*cfg.PasswordSettings.Symbol = false
*cfg.PasswordSettings.Number = false
})
if enterprise { if enterprise {
th.App.SetLicense(model.NewTestLicense()) th.App.SetLicense(model.NewTestLicense())
} else { } else {
@@ -274,7 +283,7 @@ func (me *TestHelper) CreateUserWithClient(client *model.Client4) *model.User {
Nickname: "nn_" + id, Nickname: "nn_" + id,
FirstName: "f_" + id, FirstName: "f_" + id,
LastName: "l_" + id, LastName: "l_" + id,
Password: "Password1", Password: "Pa$$word11",
} }
utils.DisableDebugLogForTest() utils.DisableDebugLogForTest()
@@ -283,7 +292,7 @@ func (me *TestHelper) CreateUserWithClient(client *model.Client4) *model.User {
panic(response.Error) panic(response.Error)
} }
ruser.Password = "Password1" ruser.Password = "Pa$$word11"
store.Must(me.App.Srv.Store.User().VerifyEmail(ruser.Id, ruser.Email)) store.Must(me.App.Srv.Store.User().VerifyEmail(ruser.Id, ruser.Email))
utils.EnableDebugLogForTest() utils.EnableDebugLogForTest()
return ruser return ruser

Просмотреть файл

@@ -37,6 +37,11 @@ func (tl TokenLocation) String() string {
} }
func (a *App) IsPasswordValid(password string) *model.AppError { func (a *App) IsPasswordValid(password string) *model.AppError {
if *a.Config().ServiceSettings.EnableDeveloper {
return nil
}
return utils.IsPasswordValidWithSettings(password, &a.Config().PasswordSettings) return utils.IsPasswordValidWithSettings(password, &a.Config().PasswordSettings)
} }

Просмотреть файл

@@ -71,6 +71,15 @@ func setupTestHelper(enterprise bool, tb testing.TB) *TestHelper {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = true }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = true })
// Disable strict password requirements for test
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.PasswordSettings.MinimumLength = 5
*cfg.PasswordSettings.Lowercase = false
*cfg.PasswordSettings.Uppercase = false
*cfg.PasswordSettings.Symbol = false
*cfg.PasswordSettings.Number = false
})
if enterprise { if enterprise {
th.App.SetLicense(model.NewTestLicense()) th.App.SetLicense(model.NewTestLicense())
} else { } else {

Просмотреть файл

@@ -81,6 +81,14 @@ func (h *testHelper) ConfigPath() string {
// SetConfig replaces the configuration passed to a running command. // SetConfig replaces the configuration passed to a running command.
func (h *testHelper) SetConfig(config *model.Config) { func (h *testHelper) SetConfig(config *model.Config) {
config.SqlSettings = *mainHelper.GetSqlSettings() config.SqlSettings = *mainHelper.GetSqlSettings()
// Disable strict password requirements for test
*config.PasswordSettings.MinimumLength = 5
*config.PasswordSettings.Lowercase = false
*config.PasswordSettings.Uppercase = false
*config.PasswordSettings.Symbol = false
*config.PasswordSettings.Number = false
h.config = config h.config = config
if err := ioutil.WriteFile(h.configFilePath, []byte(config.ToJson()), 0600); err != nil { if err := ioutil.WriteFile(h.configFilePath, []byte(config.ToJson()), 0600); err != nil {

Просмотреть файл

@@ -159,11 +159,11 @@
"FileLocation": "" "FileLocation": ""
}, },
"PasswordSettings": { "PasswordSettings": {
"MinimumLength": 5, "MinimumLength": 10,
"Lowercase": false, "Lowercase": true,
"Number": false, "Number": true,
"Uppercase": false, "Uppercase": true,
"Symbol": false "Symbol": true
}, },
"FileSettings": { "FileSettings": {
"EnableFileAttachments": true, "EnableFileAttachments": true,

Просмотреть файл

@@ -962,23 +962,23 @@ type PasswordSettings struct {
func (s *PasswordSettings) SetDefaults() { func (s *PasswordSettings) SetDefaults() {
if s.MinimumLength == nil { if s.MinimumLength == nil {
s.MinimumLength = NewInt(PASSWORD_MINIMUM_LENGTH) s.MinimumLength = NewInt(10)
} }
if s.Lowercase == nil { if s.Lowercase == nil {
s.Lowercase = NewBool(false) s.Lowercase = NewBool(true)
} }
if s.Number == nil { if s.Number == nil {
s.Number = NewBool(false) s.Number = NewBool(true)
} }
if s.Uppercase == nil { if s.Uppercase == nil {
s.Uppercase = NewBool(false) s.Uppercase = NewBool(true)
} }
if s.Symbol == nil { if s.Symbol == nil {
s.Symbol = NewBool(false) s.Symbol = NewBool(true)
} }
} }

Просмотреть файл

@@ -10,14 +10,6 @@ import (
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
) )
func IsPasswordValid(password string) *model.AppError {
if len(password) > model.PASSWORD_MAXIMUM_LENGTH || len(password) < model.PASSWORD_MINIMUM_LENGTH {
return model.NewAppError("User.IsValid", "model.user.is_valid.pwd.app_error", map[string]interface{}{"Min": model.PASSWORD_MINIMUM_LENGTH}, "", http.StatusBadRequest)
}
return nil
}
func IsPasswordValidWithSettings(password string, settings *model.PasswordSettings) *model.AppError { func IsPasswordValidWithSettings(password string, settings *model.PasswordSettings) *model.AppError {
id := "model.user.is_valid.pwd" id := "model.user.is_valid.pwd"
isError := false isError := false

Просмотреть файл

@@ -9,36 +9,6 @@ import (
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
) )
func TestIsPasswordValid(t *testing.T) {
for name, tc := range map[string]struct {
Password string
ExpectedError string
}{
"Short": {
Password: strings.Repeat("x", model.PASSWORD_MINIMUM_LENGTH),
},
"Long": {
Password: strings.Repeat("x", model.PASSWORD_MAXIMUM_LENGTH),
},
"TooShort": {
Password: strings.Repeat("x", model.PASSWORD_MINIMUM_LENGTH-1),
ExpectedError: "model.user.is_valid.pwd.app_error",
},
"TooLong": {
Password: strings.Repeat("x", model.PASSWORD_MAXIMUM_LENGTH+1),
ExpectedError: "model.user.is_valid.pwd.app_error",
},
} {
t.Run(name, func(t *testing.T) {
if err := IsPasswordValid(tc.Password); tc.ExpectedError == "" {
assert.Nil(t, err)
} else {
assert.Equal(t, tc.ExpectedError, err.Id)
}
})
}
}
func TestIsPasswordValidWithSettings(t *testing.T) { func TestIsPasswordValidWithSettings(t *testing.T) {
for name, tc := range map[string]struct { for name, tc := range map[string]struct {
Password string Password string
@@ -49,54 +19,84 @@ func TestIsPasswordValidWithSettings(t *testing.T) {
Password: strings.Repeat("x", 3), Password: strings.Repeat("x", 3),
Settings: &model.PasswordSettings{ Settings: &model.PasswordSettings{
MinimumLength: model.NewInt(3), MinimumLength: model.NewInt(3),
Lowercase: model.NewBool(false),
Uppercase: model.NewBool(false),
Number: model.NewBool(false),
Symbol: model.NewBool(false),
}, },
}, },
"Long": { "Long": {
Password: strings.Repeat("x", model.PASSWORD_MAXIMUM_LENGTH), Password: strings.Repeat("x", model.PASSWORD_MAXIMUM_LENGTH),
Settings: &model.PasswordSettings{}, Settings: &model.PasswordSettings{
Lowercase: model.NewBool(false),
Uppercase: model.NewBool(false),
Number: model.NewBool(false),
Symbol: model.NewBool(false),
},
}, },
"TooShort": { "TooShort": {
Password: strings.Repeat("x", 2), Password: strings.Repeat("x", 2),
Settings: &model.PasswordSettings{ Settings: &model.PasswordSettings{
MinimumLength: model.NewInt(3), MinimumLength: model.NewInt(3),
Lowercase: model.NewBool(false),
Uppercase: model.NewBool(false),
Number: model.NewBool(false),
Symbol: model.NewBool(false),
}, },
ExpectedError: "model.user.is_valid.pwd.app_error", ExpectedError: "model.user.is_valid.pwd.app_error",
}, },
"TooLong": { "TooLong": {
Password: strings.Repeat("x", model.PASSWORD_MAXIMUM_LENGTH+1), Password: strings.Repeat("x", model.PASSWORD_MAXIMUM_LENGTH+1),
Settings: &model.PasswordSettings{}, Settings: &model.PasswordSettings{
Lowercase: model.NewBool(false),
Uppercase: model.NewBool(false),
Number: model.NewBool(false),
Symbol: model.NewBool(false),
},
ExpectedError: "model.user.is_valid.pwd.app_error", ExpectedError: "model.user.is_valid.pwd.app_error",
}, },
"MissingLower": { "MissingLower": {
Password: "ASD123!@#", Password: "AAAAAAAAAAASD123!@#",
Settings: &model.PasswordSettings{ Settings: &model.PasswordSettings{
Lowercase: model.NewBool(true), Lowercase: model.NewBool(true),
Uppercase: model.NewBool(false),
Number: model.NewBool(false),
Symbol: model.NewBool(false),
}, },
ExpectedError: "model.user.is_valid.pwd_lowercase.app_error", ExpectedError: "model.user.is_valid.pwd_lowercase.app_error",
}, },
"MissingUpper": { "MissingUpper": {
Password: "asd123!@#", Password: "aaaaaaaaaaaaasd123!@#",
Settings: &model.PasswordSettings{ Settings: &model.PasswordSettings{
Uppercase: model.NewBool(true), Uppercase: model.NewBool(true),
Lowercase: model.NewBool(false),
Number: model.NewBool(false),
Symbol: model.NewBool(false),
}, },
ExpectedError: "model.user.is_valid.pwd_uppercase.app_error", ExpectedError: "model.user.is_valid.pwd_uppercase.app_error",
}, },
"MissingNumber": { "MissingNumber": {
Password: "asdASD!@#", Password: "asasdasdsadASD!@#",
Settings: &model.PasswordSettings{ Settings: &model.PasswordSettings{
Number: model.NewBool(true), Number: model.NewBool(true),
Lowercase: model.NewBool(false),
Uppercase: model.NewBool(false),
Symbol: model.NewBool(false),
}, },
ExpectedError: "model.user.is_valid.pwd_number.app_error", ExpectedError: "model.user.is_valid.pwd_number.app_error",
}, },
"MissingSymbol": { "MissingSymbol": {
Password: "asdASD123", Password: "asdasdasdasdasdASD123",
Settings: &model.PasswordSettings{ Settings: &model.PasswordSettings{
Symbol: model.NewBool(true), Symbol: model.NewBool(true),
Lowercase: model.NewBool(false),
Uppercase: model.NewBool(false),
Number: model.NewBool(false),
}, },
ExpectedError: "model.user.is_valid.pwd_symbol.app_error", ExpectedError: "model.user.is_valid.pwd_symbol.app_error",
}, },
"MissingMultiple": { "MissingMultiple": {
Password: "asd", Password: "asdasdasdasdasdasd",
Settings: &model.PasswordSettings{ Settings: &model.PasswordSettings{
Lowercase: model.NewBool(true), Lowercase: model.NewBool(true),
Uppercase: model.NewBool(true), Uppercase: model.NewBool(true),

Просмотреть файл

@@ -66,6 +66,15 @@ func Setup() *TestHelper {
} }
a.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = prevListenAddress }) a.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = prevListenAddress })
// Disable strict password requirements for test
a.UpdateConfig(func(cfg *model.Config) {
*cfg.PasswordSettings.MinimumLength = 5
*cfg.PasswordSettings.Lowercase = false
*cfg.PasswordSettings.Uppercase = false
*cfg.PasswordSettings.Symbol = false
*cfg.PasswordSettings.Number = false
})
web := New(s, s.AppOptions, s.Router) web := New(s, s.AppOptions, s.Router)
URL = fmt.Sprintf("http://localhost:%v", a.Srv.ListenAddr.Port) URL = fmt.Sprintf("http://localhost:%v", a.Srv.ListenAddr.Port)
ApiClient = model.NewAPIv4Client(URL) ApiClient = model.NewAPIv4Client(URL)