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 Users and Account::PermissionsController#update #2598

Merged
merged 6 commits into from
Dec 14, 2023

Conversation

chrisroos
Copy link
Contributor

@chrisroos chrisroos commented Dec 14, 2023

Trello: https://trello.com/c/09yMSdcU

We noticed some problems with the implementation of both Users::PermissionsController#update and Account::PermissionsController#update:

  1. The signin permission would be removed unless explicitly added in the call to update
  2. Permissions not grantable from the UI would be removed unless explicitly added in the call to update
  3. Permissions not grantable from the UI could be added if explicitly added in the call to update
  4. Permissions could be passed to UserUpdate for applications other than the application whose permissions were being viewed

See the individual commits for more detail.

We're fixing these problems before moving the API User edit page to use similar functionality.

chrisroos and others added 2 commits December 14, 2023 11:00
This test is satisfied by the behaviour in the `UserUpdate` service.
We're adding it to give us confidence to make changes in this area in
future.

Co-authored-by: Chris Lowis <chris.lowis@gofreerange.com>
We noticed that it was possible to construct a request to the
`Users::PermissionsController#update` action that resulted in the user's
signin permission being removed from the application. This won't happen
through normal use of the user interface but we should guard against it
nonetheless.

The same problem exists in `Account::PermissionsController#update` and
we intend to fix that in a separate commit.

Co-authored-by: Chris Lowis <chris.lowis@gofreerange.com>
@chrisroos chrisroos force-pushed the fix-permissions-controllers branch 2 times, most recently from 08af8f3 to b0d03d2 Compare December 14, 2023 12:53
@chrisroos chrisroos changed the title Fix permissions controllers Fix Users and Account::PermissionsController#update Dec 14, 2023
@chrisroos chrisroos marked this pull request as ready for review December 14, 2023 12:58
@floehopper floehopper self-assigned this Dec 14, 2023
@floehopper
Copy link
Contributor

@chrisroos @chrislo The 3rd commit note "Fix issues in Users::PermissionsController#update" looks like it's got the remnants of a squashed commit at the bottom - it would be nice to tidy that up

Copy link
Contributor

@floehopper floehopper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 3rd commit note "Fix issues in Users::PermissionsController#update" looks like it's got the remnants of a squashed commit at the bottom - it would be nice to tidy that up.

As I mentioned earlier today, I'm nervous about the complexity of PunditHelpers#stub_policy but I think that's something we can tackle separately.

Otherwise this all looks great to me - good work!

chrisroos and others added 4 commits December 14, 2023 14:07
In addition to losing the signin permission (which we fixed in the
previous commit) we also noticed 3 other issues with the current
implementation. We attempted to add each of these tests in their own
commit but the fix we've come up with resolves all 3 so we've decided to
combine them.

1. should "not remove permissions the user already has that are not
   grantable from ui"

If a user had been granted a permission that isn't grantable from the UI
then submitting the form to update their permissions would result in
that permission being lost. In production the only permission not
grantable from the UI is the `user_update_permission` and this is only
given to the "Signon API Client" (in `SSOPushCredential.credentials`[1])
so we don't think this has been causing any real problems but we should
fix it nonetheless.

Note that we're sorting the `SupportedPermission#id`s that end up in the
arguments to `UserUpdate.new` to make it easier to test.

2. should "prevent permissions being added for other apps"

Although not possible through the user interface, it was possible to
craft a request to give permissions to applications other than the
application identified by `params[:application_id]` in the URL. There
was no real danger here because the `UserUpdate` service ensures that
the current user can only modify permissions for users and applications
they're allowed to manage.

3. should "prevent permissions being added that are not grantable from
   the ui"

Although not possible through the user interface, it was possible to
craft a request to add a permission that's not grantable through the UI.
As explained in (1) above, the only permission not grantable from the UI
is the `user_update_permission` and this doesn't have any effect for
users other than the "Signon API Client".

These same problems exists in `Account::PermissionsController#update` and
we intend to fix that in a separate commit.

[1]: https://github.com/alphagov/signon/blob/f6690d8d13890da3279313e47cedfcefaeeeb7e3/lib/sso_push_credential.rb#L11

Co-authored-by: Chris Lowis <chris.lowis@gofreerange.com>
Co-authored-by: Chris Lowis <chris.lowis@gofreerange.com>
Co-authored-by: Chris Lowis <chris.lowis@gofreerange.com>
By duplicating the tests and implementation we've recently added for
`Users::PermissionsController#update`.

We discussed extracting a method to remove the duplication between the
two `build_user_update_params` methods but couldn't agree on where the
responsibility should live, so we've decided to leave it for now.

Note the change to `PunditHelpers#stub_policy` where we've updated it to
handle namespaced policies.

Co-authored-by: Chris Lowis <chris.lowis@gofreerange.com>
@chrislo
Copy link
Contributor

chrislo commented Dec 14, 2023

Thanks @floehopper - we've fixed that commit message. Rebasing on main before merging.

@chrislo chrislo merged commit 65e619f into main Dec 14, 2023
14 checks passed
@chrislo chrislo deleted the fix-permissions-controllers branch December 14, 2023 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants