Skip to content

Commit

Permalink
[MM-62687] Patch permission check to avoid modifying the system admin (
Browse files Browse the repository at this point in the history
…mattermost#30292)

* [MM-62687] Patch permission check to avoid modifying the system admin

* Check for manage system first

* PR feedback

* Add another test

* Lint

* Fix test
  • Loading branch information
devinbinnie authored Feb 26, 2025
1 parent a732962 commit 9f49403
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 8 additions & 0 deletions server/channels/api4/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4237,6 +4237,14 @@ func TestSetDefaultProfileImage(t *testing.T) {
_, err = th.SystemAdminClient.SetDefaultProfileImage(context.Background(), user.Id)
require.NoError(t, err)

// Check that a system admin can set the default profile image for another system admin
anotherAdmin := th.CreateUser()
_, appErr := th.App.UpdateUserRoles(th.Context, anotherAdmin.Id, model.SystemAdminRoleId+" "+model.SystemUserRoleId, false)
require.Nil(t, appErr)

_, err = th.SystemAdminClient.SetDefaultProfileImage(context.Background(), anotherAdmin.Id)
require.NoError(t, err)

ruser, appErr := th.App.GetUser(user.Id)
require.Nil(t, appErr)
assert.Less(t, ruser.LastPictureUpdate, iuser.LastPictureUpdate, "LastPictureUpdate should be updated to a lower negative number")
Expand Down
17 changes: 13 additions & 4 deletions server/channels/app/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,28 @@ func (a *App) SessionHasPermissionToUser(session model.Session, userID string) b
if userID == "" {
return false
}
if session.IsUnrestricted() {
if session.IsUnrestricted() || a.SessionHasPermissionTo(session, model.PermissionManageSystem) {
return true
}

if session.UserId == userID {
return true
}

if a.SessionHasPermissionTo(session, model.PermissionEditOtherUsers) {
return true
if !a.SessionHasPermissionTo(session, model.PermissionEditOtherUsers) {
return false
}

return false
user, err := a.GetUser(userID)
if err != nil {
return false
}

if user.IsSystemAdmin() {
return false
}

return true
}

func (a *App) SessionHasPermissionToUserOrBot(rctx request.CTX, session model.Session, userID string) bool {
Expand Down
1 change: 1 addition & 0 deletions server/channels/app/authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ func TestSessionHasPermissionToUser(t *testing.T) {

th.AddPermissionToRole(model.PermissionEditOtherUsers.Id, model.SystemUserManagerRoleId)
assert.True(t, th.App.SessionHasPermissionToUser(session, th.BasicUser2.Id))
assert.False(t, th.App.SessionHasPermissionToUser(session, th.SystemAdminUser.Id))
th.RemovePermissionFromRole(model.PermissionEditOtherUsers.Id, model.SystemUserManagerRoleId)

bot, err := th.App.CreateBot(th.Context, &model.Bot{
Expand Down

0 comments on commit 9f49403

Please sign in to comment.