[MM-62502] Ensure MFA Check on search routes (#29880)

Этот коммит содержится в:
Julien Tant
2025-01-27 11:03:16 -07:00
коммит произвёл GitHub
родитель dbf52d670d
Коммит 8b5a3e6217
2 изменённых файлов: 63 добавлений и 5 удалений

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

@@ -183,7 +183,7 @@ func (api *API) APISessionRequiredDisableWhenBusy(h handlerFunc, opts ...APIHand
HandlerName: web.GetHandlerName(h), HandlerName: web.GetHandlerName(h),
RequireSession: true, RequireSession: true,
TrustRequester: false, TrustRequester: false,
RequireMfa: false, RequireMfa: true,
IsStatic: false, IsStatic: false,
IsLocal: false, IsLocal: false,
DisableWhenBusy: true, DisableWhenBusy: true,

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

@@ -2410,7 +2410,8 @@ func TestUserUnicodeNames(t *testing.T) {
Nickname: "Ender\u2028 Wiggin", Nickname: "Ender\u2028 Wiggin",
Password: "hello1", Password: "hello1",
Username: "\ufeffwiggin77", Username: "\ufeffwiggin77",
Roles: model.SystemAdminRoleId + " " + model.SystemUserRoleId} Roles: model.SystemAdminRoleId + " " + model.SystemUserRoleId,
}
ruser, resp, err := client.CreateUser(context.Background(), &user) ruser, resp, err := client.CreateUser(context.Background(), &user)
require.NoError(t, err) require.NoError(t, err)
@@ -6695,6 +6696,7 @@ func TestMigrateAuthToSAML(t *testing.T) {
CheckNotImplementedStatus(t, resp) CheckNotImplementedStatus(t, resp)
}) })
} }
func TestUpdatePassword(t *testing.T) { func TestUpdatePassword(t *testing.T) {
th := Setup(t) th := Setup(t)
defer th.TearDown() defer th.TearDown()
@@ -7407,7 +7409,8 @@ func TestThreadSocketEvents(t *testing.T) {
preMentions: 2, preMentions: 2,
replies: 0, replies: 0,
mentions: 0, mentions: 0,
}, { },
{
post: &model.Post{ChannelId: th.BasicChannel.Id, Message: "simple reply", UserId: th.BasicUser2.Id, RootId: rpost.Id}, post: &model.Post{ChannelId: th.BasicChannel.Id, Message: "simple reply", UserId: th.BasicUser2.Id, RootId: rpost.Id},
preReplies: 0, preReplies: 0,
preMentions: 0, preMentions: 0,
@@ -8270,6 +8273,7 @@ func TestGetUsersWithInvalidEmails(t *testing.T) {
require.Error(t, err) require.Error(t, err)
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
} }
func TestUserUpdateEvents(t *testing.T) { func TestUserUpdateEvents(t *testing.T) {
th := Setup(t).InitBasic() th := Setup(t).InitBasic()
defer th.TearDown() defer th.TearDown()
@@ -8977,3 +8981,57 @@ func TestRevokeAllSessionsForUser(t *testing.T) {
CheckForbiddenStatus(t, resp) CheckForbiddenStatus(t, resp)
}) })
} }
func TestSearchUsersWithMfaEnforced(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
th.App.Srv().SetLicense(model.NewTestLicense("mfa"))
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.EnableMultifactorAuthentication = true
*cfg.ServiceSettings.EnforceMultifactorAuthentication = true
})
t.Run("user with MFA active can search users", func(t *testing.T) {
userWithMFAOK := th.BasicUser
secret, appErr := th.App.GenerateMfaSecret(userWithMFAOK.Id)
assert.Nil(t, appErr)
// Fake user has MFA enabled
err := th.Server.Store().User().UpdateMfaActive(userWithMFAOK.Id, true)
require.NoError(t, err)
err = th.Server.Store().User().UpdateMfaSecret(userWithMFAOK.Id, secret.Secret)
require.NoError(t, err)
code := dgoogauth.ComputeCode(secret.Secret, time.Now().UTC().Unix()/30)
client := th.CreateClient()
user, _, err := client.LoginWithMFA(context.Background(), th.BasicUser.Email, th.BasicUser.Password, fmt.Sprintf("%06d", code))
require.NoError(t, err)
assert.NotNil(t, user)
_, _, err = client.SearchUsers(context.Background(), &model.UserSearch{
Term: "user",
})
require.NoError(t, err)
})
t.Run("user with MFA not active can't search users", func(t *testing.T) {
userWithMFANotOk := th.BasicUser2
err := th.Server.Store().User().UpdateMfaActive(userWithMFANotOk.Id, false)
require.NoError(t, err)
client := th.CreateClient()
_, _, err = client.Login(context.Background(), userWithMFANotOk.Email, userWithMFANotOk.Password)
require.NoError(t, err)
_, resp, err := client.SearchUsers(context.Background(), &model.UserSearch{
Term: "user",
})
CheckErrorID(t, err, "api.context.mfa_required.app_error")
CheckForbiddenStatus(t, resp)
})
}