Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Tweak vault_sync logic to separate explicit legacy behavior for future removal
Leendert de Borstce3d56430be5
diff --git a/core/rust/src/vault_sync/engine.rs b/core/rust/src/vault_sync/engine.rsindex 527f40ccd..2f33ecd8a 100644--- a/core/rust/src/vault_sync/engine.rs+++ b/core/rust/src/vault_sync/engine.rs@@ -101,13 +101,8 @@ pub(crate) async fn full_sync(ctx: &mut Ctx) -> SyncResult<FullSyncResult> {if dropped_local_vault || !matches!(error, SyncError::VaultDecryptFailed(_)) {return Err(error);}- ctx.warn(format!("[Sync] The stored vault does not decrypt with the session key; dropping it and pulling the server's vault. {}", error)).await;+ drop_local_vault_for_pull(ctx, &error).await?;dropped_local_vault = true;- ctx.discard_local_vault();- if ctx.is_dirty && state::mark_clean(&ctx.host, ctx.mutation_sequence).await? {- ctx.is_dirty = false;- }- ctx.request.force_pull = true;continue;}};@@ -127,6 +122,17 @@ pub(crate) async fn full_sync(ctx: &mut Ctx) -> SyncResult<FullSyncResult> {}}+/// Treat a stored vault the session key does not open as absent and force the retry to pull the server's vault in its place.+async fn drop_local_vault_for_pull(ctx: &mut Ctx, error: &SyncError) -> SyncResult<()> {+ ctx.warn(format!("[Sync] The stored vault does not decrypt with the session key; dropping it and pulling the server's vault. {}", error)).await;+ ctx.discard_local_vault();+ if ctx.is_dirty && state::mark_clean(&ctx.host, ctx.mutation_sequence).await? {+ ctx.is_dirty = false;+ }+ ctx.request.force_pull = true;+ Ok(())+}+async fn full_sync_once(ctx: &mut Ctx) -> SyncResult<Flow> {ctx.log("[Sync] Sync started").await;let status = match check_session(ctx).await? {@@ -343,6 +349,7 @@ async fn refresh_manifest_names(ctx: &mut Ctx, status: &StatusResponse, needs_pu/// Outcome for a sync that stored a freshly pulled vault.async fn materialized_vault_result(ctx: &mut Ctx, pulled: &PulledVault) -> SyncResult<FullSyncResult> {let migration_required = migration::vault_requires_manifest_migration(ctx).await?;+ // LEGACY: a pulled sqlite-blob vault can still be on the frozen upgrade chain.let legacy_chain = migration::schema_state(ctx).await? == db::SchemaState::LegacyChain;Ok(FullSyncResult { success: true, has_new_vault: true, sqlite_blob_upgrade_required: legacy_chain, manifest_migration_required: migration_required, pulled_revision: Some(pulled.personal_revision), ..Default::default() })}diff --git a/core/rust/src/vault_sync/keys.rs b/core/rust/src/vault_sync/keys.rsindex 1ea3b0817..0bb7197aa 100644--- a/core/rust/src/vault_sync/keys.rs+++ b/core/rust/src/vault_sync/keys.rs@@ -7,6 +7,7 @@ use zeroize::Zeroizing;use super::errors::{SyncError, SyncResult};use super::http;+use super::legacy;use super::session::Host;use super::state::{self, Ctx};use super::types::{ManifestDto, SharedManifestDto, VaultKeyGetResponse, VaultKeyResponse, ALGORITHM_AES256_GCM, ALGORITHM_RSA_OAEP_SHA256};@@ -20,7 +21,7 @@ pub(crate) async fn has_cached_key_chain(host: &Host) -> SyncResult<bool> {}/// `GET v2/VaultKey/Password`: the account's vault key, `None` when the server holds none.-async fn fetch_vault_key(host: &Host) -> SyncResult<Option<VaultKeyResponse>> {+pub(super) async fn fetch_vault_key(host: &Host) -> SyncResult<Option<VaultKeyResponse>> {match http::get::<VaultKeyGetResponse>(host, http::VAULT_KEY_PASSWORD_ENDPOINT, false).await {Ok(response) => Ok(response.vault_key),Err(SyncError::Http { status: 404, .. }) => Ok(None),@@ -40,7 +41,7 @@ fn ensure_known_unlock_algorithm(vault_key: &VaultKeyResponse) -> SyncResult<()>const CHAIN_STATE: [&str; 6] = [state::ENCRYPTED_ACCOUNT_KEY, state::ENCRYPTED_VEK, state::ACCOUNT_PUBLIC_KEY, state::ENCRYPTED_ACCOUNT_PRIVATE_KEY, state::SIGNING_PUBLIC_KEY, state::ENCRYPTED_SIGNING_PRIVATE_KEY];/// Cache a server vault-key response's encrypted blobs; a half the server does not hold is removed.-async fn cache_vault_key_blobs(host: &Host, vault_key: &VaultKeyResponse) -> SyncResult<()> {+pub(super) async fn cache_vault_key_blobs(host: &Host, vault_key: &VaultKeyResponse) -> SyncResult<()> {let values = [Some(&vault_key.encrypted_account_key), vault_key.encrypted_vek.as_ref(), vault_key.account_public_key.as_ref(), vault_key.encrypted_account_private_key.as_ref(), vault_key.signing_public_key.as_ref(), vault_key.encrypted_signing_private_key.as_ref()];for (key, value) in CHAIN_STATE.iter().zip(values) {match value {@@ -74,7 +75,7 @@ async fn open_chain(ctx: &mut Ctx, encrypted_account_key: &str, encrypted_vek: &}/// Open the chain of a server vault-key response with `unlock_key`.-async fn open_server_chain(ctx: &mut Ctx, vault_key: &VaultKeyResponse, unlock_key: &str) -> SyncResult<SecretString> {+pub(super) async fn open_server_chain(ctx: &mut Ctx, vault_key: &VaultKeyResponse, unlock_key: &str) -> SyncResult<SecretString> {ensure_known_unlock_algorithm(vault_key)?;let vek = open_chain(ctx, &vault_key.encrypted_account_key, vault_key.encrypted_vek.as_deref().unwrap_or_default(), vault_key.encrypted_account_private_key.as_deref(), unlock_key).await?;if let (Some(public_key), Some(private_key)) = (&vault_key.account_public_key, &ctx.account_private_key) {@@ -135,47 +136,14 @@ pub(crate) async fn resolve_vault_key(ctx: &mut Ctx) -> SyncResult<bool> {}}-/// Accept a key hierarchy another device created since this device logged in; false when the session key does not open it.-pub(crate) async fn accept_hierarchy_created_elsewhere(ctx: &mut Ctx) -> SyncResult<bool> {- // LEGACY: only a device that logged in to a sqlite-blob account, before another device upgraded it, gets here.- // TODO: remove once every account has upgraded.- if ctx.vault_key_probed {- return Ok(true);- }- let session_key = ctx.encryption_key()?;- // A failed probe fails the run: assuming "no hierarchy" would send a migration write that creates a second one.- let fetched = fetch_vault_key(&ctx.host).await?;- ctx.vault_key_probed = true;- let vault_key = match fetched {- Some(vault_key) if vault_key.encrypted_vek.is_some() => vault_key,- _ => return Ok(true),- };- let vek = match open_server_chain(ctx, &vault_key, session_key.expose_secret()).await {- Ok(vek) => vek,- Err(SyncError::UnlockKeyRejected) => {- ctx.warn("[Keys] The session key does not open the key hierarchy the server holds; a re-login is needed.").await;- return Ok(false);- }- Err(error) => return Err(error),- };- // The chain is cached before the vault is stored under the VEK: hosts hold the unlock key and derive the vault key from it.- cache_vault_key_blobs(&ctx.host, &vault_key).await?;- if let Some(encrypted_vault) = state::load_vault(&ctx.host).await? {- let plaintext = state::decrypt_vault_blob(&encrypted_vault, session_key.expose_secret())?;- state::store_vault_with_key(&ctx.host, &state::encrypt_vault_blob(&plaintext, vek.expose_secret())?, false, None, None, Some(vek.clone())).await?;- }- re_encrypt_shared_manifest_records(ctx, vek.expose_secret()).await?;- ctx.set_encryption_key(vek);- ctx.log("[Keys] Another device created the account's key hierarchy; accepted it and swapped the session key to the VEK.").await;- Ok(true)-}-/// Whether this device holds the key chain, after accepting one another device created; `KeyOutOfSync` when the session key does not open it.pub(crate) async fn ensure_key_chain_accepted(ctx: &mut Ctx) -> SyncResult<bool> {if has_cached_key_chain(&ctx.host).await? {return Ok(true);}- if !accept_hierarchy_created_elsewhere(ctx).await? {+ // LEGACY: only an account upgraded on another device since this one logged in has no chain cached here.+ // This can be removed once all accounts are on 0.31.0+ manifest-v1 storage.+ if !legacy::accept_hierarchy_created_elsewhere(ctx).await? {return Err(SyncError::KeyOutOfSync);}has_cached_key_chain(&ctx.host).awaitdiff --git a/core/rust/src/vault_sync/legacy.rs b/core/rust/src/vault_sync/legacy.rsindex 61919407d..ff7815a5b 100644--- a/core/rust/src/vault_sync/legacy.rs+++ b/core/rust/src/vault_sync/legacy.rs@@ -1,12 +1,11 @@//! LEGACY: what old sqlite-blob accounts need, including their one-time upgrade from sqlite-blob to manifest-v1-//! ([`upgrade_account_to_manifest_v1`]). TODO: remove this module once every account has upgraded, together with the-//! `Snapshot::LegacySqliteBlob` arm in `pull.rs` and `merge.rs`, `db::schema_state`'s frozen chain check, the branch-//! without a key chain in `keys::resolve_vault_key`, `keys::accept_hierarchy_created_elsewhere`, the branch without a-//! vault key in `migration::migrate_manifest`, the upgrade-elsewhere check in `migration::migration_status` and the-//! unsigned approval in `sharing::approve_mobile_login`.+//! ([`upgrade_account_to_manifest_v1`]). An account registered on 0.31.0 or later has a key chain from the start and+//! never reaches this module. TODO: remove it once every account has upgraded, together with the call sites labelled+//! `LEGACY:` in the other modules.use std::collections::HashMap;+use secrecy::{ExposeSecret, SecretString};use serde_json::Value;use super::db::{self, SchemaState};@@ -18,6 +17,7 @@ use super::push::{resolve_personal_manifest_id, WriteKind};use super::state::{self, Ctx};use super::types::{Db, GetResponse};use crate::common::encoding::base64_decode;+use crate::crypto;use crate::sqlite_host::SqlStatement;use crate::vault_codec::row::inline_bytes;@@ -88,6 +88,31 @@ pub(crate) async fn accept_upgrade_done_elsewhere(ctx: &mut Ctx) -> SyncResult<bOk(true)}+/// The key hierarchy the upgrade push creates, with its KEK derived from the unlock key; `None` for any other write.+pub(crate) async fn start_account_key_migration(ctx: &Ctx, unlock_key: &str, kind: WriteKind) -> SyncResult<Option<crypto::AccountKeyHierarchy>> {+ if kind != WriteKind::AccountKeyMigration {+ return Ok(None);+ }+ let hierarchy = crypto::create_account_key_hierarchy(unlock_key)?;+ ctx.log("[Push] Account-key migration: generated new VEK, AK, account keypair and signing keypair; vault content and all blobs will be re-encrypted and re-uploaded.").await;+ Ok(Some(hierarchy))+}++/// Store the hierarchy the upgrade push just committed: cache the encrypted chain and stage the private keys.+pub(crate) async fn complete_account_key_migration(ctx: &mut Ctx, migration: &crypto::AccountKeyHierarchy) -> SyncResult<()> {+ let blobs = &migration.account_keys;+ state::set(&ctx.host, state::ENCRYPTED_ACCOUNT_KEY, &blobs.encrypted_account_key).await?;+ state::set(&ctx.host, state::ENCRYPTED_VEK, &blobs.encrypted_vek).await?;+ state::set(&ctx.host, state::ACCOUNT_PUBLIC_KEY, &blobs.account_public_key).await?;+ state::set(&ctx.host, state::ENCRYPTED_ACCOUNT_PRIVATE_KEY, &blobs.encrypted_account_private_key).await?;+ state::set(&ctx.host, state::SIGNING_PUBLIC_KEY, &blobs.signing_public_key).await?;+ state::set(&ctx.host, state::ENCRYPTED_SIGNING_PRIVATE_KEY, &blobs.encrypted_signing_private_key).await?;+ ctx.account_public_key = Some(blobs.account_public_key.clone());+ ctx.account_private_key = Some(migration.account_private_key.as_str().into());+ ctx.signing_private_key = Some(migration.signing_private_key.as_str().into());+ Ok(())+}+/// Pull the personal manifest id and revision baseline the upgrade push needs, when a pre-manifest session lacks them.async fn record_server_baseline_if_missing(ctx: &mut Ctx) -> SyncResult<()> {if state::get::<String>(&ctx.host, state::VAULT_PERSONAL_MANIFEST_ID).await?.is_some() {@@ -106,6 +131,54 @@ async fn record_server_baseline_if_missing(ctx: &mut Ctx) -> SyncResult<()> {pull::commit_revisions(ctx, &pulled).await}+/*+ * An upgrade done on another device: this device logged in while the account was still sqlite-blob, so it holds the+ * unlock key as its session key and no cached key chain.+ */++/// Accept the key chain another device created since this device logged in; false when the session key does not open it.+pub(crate) async fn accept_hierarchy_created_elsewhere(ctx: &mut Ctx) -> SyncResult<bool> {+ if ctx.vault_key_probed {+ return Ok(true);+ }+ let unlock_key = ctx.encryption_key()?;+ // A failed probe fails the run: assuming "no hierarchy" would send a migration write that creates a second one.+ let Some(vault_key) = keys::fetch_vault_key(&ctx.host).await?.filter(|vault_key| vault_key.encrypted_vek.is_some()) else {+ ctx.vault_key_probed = true;+ return Ok(true);+ };+ let vek = match keys::open_server_chain(ctx, &vault_key, unlock_key.expose_secret()).await {+ Ok(vek) => vek,+ Err(SyncError::UnlockKeyRejected) => {+ ctx.warn("[Keys] The session key does not open the key hierarchy the server holds; a re-login is needed.").await;+ ctx.vault_key_probed = true;+ return Ok(false);+ }+ Err(error) => return Err(error),+ };+ // Read before anything is cached: a stored vault the unlock key does not open fails the run here, and the sync's retry drops it and accepts again.+ let plaintext = stored_vault_plaintext(ctx, &unlock_key).await?;+ ctx.vault_key_probed = true;+ // The chain is cached before the vault is stored under the VEK: hosts hold the unlock key and derive the vault key from it.+ keys::cache_vault_key_blobs(&ctx.host, &vault_key).await?;+ if let Some(plaintext) = plaintext {+ state::store_vault_with_key(&ctx.host, &state::encrypt_vault_blob(&plaintext, vek.expose_secret())?, false, None, None, Some(vek.clone())).await?;+ }+ keys::re_encrypt_shared_manifest_records(ctx, vek.expose_secret()).await?;+ ctx.set_encryption_key(vek);+ ctx.log("[Keys] Another device created the account's key hierarchy; accepted it and swapped the session key to the VEK.").await;+ Ok(true)+}++/// The stored vault decrypted with the unlock key; `None` without a stored vault or after the sync dropped it.+async fn stored_vault_plaintext(ctx: &mut Ctx, unlock_key: &SecretString) -> SyncResult<Option<Vec<u8>>> {+ if !ctx.has_local_vault().await? {+ return Ok(None);+ }+ let Some(encrypted_vault) = state::load_vault(&ctx.host).await? else { return Ok(None) };+ Ok(Some(state::decrypt_vault_blob(&encrypted_vault, unlock_key.expose_secret())?))+}+/// Decode the base64 TEXT the 0.30.x merges wrote into BLOB columns back to bytes, in place; text that is not base64 is left as-is.async fn decode_base64_text_in_blob_columns(ctx: &Ctx) -> SyncResult<()> {let mut decoded = 0usize;diff --git a/core/rust/src/vault_sync/push.rs b/core/rust/src/vault_sync/push.rsindex c6950f5cd..0eeefddd5 100644--- a/core/rust/src/vault_sync/push.rs+++ b/core/rust/src/vault_sync/push.rs@@ -13,7 +13,7 @@ use super::state::{self, Ctx};use super::types::{BlobDto, BlobHashesRequest, BlobRef, BlobUploadRequest, BucketRevision, BucketWrite, Db, ManifestRevision, ManifestWrite, MissingBlobsResponse, VaultWriteMigration, VaultWriteRequest, VaultWriteResponse, VaultWriteStatus, ALGORITHM_RSA_OAEP_SHA256};use super::blob_keys::{self, EncryptedBlob};use super::write_set::{self, ManifestRecord, SkipReason};-use super::{db, http, keys};+use super::{db, http, keys, legacy};use secrecy::{ExposeSecret, SecretString};use crate::crypto;use crate::vault_codec::{self, BlobEntry, CanonicalizeInput, CanonicalizedVault, DataBucket, Manifest, ManifestSpec};@@ -31,7 +31,7 @@ pub(crate) enum WriteKind {Changes { buckets_only: bool },/// Every manifest and bucket, whatever the fingerprints say: the server never saw this vault.Everything,- /// The one-time account upgrade, which creates the key hierarchy and writes the whole vault under it.+ /// LEGACY: the one-time account upgrade, which creates the key hierarchy and writes the whole vault under it.AccountKeyMigration,}@@ -164,6 +164,7 @@ pub(crate) async fn vault_holds_unwritable_manifests(ctx: &Ctx) -> bool {/// Upload the stored vault; an account without a key hierarchy gets the migration write whatever `kind` asked for.pub(crate) async fn upload_vault(ctx: &mut Ctx, cache: Option<(u64, CanonicalizedSet)>, kind: WriteKind) -> SyncResult<UploadOutcome> {let mutation_seq_at_start = ctx.mutation_sequence;+ // LEGACY: an account without a key chain is still on sqlite-blob, so its first write is the account upgrade.let kind = if keys::ensure_key_chain_accepted(ctx).await? { kind } else { WriteKind::AccountKeyMigration };if kind == (WriteKind::Changes { buckets_only: true }) {@@ -256,7 +257,7 @@ async fn write_whole_vault(ctx: &mut Ctx, cached: Option<CanonicalizedSet>, kindlet (status, new_key) = http::with_outdated_server_guard(write_manifests_and_buckets(ctx, cached, kind).await)?;if let Some(new_key) = new_key {- // Migration succeeded: from now on the session key is the VEK, not the password-derived key.+ // LEGACY: the account upgrade succeeded; from now on the session key is the VEK, not the unlock key.keys::re_encrypt_shared_manifest_records(ctx, new_key.expose_secret()).await?;ctx.set_encryption_key(new_key);}@@ -394,7 +395,8 @@ async fn write_manifests_and_buckets(ctx: &mut Ctx, cached: Option<Canonicalizedreturn Ok((PushStatus::Outdated, None));}- let migration = start_account_key_migration(ctx, vek.expose_secret(), kind).await?;+ // LEGACY: the one-time account upgrade creates the key hierarchy and writes the whole vault under its VEK.+ let migration = legacy::start_account_key_migration(ctx, vek.expose_secret(), kind).await?;let content_key = migration.as_ref().map(|m| SecretString::from(m.vault_encryption_key.as_str())).unwrap_or_else(|| vek.clone());let set = match cached {@@ -442,22 +444,12 @@ async fn write_manifests_and_buckets(ctx: &mut Ctx, cached: Option<Canonicalizedcommit_push_baselines(ctx, baselines, &response.manifest_revisions, &response.bucket_revisions, written).await?;commit_blob_baselines(ctx, &blobs, &uploaded).await?;if let Some(migration) = &migration {- complete_account_key_migration(ctx, migration).await?;+ legacy::complete_account_key_migration(ctx, migration).await?;ctx.log("[Push] Account-key migration complete: hierarchy created server-side, blob chain cached locally.").await;}Ok((PushStatus::Ok, migration.map(|_| content_key)))}-/// The key hierarchy a migration push creates, with its KEK derived from the legacy session key (the unlock key).-async fn start_account_key_migration(ctx: &Ctx, unlock_key: &str, kind: WriteKind) -> SyncResult<Option<crypto::AccountKeyHierarchy>> {- if kind != WriteKind::AccountKeyMigration {- return Ok(None);- }- let hierarchy = crypto::create_account_key_hierarchy(unlock_key)?;- ctx.log("[Push] Account-key migration: generated new VEK, AK, account keypair and signing keypair; vault content and all blobs will be re-encrypted and re-uploaded.").await;- Ok(Some(hierarchy))-}-/// Every canonicalized manifest a record exists for, with the key it encrypts under.fn collect_candidates<'a>(canonicalized: &'a CanonicalizedVault, records: &'a [ManifestRecord], content_key: &SecretString, baselines: &PushBaselines) -> Vec<Candidate<'a>> {let record_by_id: HashMap<&str, &ManifestRecord> = records.iter().map(|r| (r.manifest_id.as_str(), r)).collect();@@ -660,21 +652,6 @@ async fn commit_blob_baselines(ctx: &Ctx, blobs: &UploadBlobs, uploaded: &HashMastate::set(&ctx.host, state::VAULT_BLOB_CIPHER_CACHE, &new_cache).await}-/// Store the hierarchy a migration push just committed: cache the encrypted chain and stage the private keys.-async fn complete_account_key_migration(ctx: &mut Ctx, migration: &crypto::AccountKeyHierarchy) -> SyncResult<()> {- let blobs = &migration.account_keys;- state::set(&ctx.host, state::ENCRYPTED_ACCOUNT_KEY, &blobs.encrypted_account_key).await?;- state::set(&ctx.host, state::ENCRYPTED_VEK, &blobs.encrypted_vek).await?;- state::set(&ctx.host, state::ACCOUNT_PUBLIC_KEY, &blobs.account_public_key).await?;- state::set(&ctx.host, state::ENCRYPTED_ACCOUNT_PRIVATE_KEY, &blobs.encrypted_account_private_key).await?;- state::set(&ctx.host, state::SIGNING_PUBLIC_KEY, &blobs.signing_public_key).await?;- state::set(&ctx.host, state::ENCRYPTED_SIGNING_PRIVATE_KEY, &blobs.encrypted_signing_private_key).await?;- ctx.account_public_key = Some(blobs.account_public_key.clone());- ctx.account_private_key = Some(migration.account_private_key.as_str().into());- ctx.signing_private_key = Some(migration.signing_private_key.as_str().into());- Ok(())-}-/// Encrypt and upload the given blobs in batches per owning manifest; returns hash to ciphertext.async fn upload_blobs(ctx: &Ctx, blobs: &UploadBlobs, hashes: &[String], overwrite: bool) -> SyncResult<HashMap<String, EncryptedBlob>> {let mut encrypted_blobs = HashMap::new();diff --git a/core/rust/src/vault_sync/state.rs b/core/rust/src/vault_sync/state.rsindex f193ee0d5..aa2aabea4 100644--- a/core/rust/src/vault_sync/state.rs+++ b/core/rust/src/vault_sync/state.rs@@ -29,6 +29,7 @@ pub(crate) struct Ctx {pub new_encryption_key: Option<SecretString>,pub vault_changed: bool,pub schema: Option<SchemaInfo>,+ /// LEGACY: whether this run already asked the server for a key chain this device has not cached.pub vault_key_probed: bool,pub served_manifest_names: HashMap<String, String>,has_local_vault: bool,@@ -107,7 +108,7 @@ impl Ctx {/// Report a failed local database command as `VaultDecryptFailed` when the stored vault does not decrypt.pub async fn diagnose_local_vault_failure(&self, error: SyncError) -> SyncError {- if !matches!(error, SyncError::Host { command: "dbQuery" | "dbExec" | "dbExport", .. }) {+ if self.local_vault_discarded || !matches!(error, SyncError::Host { command: "dbQuery" | "dbExec" | "dbExport", .. }) {return error;}let (Some(key), Ok(Some(blob))) = (self.encryption_key.as_ref(), load_vault(&self.host).await) else { return error };diff --git a/core/rust/src/vault_sync/types.rs b/core/rust/src/vault_sync/types.rsindex d8b7f7e0e..5f7f53cff 100644--- a/core/rust/src/vault_sync/types.rs+++ b/core/rust/src/vault_sync/types.rs@@ -174,6 +174,7 @@ pub struct FullSyncResult {pub success: bool,pub has_new_vault: bool,pub was_offline: bool,+ /// LEGACY: the local vault is still on the frozen sqlite-blob upgrade chain, which the host walks first.pub sqlite_blob_upgrade_required: bool,pub manifest_migration_required: bool,#[serde(flatten)]