Skip to content

[PM-36678] Fix custom users modifying admins for restore and revoke.#7601

Draft
JimmyVo16 wants to merge 1 commit into
mainfrom
ac/pm-36678/fix-fix-custom-users-modifying-admins
Draft

[PM-36678] Fix custom users modifying admins for restore and revoke.#7601
JimmyVo16 wants to merge 1 commit into
mainfrom
ac/pm-36678/fix-fix-custom-users-modifying-admins

Conversation

@JimmyVo16
Copy link
Copy Markdown
Contributor

🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-36678

📔 Objective

📸 Screenshots

@JimmyVo16 JimmyVo16 self-assigned this May 8, 2026

namespace Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Validators;

public class CustomUserActingOnAdminValidator(ICurrentContext currentContext) : ICustomUserActingOnAdminValidator
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hey @sven-bitwarden , this is a new pattern I’m trying out. Happy to get any feedback.

We’re obviously doing the same check in a lot of places, so I was thinking we could abstract it into a single validator.

Benefits:

  1. Keeps the logic DRY
  2. Handles both single-user and bulk-user org cases without requiring multiple async calls.

// Probes with any admin in the batch. The rule's answer is uniform across every admin
// in the same organization, so one cached lookup covers the whole batch.
private async Task<bool> CustomUserCannotRevokeAdminAsync(IEnumerable<OrganizationUser> users)
{
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I still need to iterate on this a bit.

@@ -0,0 +1,8 @@
namespace Bit.Core.AdminConsole.Enums;

public enum OrganizationUserActionType
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The goal of this was to allow the commands to just pass in the action, while keeping the error messages in the validator. This is mainly to reduce logic in the commands. Also, most of the messages are the same, so centralizing them is a good idea.

I thought about passing in the class type, but that would narrow the calling code to only those types. An enum seems like the easiest solution, but I’m open to ideas.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 8, 2026

@codecov
Copy link
Copy Markdown

codecov Bot commented May 8, 2026

Codecov Report

❌ Patch coverage is 56.25000% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.72%. Comparing base (2d07451) to head (38556bd).

Files with missing lines Patch % Lines
...ers/Validators/CustomUserActingOnAdminValidator.cs 35.71% 16 Missing and 2 partials ⚠️
.../RevokeUser/v2/RevokeOrganizationUsersValidator.cs 81.81% 1 Missing and 1 partial ⚠️
...Features/OrganizationUsers/RevokeUser/v2/Errors.cs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7601      +/-   ##
==========================================
- Coverage   59.73%   59.72%   -0.01%     
==========================================
  Files        2109     2110       +1     
  Lines       92752    92797      +45     
  Branches     8246     8252       +6     
==========================================
+ Hits        55403    55425      +22     
- Misses      35384    35403      +19     
- Partials     1965     1969       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@JimmyVo16 JimmyVo16 requested a review from sven-bitwarden May 11, 2026 13:36
Copy link
Copy Markdown
Member

@eliykat eliykat left a comment

Choose a reason for hiding this comment

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

A couple of specific comments below, but more generally:

I do think that we need to develop a pattern of having smaller, more composable validators for shared logic. However, in this case, this is an authorization check that I think needs to live in the api layer. It is effectively an extension of RBAC: you must have this role, but also that role must be equal or greater to the user you're acting on.

Therefore I think this should be handled by the authorization layer, potentially generalizing the RecoveryAccountAuthorizationHandler which implements this logic.

You could imagine, for example, an attribute that handles this for you in a reusable way:

    [HttpPut("restore")]
    [Authorize<ManageUsersRequirement>]
    [Authorize<EqualOrGreaterRoleRequirement>]   // additional requirement
    public async Task<ListResponseModel<OrganizationUserBulkResponseModel>> BulkRestoreAsync(Guid orgId, [FromBody] OrganizationUserBulkRequestModel model)

Or - and I suspect this is the case - it may be too difficult or inefficient to do it from an attribute, in which case you can do it manually. This also means you have the orgUser objects to pass into the command later:

        var organizationUsers = await _organizationUserRepository.GetManyAsync(model.Ids);
        var authorized =
            await _authorizationService.AuthorizeAsync(User, organizationUsers, new EqualOrGreaterRoleRequirement());
// check authorized result, then pass orgUsers into command instead of just ids

I definitely agree that we need a reusable solution though.

Let me know what you think.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is too narrow - the general pattern here is not "custom users cannot act on admins" (specific), it's "users with lower privileges cannot act on users with higher privileges" (more general).

Here is some example logic from account recovery which fully expresses the rule:

// Current user must have equal or greater permissions than the user account being recovered
var authorized = targetOrganizationUser.Type switch
{
OrganizationUserType.Owner => currentContextOrganization.Type is OrganizationUserType.Owner,
OrganizationUserType.Admin => currentContextOrganization.Type is OrganizationUserType.Owner or OrganizationUserType.Admin,
_ => currentContextOrganization is
{ Type: OrganizationUserType.Owner or OrganizationUserType.Admin }
or { Type: OrganizationUserType.Custom, Permissions.ManageResetPassword: true }
};

Comment on lines +39 to +40
var isCustom = await currentContext.OrganizationCustom(organizationId);
_organizationCustomLookup[organizationId] = isCustom;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Although this is async, currentContext caches its results locally so you don't need a second layer of caching here. (I also generally think that this is an antipattern so would be pretty hesitant to extend this kind of behavior)

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.

2 participants