MM-53098 Fix for checking bot and user permissions on shared endpoints (#23751)
* temp commit * update test to allow bot creation * add bot check to updateUser and deleteUser * add more unit tests * lint fixes * lint fix * update based on doc * add more unit tests * lint fixes * fix unit tests * fix unit tests --------- Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
2abcdfe76a
Коммит
30140c0a27
@@ -106,6 +106,7 @@ func TestCreateBot(t *testing.T) {
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = true })
|
||||
th.AddPermissionToRole(model.PermissionCreateBot.Id, model.TeamUserRoleId)
|
||||
th.AddPermissionToRole(model.PermissionManageBots.Id, model.TeamUserRoleId)
|
||||
th.AddPermissionToRole(model.PermissionEditOtherUsers.Id, model.TeamUserRoleId)
|
||||
th.App.UpdateUserRoles(th.Context, th.BasicUser.Id, model.TeamUserRoleId+" "+model.SystemUserAccessTokenRoleId, false)
|
||||
|
||||
|
||||
@@ -428,7 +428,7 @@ func setProfileImage(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if !c.App.SessionHasPermissionToUser(*c.AppContext.Session(), c.Params.UserId) {
|
||||
if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), c.Params.UserId) {
|
||||
c.SetPermissionError(model.PermissionEditOtherUsers)
|
||||
return
|
||||
}
|
||||
@@ -499,7 +499,7 @@ func setDefaultProfileImage(c *Context, w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
if !c.App.SessionHasPermissionToUser(*c.AppContext.Session(), c.Params.UserId) {
|
||||
if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), c.Params.UserId) {
|
||||
c.SetPermissionError(model.PermissionEditOtherUsers)
|
||||
return
|
||||
}
|
||||
@@ -1249,7 +1249,7 @@ func updateUser(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if !c.App.SessionHasPermissionToUser(*c.AppContext.Session(), user.Id) {
|
||||
if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), user.Id) {
|
||||
c.SetPermissionError(model.PermissionEditOtherUsers)
|
||||
return
|
||||
}
|
||||
@@ -1259,6 +1259,7 @@ func updateUser(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
// Cannot update a system admin unless user making request is a systemadmin also.
|
||||
if ouser.IsSystemAdmin() && !c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PermissionManageSystem) {
|
||||
c.SetPermissionError(model.PermissionManageSystem)
|
||||
@@ -1325,7 +1326,7 @@ func patchUser(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
audit.AddEventParameterAuditable(auditRec, "user_patch", &patch)
|
||||
defer c.LogAuditRec(auditRec)
|
||||
|
||||
if !c.App.SessionHasPermissionToUser(*c.AppContext.Session(), c.Params.UserId) {
|
||||
if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), c.Params.UserId) {
|
||||
c.SetPermissionError(model.PermissionEditOtherUsers)
|
||||
return
|
||||
}
|
||||
@@ -1335,6 +1336,7 @@ func patchUser(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.SetInvalidParam("user_id")
|
||||
return
|
||||
}
|
||||
|
||||
auditRec.AddEventPriorState(ouser)
|
||||
auditRec.AddEventObjectType("user")
|
||||
|
||||
@@ -1402,7 +1404,7 @@ func deleteUser(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
audit.AddEventParameter(auditRec, "user_id", c.Params.UserId)
|
||||
defer c.LogAuditRec(auditRec)
|
||||
|
||||
if !c.App.SessionHasPermissionToUser(*c.AppContext.Session(), userId) {
|
||||
if !c.App.SessionHasPermissionToUserOrBot(*c.AppContext.Session(), userId) {
|
||||
c.SetPermissionError(model.PermissionEditOtherUsers)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1932,6 +1932,27 @@ func TestUpdateAdminUser(t *testing.T) {
|
||||
require.Equal(t, user.Email, u2.Email)
|
||||
}
|
||||
|
||||
func TestUpdateBotUser(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
th.App.UpdateConfig(func(c *model.Config) {
|
||||
*c.ServiceSettings.EnableBotAccountCreation = true
|
||||
})
|
||||
|
||||
bot := th.CreateBotWithSystemAdminClient()
|
||||
botUser, _, err := th.SystemAdminClient.GetUser(context.Background(), bot.UserId, "")
|
||||
require.NoError(t, err)
|
||||
|
||||
updateUser, _, err := th.SystemAdminClient.UpdateUser(context.Background(), botUser)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, botUser.Id, updateUser.Id)
|
||||
|
||||
_, resp, err := th.Client.UpdateUser(context.Background(), botUser)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
}
|
||||
|
||||
func TestPatchUser(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
@@ -2043,6 +2064,27 @@ func TestPatchUser(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestPatchBotUser(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
th.App.UpdateConfig(func(c *model.Config) {
|
||||
*c.ServiceSettings.EnableBotAccountCreation = true
|
||||
})
|
||||
|
||||
bot := th.CreateBotWithSystemAdminClient()
|
||||
patch := &model.UserPatch{}
|
||||
patch.Email = model.NewString("newemail@test.com")
|
||||
|
||||
user, _, err := th.SystemAdminClient.PatchUser(context.Background(), bot.UserId, patch)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, bot.UserId, user.Id)
|
||||
|
||||
_, resp, err := th.Client.PatchUser(context.Background(), bot.UserId, patch)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
}
|
||||
|
||||
func TestPatchAdminUser(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
@@ -2063,6 +2105,7 @@ func TestPatchAdminUser(t *testing.T) {
|
||||
_, _, err = th.SystemAdminClient.PatchUser(context.Background(), user.Id, patch)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestUserUnicodeNames(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
@@ -2229,6 +2272,21 @@ func TestDeleteUser(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestDeleteBotUser(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
th.App.UpdateConfig(func(c *model.Config) {
|
||||
*c.ServiceSettings.EnableBotAccountCreation = true
|
||||
})
|
||||
|
||||
bot := th.CreateBotWithSystemAdminClient()
|
||||
|
||||
_, err := th.Client.DeleteUser(context.Background(), bot.UserId)
|
||||
require.Error(t, err)
|
||||
require.Equal(t, err.Error(), ": You do not have the appropriate permissions.")
|
||||
}
|
||||
|
||||
func TestPermanentDeleteUser(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
Ссылка в новой задаче
Block a user