Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Improve local vault mode, do all sync operations in background without blocking UI (#1404)
Leendert de Borst644794944a40
diff --git a/apps/browser-extension/src/entrypoints/background.ts b/apps/browser-extension/src/entrypoints/background.tsindex 91ddee616..81d67551c 100644--- a/apps/browser-extension/src/entrypoints/background.ts+++ b/apps/browser-extension/src/entrypoints/background.ts@@ -40,7 +40,7 @@ export default defineBackground({onMessage('STORE_ENCRYPTION_KEY_DERIVATION_PARAMS', ({ data }) => handleStoreEncryptionKeyDerivationParams(data as EncryptionKeyDerivationParams));onMessage('GET_ENCRYPTED_VAULT', () => handleGetEncryptedVault());- onMessage('STORE_ENCRYPTED_VAULT', ({ data }) => handleStoreEncryptedVault(data as { vaultBlob: string; markDirty?: boolean; serverRevision?: number }));+ onMessage('STORE_ENCRYPTED_VAULT', ({ data }) => handleStoreEncryptedVault(data as { vaultBlob: string; markDirty?: boolean; serverRevision?: number; expectedMutationSeq?: number }));onMessage('GET_SYNC_STATE', () => handleGetSyncState());onMessage('MARK_VAULT_CLEAN', ({ data }) => handleMarkVaultClean(data as { mutationSeqAtStart: number; newServerRevision: number }));onMessage('GET_SERVER_REVISION', () => handleGetServerRevision());diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.tsindex 2278409e5..8cee51420 100644--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts@@ -711,21 +711,36 @@ export async function handleGetEncryptedVault(): Promise<string | null> {}/**- * Store the encrypted vault blob and mark as dirty.- * Atomically increments mutation sequence for sync coordination.+ * Store the encrypted vault blob.+ *+ * Two modes:+ * 1. Local mutation (markDirty=true): Always succeeds, increments mutation sequence+ * 2. Sync operation (expectedMutationSeq provided): Only succeeds if no mutations happened+ * since sync started. This prevents sync from overwriting concurrent local changes.** @param request Object with:* - vaultBlob: The encrypted vault data- * - markDirty: If true, marks vault as dirty and increments mutation sequence+ * - markDirty: If true, marks vault as dirty and increments mutation sequence (for local mutations)* - serverRevision: Optional explicit server revision (for sync operations)+ * - expectedMutationSeq: If provided, only store if current sequence matches (for sync operations)+ * @returns { success, mutationSequence } - success=false if expectedMutationSeq didn't match*/export async function handleStoreEncryptedVault(request: {vaultBlob: string;markDirty?: boolean;serverRevision?: number;-}): Promise<{ mutationSequence: number }> {+ expectedMutationSeq?: number;+}): Promise<{ success: boolean; mutationSequence: number }> {let mutationSequence = await storage.getItem('local:mutationSequence') as number | null ?? 0;+ /*+ * If expectedMutationSeq is provided, this is a sync operation.+ * Reject if mutations happened during sync to avoid overwriting local changes.+ */+ if (request.expectedMutationSeq !== undefined && request.expectedMutationSeq !== mutationSequence) {+ return { success: false, mutationSequence };+ }+if (request.markDirty) {// Increment mutation sequence and mark dirtymutationSequence++;@@ -758,7 +773,7 @@ export async function handleStoreEncryptedVault(request: {cachedSqliteClient = null;cachedVaultBlob = null;- return { mutationSequence };+ return { success: true, mutationSequence };}/**diff --git a/apps/browser-extension/src/entrypoints/popup/components/Layout/Header.tsx b/apps/browser-extension/src/entrypoints/popup/components/Layout/Header.tsxindex 2aed5ecde..c8505ec4b 100644--- a/apps/browser-extension/src/entrypoints/popup/components/Layout/Header.tsx+++ b/apps/browser-extension/src/entrypoints/popup/components/Layout/Header.tsx@@ -5,7 +5,7 @@ import { useNavigate, useLocation } from 'react-router-dom';import Logo from '@/entrypoints/popup/components/Logo';import { useApp } from '@/entrypoints/popup/context/AppContext';-import OfflineIndicator from './OfflineIndicator';+import ServerSyncIndicator from './ServerSyncIndicator';/*** Header props.@@ -106,8 +106,8 @@ const Header: React.FC<HeaderProps> = ({<div className="flex-grow" />- {/* Offline mode indicator */}- <OfflineIndicator />+ {/* Server sync indicator */}+ <ServerSyncIndicator /><div className="flex items-center gap-2">{!app.isLoggedIn ? (diff --git a/apps/browser-extension/src/entrypoints/popup/components/Layout/OfflineIndicator.tsx b/apps/browser-extension/src/entrypoints/popup/components/Layout/ServerSyncIndicator.tsxsimilarity index 64%rename from apps/browser-extension/src/entrypoints/popup/components/Layout/OfflineIndicator.tsxrename to apps/browser-extension/src/entrypoints/popup/components/Layout/ServerSyncIndicator.tsxindex fdd6de8da..de91d16c9 100644--- a/apps/browser-extension/src/entrypoints/popup/components/Layout/OfflineIndicator.tsx+++ b/apps/browser-extension/src/entrypoints/popup/components/Layout/ServerSyncIndicator.tsx@@ -8,7 +8,7 @@ import { useDb } from '@/entrypoints/popup/context/DbContext';* Sync status indicator component.* Displays status badges for offline mode and pending sync.*/-const OfflineIndicator: React.FC = () => {+const ServerSyncIndicator: React.FC = () => {const { t } = useTranslation();const app = useApp();const dbContext = useDb();@@ -38,10 +38,26 @@ const OfflineIndicator: React.FC = () => {);}- // Show pending sync indicator (only when online but has pending changes)+ // Show syncing indicator (downloading new vault from server)+ if (dbContext.isSyncing) {+ return (+ <div title={t('common.syncingVault')} className="flex items-center gap-1.5 px-2 py-1 bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-400 rounded-md text-xs font-medium">+ <svg className="w-3.5 h-3.5 animate-spin" fill="none" stroke="currentColor" viewBox="0 0 24 24">+ <path+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth={2}+ d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"+ />+ </svg>+ </div>+ );+ }++ // Show pending sync indicator (local changes waiting to be uploaded)if (dbContext.isDirty) {return (- <div className="flex items-center gap-1.5 px-2 py-1 bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-400 rounded-md text-xs font-medium">+ <div title={t('common.pendingSync')} className="flex items-center gap-1.5 px-2 py-1 bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-400 rounded-md text-xs font-medium"><svg className="w-3.5 h-3.5 animate-spin" fill="none" stroke="currentColor" viewBox="0 0 24 24"><pathstrokeLinecap="round"@@ -50,7 +66,6 @@ const OfflineIndicator: React.FC = () => {d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"/></svg>- <span>{t('common.pendingSync')}</span></div>);}@@ -58,4 +73,4 @@ const OfflineIndicator: React.FC = () => {return null;};-export default OfflineIndicator;+export default ServerSyncIndicator;diff --git a/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx b/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsxindex b688c5e8a..2833da4dc 100644--- a/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx+++ b/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx@@ -26,11 +26,19 @@ type DbContextType = {* True if local vault has changes not yet synced to server.*/isDirty: boolean;+ /**+ * True if a background sync is in progress.+ */+ isSyncing: boolean;/*** Current server revision number.*/serverRevision: number;setIsOffline: (offline: boolean) => Promise<void>;+ /**+ * Set the syncing state.+ */+ setIsSyncing: (syncing: boolean) => void;/*** Load a decrypted vault into memory (SQLite client).*/@@ -82,6 +90,11 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }*/const [isDirty, setIsDirty] = useState(false);+ /**+ * Syncing state - true if a background sync is in progress.+ */+ const [isSyncing, setIsSyncing] = useState(false);+/*** Server revision number.*/@@ -246,8 +259,10 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }dbAvailable,isOffline,isDirty,+ isSyncing,serverRevision,setIsOffline,+ setIsSyncing,loadDatabase,loadStoredDatabase,storeEncryptionKey,@@ -256,7 +271,7 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }getVaultMetadata,refreshSyncState,hasPendingMigrations,- }), [sqliteClient, dbInitialized, dbAvailable, isOffline, isDirty, serverRevision, setIsOffline, loadDatabase, loadStoredDatabase, storeEncryptionKey, storeEncryptionKeyDerivationParams, clearDatabase, getVaultMetadata, refreshSyncState, hasPendingMigrations]);+ }), [sqliteClient, dbInitialized, dbAvailable, isOffline, isDirty, isSyncing, serverRevision, setIsOffline, loadDatabase, loadStoredDatabase, storeEncryptionKey, storeEncryptionKeyDerivationParams, clearDatabase, getVaultMetadata, refreshSyncState, hasPendingMigrations]);return (<DbContext.Provider value={contextValue}>diff --git a/apps/browser-extension/src/entrypoints/popup/hooks/useVaultSync.ts b/apps/browser-extension/src/entrypoints/popup/hooks/useVaultSync.tsindex 45b196b12..902abf924 100644--- a/apps/browser-extension/src/entrypoints/popup/hooks/useVaultSync.ts+++ b/apps/browser-extension/src/entrypoints/popup/hooks/useVaultSync.ts@@ -15,7 +15,6 @@ import type { VaultUploadResponse } from '@/utils/types/messaging/VaultUploadResimport { vaultMergeService } from '@/utils/VaultMergeService';type VaultSyncOptions = {- initialSync?: boolean;onSuccess?: (hasNewVault: boolean) => void;onError?: (error: string) => void;onStatus?: (message: string) => void;@@ -23,26 +22,6 @@ type VaultSyncOptions = {onUpgradeRequired?: () => void;}-/**- * Utility function to ensure a minimum time has elapsed for an operation- */-const withMinimumDelay = async <T>(operation: () => Promise<T>, minDelayMs: number, enableDelay: boolean = true): Promise<T> => {- if (!enableDelay) {- // If delay is disabled, return the result immediately.- return operation();- }-- const startTime = Date.now();- const result = await operation();- const elapsedTime = Date.now() - startTime;-- if (elapsedTime < minDelayMs) {- await new Promise(resolve => setTimeout(resolve, minDelayMs - elapsedTime));- }-- return result;-};-/*** Hook to sync the vault with the server.* Supports offline mode: if server is unavailable, continues with local vault.@@ -89,10 +68,7 @@ export const useVaultSync = (): { syncVault: (options?: VaultSyncOptions) => Pro}, [dbContext, t]);const syncVault = useCallback(async (options: VaultSyncOptions = {}) => {- const { initialSync = false, onSuccess, onError, onStatus, onOffline, onUpgradeRequired } = options;-- // For the initial sync, we add an artifical delay to various steps which makes it feel more fluid.- const enableDelay = initialSync;+ const { onSuccess, onError, onStatus, onOffline, onUpgradeRequired } = options;try {const isLoggedIn = await app.initializeAuth();@@ -104,7 +80,19 @@ export const useVaultSync = (): { syncVault: (options?: VaultSyncOptions) => Pro// Check app status and vault revisiononStatus?.(t('common.checkingVaultUpdates'));- const statusResponse = await withMinimumDelay(() => webApi.getStatus(), 300, enableDelay);+ const statusResponse = await webApi.getStatus();++ // Get current sync state to check if server has newer vault+ const syncState = await sendMessage('GET_SYNC_STATE', {}, 'background') as {+ isDirty: boolean;+ mutationSequence: number;+ serverRevision: number;+ };++ // Set syncing indicator if server has newer vault (actual sync will happen)+ if (statusResponse.vaultRevision > syncState.serverRevision) {+ dbContext.setIsSyncing(true);+ }// Check if server is actually available, 0.0.0 indicates connection error which triggers offline mode.if (statusResponse.serverVersion === '0.0.0') {@@ -142,107 +130,117 @@ export const useVaultSync = (): { syncVault: (options?: VaultSyncOptions) => Proawait dbContext.setIsOffline(false);}- // Get current sync state- const syncState = await sendMessage('GET_SYNC_STATE', {}, 'background') as {- isDirty: boolean;- mutationSequence: number;- serverRevision: number;- };-if (statusResponse.vaultRevision > syncState.serverRevision) {/*- * Server has a newer vault. Before overwriting local vault, check if we have- * pending local changes that need to be merged.+ * Server has a newer vault.*/onStatus?.(t('common.syncingUpdatedVault'));- const vaultResponseJson = await withMinimumDelay(() => webApi.get<VaultResponse>('Vault'), 1000, enableDelay) as VaultResponse;+ const vaultResponseJson = await webApi.get<VaultResponse>('Vault');try {- // Get encryption key from background workerconst encryptionKey = await sendMessage('GET_ENCRYPTION_KEY', {}, 'background') as string;if (syncState.isDirty) {/*- * We have local changes that haven't been synced to server.+ * We have local changes AND server has newer vault.* Merge local vault with server vault, then upload the merged result.*/onStatus?.(t('common.mergingVaultChanges'));const localEncryptedVault = await sendMessage('GET_ENCRYPTED_VAULT', {}, 'background') as string | null;if (localEncryptedVault) {- // Decrypt both vaultsconst localDecrypted = await EncryptionUtility.symmetricDecrypt(localEncryptedVault, encryptionKey);const serverDecrypted = await EncryptionUtility.symmetricDecrypt(vaultResponseJson.vault.blob, encryptionKey);- // Perform LWW mergeconst mergeResult = await vaultMergeService.merge(localDecrypted, serverDecrypted);if (mergeResult.success) {console.info('Vault merge during sync completed:', mergeResult.stats);- // Re-encrypt the merged vaultconst mergedEncryptedVault = await EncryptionUtility.symmetricEncrypt(mergeResult.mergedVaultBase64,encryptionKey);- // Update the vault response with the merged blob- vaultResponseJson.vault.blob = mergedEncryptedVault;-/*- * Store the merged vault locally, keeping it dirty since we need to upload.- * Update server revision to track what we merged with.+ * Store merged vault. Use expectedMutationSeq to detect if a local mutation+ * happened during merge - if so, reject and re-sync.*/- await sendMessage('STORE_ENCRYPTED_VAULT', {+ const storeResult = await sendMessage('STORE_ENCRYPTED_VAULT', {vaultBlob: mergedEncryptedVault,- serverRevision: vaultResponseJson.vault.currentRevisionNumber- }, 'background');+ serverRevision: vaultResponseJson.vault.currentRevisionNumber,+ expectedMutationSeq: syncState.mutationSequence+ }, 'background') as { success: boolean; mutationSequence: number };++ if (!storeResult.success) {+ console.info('Mutation detected during merge, re-syncing...');+ return syncVault(options);+ }+ // Upload merged vault to serveronStatus?.(t('common.uploadingVault'));const uploadResponse = await sendMessage('UPLOAD_VAULT', {}, 'background') as VaultUploadResponse;if (uploadResponse.success && uploadResponse.status === 0) {- // Upload succeeded - try to clear dirty flagawait sendMessage('MARK_VAULT_CLEAN', {mutationSeqAtStart: uploadResponse.mutationSeqAtStart,newServerRevision: uploadResponse.newRevisionNumber}, 'background');- await dbContext.refreshSyncState();+ } else if (uploadResponse.status === 2) {+ // Server returned Outdated - another device uploaded. Re-sync.+ return syncVault(options);} else {console.error('Failed to upload merged vault:', uploadResponse.error);}++ // Store metadata and load merged vault into memory+ await sendMessage('STORE_VAULT_METADATA', {+ publicEmailDomainList: vaultResponseJson.vault.publicEmailDomainList,+ privateEmailDomainList: vaultResponseJson.vault.privateEmailDomainList,+ hiddenPrivateEmailDomainList: vaultResponseJson.vault.hiddenPrivateEmailDomainList,+ }, 'background');++ await dbContext.loadDatabase(mergeResult.mergedVaultBase64);+ await dbContext.refreshSyncState();++ if (await checkAndHandleUpgrade(onUpgradeRequired)) {+ return false;+ }++ onSuccess?.(true);+ return true;} else {console.error('Vault merge failed during sync, using server vault');+ // Fall through to use server vault}}}/*- * Persist vault and metadata to local storage.- * If merge happened, the merged vault was already stored via STORE_ENCRYPTED_VAULT above.- * If no merge (isDirty was false), we store the server vault now.+ * No local changes (or merge failed) - just use server vault.+ * Use expectedMutationSeq to detect concurrent mutations.*/- if (!syncState.isDirty) {- await sendMessage('STORE_ENCRYPTED_VAULT', {- vaultBlob: vaultResponseJson.vault.blob,- serverRevision: vaultResponseJson.vault.currentRevisionNumber,- }, 'background');+ const storeResult = await sendMessage('STORE_ENCRYPTED_VAULT', {+ vaultBlob: vaultResponseJson.vault.blob,+ serverRevision: vaultResponseJson.vault.currentRevisionNumber,+ expectedMutationSeq: syncState.mutationSequence+ }, 'background') as { success: boolean; mutationSequence: number };++ if (!storeResult.success) {+ console.info('Mutation detected during sync, re-syncing...');+ return syncVault(options);}- // Refresh sync state after storing vault- await dbContext.refreshSyncState();-await sendMessage('STORE_VAULT_METADATA', {publicEmailDomainList: vaultResponseJson.vault.publicEmailDomainList,privateEmailDomainList: vaultResponseJson.vault.privateEmailDomainList,hiddenPrivateEmailDomainList: vaultResponseJson.vault.hiddenPrivateEmailDomainList,}, 'background');- // Decrypt and load the vault into memoryconst decryptedVault = await EncryptionUtility.symmetricDecrypt(vaultResponseJson.vault.blob, encryptionKey);await dbContext.loadDatabase(decryptedVault);+ await dbContext.refreshSyncState();- // Check if upgrade is required after loading databaseif (await checkAndHandleUpgrade(onUpgradeRequired)) {return false;}@@ -250,12 +248,10 @@ export const useVaultSync = (): { syncVault: (options?: VaultSyncOptions) => ProonSuccess?.(true);return true;} catch (error) {- // Check if it's a version-related error (app needs to be updated)if (error instanceof VaultVersionIncompatibleError) {await app.logout(error.message);return false;}- // Vault could not be decrypted, throw an errorthrow new Error('Vault could not be decrypted, if the problem persists please logout and login again.');}} else if (statusResponse.vaultRevision === syncState.serverRevision) {@@ -295,7 +291,7 @@ export const useVaultSync = (): { syncVault: (options?: VaultSyncOptions) => Proreturn false;}- await withMinimumDelay(() => Promise.resolve(onSuccess?.(false)), 300, enableDelay);+ onSuccess?.(false);return false;} catch (err) {const errorMessage = err instanceof Error ? err.message : 'Unknown error during vault sync';@@ -316,6 +312,9 @@ export const useVaultSync = (): { syncVault: (options?: VaultSyncOptions) => ProonError?.(errorMessage);return false;+ } finally {+ // Always clear syncing state when done+ dbContext.setIsSyncing(false);}}, [app, dbContext, webApi, t, checkAndHandleUpgrade, enterOfflineMode]);diff --git a/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx b/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsxindex 3c34c56da..30f558403 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/Reinitialize.tsx@@ -109,42 +109,38 @@ const Reinitialize: React.FC = () => {// Only perform vault sync once during initializationhasInitialized.current = true;- // Perform vault sync and restore state+ /*+ * Navigate immediately using local vault - don't block on sync.+ * This ensures the UI is responsive even if server is slow.+ */+ setIsInitialLoading(false);+ if (inlineUnlock) {+ navigate('/unlock-success', { replace: true });+ } else if (pendingRedirectUrl) {+ navigate(pendingRedirectUrl, { replace: true });+ } else {+ await restoreLastPage();+ }++ /*+ * Run sync in background. If server has newer vault, useVaultSync will:+ * 1. Download and merge (if needed)+ * 2. Call dbContext.loadDatabase() which updates sqliteClient+ * 3. ItemsList reacts to sqliteClient changes and auto-refreshes+ */syncVault({- initialSync: false,/**- * Handle successful vault sync.+ * Handle upgrade required - redirect to upgrade page.*/- onSuccess: async () => {- // After successful sync, try to restore last page or go to credentials- if (inlineUnlock) {- setIsInitialLoading(false);- navigate('/unlock-success', { replace: true });- } else if (pendingRedirectUrl) {- // If there's a pending redirect URL in storage, use it (most reliable)- setIsInitialLoading(false);- navigate(pendingRedirectUrl, { replace: true });- } else {- await restoreLastPage();- }+ onUpgradeRequired: () => {+ navigate('/upgrade', { replace: true });},/**- * Handle vault sync error.+ * Handle sync errors silently - user already has local vault.* @param error Error message*/onError: (error) => {- console.error('Vault sync error during initialization:', error);- // Even if sync fails, continue with initialization- restoreLastPage().then(() => {- setIsInitialLoading(false);- });- },- /**- * Handle upgrade required.- */- onUpgradeRequired: () => {- navigate('/upgrade', { replace: true });- setIsInitialLoading(false);+ console.error('Background vault sync error:', error);}});} else {