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 4 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
8 changes: 7 additions & 1 deletion x/subspaces/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ func (k msgServer) SetUserGroupPermissions(goCtx context.Context, msg *types.Msg
ctx := sdk.UnwrapSDKContext(goCtx)

// Check if the subspace exists
if !k.HasSubspace(ctx, msg.SubspaceID) {
subspace, found := k.GetSubspace(ctx, msg.SubspaceID)
if !found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "subspace with id %d not found", msg.SubspaceID)
}

Expand All @@ -281,6 +282,11 @@ func (k msgServer) SetUserGroupPermissions(goCtx context.Context, msg *types.Msg
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid permission value")
}

// Make sure that the user is not part of the group they want to change the permissions for, unless they are the owner
if subspace.Owner != msg.Signer && 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
92 changes: 89 additions & 3 deletions x/subspaces/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,92 @@ func (suite *KeeperTestsuite) TestMsgServer_SetUserGroupPermissions() {
),
shouldErr: true,
},
{
name: "setting the permissions for a group you are part of returns error if not owner",
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: "setting the permissions for a group you are part of does not return error if owner",
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,
"cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5",
),
shouldErr: false,
expEvents: sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5"),
),
sdk.NewEvent(
types.EventTypeSetUserGroupPermissions,
sdk.NewAttribute(types.AttributeKeySubspaceID, "1"),
sdk.NewAttribute(types.AttributeKeyUserGroupID, "1"),
),
},
check: func(ctx sdk.Context) {
group, found := suite.k.GetUserGroup(ctx, 1, 1)
suite.Require().True(found)

suite.Require().Equal(types.PermissionEverything, group.Permissions)
},
},
{
name: "group permissions are updated correctly",
store: func(ctx sdk.Context) {
Expand Down Expand Up @@ -1605,22 +1691,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
7 changes: 7 additions & 0 deletions x/subspaces/simulation/operations_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ func randomSetUserGroupPermissionsFields(
}
account = *acc

// Make sure the user can change this group's permissions
if subspace.Owner != account.Address.String() && k.IsMemberOfGroup(ctx, subspaceID, groupID, account.Address) {
// If the user is not the subspace owner and it's part of the user group they cannot edit the group permissions
skip = true
return
}

return subspaceID, groupID, permissions, account, false
}

Expand Down
6 changes: 6 additions & 0 deletions x/subspaces/simulation/operations_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,11 @@ func randomSetUserPermissionsFields(
}
account = *acc

// Make sure the signer and the user are not the same
if acc.Address.String() == target {
skip = true
return
}

return subspaceID, target, permissions, account, false
}
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