Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not allow setting permissions for self #802

Merged
merged 5 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: fix
module: x/subspaces
pull_request: 802
description: Made it not possible for users to set their own permissions
backward_compatible: true
date: 2022-04-01T09:30:07.876291335Z
5 changes: 5 additions & 0 deletions x/subspaces/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ func (k msgServer) SetUserGroupPermissions(goCtx context.Context, msg *types.Msg
return nil, sdkerrors.Wrapf(types.ErrPermissionDenied, "you cannot manage permissions in this subspace")
}

// Make sure that the user is not part of the group they want to change the permissions for
manu0466 marked this conversation as resolved.
Show resolved Hide resolved
if k.IsMemberOfGroup(ctx, msg.SubspaceID, msg.GroupID, signer) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "cannot set the permissions for a group you are part of")
}

// Set the group permissions and store the group
group.Permissions = msg.Permissions
k.SaveUserGroup(ctx, group)
Expand Down
40 changes: 37 additions & 3 deletions x/subspaces/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,40 @@ func (suite *KeeperTestsuite) TestMsgServer_SetUserGroupPermissions() {
),
shouldErr: true,
},
{
name: "setting the permissions for a group you are part of returns error",
store: func(ctx sdk.Context) {
suite.k.SaveSubspace(ctx, types.NewSubspace(
1,
"Test subspace",
"This is a test subspace",
"cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69",
"cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5",
"cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69",
time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC),
))
suite.k.SaveUserGroup(ctx, types.NewUserGroup(
1,
1,
"Test group",
"This is a test group",
types.PermissionSetPermissions,
))

sdkAddr, err := sdk.AccAddressFromBech32("cosmos1x5pjlvufs4znnhhkwe8v4tw3kz30f3lxgwza53")
suite.Require().NoError(err)

err = suite.k.AddUserToGroup(ctx, 1, 1, sdkAddr)
suite.Require().NoError(err)
},
msg: types.NewMsgSetUserGroupPermissions(
1,
1,
types.PermissionEverything,
"cosmos1x5pjlvufs4znnhhkwe8v4tw3kz30f3lxgwza53",
),
shouldErr: true,
},
{
name: "existing group is deleted properly",
store: func(ctx sdk.Context) {
Expand Down Expand Up @@ -1548,22 +1582,22 @@ func (suite *KeeperTestsuite) TestMsgServer_SetUserPermissions() {
time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC),
))

sdkAddr, err := sdk.AccAddressFromBech32("cosmos1x5pjlvufs4znnhhkwe8v4tw3kz30f3lxgwza53")
sdkAddr, err := sdk.AccAddressFromBech32("cosmos17ua98rre5j9ce7hfude0y5y3rh4gtqkygm8hru")
suite.Require().NoError(err)
suite.k.SetUserPermissions(ctx, 1, sdkAddr, types.PermissionSetPermissions)
},
msg: types.NewMsgSetUserPermissions(
1,
"cosmos1x5pjlvufs4znnhhkwe8v4tw3kz30f3lxgwza53",
types.PermissionWrite,
"cosmos1x5pjlvufs4znnhhkwe8v4tw3kz30f3lxgwza53",
"cosmos17ua98rre5j9ce7hfude0y5y3rh4gtqkygm8hru",
),
shouldErr: false,
expEvents: sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, "cosmos1x5pjlvufs4znnhhkwe8v4tw3kz30f3lxgwza53"),
sdk.NewAttribute(sdk.AttributeKeySender, "cosmos17ua98rre5j9ce7hfude0y5y3rh4gtqkygm8hru"),
),
sdk.NewEvent(
types.EventTypeSetUserPermissions,
Expand Down
4 changes: 4 additions & 0 deletions x/subspaces/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ func (msg MsgSetUserPermissions) ValidateBasic() error {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid signer address")
}

if msg.User == msg.Signer {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "cannot set the permissions for yourself")
}

return nil
}

Expand Down
12 changes: 11 additions & 1 deletion x/subspaces/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,12 +783,22 @@ func TestMsgSetUserPermissions_ValidateBasic(t *testing.T) {
name: "invalid signer returns error",
msg: types.NewMsgSetUserPermissions(
1,
"group",
"cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5",
types.PermissionWrite,
"cosmos1m0czrla04f7rp3zg7d",
),
shouldErr: true,
},
{
name: "same user and signer returns error",
msg: types.NewMsgSetUserPermissions(
1,
"cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5",
types.PermissionWrite,
"cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5",
),
shouldErr: true,
},
{
name: "valid message returns no error",
msg: msgSetUserPermissions,
Expand Down