Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Refactor and simplify vault push endpoint (#2159)
Leendert de Borstbc47f802b433
diff --git a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs b/apps/server/AliasVault.Api/Controllers/V2/VaultController.csindex 4f3eaabc4..266b1f800 100644--- a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs+++ b/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs@@ -233,8 +233,8 @@ public class VaultController(}/// <summary>- /// Unified atomic write. Applies any number of changed manifests (the personal manifest and/or shared manifests), changed data- /// buckets, and new blobs in a single all-or-nothing DB transaction.+ /// Unified atomic write. Applies any number of changed manifests (personal and/or shared) and changed data buckets in a single+ /// all-or-nothing DB transaction. Blobs are uploaded beforehand through POST v2/Vault/blobs./// </summary>/// <param name="model">Vault write request DTO.</param>/// <returns>Vault write response DTO.</returns>@@ -315,7 +315,7 @@ public class VaultController(// Account-key migration: a legacy vault's first manifest-v1 push includes a newly created Account Key hierarchy, which is accepted exactly once.// Every later personal write must find the stored password unlock key. TODO: remove once legacy accounts are no longer supported.- var accountKeys = model.AccountKeys;+ var accountKeys = model.Migration?.AccountKeys;var hasExistingUnlockKey = await context.UserUnlockKeys.AnyAsync(x => x.UserId == user.Id && x.Type == UnlockMethodType.Password);if (accountKeys != null){@@ -369,24 +369,7 @@ public class VaultController({await using var tx = await context.Database.BeginTransactionAsync();- // 1) Upsert any new blob objects, each under the manifests in this write that reference it.- if (model.NewBlobs.Count > 0)- {- foreach (var (mw, row) in resolved)- {- var referenced = mw.BlobReferences.Select(br => br.Hash).ToHashSet(StringComparer.Ordinal);- var referencedBlobs = model.NewBlobs.Where(b => referenced.Contains(b.Hash)).ToList();- if (!await TryUpsertBlobObjectsAsync(context, row.ManifestId, referencedBlobs, overwrite: accountKeys != null && row.OwnerGroupId == user.PersonalGroupId))- {- await tx.RollbackAsync();- return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.VAULT_NOT_UP_TO_DATE, 400));- }- }-- await context.SaveChangesAsync();- }-- // 2) Validate every referenced hash exists in the store of the manifest that references it.+ // 1) Validate every referenced hash exists in the store of the manifest that references it.var referencedHashes = resolved.SelectMany(r => r.Write.BlobReferences).Select(br => br.Hash).Distinct().ToList();var missing = new List<string>();if (referencedHashes.Count > 0)@@ -412,7 +395,7 @@ public class VaultController(});}- // 3) Apply each manifest: archive the current revision into history, update the row in place, run the+ // 2) Apply each manifest: archive the current revision into history, update the row in place, run the// personal-only side effects (email claims count + KEK/VEK key creation), and prune history per retention.var manifestResults = new List<ManifestWriteResult>();foreach (var (mw, row) in resolved)@@ -519,7 +502,7 @@ public class VaultController(return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.VAULT_KEY_ALREADY_EXISTS, 400));}- // 4) Add blob references for each manifest's new revision.+ // 3) Add blob references for each manifest's new revision.foreach (var (mw, row) in resolved){foreach (var dto in mw.BlobReferences)@@ -533,7 +516,7 @@ public class VaultController(}}- // 5) Data bucket upserts (settings, etc.). Each insert adds a new revision row (history).+ // 4) Data bucket upserts (settings, etc.). Each insert adds a new revision row (history).var newBucketRevisions = new List<BucketRevision>();foreach (var bucket in model.Buckets){diff --git a/apps/server/AliasVault.Client/Services/VaultSync/VaultSyncService.cs b/apps/server/AliasVault.Client/Services/VaultSync/VaultSyncService.csindex 2f96e7cb6..e2fe3f6a1 100644--- a/apps/server/AliasVault.Client/Services/VaultSync/VaultSyncService.cs+++ b/apps/server/AliasVault.Client/Services/VaultSync/VaultSyncService.cs@@ -363,9 +363,8 @@ public sealed class VaultSyncService(HttpClient httpClient, AuthService authServUsername = await authService.GetUsernameAsync(),Manifests = manifestWrites,Buckets = bucketWrites,- NewBlobs = [],EmailRouting = emailRouting,- AccountKeys = migration?.Keys,+ Migration = migration is null ? null : new VaultWriteMigration { AccountKeys = migration.Keys },};var response = await PostWriteAsync(request);diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteMigration.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteMigration.csnew file mode 100644index 000000000..c9dfbc93d--- /dev/null+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteMigration.cs@@ -0,0 +1,23 @@+//-----------------------------------------------------------------------+// <copyright file="VaultWriteMigration.cs" company="aliasvault">+// Copyright (c) aliasvault. All rights reserved.+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.+// </copyright>+//-----------------------------------------------------------------------++namespace AliasVault.Shared.Models.WebApi.V2.Vault;++using System.Text.Json.Serialization;++/// <summary>+/// One-time migrations applied atomically with a vault write. Each migration is its own optional field.+/// </summary>+public class VaultWriteMigration+{+ /// <summary>+ /// Gets or sets the newly created account key hierarchy for a legacy vault's first manifest-v1 push (pre-0.31.0).+ /// TODO: remove once legacy accounts are no longer supported.+ /// </summary>+ [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]+ public AccountKeysUpload? AccountKeys { get; set; }+}diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteRequest.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteRequest.csindex e81ff29b2..99ae0c6f9 100644--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteRequest.cs+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteRequest.cs@@ -7,6 +7,8 @@namespace AliasVault.Shared.Models.WebApi.V2.Vault;+using System.Text.Json.Serialization;+/// <summary>/// Unified atomic write for POST /v2/Vault./// </summary>@@ -21,15 +23,11 @@ public class VaultWriteRequest/// <summary>Gets or sets the data buckets to upsert.</summary>public List<BucketWrite> Buckets { get; set; } = [];- /// <summary>Gets or sets the new blob objects the client is uploading for this write. Each is stored under every- /// manifest in this write that references its hash.</summary>- public List<Blob> NewBlobs { get; set; } = [];-/// <summary>Gets or sets the email routing data to update server-side.</summary>+ [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]public EmailRoutingPush? EmailRouting { get; set; }- /// <summary>- /// Gets or sets a newly created account key hierarchy. Only sent for the one-time legacy vault's migration push (pre-0.31.0).- /// </summary>- public AccountKeysUpload? AccountKeys { get; set; }+ /// <summary>Gets or sets the one-time migrations to apply atomically with this write.</summary>+ [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]+ public VaultWriteMigration? Migration { get; set; }}diff --git a/core/rust/src/vault_sync/push.rs b/core/rust/src/vault_sync/push.rsindex de0f66953..fc78fbddc 100644--- a/core/rust/src/vault_sync/push.rs+++ b/core/rust/src/vault_sync/push.rs@@ -8,7 +8,7 @@ use crate::vault_model::{id_key, ids_equal, OVERFLOW_TABLE, TRASH_RETENTION_DEFAuse super::email_routing::build_email_routing;use super::errors::{SyncError, SyncResult};use super::state::{self, Ctx};-use super::types::{BlobDto, BlobHashesRequest, BlobRef, BlobUploadRequest, BucketRevision, BucketWrite, Db, ManifestRevision, ManifestWrite, MissingBlobsResponse, VaultWriteRequest, VaultWriteResponse};+use super::types::{BlobDto, BlobHashesRequest, BlobRef, BlobUploadRequest, BucketRevision, BucketWrite, Db, ManifestRevision, ManifestWrite, MissingBlobsResponse, VaultWriteMigration, VaultWriteRequest, VaultWriteResponse};use super::blob_keys::{self, EncryptedBlob};use super::{db, http, keys};use crate::crypto;@@ -458,7 +458,7 @@ async fn push_internal(ctx: &mut Ctx, cached: Option<CanonicalizedSet>, create_vlet mut uploaded = upload_missing_blobs(ctx, &blobs, &baselines, gate).await?;let email_routing = build_email_routing(&canonicalized.manifests.iter().map(|m| m.manifest.clone()).collect::<Vec<_>>(), &ctx.request.private_email_domains);- let payload = VaultWriteRequest { username: ctx.request.username.clone(), manifests: manifest_writes, buckets: bucket_writes, new_blobs: Vec::new(), email_routing: Some(email_routing), account_keys: migration.as_ref().map(|m| m.account_keys.clone()) };+ let payload = VaultWriteRequest { username: ctx.request.username.clone(), manifests: manifest_writes, buckets: bucket_writes, email_routing: Some(email_routing), migration: migration.as_ref().map(|m| VaultWriteMigration { account_keys: Some(m.account_keys.clone()) }) };let response = write_vault(ctx, &payload, &blobs, gate, &mut uploaded).await?;if response.status != 0 {@@ -748,9 +748,8 @@ async fn push_data_bucket_only_internal(ctx: &Ctx, bucket: &DataBucket, vek: &stusername: ctx.request.username.clone(),manifests: Vec::new(),buckets: vec![BucketWrite { manifest_id: bucket.manifest_id.clone(), category: bucket.category.clone(), blob: encrypted.ciphertext, ciphertext_hash: encrypted.hash, current_revision }],- new_blobs: Vec::new(),email_routing: None,- account_keys: None,+ migration: None,};let response: VaultWriteResponse = http::post(&ctx.host, http::VAULT_ENDPOINT, &payload, true).await?;let reported = response.bucket_revisions.iter().find(|b| ids_equal(&b.manifest_id, &bucket.manifest_id) && b.category == bucket.category).map(|b| b.revision);diff --git a/core/rust/src/vault_sync/tests/mod.rs b/core/rust/src/vault_sync/tests/mod.rsindex ea438312b..8ab5a4100 100644--- a/core/rust/src/vault_sync/tests/mod.rs+++ b/core/rust/src/vault_sync/tests/mod.rs@@ -462,8 +462,8 @@ fn manifest_migration_generates_the_key_hierarchy_and_pushes() {assert_ne!(new_key, kek, "the host receives the new VEK through the store command");let posts: Vec<_> = host.requests_to("Vault").into_iter().filter(|r| r.method == "POST").collect();let body = posts[0].body.as_ref().unwrap();- assert!(body["accountKeys"]["encryptedAccountKey"].is_string(), "the migration push carries the key hierarchy");- let (vek, _) = crypto::resolve_vault_encryption_key(body["accountKeys"]["encryptedAccountKey"].as_str().unwrap(), body["accountKeys"]["encryptedVek"].as_str().unwrap(), &kek).unwrap();+ assert!(body["migration"]["accountKeys"]["encryptedAccountKey"].is_string(), "the migration push carries the key hierarchy");+ let (vek, _) = crypto::resolve_vault_encryption_key(body["migration"]["accountKeys"]["encryptedAccountKey"].as_str().unwrap(), body["migration"]["accountKeys"]["encryptedVek"].as_str().unwrap(), &kek).unwrap();assert_eq!(*vek, new_key);assert!(host.state.contains_key(state::ENCRYPTED_ACCOUNT_KEY));assert!(host.state.contains_key(state::ENCRYPTED_ACCOUNT_PRIVATE_KEY));@@ -502,7 +502,7 @@ fn schema_rebuild_of_a_stale_vault_pushes_without_touching_the_key_hierarchy() {assert_eq!(item_names(&host.local), vec!["Kept item"]);assert!(host.requests_to("VaultKey/Password").is_empty(), "a migrated account is not probed for a key hierarchy");let posts: Vec<_> = host.requests_to("Vault").into_iter().filter(|r| r.method == "POST").collect();- assert!(posts[0].body.as_ref().unwrap()["accountKeys"].is_null(), "no key hierarchy is created");+ assert!(posts[0].body.as_ref().unwrap()["migration"].is_null(), "no key hierarchy is created");assert_eq!(host.vault_key, vek, "the session key stays the VEK");}@@ -549,7 +549,7 @@ fn manifest_migration_of_a_pre_format_session_pulls_the_server_vault_first() {let posts: Vec<_> = host.requests_to("Vault").into_iter().filter(|r| r.method == "POST").collect();let body = posts[0].body.as_ref().unwrap();assert_eq!(body["manifests"][0]["currentRevision"], 3, "the migration push names the revision the server holds");- assert!(body["accountKeys"]["encryptedAccountKey"].is_string());+ assert!(body["migration"]["accountKeys"]["encryptedAccountKey"].is_string());assert_eq!(host.state[state::SERVER_MANIFEST_REVISIONS][PERSONAL_MANIFEST_ID], 4);}diff --git a/core/rust/src/vault_sync/types.rs b/core/rust/src/vault_sync/types.rsindex 01f43ce63..773fa278f 100644--- a/core/rust/src/vault_sync/types.rs+++ b/core/rust/src/vault_sync/types.rs@@ -685,8 +685,18 @@ pub struct VaultWriteRequest {pub username: String,pub manifests: Vec<ManifestWrite>,pub buckets: Vec<BucketWrite>,- pub new_blobs: Vec<BlobDto>,+ #[serde(skip_serializing_if = "Option::is_none")]pub email_routing: Option<EmailRoutingPush>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub migration: Option<VaultWriteMigration>,+}++/// One-time migrations applied atomically with a vault write (optional field).+#[derive(Debug, Clone, Serialize)]+#[serde(rename_all = "camelCase")]+pub struct VaultWriteMigration {+ /// The newly created key hierarchy for a legacy vault's first manifest-v1 push (pre-0.31.0).+ #[serde(skip_serializing_if = "Option::is_none")]pub account_keys: Option<crate::crypto::AccountKeyBlobs>,}