mirror of
https://github.com/bitwarden/server.git
synced 2025-05-23 04:21:05 -05:00
Validate user that encrypted a cipher matches the user posting the request
This commit is contained in:
parent
e1d6caa10f
commit
7100b2bfad
@ -151,6 +151,16 @@ public class CiphersController : Controller
|
||||
public async Task<CipherResponseModel> Post([FromBody] CipherRequestModel model)
|
||||
{
|
||||
var user = await _userService.GetUserByPrincipalAsync(User);
|
||||
|
||||
// Validate the model was encrypted for the posting user
|
||||
if (model.EncryptedFor != null)
|
||||
{
|
||||
if (model.EncryptedFor != user.Id)
|
||||
{
|
||||
throw new BadRequestException("Cipher was not encrypted for the current user. Please try again.");
|
||||
}
|
||||
}
|
||||
|
||||
var cipher = model.ToCipherDetails(user.Id);
|
||||
if (cipher.OrganizationId.HasValue && !await _currentContext.OrganizationUser(cipher.OrganizationId.Value))
|
||||
{
|
||||
@ -170,6 +180,16 @@ public class CiphersController : Controller
|
||||
public async Task<CipherResponseModel> PostCreate([FromBody] CipherCreateRequestModel model)
|
||||
{
|
||||
var user = await _userService.GetUserByPrincipalAsync(User);
|
||||
|
||||
// Validate the model was encrypted for the posting user
|
||||
if (model.Cipher.EncryptedFor != null)
|
||||
{
|
||||
if (model.Cipher.EncryptedFor != user.Id)
|
||||
{
|
||||
throw new BadRequestException("Cipher was not encrypted for the current user. Please try again.");
|
||||
}
|
||||
}
|
||||
|
||||
var cipher = model.Cipher.ToCipherDetails(user.Id);
|
||||
if (cipher.OrganizationId.HasValue && !await _currentContext.OrganizationUser(cipher.OrganizationId.Value))
|
||||
{
|
||||
@ -192,6 +212,16 @@ public class CiphersController : Controller
|
||||
}
|
||||
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
|
||||
// Validate the model was encrypted for the posting user
|
||||
if (model.Cipher.EncryptedFor != null)
|
||||
{
|
||||
if (model.Cipher.EncryptedFor != userId)
|
||||
{
|
||||
throw new BadRequestException("Cipher was not encrypted for the current user. Please try again.");
|
||||
}
|
||||
}
|
||||
|
||||
await _cipherService.SaveAsync(cipher, userId, model.Cipher.LastKnownRevisionDate, model.CollectionIds, true, false);
|
||||
|
||||
var response = new CipherMiniResponseModel(cipher, _globalSettings, false);
|
||||
@ -209,6 +239,15 @@ public class CiphersController : Controller
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
// Validate the model was encrypted for the posting user
|
||||
if (model.EncryptedFor != null)
|
||||
{
|
||||
if (model.EncryptedFor != user.Id)
|
||||
{
|
||||
throw new BadRequestException("Cipher was not encrypted for the current user. Please try again.");
|
||||
}
|
||||
}
|
||||
|
||||
ValidateClientVersionForFido2CredentialSupport(cipher);
|
||||
|
||||
var collectionIds = (await _collectionCipherRepository.GetManyByUserIdCipherIdAsync(user.Id, id)).Select(c => c.CollectionId).ToList();
|
||||
@ -237,6 +276,15 @@ public class CiphersController : Controller
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var cipher = await _cipherRepository.GetOrganizationDetailsByIdAsync(id);
|
||||
|
||||
// Validate the model was encrypted for the posting user
|
||||
if (model.EncryptedFor != null)
|
||||
{
|
||||
if (model.EncryptedFor != userId)
|
||||
{
|
||||
throw new BadRequestException("Cipher was not encrypted for the current user. Please try again.");
|
||||
}
|
||||
}
|
||||
|
||||
ValidateClientVersionForFido2CredentialSupport(cipher);
|
||||
|
||||
if (cipher == null || !cipher.OrganizationId.HasValue ||
|
||||
@ -658,6 +706,15 @@ public class CiphersController : Controller
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
// Validate the model was encrypted for the posting user
|
||||
if (model.Cipher.EncryptedFor != null)
|
||||
{
|
||||
if (model.Cipher.EncryptedFor != user.Id)
|
||||
{
|
||||
throw new BadRequestException("Cipher was not encrypted for the current user. Please try again.");
|
||||
}
|
||||
}
|
||||
|
||||
ValidateClientVersionForFido2CredentialSupport(cipher);
|
||||
|
||||
var original = cipher.Clone();
|
||||
@ -1019,6 +1076,18 @@ public class CiphersController : Controller
|
||||
var ciphers = await _cipherRepository.GetManyByUserIdAsync(userId, withOrganizations: false);
|
||||
var ciphersDict = ciphers.ToDictionary(c => c.Id);
|
||||
|
||||
// Validate the model was encrypted for the posting user
|
||||
foreach (var cipher in model.Ciphers)
|
||||
{
|
||||
if (cipher.EncryptedFor != null)
|
||||
{
|
||||
if (cipher.EncryptedFor != userId)
|
||||
{
|
||||
throw new BadRequestException("Cipher was not encrypted for the current user. Please try again.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var shareCiphers = new List<(Cipher, DateTime?)>();
|
||||
foreach (var cipher in model.Ciphers)
|
||||
{
|
||||
|
@ -11,6 +11,10 @@ namespace Bit.Api.Vault.Models.Request;
|
||||
|
||||
public class CipherRequestModel
|
||||
{
|
||||
/// <summary>
|
||||
/// The Id of the user that encrypted the cipher. It should always represent a UserId.
|
||||
/// </summary>
|
||||
public Guid? EncryptedFor { get; set; }
|
||||
public CipherType Type { get; set; }
|
||||
|
||||
[StringLength(36)]
|
||||
|
Loading…
x
Reference in New Issue
Block a user