mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
[PM-11360] Remove export permission for providers (#5051)
- also fix managed collections export from CLI
This commit is contained in:
@ -0,0 +1,38 @@
|
||||
using Bit.Core.AdminConsole.OrganizationFeatures.Shared.Authorization;
|
||||
using Bit.Core.Context;
|
||||
using Bit.Core.Enums;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace Bit.Api.Tools.Authorization;
|
||||
|
||||
public class VaultExportAuthorizationHandler(ICurrentContext currentContext)
|
||||
: AuthorizationHandler<VaultExportOperationRequirement, OrganizationScope>
|
||||
{
|
||||
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
|
||||
VaultExportOperationRequirement requirement, OrganizationScope organizationScope)
|
||||
{
|
||||
var org = currentContext.GetOrganization(organizationScope);
|
||||
|
||||
var authorized = requirement switch
|
||||
{
|
||||
not null when requirement == VaultExportOperations.ExportWholeVault =>
|
||||
CanExportWholeVault(org),
|
||||
not null when requirement == VaultExportOperations.ExportManagedCollections =>
|
||||
CanExportManagedCollections(org),
|
||||
_ => false
|
||||
};
|
||||
|
||||
if (authorized)
|
||||
{
|
||||
context.Succeed(requirement);
|
||||
}
|
||||
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
private bool CanExportWholeVault(CurrentContextOrganization organization) => organization is
|
||||
{ Type: OrganizationUserType.Owner or OrganizationUserType.Admin } or
|
||||
{ Type: OrganizationUserType.Custom, Permissions.AccessImportExport: true };
|
||||
|
||||
private bool CanExportManagedCollections(CurrentContextOrganization organization) => organization is not null;
|
||||
}
|
20
src/Api/Tools/Authorization/VaultExportOperations.cs
Normal file
20
src/Api/Tools/Authorization/VaultExportOperations.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using Microsoft.AspNetCore.Authorization.Infrastructure;
|
||||
|
||||
namespace Bit.Api.Tools.Authorization;
|
||||
|
||||
public class VaultExportOperationRequirement : OperationAuthorizationRequirement;
|
||||
|
||||
public static class VaultExportOperations
|
||||
{
|
||||
/// <summary>
|
||||
/// Exporting the entire organization vault.
|
||||
/// </summary>
|
||||
public static readonly VaultExportOperationRequirement ExportWholeVault =
|
||||
new() { Name = nameof(ExportWholeVault) };
|
||||
|
||||
/// <summary>
|
||||
/// Exporting only the organization items that the user has Can Manage permissions for
|
||||
/// </summary>
|
||||
public static readonly VaultExportOperationRequirement ExportManagedCollections =
|
||||
new() { Name = nameof(ExportManagedCollections) };
|
||||
}
|
@ -1,11 +1,17 @@
|
||||
using Bit.Api.Models.Response;
|
||||
using Bit.Api.Tools.Authorization;
|
||||
using Bit.Api.Tools.Models.Response;
|
||||
using Bit.Api.Vault.Models.Response;
|
||||
using Bit.Core;
|
||||
using Bit.Core.AdminConsole.OrganizationFeatures.Shared.Authorization;
|
||||
using Bit.Core.Context;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Core.Vault.Models.Data;
|
||||
using Bit.Core.Vault.Queries;
|
||||
using Bit.Core.Vault.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@ -21,24 +27,41 @@ public class OrganizationExportController : Controller
|
||||
private readonly ICollectionService _collectionService;
|
||||
private readonly ICipherService _cipherService;
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly IFeatureService _featureService;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly IOrganizationCiphersQuery _organizationCiphersQuery;
|
||||
private readonly ICollectionRepository _collectionRepository;
|
||||
|
||||
public OrganizationExportController(
|
||||
ICurrentContext currentContext,
|
||||
ICipherService cipherService,
|
||||
ICollectionService collectionService,
|
||||
IUserService userService,
|
||||
GlobalSettings globalSettings)
|
||||
GlobalSettings globalSettings,
|
||||
IFeatureService featureService,
|
||||
IAuthorizationService authorizationService,
|
||||
IOrganizationCiphersQuery organizationCiphersQuery,
|
||||
ICollectionRepository collectionRepository)
|
||||
{
|
||||
_currentContext = currentContext;
|
||||
_cipherService = cipherService;
|
||||
_collectionService = collectionService;
|
||||
_userService = userService;
|
||||
_globalSettings = globalSettings;
|
||||
_featureService = featureService;
|
||||
_authorizationService = authorizationService;
|
||||
_organizationCiphersQuery = organizationCiphersQuery;
|
||||
_collectionRepository = collectionRepository;
|
||||
}
|
||||
|
||||
[HttpGet("export")]
|
||||
public async Task<IActionResult> Export(Guid organizationId)
|
||||
{
|
||||
if (_featureService.IsEnabled(FeatureFlagKeys.PM11360RemoveProviderExportPermission))
|
||||
{
|
||||
return await Export_vNext(organizationId);
|
||||
}
|
||||
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
|
||||
IEnumerable<Collection> orgCollections = await _collectionService.GetOrganizationCollectionsAsync(organizationId);
|
||||
@ -65,6 +88,35 @@ public class OrganizationExportController : Controller
|
||||
return Ok(organizationExportListResponseModel);
|
||||
}
|
||||
|
||||
private async Task<IActionResult> Export_vNext(Guid organizationId)
|
||||
{
|
||||
var canExportAll = await _authorizationService.AuthorizeAsync(User, new OrganizationScope(organizationId),
|
||||
VaultExportOperations.ExportWholeVault);
|
||||
if (canExportAll.Succeeded)
|
||||
{
|
||||
var allOrganizationCiphers = await _organizationCiphersQuery.GetAllOrganizationCiphers(organizationId);
|
||||
var allCollections = await _collectionRepository.GetManyByOrganizationIdAsync(organizationId);
|
||||
return Ok(new OrganizationExportResponseModel(allOrganizationCiphers, allCollections, _globalSettings));
|
||||
}
|
||||
|
||||
var canExportManaged = await _authorizationService.AuthorizeAsync(User, new OrganizationScope(organizationId),
|
||||
VaultExportOperations.ExportManagedCollections);
|
||||
if (canExportManaged.Succeeded)
|
||||
{
|
||||
var userId = _userService.GetProperUserId(User)!.Value;
|
||||
|
||||
var allUserCollections = await _collectionRepository.GetManyByUserIdAsync(userId);
|
||||
var managedOrgCollections = allUserCollections.Where(c => c.OrganizationId == organizationId && c.Manage).ToList();
|
||||
var managedCiphers =
|
||||
await _organizationCiphersQuery.GetOrganizationCiphersByCollectionIds(organizationId, managedOrgCollections.Select(c => c.Id));
|
||||
|
||||
return Ok(new OrganizationExportResponseModel(managedCiphers, managedOrgCollections, _globalSettings));
|
||||
}
|
||||
|
||||
// Unauthorized
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
private ListResponseModel<CollectionResponseModel> GetOrganizationCollectionsResponse(IEnumerable<Collection> orgCollections)
|
||||
{
|
||||
var collections = orgCollections.Select(c => new CollectionResponseModel(c));
|
||||
|
@ -1,6 +1,9 @@
|
||||
using Bit.Api.Models.Response;
|
||||
using Bit.Api.Vault.Models.Response;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models.Api;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Core.Vault.Models.Data;
|
||||
|
||||
namespace Bit.Api.Tools.Models.Response;
|
||||
|
||||
@ -10,6 +13,13 @@ public class OrganizationExportResponseModel : ResponseModel
|
||||
{
|
||||
}
|
||||
|
||||
public OrganizationExportResponseModel(IEnumerable<CipherOrganizationDetailsWithCollections> ciphers,
|
||||
IEnumerable<Collection> collections, GlobalSettings globalSettings) : this()
|
||||
{
|
||||
Ciphers = ciphers.Select(c => new CipherMiniDetailsResponseModel(c, globalSettings));
|
||||
Collections = collections.Select(c => new CollectionResponseModel(c));
|
||||
}
|
||||
|
||||
public IEnumerable<CollectionResponseModel> Collections { get; set; }
|
||||
|
||||
public IEnumerable<CipherMiniDetailsResponseModel> Ciphers { get; set; }
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Bit.Api.Vault.AuthorizationHandlers.Collections;
|
||||
using Bit.Api.Tools.Authorization;
|
||||
using Bit.Api.Vault.AuthorizationHandlers.Collections;
|
||||
using Bit.Core.AdminConsole.OrganizationFeatures.Groups.Authorization;
|
||||
using Bit.Core.IdentityServer;
|
||||
using Bit.Core.Settings;
|
||||
@ -99,5 +100,6 @@ public static class ServiceCollectionExtensions
|
||||
services.AddScoped<IAuthorizationHandler, BulkCollectionAuthorizationHandler>();
|
||||
services.AddScoped<IAuthorizationHandler, CollectionAuthorizationHandler>();
|
||||
services.AddScoped<IAuthorizationHandler, GroupAuthorizationHandler>();
|
||||
services.AddScoped<IAuthorizationHandler, VaultExportAuthorizationHandler>();
|
||||
}
|
||||
}
|
||||
|
@ -166,5 +166,12 @@ public class CipherMiniDetailsResponseModel : CipherMiniResponseModel
|
||||
CollectionIds = cipher.CollectionIds ?? new List<Guid>();
|
||||
}
|
||||
|
||||
public CipherMiniDetailsResponseModel(CipherOrganizationDetailsWithCollections cipher,
|
||||
GlobalSettings globalSettings, string obj = "cipherMiniDetails")
|
||||
: base(cipher, globalSettings, cipher.OrganizationUseTotp, obj)
|
||||
{
|
||||
CollectionIds = cipher.CollectionIds ?? new List<Guid>();
|
||||
}
|
||||
|
||||
public IEnumerable<Guid> CollectionIds { get; set; }
|
||||
}
|
||||
|
@ -153,6 +153,7 @@ public static class FeatureFlagKeys
|
||||
public const string NewDeviceVerificationPermanentDismiss = "new-device-permanent-dismiss";
|
||||
public const string SecurityTasks = "security-tasks";
|
||||
public const string PM14401_ScaleMSPOnClientOrganizationUpdate = "PM-14401-scale-msp-on-client-organization-update";
|
||||
public const string PM11360RemoveProviderExportPermission = "pm-11360-remove-provider-export-permission";
|
||||
public const string DisableFreeFamiliesSponsorship = "PM-12274-disable-free-families-sponsorship";
|
||||
public const string MacOsNativeCredentialSync = "macos-native-credential-sync";
|
||||
public const string PM9111ExtensionPersistAddEditForm = "pm-9111-extension-persist-add-edit-form";
|
||||
|
@ -27,4 +27,14 @@ public interface IOrganizationCiphersQuery
|
||||
/// </summary>
|
||||
/// <exception cref="FeatureUnavailableException"></exception>
|
||||
Task<IEnumerable<CipherOrganizationDetails>> GetUnassignedOrganizationCiphers(Guid organizationId);
|
||||
|
||||
/// <summary>
|
||||
/// Returns ciphers belonging to the organization that are in the specified collections.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Note that the <see cref="CipherOrganizationDetailsWithCollections.CollectionIds"/> will include all collections
|
||||
/// the cipher belongs to even if it is not in the <paramref name="collectionIds"/> parameter.
|
||||
/// </remarks>
|
||||
public Task<IEnumerable<CipherOrganizationDetailsWithCollections>> GetOrganizationCiphersByCollectionIds(
|
||||
Guid organizationId, IEnumerable<Guid> collectionIds);
|
||||
}
|
||||
|
@ -52,4 +52,13 @@ public class OrganizationCiphersQuery : IOrganizationCiphersQuery
|
||||
{
|
||||
return await _cipherRepository.GetManyUnassignedOrganizationDetailsByOrganizationIdAsync(organizationId);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IEnumerable<CipherOrganizationDetailsWithCollections>> GetOrganizationCiphersByCollectionIds(
|
||||
Guid organizationId, IEnumerable<Guid> collectionIds)
|
||||
{
|
||||
var managedCollectionIds = collectionIds.ToHashSet();
|
||||
var allOrganizationCiphers = await GetAllOrganizationCiphers(organizationId);
|
||||
return allOrganizationCiphers.Where(c => c.CollectionIds.Intersect(managedCollectionIds).Any());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user