Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Fix same-time client legacy migration race condition bug
Leendert de Borst2d4f47a2218a
diff --git a/core/rust/src/vault_sync/legacy.rs b/core/rust/src/vault_sync/legacy.rsindex de70bd695..f76474272 100644--- a/core/rust/src/vault_sync/legacy.rs+++ b/core/rust/src/vault_sync/legacy.rs@@ -1,7 +1,7 @@//! LEGACY: what old sqlite-blob accounts need, including their one-time upgrade from sqlite-blob to manifest-v1//! ([`upgrade_account_to_manifest_v1`]). 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 and the-//! branch without a vault key in `migration::migrate_manifest`.+//! `Snapshot::LegacySqliteBlob` arm in `pull.rs` and `merge.rs`, `db::schema_state`'s frozen chain check, the+//! branch without a vault key in `migration::migrate_manifest` and the upgrade-elsewhere check in `migration::migration_status`.use std::collections::HashMap;@@ -47,14 +47,15 @@ const BLOB_TYPED_COLUMNS: &str = "SELECT m.name AS TableName, p.name AS ColumnNa/// rows stamped with the personal manifest, and the push that carries it creates the account key hierarchy. Returns/// whether that push reached the server.pub(crate) async fn upgrade_account_to_manifest_v1(ctx: &mut Ctx) -> SyncResult<bool> {+ if accept_upgrade_done_elsewhere(ctx).await? {+ return migration::migrate_schema(ctx).await;+ }if migration::schema_state(ctx).await? == SchemaState::LegacyChain {return Err(SyncError::LegacyUpgradePending);}- // Another device may have created the hierarchy since this one logged in. Accepting it swaps the session key to the VEK, which the baseline pull below needs.- keys::ensure_key_chain_accepted(ctx).await?;record_server_baseline_if_missing(ctx).await?;if keys::has_cached_key_chain(&ctx.host).await? {- // The account turned out to be upgraded already (accepted above, or pulled with the baseline); a schema rebuild is all that can remain.+ // The baseline pull turned up an account that is upgraded already; a schema rebuild is all that can remain.return migration::migrate_schema(ctx).await;}if migration::schema_state(ctx).await? == SchemaState::LegacyChain {@@ -67,6 +68,24 @@ pub(crate) async fn upgrade_account_to_manifest_v1(ctx: &mut Ctx) -> SyncResult<migration::push_migrated_vault(ctx, WriteKind::AccountKeyMigration).await}+/// Accept a key hierarchy another device created since this session logged in, and replace the local sqlite-blob vault+/// with the server's: that device already upgraded the account, and the local rows that name no manifest cannot be+/// canonicalized. Returns whether that happened. Call only while no key chain is cached.+pub(crate) async fn accept_upgrade_done_elsewhere(ctx: &mut Ctx) -> SyncResult<bool> {+ // Accepting swaps the session key to the VEK, which the pull below needs.+ if !keys::ensure_key_chain_accepted(ctx).await? {+ return Ok(false);+ }+ ctx.warn("[Migration] Another device already upgraded this account; replacing the local sqlite-blob vault with the server's.").await;+ let pulled = pull::pull(ctx).await?;+ if !ctx.store_vault(&pulled.encrypted_vault, false, Some(ctx.mutation_sequence), Some(pulled.personal_revision)).await?.success {+ return Err(SyncError::Other("a mutation raced the pull of the upgraded vault; run the migration again".to_string()));+ }+ ctx.vault_changed = true;+ pull::commit_revisions(ctx, &pulled).await?;+ Ok(true)+}+/// 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() {diff --git a/core/rust/src/vault_sync/migration.rs b/core/rust/src/vault_sync/migration.rsindex c48ee6341..ea563e0bb 100644--- a/core/rust/src/vault_sync/migration.rs+++ b/core/rust/src/vault_sync/migration.rs@@ -50,18 +50,14 @@ pub(crate) async fn pending_migration_result(ctx: &mut Ctx) -> SyncResult<Option/// Classify the pending migration so the upgrade gate knows whether it may run on its own.pub(crate) async fn migration_status(ctx: &mut Ctx) -> MigrationStatusResult {let classified: SyncResult<MigrationKind> = async {- let schema = schema_state(ctx).await?;- if schema == SchemaState::LegacyChain {+ if schema_state(ctx).await? == SchemaState::LegacyChain {return Ok(MigrationKind::None);}- if !keys::has_cached_key_chain(&ctx.host).await? {- // A hierarchy another device created since this device logged in classifies as no migration at all.- keys::accept_hierarchy_created_elsewhere(ctx).await?;- }- if !keys::has_cached_key_chain(&ctx.host).await? {+ if !keys::has_cached_key_chain(&ctx.host).await? && !legacy::accept_upgrade_done_elsewhere(ctx).await? {return Ok(MigrationKind::StorageFormatUpgrade);}- Ok(if schema == SchemaState::Stale { MigrationKind::SchemaRebuild } else { MigrationKind::None })+ // Read again: accepting an upgrade done elsewhere replaced the local vault with the server's.+ Ok(if schema_state(ctx).await? == SchemaState::Stale { MigrationKind::SchemaRebuild } else { MigrationKind::None })}.await;let kind = match classified {diff --git a/core/rust/src/vault_sync/tests/migration.rs b/core/rust/src/vault_sync/tests/migration.rsindex a8fcbb6e1..11f5a5b0f 100644--- a/core/rust/src/vault_sync/tests/migration.rs+++ b/core/rust/src/vault_sync/tests/migration.rs@@ -118,6 +118,40 @@ fn schema_rebuild_of_a_stale_vault_pushes_without_touching_the_key_hierarchy() {assert_eq!(host.vault_key, vek, "the session key stays the VEK");}+/// Two legacy sessions of one account: the first upgrades the account, the second still holds its sqlite-blob vault+/// with a row that names no manifest, which the schema rebuild refuses.+fn session_of_an_account_upgraded_elsewhere() -> TestHost {+ let unlock_key = crypto::generate_key_base64();+ let server = FakeServer::new();+ server.borrow_mut().publish_contentless_personal(3);+ let mut upgraded = legacy_device(&unlock_key, &server, |db| insert_item(db, ITEM_A, "Upgraded item"));+ assert_eq!(upgraded.run("migrateManifest")["pushed"], true);+ legacy_device(&unlock_key, &server, |db| {+ insert_item(db, ITEM_B, "Stale item");+ db.execute("INSERT INTO EncryptionKeys (Id, ManifestId, PublicKey, PrivateKey, IsPrimary, CreatedAt, UpdatedAt, IsDeleted) VALUES ('dddddddd-0000-4000-8000-000000000002', '', 'public', 'private', 1, ?, ?, 0)", rusqlite::params![super::now(), super::now()]).unwrap();+ })+}++#[test]+fn migration_status_of_an_account_upgraded_elsewhere_takes_the_server_vault() {+ let mut host = session_of_an_account_upgraded_elsewhere();++ assert_eq!(host.run("migrationStatus")["kind"], "none", "{:?}", host.logs);+ assert_eq!(host.item_names(), vec!["Upgraded item"]);+ assert!(host.state.contains_key(state::ENCRYPTED_ACCOUNT_KEY));+}++#[test]+fn manifest_migration_of_an_account_upgraded_elsewhere_takes_the_server_vault() {+ let mut host = session_of_an_account_upgraded_elsewhere();++ let result = host.run("migrateManifest");++ assert_eq!(result["success"], true, "{} {:?}", result, host.logs);+ assert_eq!(host.item_names(), vec!["Upgraded item"], "the local sqlite-blob vault is replaced, not rebuilt");+ assert!(host.vault_writes().is_empty(), "nothing is pushed over the upgrade the other device made");+}+/// A device as a client predating the manifest storage format leaves it: a sqlite blob under the unlock key, no personal/// manifest id and no revision baseline, against a server that still holds the account as a legacy vault.fn pre_format_session(unlock_key: &str, dirty: bool) -> TestHost {