Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Update merge logic to use server revision as base (#2159)

Leendert de Borstb53f16e95bbc
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
index d59747ff0..040bd0024 100644
--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
@@ -10,7 +10,7 @@ import type { VaultResponse, VaultPostResponse, StatusResponseV2, ManifestRevisi
import { EncryptionUtility } from '@/utils/EncryptionUtility';
import { LocalPreferencesService } from '@/utils/LocalPreferencesService';
import { RecentlySelectedItemService } from '@/utils/RecentlySelectedItemService';
-import { filterItems, AutofillMatchingMode, extractRootDomain, isUrlAlreadyLinked, generatePassword, vaultCodecExtractBucket, vaultCodecBucketLayout } from '@/utils/RustCore';
+import { filterItems, AutofillMatchingMode, extractRootDomain, isUrlAlreadyLinked, generatePassword, vaultCodecExtractBucket, vaultCodecBucketLayout, vaultCodecOverflowTable } from '@/utils/RustCore';
import { ServiceDetectionUtility } from '@/utils/serviceDetection/ServiceDetectionUtility';
import { SqliteClient } from '@/utils/SqliteClient';
import { getItemWithFallback } from '@/utils/StorageUtility';
@@ -34,7 +34,7 @@ import { VaultUploadResponse as messageVaultUploadResponse } from '@/utils/types
import { type VaultMutationScope, ALL_VAULT_MUTATION_SCOPES, DEFAULT_VAULT_MUTATION_SCOPE, dirtyScopeStorageKey, isManifestScope } from '@/utils/types/VaultMutationScope';
import { VaultCodec } from '@/utils/VaultCodec';
import { vaultMergeService } from '@/utils/VaultMergeService';
-import { vaultSyncService, SERVER_MANIFEST_REVISIONS_STORAGE_KEY, CODEC_OVERFLOW_STORAGE_KEY } from '@/utils/VaultSyncService';
+import { vaultSyncService, SERVER_MANIFEST_REVISIONS_STORAGE_KEY } from '@/utils/VaultSyncService';
import { WebApiService } from '@/utils/WebApiService';
import { t } from '@/i18n/StandaloneI18n';
@@ -440,7 +440,6 @@ export async function handleClearVaultData(): Promise<messageBoolResponse> {
'local:hiddenPrivateEmailDomains',
'local:serverRevision',
SERVER_MANIFEST_REVISIONS_STORAGE_KEY,
- CODEC_OVERFLOW_STORAGE_KEY,
'local:isDirty',
...ALL_VAULT_MUTATION_SCOPES.map(scope => dirtyScopeStorageKey(scope)),
'local:mutationSequence',
@@ -821,9 +820,12 @@ async function uploadDirtyBucketsOnly(sqliteClient: SqliteClient, scopes: VaultM
return (await uploadNewVaultToServer(sqliteClient)).response;
}
- const tables = VaultCodec.readNamedTables(sqliteClient, spec.tables);
- // Overflow re-merges a newer client's bucket tables/columns so this partial push doesn't drop them.
- const bucket = await vaultCodecExtractBucket(category, tables, await vaultSyncService.loadOverflow(encryptionKey));
+ /*
+ * Read the CodecOverflows carrier row alongside the bucket's tables: extract_bucket consumes it
+ * to re-merge a newer client's bucket tables/columns, so this partial push doesn't drop them.
+ */
+ const tables = VaultCodec.readNamedTables(sqliteClient, [...spec.tables, await vaultCodecOverflowTable()]);
+ const bucket = await vaultCodecExtractBucket(category, tables);
const result = await vaultSyncService.pushDataBucketOnly(bucket, encryptionKey);
if (result.status !== 'ok') {
// Conflict persisted even after the rebase-retry, let the caller run a full re-sync (status 2).
diff --git a/apps/browser-extension/src/utils/RustCore.ts b/apps/browser-extension/src/utils/RustCore.ts
index 0de7369b9..50ccff009 100644
--- a/apps/browser-extension/src/utils/RustCore.ts
+++ b/apps/browser-extension/src/utils/RustCore.ts
@@ -215,8 +215,10 @@ export type CodecCanonicalized = {
/**
* Data a newer writer put in the manifest that this client's local SQLite schema cannot hold:
* whole unknown manifest tables, whole unknown bucket tables (per category), and unknown columns
- * keyed by table > row primary-key value. Produced by materialize, persisted opaquely by the
- * platform, and fed back into canonicalize / extractBucket so a push never drops the data.
+ * keyed by table > row primary-key value. Materialize carries it INSIDE the vault DB as a regular
+ * `CodecOverflows` table row, and canonicalize/extractBucket consume that row from the ordinary
+ * table read — so a push never drops the data and no separate persistence is needed. This type only
+ * describes the diagnostics copy on `CodecMaterialized` (used for logging).
*/
export type CodecOverflow = {
tables: Record<string, Array<Record<string, unknown>>>;
@@ -231,10 +233,9 @@ export type CodecCanonicalizeInput = {
migrationId: string;
version: string;
canonicalizedAt: string;
- overflow?: CodecOverflow;
};
-/** Materialized tables the platform inserts into a fresh SQLite DB. */
+/** Materialized tables the platform inserts into a fresh SQLite DB (`overflow` is a diagnostics copy). */
export type CodecMaterialized = { tables: CodecTableData[]; migrationId: string; overflow: CodecOverflow };
/** One entry in the bucket layout: a category and the tables it owns. */
@@ -254,8 +255,8 @@ export async function vaultCodecCanonicalizeFromSqlite(input: CodecCanonicalizeI
/**
* Materialize the manifest + its data buckets into the table set the platform inserts.
* `schemaColumns` (table > column names of the local schema) makes Rust split anything the schema
- * can't hold into `overflow` instead of emitting it; the caller must persist that overflow and pass
- * it back into the next canonicalize so the data survives the round trip.
+ * can't hold into the `CodecOverflows` carrier row (included in the returned tables) instead of
+ * emitting it, so unknown newer-client data survives the round trip inside the vault DB itself.
*/
export async function vaultCodecMaterializeAsSqlite(manifest: CodecManifest, dataBuckets: CodecDataBucket[], schemaColumns?: Record<string, string[]>): Promise<CodecMaterialized> {
await initRustCore();
@@ -263,12 +264,21 @@ export async function vaultCodecMaterializeAsSqlite(manifest: CodecManifest, dat
}
/**
- * Build a single data bucket for `category` from its tables (bucket-only push path). `overflow`
- * (persisted from the last materialize) re-merges a newer writer's columns/tables so they survive.
+ * Build a single data bucket for `category` from its tables (bucket-only push path). Include the
+ * `CodecOverflows` table (see {@link vaultCodecOverflowTable}) in `tables` so a newer writer's
+ * columns/tables re-merge and survive; it is consumed and never emitted into the bucket.
*/
-export async function vaultCodecExtractBucket(category: string, tables: Record<string, Array<Record<string, unknown>>>, overflow?: CodecOverflow): Promise<CodecDataBucket> {
+export async function vaultCodecExtractBucket(category: string, tables: Record<string, Array<Record<string, unknown>>>): Promise<CodecDataBucket> {
await initRustCore();
- return core.vaultCodecExtractBucket({ category, tables, overflow }) as CodecDataBucket;
+ return core.vaultCodecExtractBucket({ category, tables }) as CodecDataBucket;
+}
+
+/**
+ * The name of the client-local SQLite table that carries the codec overflow inside the vault DB.
+ */
+export async function vaultCodecOverflowTable(): Promise<string> {
+ await initRustCore();
+ return core.vaultCodecOverflowTable();
}
/**
diff --git a/apps/browser-extension/src/utils/VaultMergeService.ts b/apps/browser-extension/src/utils/VaultMergeService.ts
index fc9189900..1ec872aab 100644
--- a/apps/browser-extension/src/utils/VaultMergeService.ts
+++ b/apps/browser-extension/src/utils/VaultMergeService.ts
@@ -153,12 +153,14 @@ export class VaultMergeService {
/**
* Merge local vault changes with server vault using LWW strategy.
*
- * Uses Rust WASM for the merge logic:
+ * Uses Rust WASM for the merge logic. The merge base is the SERVER vault (it is freshly
+ * materialized with the newest schema and the newest codec overflow carrier), and the local
+ * vault's winning changes are applied on top of it:
* 1. Load both SQLite databases with sql.js
- * 2. Read all tables as JSON
- * 3. Call Rust merge (returns SQL statements)
- * 4. Execute SQL statements on local database
- * 5. Export merged database
+ * 2. Read the syncable tables from both as JSON
+ * 3. Call Rust merge (returns SQL statements that bring local changes onto the server base)
+ * 4. Execute SQL statements on the SERVER database
+ * 5. Export the merged (server-based) database - the codec overflow carrier rides along untouched
*
* @param localVaultBase64 - The local vault (with offline changes) as base64 SQLite
* @param serverVaultBase64 - The server vault (latest version) as base64 SQLite
@@ -208,15 +210,16 @@ export class VaultMergeService {
const mergeOutput = mergeVaults(mergeInput) as MergeOutput;
- // Execute SQL statements from Rust on local database
+ // Execute SQL statements from Rust on the SERVER database (the merge base). The exported
+ // server DB carries the newest codec overflow carrier untouched.
for (const stmt of mergeOutput.statements) {
// Convert undefined to null for sql.js (serde-wasm-bindgen may convert null to undefined)
const sanitizedParams = stmt.params.map(p => p === undefined ? null : p);
- localDb.run(stmt.sql, sanitizedParams);
+ serverDb.run(stmt.sql, sanitizedParams);
}
- // Export the merged database
- const mergedVaultBase64 = this.exportDatabase(localDb);
+ // Export the merged (server-based) database
+ const mergedVaultBase64 = this.exportDatabase(serverDb);
return {
success: mergeOutput.success,
@@ -363,6 +366,10 @@ export class VaultMergeService {
* @returns Array of records as JSON objects
*/
private readTableAsJson(db: Database, tableName: string): JsonRecord[] {
+ const exists = this.readQueryAsJson(db, `SELECT name FROM sqlite_master WHERE type='table' AND name='${tableName}'`).length > 0;
+ if (!exists) {
+ return [];
+ }
return this.readQueryAsJson(db, `SELECT * FROM ${tableName}`);
}
diff --git a/apps/browser-extension/src/utils/VaultSyncService.ts b/apps/browser-extension/src/utils/VaultSyncService.ts
index f4847b49c..441134c1e 100644
--- a/apps/browser-extension/src/utils/VaultSyncService.ts
+++ b/apps/browser-extension/src/utils/VaultSyncService.ts
@@ -11,7 +11,7 @@ import { VaultDataBucketCategory } from '@/utils/dist/core/models/vault';
import type { VaultResponse } from '@/utils/dist/core/models/webapi';
import { VaultSqlGenerator } from '@/utils/dist/core/vault';
import { EncryptionUtility } from '@/utils/EncryptionUtility';
-import {type CodecOverflow, vaultCodecComputeCiphertextHash, vaultCodecCanonicalizeFromSqlite, vaultCodecGenerateUserSalt, vaultCodecUnpackPayload, vaultCodecMaterializeAsSqlite, vaultCodecPackPayload, vaultCodecValidateManifest, vaultCodecValidateDataBucket} from '@/utils/RustCore';
+import {vaultCodecComputeCiphertextHash, vaultCodecCanonicalizeFromSqlite, vaultCodecGenerateUserSalt, vaultCodecUnpackPayload, vaultCodecMaterializeAsSqlite, vaultCodecPackPayload, vaultCodecValidateManifest, vaultCodecValidateDataBucket} from '@/utils/RustCore';
import { SqliteClient } from '@/utils/SqliteClient';
import { ServerUpdateRequiredError } from '@/utils/types/errors/ServerUpdateRequiredError';
import { VaultProcessingError } from '@/utils/types/errors/VaultProcessingError';
@@ -96,11 +96,6 @@ const bucketRevKey = (category: string): `local:${string}` => `local:vaultV2Buck
const BLOB_CACHE_STORAGE_KEY = 'local:vaultV2BlobCipherCache';
/** Hashes the server has stored (refreshed on every pull/push). */
const SERVER_HASHES_STORAGE_KEY = 'local:vaultV2ServerBlobHashes';
-/**
- * Codec overflow persisted between pull and push: a newer client's tables/columns that this client's
- * local SQLite schema can't hold.
- */
-export const CODEC_OVERFLOW_STORAGE_KEY = 'local:vaultV2CodecOverflow';
/**
* The client's last-known revision per non-Main manifest (manifestId > revision), refreshed on every materialize.
*/
@@ -450,11 +445,10 @@ export class VaultSyncService {
const materialized = await vaultCodecMaterializeAsSqlite(manifest, dataBuckets, schemaColumns);
/*
- * Anything a newer client wrote that our schema can't hold was split into overflow instead of the
- * insert set. Persist it so push/extract re-merge it into the manifest: without this, our next
- * push would silently delete those tables/columns for every other client.
+ * Anything a newer client wrote that our schema can't hold was split off the insert set into the
+ * CodecOverflows carrier row (part of the tables inserted below, so it lives inside the encrypted
+ * vault DB). Canonicalize re-merges it on push, so this client never deletes a newer writer's data.
*/
- await this.saveOverflow(materialized.overflow, vek);
const overflowTableCount = Object.keys(materialized.overflow.tables).length + Object.values(materialized.overflow.bucketTables).reduce((n, t) => n + Object.keys(t).length, 0);
const overflowColumnTables = Object.keys(materialized.overflow.columns);
if (overflowTableCount > 0 || overflowColumnTables.length > 0) {
@@ -532,13 +526,12 @@ export class VaultSyncService {
// Read tables from the SQLite database and apply the manifest-v1 format rules.
const tables = VaultCodec.readTables(sqliteClient);
const migrationId = VaultCodec.getLatestMigrationId(sqliteClient);
- const canonicalized = await timedStage('canonicalize (incl. Rust→JS conversion)', async () => vaultCodecCanonicalizeFromSqlite({
+ const canonicalized = await timedStage('canonicalize (incl. Rust→JS conversion)', () => vaultCodecCanonicalizeFromSqlite({
tables,
userSalt,
migrationId,
version: '2.0.0',
canonicalizedAt: new Date().toISOString(),
- overflow: await this.loadOverflow(vek),
}));
// Plaintext blob bytes held platform-side for encryption/upload.
@@ -821,37 +814,6 @@ export class VaultSyncService {
await storage.setItem(BLOB_CACHE_STORAGE_KEY, cache);
}
- /**
- * Load the codec overflow persisted by the last pull (undefined when none): a newer client's
- * tables/columns our local schema can't hold. Callers pass it into canonicalize/extractBucket so a
- * push from this client re-emits that data instead of dropping it. Stored AES-GCM encrypted; an
- * entry that no longer decrypts (key rotated, account switched) is discarded: the next pull
- * rebuilds it from the server manifest.
- * @param vek - the user's symmetric key
- */
- public async loadOverflow(vek: string): Promise<CodecOverflow | undefined> {
- const ciphertext = (await storage.getItem(CODEC_OVERFLOW_STORAGE_KEY)) as string | null;
- if (!ciphertext) {
- return undefined;
- }
- try {
- return JSON.parse(await EncryptionUtility.symmetricDecrypt(ciphertext, vek)) as CodecOverflow;
- } catch {
- devWarn('[V2Sync] Persisted codec overflow could not be decrypted with the current key, discarding it. It will be rebuilt on the next pull.');
- await storage.removeItem(CODEC_OVERFLOW_STORAGE_KEY);
- return undefined;
- }
- }
-
- /**
- * Persist the codec overflow produced by materialize (rebuilt wholesale on every pull),
- * AES-GCM encrypted so no vault row data sits in extension storage as plaintext.
- * @param overflow - the overflow split off by the last materialize
- * @param vek - the user's symmetric key
- */
- private async saveOverflow(overflow: CodecOverflow, vek: string): Promise<void> {
- await storage.setItem(CODEC_OVERFLOW_STORAGE_KEY, await EncryptionUtility.symmetricEncrypt(JSON.stringify(overflow), vek));
- }
}
export const vaultSyncService = new VaultSyncService();
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/rustcore/VaultMergeService.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/rustcore/VaultMergeService.kt
index e3e568179..4c70e4124 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/rustcore/VaultMergeService.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/rustcore/VaultMergeService.kt
@@ -125,8 +125,10 @@ object VaultMergeService {
)
}
- val localDb = SQLiteDatabase.openDatabase(localDbFile.absolutePath, null, SQLiteDatabase.OPEN_READWRITE)
- val serverDb = SQLiteDatabase.openDatabase(serverDbFile.absolutePath, null, SQLiteDatabase.OPEN_READONLY)
+ // The merge base is the SERVER database (opened read-write so the merge statements land on
+ // it); the local database is only read for its offline changes.
+ val localDb = SQLiteDatabase.openDatabase(localDbFile.absolutePath, null, SQLiteDatabase.OPEN_READONLY)
+ val serverDb = SQLiteDatabase.openDatabase(serverDbFile.absolutePath, null, SQLiteDatabase.OPEN_READWRITE)
try {
// Read all syncable tables
@@ -158,15 +160,16 @@ object VaultMergeService {
val outputJson = uniffi.aliasvault_core.mergeVaultsJson(mergeInput.toString())
val statements = parseStatements(outputJson)
- // Apply SQL statements
- localDb.beginTransaction()
+ // Apply SQL statements to the SERVER database (the merge base). The exported server DB
+ // carries the newest codec overflow carrier untouched.
+ serverDb.beginTransaction()
try {
for ((sql, params) in statements) {
- localDb.execSQL(sql, params.toTypedArray())
+ serverDb.execSQL(sql, params.toTypedArray())
}
- localDb.setTransactionSuccessful()
+ serverDb.setTransactionSuccessful()
} finally {
- localDb.endTransaction()
+ serverDb.endTransaction()
}
localDb.close()
@@ -174,7 +177,7 @@ object VaultMergeService {
// Encrypt and return
// Output format must match input: base64(encrypted(base64(sqlite_bytes)))
- val mergedData = localDbFile.readBytes()
+ val mergedData = serverDbFile.readBytes()
val mergedBase64 = Base64.encodeToString(mergedData, Base64.NO_WRAP)
val encrypted = VaultCrypto.encrypt(mergedBase64.toByteArray(Charsets.UTF_8), encryptionKey)
return Base64.encodeToString(encrypted, Base64.DEFAULT)
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/VaultSql.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/VaultSql.kt
index be8b79731..4ab2d11a5 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/VaultSql.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/VaultSql.kt
@@ -88,16 +88,25 @@ CREATE INDEX "IX_Credentials_ServiceId" ON "Credentials" ("ServiceId");
CREATE INDEX "IX_Passwords_CredentialId" ON "Passwords" ("CredentialId");
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
-VALUES ('20240708094944_1.0.0-InitialMigration', '9.0.4');
+VALUES ('20240708094944_1.0.0-InitialMigration', '10.0.10');
+COMMIT;
+
+BEGIN TRANSACTION;
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
-VALUES ('20240708224522_1.0.1-EmptyTestMigration', '9.0.4');
+VALUES ('20240708224522_1.0.1-EmptyTestMigration', '10.0.10');
+
+COMMIT;
+BEGIN TRANSACTION;
ALTER TABLE "Aliases" RENAME COLUMN "EmailPrefix" TO "Email";
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
-VALUES ('20240711204207_1.0.2-ChangeEmailColumn', '9.0.4');
+VALUES ('20240711204207_1.0.2-ChangeEmailColumn', '10.0.10');
+
+COMMIT;
+BEGIN TRANSACTION;
CREATE TABLE "EncryptionKeys" (
"Id" TEXT NOT NULL CONSTRAINT "PK_EncryptionKeys" PRIMARY KEY,
"PublicKey" TEXT NOT NULL,
@@ -108,8 +117,11 @@ CREATE TABLE "EncryptionKeys" (
);
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
-VALUES ('20240729105618_1.1.0-AddPkiTables', '9.0.4');
+VALUES ('20240729105618_1.1.0-AddPkiTables', '10.0.10');
+COMMIT;
+
+BEGIN TRANSACTION;
CREATE TABLE "Settings" (
"Key" TEXT NOT NULL CONSTRAINT "PK_Settings" PRIMARY KEY,
"Value" TEXT NULL,
@@ -118,8 +130,11 @@ CREATE TABLE "Settings" (
);
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
-VALUES ('20240805073413_1.2.0-AddSettingsTable', '9.0.4');
+VALUES ('20240805073413_1.2.0-AddSettingsTable', '10.0.10');
+
+COMMIT;
+BEGIN TRANSACTION;
CREATE TABLE "ef_temp_Aliases" (
"Id" TEXT NOT NULL CONSTRAINT "PK_Aliases" PRIMARY KEY,
"BirthDate" TEXT NOT NULL,
@@ -150,7 +165,7 @@ COMMIT;
PRAGMA foreign_keys = 1;
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
-VALUES ('20240805122422_1.3.0-UpdateIdentityStructure', '9.0.4');
+VALUES ('20240805122422_1.3.0-UpdateIdentityStructure', '10.0.10');
BEGIN TRANSACTION;
CREATE TABLE "ef_temp_Credentials" (
@@ -190,7 +205,7 @@ CREATE INDEX "IX_Credentials_ServiceId" ON "Credentials" ("ServiceId");
COMMIT;
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
-VALUES ('20240812141727_1.3.1-MakeUsernameOptional', '9.0.4');
+VALUES ('20240812141727_1.3.1-MakeUsernameOptional', '10.0.10');
BEGIN TRANSACTION;
ALTER TABLE "Settings" ADD "IsDeleted" INTEGER NOT NULL DEFAULT 0;
@@ -208,8 +223,11 @@ ALTER TABLE "Attachment" ADD "IsDeleted" INTEGER NOT NULL DEFAULT 0;
ALTER TABLE "Aliases" ADD "IsDeleted" INTEGER NOT NULL DEFAULT 0;
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
-VALUES ('20240916105320_1.4.0-AddSyncSupport', '9.0.4');
+VALUES ('20240916105320_1.4.0-AddSyncSupport', '10.0.10');
+COMMIT;
+
+BEGIN TRANSACTION;
ALTER TABLE "Attachment" RENAME TO "Attachments";
CREATE TABLE "ef_temp_Attachments" (
@@ -246,7 +264,7 @@ CREATE INDEX "IX_Attachments_CredentialId" ON "Attachments" ("CredentialId");
COMMIT;
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
-VALUES ('20240917191243_1.4.1-RenameAttachmentsPlural', '9.0.4');
+VALUES ('20240917191243_1.4.1-RenameAttachmentsPlural', '10.0.10');
BEGIN TRANSACTION;
CREATE TABLE "TotpCodes" (
@@ -263,8 +281,11 @@ CREATE TABLE "TotpCodes" (
CREATE INDEX "IX_TotpCodes_CredentialId" ON "TotpCodes" ("CredentialId");
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
-VALUES ('20250310131554_1.5.0-AddTotpCodes', '9.0.4');
+VALUES ('20250310131554_1.5.0-AddTotpCodes', '10.0.10');
+COMMIT;
+
+BEGIN TRANSACTION;
PRAGMA foreign_keys = OFF;
@@ -683,7 +704,7 @@ CREATE INDEX "IX_Passkeys_CredentialId" ON "Passkeys" ("CredentialId");
CREATE INDEX "IX_Passkeys_RpId" ON "Passkeys" ("RpId");
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
-VALUES ('20251014122838_1.6.0-AddPasskeys', '9.0.4');
+VALUES ('20251014122838_1.6.0-AddPasskeys', '10.0.10');
COMMIT;
@@ -1282,11 +1303,22 @@ CREATE INDEX "IX_TotpCodes_ItemId" ON "TotpCodes" ("ItemId");
COMMIT;
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
-VALUES ('20251213111207_1.7.0-FieldBasedDataModelUpdate', '9.0.4');
+VALUES ('20251213111207_1.7.0-FieldBasedDataModelUpdate', '10.0.10');
BEGIN TRANSACTION;
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
-VALUES ('20260130221620_2.0.0-MajorVersionBump', '9.0.4');
+VALUES ('20260130221620_2.0.0-MajorVersionBump', '10.0.10');
+
+COMMIT;
+
+BEGIN TRANSACTION;
+CREATE TABLE "CodecOverflows" (
+ "Id" TEXT NOT NULL CONSTRAINT "PK_CodecOverflows" PRIMARY KEY,
+ "Data" TEXT NOT NULL
+);
+
+INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
+VALUES ('20260721081936_2.1.0-AddCodecOverflows', '10.0.10');
COMMIT;
""".trimIndent()
diff --git a/apps/mobile-app/ios/VaultModels/VaultSql.swift b/apps/mobile-app/ios/VaultModels/VaultSql.swift
index 6809fe70e..e131e0f97 100644
--- a/apps/mobile-app/ios/VaultModels/VaultSql.swift
+++ b/apps/mobile-app/ios/VaultModels/VaultSql.swift
@@ -84,16 +84,25 @@ public struct VaultSql {
CREATE INDEX \"IX_Passwords_CredentialId\" ON \"Passwords\" (\"CredentialId\");
INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\")
- VALUES ('20240708094944_1.0.0-InitialMigration', '9.0.4');
+ VALUES ('20240708094944_1.0.0-InitialMigration', '10.0.10');
+ COMMIT;
+
+ BEGIN TRANSACTION;
INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\")
- VALUES ('20240708224522_1.0.1-EmptyTestMigration', '9.0.4');
+ VALUES ('20240708224522_1.0.1-EmptyTestMigration', '10.0.10');
+ COMMIT;
+
+ BEGIN TRANSACTION;
ALTER TABLE \"Aliases\" RENAME COLUMN \"EmailPrefix\" TO \"Email\";
INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\")
- VALUES ('20240711204207_1.0.2-ChangeEmailColumn', '9.0.4');
+ VALUES ('20240711204207_1.0.2-ChangeEmailColumn', '10.0.10');
+
+ COMMIT;
+ BEGIN TRANSACTION;
CREATE TABLE \"EncryptionKeys\" (
\"Id\" TEXT NOT NULL CONSTRAINT \"PK_EncryptionKeys\" PRIMARY KEY,
\"PublicKey\" TEXT NOT NULL,
@@ -104,8 +113,11 @@ public struct VaultSql {
);
INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\")
- VALUES ('20240729105618_1.1.0-AddPkiTables', '9.0.4');
+ VALUES ('20240729105618_1.1.0-AddPkiTables', '10.0.10');
+
+ COMMIT;
+ BEGIN TRANSACTION;
CREATE TABLE \"Settings\" (
\"Key\" TEXT NOT NULL CONSTRAINT \"PK_Settings\" PRIMARY KEY,
\"Value\" TEXT NULL,
@@ -114,8 +126,11 @@ public struct VaultSql {
);
INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\")
- VALUES ('20240805073413_1.2.0-AddSettingsTable', '9.0.4');
+ VALUES ('20240805073413_1.2.0-AddSettingsTable', '10.0.10');
+
+ COMMIT;
+ BEGIN TRANSACTION;
CREATE TABLE \"ef_temp_Aliases\" (
\"Id\" TEXT NOT NULL CONSTRAINT \"PK_Aliases\" PRIMARY KEY,
\"BirthDate\" TEXT NOT NULL,
@@ -146,7 +161,7 @@ public struct VaultSql {
PRAGMA foreign_keys = 1;
INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\")
- VALUES ('20240805122422_1.3.0-UpdateIdentityStructure', '9.0.4');
+ VALUES ('20240805122422_1.3.0-UpdateIdentityStructure', '10.0.10');
BEGIN TRANSACTION;
CREATE TABLE \"ef_temp_Credentials\" (
@@ -186,7 +201,7 @@ public struct VaultSql {
COMMIT;
INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\")
- VALUES ('20240812141727_1.3.1-MakeUsernameOptional', '9.0.4');
+ VALUES ('20240812141727_1.3.1-MakeUsernameOptional', '10.0.10');
BEGIN TRANSACTION;
ALTER TABLE \"Settings\" ADD \"IsDeleted\" INTEGER NOT NULL DEFAULT 0;
@@ -204,8 +219,11 @@ public struct VaultSql {
ALTER TABLE \"Aliases\" ADD \"IsDeleted\" INTEGER NOT NULL DEFAULT 0;
INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\")
- VALUES ('20240916105320_1.4.0-AddSyncSupport', '9.0.4');
+ VALUES ('20240916105320_1.4.0-AddSyncSupport', '10.0.10');
+
+ COMMIT;
+ BEGIN TRANSACTION;
ALTER TABLE \"Attachment\" RENAME TO \"Attachments\";
CREATE TABLE \"ef_temp_Attachments\" (
@@ -242,7 +260,7 @@ public struct VaultSql {
COMMIT;
INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\")
- VALUES ('20240917191243_1.4.1-RenameAttachmentsPlural', '9.0.4');
+ VALUES ('20240917191243_1.4.1-RenameAttachmentsPlural', '10.0.10');
BEGIN TRANSACTION;
CREATE TABLE \"TotpCodes\" (
@@ -259,8 +277,11 @@ public struct VaultSql {
CREATE INDEX \"IX_TotpCodes_CredentialId\" ON \"TotpCodes\" (\"CredentialId\");
INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\")
- VALUES ('20250310131554_1.5.0-AddTotpCodes', '9.0.4');
+ VALUES ('20250310131554_1.5.0-AddTotpCodes', '10.0.10');
+ COMMIT;
+
+ BEGIN TRANSACTION;
PRAGMA foreign_keys = OFF;
@@ -679,7 +700,7 @@ public struct VaultSql {
CREATE INDEX \"IX_Passkeys_RpId\" ON \"Passkeys\" (\"RpId\");
INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\")
- VALUES ('20251014122838_1.6.0-AddPasskeys', '9.0.4');
+ VALUES ('20251014122838_1.6.0-AddPasskeys', '10.0.10');
COMMIT;
@@ -1278,11 +1299,22 @@ public struct VaultSql {
COMMIT;
INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\")
- VALUES ('20251213111207_1.7.0-FieldBasedDataModelUpdate', '9.0.4');
+ VALUES ('20251213111207_1.7.0-FieldBasedDataModelUpdate', '10.0.10');
BEGIN TRANSACTION;
INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\")
- VALUES ('20260130221620_2.0.0-MajorVersionBump', '9.0.4');
+ VALUES ('20260130221620_2.0.0-MajorVersionBump', '10.0.10');
+
+ COMMIT;
+
+ BEGIN TRANSACTION;
+ CREATE TABLE \"CodecOverflows\" (
+ \"Id\" TEXT NOT NULL CONSTRAINT \"PK_CodecOverflows\" PRIMARY KEY,
+ \"Data\" TEXT NOT NULL
+ );
+
+ INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\")
+ VALUES ('20260721081936_2.1.0-AddCodecOverflows', '10.0.10');
COMMIT;
"""
diff --git a/apps/mobile-app/ios/VaultStoreKit/Services/VaultMergeService.swift b/apps/mobile-app/ios/VaultStoreKit/Services/VaultMergeService.swift
index e65b0723d..307d7d8d1 100644
--- a/apps/mobile-app/ios/VaultStoreKit/Services/VaultMergeService.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/Services/VaultMergeService.swift
@@ -109,13 +109,14 @@ public class VaultMergeService {
let output = try parseOutput(outputJson)
- // Apply SQL statements to local database
+ // Apply SQL statements to the SERVER database (the merge base). The exported server DB carries
+ // the newest codec overflow carrier untouched.
for (index, statement) in output.statements.enumerated() {
- try executeStatement(on: localDb, sql: statement.sql, params: statement.params)
+ try executeStatement(on: serverDb, sql: statement.sql, params: statement.params)
}
// Export, encrypt, and return in same format: base64(encrypted(base64(sqlite)))
- let mergedData = try exportDatabase(from: localDb)
+ let mergedData = try exportDatabase(from: serverDb)
// Step 1: Base64 encode raw SQLite bytes
let innerBase64 = mergedData.base64EncodedString()
// Step 2: Encrypt the base64 string
diff --git a/apps/server/AliasVault.Client/Services/Database/DbService.cs b/apps/server/AliasVault.Client/Services/Database/DbService.cs
index d8dc838e3..2f1f44262 100644
--- a/apps/server/AliasVault.Client/Services/Database/DbService.cs
+++ b/apps/server/AliasVault.Client/Services/Database/DbService.cs
@@ -310,23 +310,7 @@ public sealed class DbService : IDisposable
/// <returns>Base64 encoded string that represents SQLite database.</returns>
public async Task<string> ExportSqliteToBase64Async()
{
- var tempFileName = Path.GetRandomFileName();
-
- // Export SQLite memory database to a temp file.
- using var memoryStream = new MemoryStream();
- await using var command = _sqlConnection!.CreateCommand();
- command.CommandText = "VACUUM main INTO @fileName";
- command.Parameters.Add(new SqliteParameter("@fileName", tempFileName));
- await command.ExecuteNonQueryAsync();
-
- // Get bytes.
- var bytes = await File.ReadAllBytesAsync(tempFileName);
- string base64String = Convert.ToBase64String(bytes);
-
- // Delete temp file.
- File.Delete(tempFileName);
-
- return base64String;
+ return await ExportConnectionToBase64Async(_sqlConnection!);
}
/// <summary>
@@ -591,10 +575,36 @@ public sealed class DbService : IDisposable
}
/// <summary>
- /// Loads a SQLite database from a base64 string which represents a .sqlite file.
+ /// Export a given in-memory SQLite connection to a base64 string.
+ /// </summary>
+ /// <param name="connection">The SQLite connection to export.</param>
+ /// <returns>Base64 encoded string that represents the SQLite database.</returns>
+ private static async Task<string> ExportConnectionToBase64Async(SqliteConnection connection)
+ {
+ var tempFileName = Path.GetRandomFileName();
+
+ // Export SQLite memory database to a temp file.
+ await using var command = connection.CreateCommand();
+ command.CommandText = "VACUUM main INTO @fileName";
+ command.Parameters.Add(new SqliteParameter("@fileName", tempFileName));
+ await command.ExecuteNonQueryAsync();
+
+ // Get bytes.
+ var bytes = await File.ReadAllBytesAsync(tempFileName);
+ string base64String = Convert.ToBase64String(bytes);
+
+ // Delete temp file.
+ File.Delete(tempFileName);
+
+ return base64String;
+ }
+
+ /// <summary>
+ /// Imports a base64-encoded SQLite database into the given connection, replacing its contents.
/// </summary>
- /// <param name="base64String">Base64 string representation of a .sqlite file.</param>
- /// <param name="connection">The connection to the database that should be used for the import.</param>
+ /// <param name="base64String">The base64-encoded SQLite database.</param>
+ /// <param name="connection">The connection to import into.</param>
+ /// <returns>Task.</returns>
private static async Task ImportDbContextFromBase64Async(string base64String, SqliteConnection connection)
{
var bytes = Convert.FromBase64String(base64String);
@@ -767,14 +777,15 @@ public sealed class DbService : IDisposable
var serverTables = await ReadTablesAsJsonAsync(serverConnection, tableNames);
_logger.LogDebug("Read {Count} server tables.", serverTables.Count);
- // Create the merge input (local has our pending changes, server has the latest).
+ // Create the merge input (local has our pending changes, server is the merge base).
var mergeInput = new MergeInput
{
LocalTables = localTables,
ServerTables = serverTables,
};
- // Call Rust WASM merge (LWW - Last Write Wins based on UpdatedAt).
+ // Call Rust WASM merge (LWW - Last Write Wins based on UpdatedAt). The merge base is the
+ // server vault; the returned statements bring the local changes onto that base.
var mergeOutput = await _rustCore.MergeVaultsAsync(mergeInput);
_logger.LogInformation(
@@ -785,8 +796,11 @@ public sealed class DbService : IDisposable
mergeOutput.Stats.RecordsInserted,
mergeOutput.Stats.Conflicts);
- // Execute the SQL statements returned by the merge to update local database.
- await ExecuteMergeSqlStatementsAsync(mergeOutput.Statements);
+ // Apply the local changes onto the server base (statements target the server database),
+ // then adopt the merged (server-based) database as the live local database.
+ await ExecuteMergeSqlStatementsAsync(mergeOutput.Statements, serverConnection);
+ var mergedBase64 = await ExportConnectionToBase64Async(serverConnection);
+ await ImportDbContextFromBase64Async(mergedBase64, _sqlConnection!);
// Verify foreign key integrity after merge.
await using (var command = _sqlConnection!.CreateCommand())
@@ -1173,11 +1187,12 @@ public sealed class DbService : IDisposable
}
/// <summary>
- /// Executes the SQL statements returned by the Rust merge operation.
+ /// Executes the SQL statements returned by the Rust merge operation against the given connection.
/// </summary>
/// <param name="statements">The SQL statements to execute.</param>
+ /// <param name="connection">The connection to execute the statements against (the merge base).</param>
/// <returns>Task.</returns>
- private async Task ExecuteMergeSqlStatementsAsync(List<SqlStatement> statements)
+ private async Task ExecuteMergeSqlStatementsAsync(List<SqlStatement> statements, SqliteConnection connection)
{
if (statements.Count == 0)
{
@@ -1188,7 +1203,7 @@ public sealed class DbService : IDisposable
_logger.LogDebug("Executing {Count} SQL statements from merge.", statements.Count);
// Disable foreign key checks during merge execution.
- await using (var pragmaCommand = _sqlConnection!.CreateCommand())
+ await using (var pragmaCommand = connection.CreateCommand())
{
pragmaCommand.CommandText = "PRAGMA foreign_keys = OFF;";
await pragmaCommand.ExecuteNonQueryAsync();
@@ -1198,7 +1213,7 @@ public sealed class DbService : IDisposable
{
foreach (var statement in statements)
{
- await using var command = _sqlConnection!.CreateCommand();
+ await using var command = connection.CreateCommand();
command.CommandText = statement.Sql;
// Add parameters in order (SQLite uses positional parameters with ?).
@@ -1232,7 +1247,7 @@ public sealed class DbService : IDisposable
finally
{
// Re-enable foreign key checks.
- await using var pragmaCommand = _sqlConnection!.CreateCommand();
+ await using var pragmaCommand = connection.CreateCommand();
pragmaCommand.CommandText = "PRAGMA foreign_keys = ON;";
await pragmaCommand.ExecuteNonQueryAsync();
}