Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add blob cipher text hash check on upload (#2159)
Leendert de Borst935cd4ef59ed
diff --git a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs b/apps/server/AliasVault.Api/Controllers/V2/VaultController.csindex 8271b75f7..a1b973f0a 100644--- a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs+++ b/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs@@ -267,7 +267,7 @@ public class VaultController(var manifestBlobs = new Dictionary<Guid, byte[]>();foreach (var mw in model.Manifests){- if (!CiphertextHelper.TryDecode(mw.ManifestBlob, out var manifestBlob))+ if (!CiphertextHelper.TryDecode(mw.ManifestBlob, out var manifestBlob) || !CiphertextHelper.MatchesHash(manifestBlob, mw.ManifestCiphertextHash)){return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.VAULT_ERROR, 400));}@@ -278,7 +278,7 @@ public class VaultController(var bucketBlobs = new Dictionary<(Guid ManifestId, VaultDataBucketCategory Category), byte[]>();foreach (var bucket in model.Buckets.Where(b => !string.IsNullOrEmpty(b.Blob))){- if (!CiphertextHelper.TryDecode(bucket.Blob, out var bucketBlob))+ if (!CiphertextHelper.TryDecode(bucket.Blob, out var bucketBlob) || !CiphertextHelper.MatchesHash(bucketBlob, bucket.CiphertextHash)){return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.VAULT_ERROR, 400));}@@ -623,6 +623,13 @@ public class VaultController(return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.VAULT_ERROR, 400));}+ // Only allow overwriting the ciphertext if the user has no unlock key yet (as part of one-time legacy migration).+ // TODO: when adding VEK rotation, overwrite will need to be made possible as well for the rotation flow itself.+ if (model.Overwrite && await context.UserUnlockKeys.AnyAsync(x => x.UserId == user.Id && x.Type == UnlockMethodType.Password))+ {+ return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.VAULT_KEY_ALREADY_EXISTS, 400));+ }+if (model.Blobs.Count == 0){return Ok(new BlobUploadResponse { AcceptedCount = 0 });diff --git a/apps/server/AliasVault.Api/Helpers/CiphertextHelper.cs b/apps/server/AliasVault.Api/Helpers/CiphertextHelper.csindex 8d2d3020e..9658814f0 100644--- a/apps/server/AliasVault.Api/Helpers/CiphertextHelper.cs+++ b/apps/server/AliasVault.Api/Helpers/CiphertextHelper.cs@@ -37,4 +37,15 @@ public static class CiphertextHelperreturn bytes.Length >= MinCiphertextLength;}++ /// <summary>+ /// Checks that the ciphertext is the one the client hashed, so a payload damaged on the way in is never stored.+ /// </summary>+ /// <param name="bytes">The decoded ciphertext.</param>+ /// <param name="expectedHash">The SHA-256 of the ciphertext as hex, as the client computed it.</param>+ /// <returns>True when the hashes match.</returns>+ public static bool MatchesHash(byte[] bytes, string? expectedHash)+ {+ return string.Equals(Convert.ToHexString(System.Security.Cryptography.SHA256.HashData(bytes)), expectedHash, StringComparison.OrdinalIgnoreCase);+ }}