Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Refactor browser extension vault persist and load methods to simplify flow (#1404)
Leendert de Borst5cda3f7f121d
diff --git a/apps/browser-extension/playwright.config.ts b/apps/browser-extension/playwright.config.tsindex effcaab68..48abf8006 100644--- a/apps/browser-extension/playwright.config.ts+++ b/apps/browser-extension/playwright.config.ts@@ -26,8 +26,8 @@ export default defineConfig({// Retry failed tests (more retries on CI)retries: process.env.CI ? 2 : 0,- // Use single worker to ensure sequential execution- workers: 1,+ // Amount of parallel tests to run+ workers: 4,// Reporter configurationreporter: [diff --git a/apps/browser-extension/src/entrypoints/background.ts b/apps/browser-extension/src/entrypoints/background.tsindex 4e81dc669..a2291df3b 100644--- a/apps/browser-extension/src/entrypoints/background.ts+++ b/apps/browser-extension/src/entrypoints/background.ts@@ -9,7 +9,7 @@ import { handleClipboardCopied, handleCancelClipboardClear, handleGetClipboardClimport { setupContextMenus } from '@/entrypoints/background/ContextMenu';import { handleGetWebAuthnSettings, handleWebAuthnCreate, handleWebAuthnGet, handlePasskeyPopupResponse, handleGetRequestData } from '@/entrypoints/background/PasskeyHandler';import { handleOpenPopup, handlePopupWithCredential, handleOpenPopupCreateCredential, handleToggleContextMenu } from '@/entrypoints/background/PopupMessageHandler';-import { handleCheckAuthStatus, handleClearPersistedFormValues, handleClearVault, handleLockVault, handleCreateIdentity, handleGetCredentials, handleGetDefaultEmailDomain, handleGetDefaultIdentitySettings, handleGetEncryptionKey, handleGetEncryptionKeyDerivationParams, handleGetPasswordSettings, handleGetPersistedFormValues, handleGetVault, handlePersistFormValues, handleStoreEncryptionKey, handleStoreEncryptionKeyDerivationParams, handleStoreVault, handleSyncVault, handleUploadVault, handleGetOfflineMode, handleSetOfflineMode, handleGetEncryptedVault, handleStoreEncryptedVault, handleGetHasPendingSync, handleSetHasPendingSync } from '@/entrypoints/background/VaultMessageHandler';+import { handleCheckAuthStatus, handleClearPersistedFormValues, handleClearVault, handleLockVault, handleCreateIdentity, handleGetCredentials, handleGetDefaultEmailDomain, handleGetDefaultIdentitySettings, handleGetEncryptionKey, handleGetEncryptionKeyDerivationParams, handleGetPasswordSettings, handleGetPersistedFormValues, handleGetVault, handlePersistFormValues, handleStoreEncryptionKey, handleStoreEncryptionKeyDerivationParams, handleStoreVaultMetadata, handleSyncVault, handleUploadVault, handleGetOfflineMode, handleSetOfflineMode, handleGetEncryptedVault, handleStoreEncryptedVault, handleGetHasPendingSync, handleSetHasPendingSync } from '@/entrypoints/background/VaultMessageHandler';import { GLOBAL_CONTEXT_MENU_ENABLED_KEY } from '@/utils/Constants';import { EncryptionKeyDerivationParams } from "@/utils/dist/shared/models/metadata";@@ -33,12 +33,17 @@ export default defineBackground({onMessage('GET_DEFAULT_IDENTITY_SETTINGS', () => handleGetDefaultIdentitySettings());onMessage('GET_PASSWORD_SETTINGS', () => handleGetPasswordSettings());- onMessage('STORE_VAULT', ({ data }) => handleStoreVault(data));+ onMessage('STORE_VAULT_METADATA', ({ data }) => handleStoreVaultMetadata(data as { publicEmailDomainList?: string[]; privateEmailDomainList?: string[]; hiddenPrivateEmailDomainList?: string[] }));onMessage('STORE_ENCRYPTION_KEY', ({ data }) => handleStoreEncryptionKey(data as string));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; hasPendingSync: boolean; vaultRevisionNumber?: number }));+ onMessage('GET_HAS_PENDING_SYNC', () => handleGetHasPendingSync());+ onMessage('SET_HAS_PENDING_SYNC', ({ data }) => handleSetHasPendingSync(data as boolean));+onMessage('CREATE_IDENTITY', ({ data }) => handleCreateIdentity(data));- onMessage('UPLOAD_VAULT', ({ data }) => handleUploadVault(data));+ onMessage('UPLOAD_VAULT', () => handleUploadVault());onMessage('SYNC_VAULT', () => handleSyncVault());onMessage('LOCK_VAULT', () => handleLockVault());onMessage('CLEAR_VAULT', () => handleClearVault());@@ -46,10 +51,6 @@ export default defineBackground({// Offline mode management messagesonMessage('GET_OFFLINE_MODE', () => handleGetOfflineMode());onMessage('SET_OFFLINE_MODE', ({ data }) => handleSetOfflineMode(data as boolean));- onMessage('GET_ENCRYPTED_VAULT', () => handleGetEncryptedVault());- onMessage('STORE_ENCRYPTED_VAULT', ({ data }) => handleStoreEncryptedVault(data as { vaultBlob: string; hasPendingSync: boolean }));- onMessage('GET_HAS_PENDING_SYNC', () => handleGetHasPendingSync());- onMessage('SET_HAS_PENDING_SYNC', ({ data }) => handleSetHasPendingSync(data as boolean));onMessage('OPEN_POPUP', () => handleOpenPopup());onMessage('OPEN_POPUP_WITH_CREDENTIAL', ({ data }) => handlePopupWithCredential(data));diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.tsindex 555c15f81..1cf13628c 100644--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts@@ -10,7 +10,6 @@ import { BoolResponse as messageBoolResponse } from '@/utils/types/messaging/Booimport { CredentialsResponse as messageCredentialsResponse } from '@/utils/types/messaging/CredentialsResponse';import { IdentitySettingsResponse } from '@/utils/types/messaging/IdentitySettingsResponse';import { PasswordSettingsResponse as messagePasswordSettingsResponse } from '@/utils/types/messaging/PasswordSettingsResponse';-import { StoreVaultRequest } from '@/utils/types/messaging/StoreVaultRequest';import { StringResponse as stringResponse } from '@/utils/types/messaging/StringResponse';import { VaultResponse as messageVaultResponse } from '@/utils/types/messaging/VaultResponse';import { VaultUploadResponse as messageVaultUploadResponse } from '@/utils/types/messaging/VaultUploadResponse';@@ -82,55 +81,32 @@ export async function handleCheckAuthStatus() : Promise<{ isLoggedIn: boolean, i}/**- * Store the vault in browser storage atomically with all required metadata.- * The encrypted vault is stored in local: storage (persistent) while metadata is in local:.- * The encryption key remains in session: storage for security.- *- * This is an atomic operation that stores both the vault blob and its sync state together,- * ensuring callers cannot forget to update the pending sync flag.+ * Store vault metadata (email domains) in browser storage.+ * This is used during login/sync when receiving vault data from the server.*/-export async function handleStoreVault(- message: any,+export async function handleStoreVaultMetadata(+ message: {+ publicEmailDomainList?: string[];+ privateEmailDomainList?: string[];+ hiddenPrivateEmailDomainList?: string[];+ },) : Promise<messageBoolResponse> {try {- const vaultRequest = message as StoreVaultRequest;-- /*- * Store encrypted vault in local: storage (persistent across browser sessions).- * This allows offline access after browser restart (user must re-enter password to unlock).- */- await storage.setItem('local:encryptedVault', vaultRequest.vaultBlob);-- /*- * Always store the pending sync state - this is required for all vault storage operations.- */- await storage.setItem('local:hasPendingSync', vaultRequest.hasPendingSync);-- /*- * For all other values, check if they have a value and store them in local: storage if they do.- * These are also persisted to enable offline mode.- * Some updates, e.g. when mutating local database, these values will not be set.- */-- if (vaultRequest.publicEmailDomainList) {- await storage.setItem('local:publicEmailDomains', vaultRequest.publicEmailDomainList);- }-- if (vaultRequest.privateEmailDomainList) {- await storage.setItem('local:privateEmailDomains', vaultRequest.privateEmailDomainList);+ if (message.publicEmailDomainList) {+ await storage.setItem('local:publicEmailDomains', message.publicEmailDomainList);}- if (vaultRequest.hiddenPrivateEmailDomainList) {- await storage.setItem('local:hiddenPrivateEmailDomains', vaultRequest.hiddenPrivateEmailDomainList);+ if (message.privateEmailDomainList) {+ await storage.setItem('local:privateEmailDomains', message.privateEmailDomainList);}- if (vaultRequest.vaultRevisionNumber) {- await storage.setItem('local:vaultRevisionNumber', vaultRequest.vaultRevisionNumber);+ if (message.hiddenPrivateEmailDomainList) {+ await storage.setItem('local:hiddenPrivateEmailDomains', message.hiddenPrivateEmailDomainList);}return { success: true };} catch (error) {- console.error('Failed to store vault:', error);+ console.error('Failed to store vault metadata:', error);return { success: false, error: await t('common.errors.unknownError') };}}@@ -446,35 +422,15 @@ export async function handleGetEncryptionKeyDerivationParams(}/**- * Upload the vault to the server.+ * Upload the currently stored vault to the server.*/export async function handleUploadVault(- message: { vaultBlob: string; baseRevisionNumber?: number }) : Promise<messageVaultUploadResponse> {try {- /*- * Store the new vault blob in local: storage with pending sync flag.- * If a baseRevisionNumber is provided (e.g., after a merge), update it atomically.- * The uploadNewVaultToServer will clear hasPendingSync on success.- */- if (message.baseRevisionNumber !== undefined) {- // Store vault, pending sync flag, and revision number atomically- await storage.setItems([- { key: 'local:encryptedVault', value: message.vaultBlob },- { key: 'local:hasPendingSync', value: true },- { key: 'local:vaultRevisionNumber', value: message.baseRevisionNumber }- ]);- } else {- await storage.setItems([- { key: 'local:encryptedVault', value: message.vaultBlob },- { key: 'local:hasPendingSync', value: true }- ]);- }-- // Create new sqlite client which will use the new vault blob.+ // Create sqlite client from the already-stored vault blob.const sqliteClient = await createVaultSqliteClient();- // Upload the new vault to the server.+ // Upload the vault to the server.const response = await uploadNewVaultToServer(sqliteClient);return { success: true, status: response.status, newRevisionNumber: response.newRevisionNumber };} catch (error) {@@ -660,11 +616,22 @@ export async function handleGetEncryptedVault(): Promise<string | null> {/*** Store the encrypted vault blob with pending sync flag.- * This is the atomic operation that should be used for all local vault mutations.+ * This is an atomic operation that should be used for all local vault mutations.*- * @param request Object with vaultBlob and hasPendingSync flag+ * @param request Object with vaultBlob, hasPendingSync flag, and optional vaultRevisionNumber*/-export async function handleStoreEncryptedVault(request: { vaultBlob: string; hasPendingSync: boolean }): Promise<void> {- await storage.setItem('local:encryptedVault', request.vaultBlob);- await storage.setItem('local:hasPendingSync', request.hasPendingSync);+export async function handleStoreEncryptedVault(request: { vaultBlob: string; hasPendingSync: boolean; vaultRevisionNumber?: number }): Promise<void> {+ if (request.vaultRevisionNumber !== undefined) {+ // Store vault, pending sync flag, and revision number atomically+ await storage.setItems([+ { key: 'local:encryptedVault', value: request.vaultBlob },+ { key: 'local:hasPendingSync', value: request.hasPendingSync },+ { key: 'local:vaultRevisionNumber', value: request.vaultRevisionNumber }+ ]);+ } else {+ await storage.setItems([+ { key: 'local:encryptedVault', value: request.vaultBlob },+ { key: 'local:hasPendingSync', value: request.hasPendingSync }+ ]);+ }}diff --git a/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx b/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsxindex 2f68a0aea..cac109bdd 100644--- a/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx+++ b/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx@@ -2,10 +2,7 @@ import React, { createContext, useContext, useState, useEffect, useCallback, useimport { sendMessage } from 'webext-bridge/popup';import type { EncryptionKeyDerivationParams, VaultMetadata } from '@/utils/dist/shared/models/metadata';-import type { VaultResponse } from '@/utils/dist/shared/models/webapi';-import EncryptionUtility from '@/utils/EncryptionUtility';import SqliteClient from '@/utils/SqliteClient';-import { StoreVaultRequest } from '@/utils/types/messaging/StoreVaultRequest';import type { VaultResponse as messageVaultResponse } from '@/utils/types/messaging/VaultResponse';import { storage } from '#imports';@@ -18,8 +15,15 @@ type DbContextType = {hasPendingSync: boolean;setIsOffline: (offline: boolean) => Promise<void>;setHasPendingSync: (hasPendingSync: boolean) => Promise<void>;- initializeDatabase: (vaultResponse: VaultResponse, derivedKey: string) => Promise<SqliteClient>;- initializeDatabaseFromDecryptedVault: (decryptedVaultBase64: string) => Promise<SqliteClient>;+ /**+ * Load a decrypted vault into memory (SQLite client).+ */+ loadDatabase: (decryptedVaultBase64: string) => Promise<SqliteClient>;+ /**+ * Load the stored (encrypted) vault from background storage into memory.+ * Returns true if vault was loaded successfully, false otherwise.+ */+ loadStoredDatabase: () => Promise<boolean>;storeEncryptionKey: (derivedKey: string) => Promise<void>;storeEncryptionKeyDerivationParams: (params: EncryptionKeyDerivationParams) => Promise<void>;clearDatabase: () => void;@@ -92,48 +96,10 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }loadSyncState();}, []);- const initializeDatabase = useCallback(async (vaultResponse: VaultResponse, derivedKey: string) => {- // Attempt to decrypt the blob.- const decryptedBlob = await EncryptionUtility.symmetricDecrypt(- vaultResponse.vault.blob,- derivedKey- );-- // Initialize the SQLite client.- const client = new SqliteClient();- await client.initializeFromBase64(decryptedBlob);-- setSqliteClient(client);- setDbInitialized(true);- setDbAvailable(true);-- /**- * Store encrypted vault and metadata in background worker (persistent storage).- * Since we just fetched from server, hasPendingSync is false - vault is in sync.- */- const request: StoreVaultRequest = {- vaultBlob: vaultResponse.vault.blob,- publicEmailDomainList: vaultResponse.vault.publicEmailDomainList,- privateEmailDomainList: vaultResponse.vault.privateEmailDomainList,- hiddenPrivateEmailDomainList: vaultResponse.vault.hiddenPrivateEmailDomainList,- vaultRevisionNumber: vaultResponse.vault.currentRevisionNumber,- hasPendingSync: false, // Fresh from server, no pending changes- };-- await sendMessage('STORE_VAULT', request, 'background');-- // Update local state to reflect sync status- setHasPendingSyncState(false);-- return client;- }, []);-/**- * Initialize the database from an already-decrypted vault (for offline mode).- * This is used when we have the decryption key but the server is unavailable.+ * Load a decrypted vault into memory (SQLite client).*/- const initializeDatabaseFromDecryptedVault = useCallback(async (decryptedVaultBase64: string) => {- // Initialize the SQLite client.+ const loadDatabase = useCallback(async (decryptedVaultBase64: string) => {const client = new SqliteClient();await client.initializeFromBase64(decryptedVaultBase64);@@ -144,7 +110,11 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }return client;}, []);- const checkStoredVault = useCallback(async () => {+ /**+ * Load the stored (encrypted) vault from background storage into memory.+ * Returns true if vault was loaded successfully, false otherwise.+ */+ const loadStoredDatabase = useCallback(async (): Promise<boolean> => {try {const response = await sendMessage('GET_VAULT', {}, 'background') as messageVaultResponse;if (response?.vault) {@@ -154,15 +124,17 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }setSqliteClient(client);setDbInitialized(true);setDbAvailable(true);- // Metadata is already stored in session storage by background worker+ return true;} else {setDbInitialized(true);setDbAvailable(false);+ return false;}} catch (error) {console.error('Error retrieving vault from background:', error);setDbInitialized(true);setDbAvailable(false);+ return false;}}, []);@@ -210,13 +182,13 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }}, [sqliteClient]);/**- * Check if database is initialized and try to retrieve vault from background+ * Check if database is initialized and try to retrieve and init stored vault*/useEffect(() : void => {if (!dbInitialized) {- checkStoredVault();+ loadStoredDatabase();}- }, [dbInitialized, checkStoredVault]);+ }, [dbInitialized, loadStoredDatabase]);/*** Store encryption key in background worker.@@ -250,15 +222,15 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }hasPendingSync,setIsOffline,setHasPendingSync,- initializeDatabase,- initializeDatabaseFromDecryptedVault,+ loadDatabase,+ loadStoredDatabase,storeEncryptionKey,storeEncryptionKeyDerivationParams,clearDatabase,getVaultMetadata,setCurrentVaultRevisionNumber,hasPendingMigrations,- }), [sqliteClient, dbInitialized, dbAvailable, isOffline, hasPendingSync, setIsOffline, setHasPendingSync, initializeDatabase, initializeDatabaseFromDecryptedVault, storeEncryptionKey, storeEncryptionKeyDerivationParams, clearDatabase, getVaultMetadata, setCurrentVaultRevisionNumber, hasPendingMigrations]);+ }), [sqliteClient, dbInitialized, dbAvailable, isOffline, hasPendingSync, setIsOffline, setHasPendingSync, loadDatabase, loadStoredDatabase, storeEncryptionKey, storeEncryptionKeyDerivationParams, clearDatabase, getVaultMetadata, setCurrentVaultRevisionNumber, 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 fe184741d..de7794010 100644--- a/apps/browser-extension/src/entrypoints/popup/hooks/useVaultSync.ts+++ b/apps/browser-extension/src/entrypoints/popup/hooks/useVaultSync.ts@@ -14,14 +14,19 @@ import { VaultVersionIncompatibleError } from '@/utils/types/errors/VaultVersionimport type { VaultUploadResponse } from '@/utils/types/messaging/VaultUploadResponse';import { vaultMergeService } from '@/utils/VaultMergeService';+type VaultSyncOptions = {+ initialSync?: boolean;+ onSuccess?: (hasNewVault: boolean) => void;+ onError?: (error: string) => void;+ onStatus?: (message: string) => void;+ onOffline?: () => void;+ 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> => {+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();@@ -38,15 +43,6 @@ const withMinimumDelay = async <T>(return result;};-type VaultSyncOptions = {- initialSync?: boolean;- onSuccess?: (hasNewVault: boolean) => void;- onError?: (error: string) => void;- onStatus?: (message: string) => void;- onOffline?: () => void;- onUpgradeRequired?: () => void;-}-/*** Hook to sync the vault with the server.* Supports offline mode: if server is unavailable, continues with local vault.@@ -73,11 +69,7 @@ export const useVaultSync = (): { syncVault: (options?: VaultSyncOptions) => Pro* Handle entering offline mode.* @returns True to indicate success (caller should return true).*/- const enterOfflineMode = useCallback(async (- onStatus?: (message: string) => void,- onOffline?: () => void,- onSuccess?: (hasNewVault: boolean) => void- ): Promise<boolean> => {+ const enterOfflineMode = useCallback(async (onStatus?: (message: string) => void, onOffline?: () => void, onSuccess?: (hasNewVault: boolean) => void): Promise<boolean> => {await dbContext.setIsOffline(true);onStatus?.(t('common.offlineMode'));onOffline?.();@@ -186,14 +178,17 @@ export const useVaultSync = (): { syncVault: (options?: VaultSyncOptions) => ProvaultResponseJson.vault.blob = mergedEncryptedVault;/*- * Upload the merged vault to server.+ * Store the merged vault locally first, then upload to server.* Pass the server's revision as our base revision since we just fetched and merged with it.*/- onStatus?.(t('common.uploadingVault'));- const uploadResponse = await sendMessage('UPLOAD_VAULT', {+ await sendMessage('STORE_ENCRYPTED_VAULT', {vaultBlob: mergedEncryptedVault,- baseRevisionNumber: statusResponse.vaultRevision- }, 'background') as VaultUploadResponse;+ hasPendingSync: true,+ vaultRevisionNumber: statusResponse.vaultRevision+ }, 'background');++ onStatus?.(t('common.uploadingVault'));+ const uploadResponse = await sendMessage('UPLOAD_VAULT', {}, 'background') as VaultUploadResponse;if (uploadResponse.success && uploadResponse.status === 0) {// Upload succeeded - update revision@@ -209,9 +204,30 @@ export const useVaultSync = (): { syncVault: (options?: VaultSyncOptions) => Pro}}- await dbContext.initializeDatabase(vaultResponseJson, encryptionKey);+ /*+ * Persist vault and metadata to local storage.+ * If merge happened, the merged vault was already stored via STORE_ENCRYPTED_VAULT above.+ * If no merge (hasPendingSync was false), we store the server vault now.+ */+ if (!hasPendingSync) {+ await sendMessage('STORE_ENCRYPTED_VAULT', {+ vaultBlob: vaultResponseJson.vault.blob,+ hasPendingSync: false,+ vaultRevisionNumber: vaultResponseJson.vault.currentRevisionNumber,+ }, 'background');+ }++ await sendMessage('STORE_VAULT_METADATA', {+ publicEmailDomainList: vaultResponseJson.vault.publicEmailDomainList,+ privateEmailDomainList: vaultResponseJson.vault.privateEmailDomainList,+ hiddenPrivateEmailDomainList: vaultResponseJson.vault.hiddenPrivateEmailDomainList,+ }, 'background');- // Check if upgrade is required after initializing database+ // Decrypt and load the vault into memory+ const decryptedVault = await EncryptionUtility.symmetricDecrypt(vaultResponseJson.vault.blob, encryptionKey);+ await dbContext.loadDatabase(decryptedVault);++ // Check if upgrade is required after loading databaseif (await checkAndHandleUpgrade(onUpgradeRequired)) {return false;}@@ -228,7 +244,7 @@ export const useVaultSync = (): { syncVault: (options?: VaultSyncOptions) => Prothrow new Error('Vault could not be decrypted, if the problem persists please logout and login again.');}} else if (statusResponse.vaultRevision === vaultRevisionNumber) {- /*+ /*** Server and local vault are at the same revision.* If we have pending local changes, upload them now.*/@@ -236,28 +252,20 @@ export const useVaultSync = (): { syncVault: (options?: VaultSyncOptions) => Proif (hasPendingSync) {onStatus?.(t('common.uploadingVault'));- // Get the current encrypted vault and upload it- const encryptedVault = await sendMessage('GET_ENCRYPTED_VAULT', {}, 'background') as string | null;-- if (encryptedVault) {- const uploadResponse = await sendMessage('UPLOAD_VAULT', {- vaultBlob: encryptedVault- }, 'background') as VaultUploadResponse;-- if (uploadResponse.success && uploadResponse.status === 0) {- // Upload succeeded - update revision and clear pending flag- await dbContext.setCurrentVaultRevisionNumber(uploadResponse.newRevisionNumber!);- await dbContext.setHasPendingSync(false);- } else if (uploadResponse.status === 2) {- /*- * Server returned Outdated - another device uploaded first.- * Recursively call syncVault to fetch, merge, and retry.- */- return syncVault(options);- } else {- console.error('Failed to upload pending vault:', uploadResponse.error);- // Keep hasPendingSync true so we retry later- }+ // Vault is already stored locally with hasPendingSync=true, just upload it+ const uploadResponse = await sendMessage('UPLOAD_VAULT', {}, 'background') as VaultUploadResponse;+ if (uploadResponse.success && uploadResponse.status === 0) {+ // Upload succeeded - update revision and clear pending flag+ await dbContext.setCurrentVaultRevisionNumber(uploadResponse.newRevisionNumber!);+ await dbContext.setHasPendingSync(false);+ } else if (uploadResponse.status === 2) {+ /**+ * Server returned Outdated - another device uploaded first.+ * Recursively call syncVault to fetch, merge, and retry.+ */+ return syncVault(options);+ } else {+ console.error('Failed to upload pending vault:', uploadResponse.error);}onSuccess?.(false);diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsxindex 37d77de2d..690c89fad 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx@@ -1,6 +1,7 @@import React, { useEffect, useState } from 'react';import { useTranslation } from 'react-i18next';import { useNavigate } from 'react-router-dom';+import { sendMessage } from 'webext-bridge/popup';import Button from '@/entrypoints/popup/components/Button';import MobileUnlockModal from '@/entrypoints/popup/components/Dialogs/MobileUnlockModal';@@ -18,6 +19,8 @@ import SrpUtility from '@/entrypoints/popup/utils/SrpUtility';import { AppInfo } from '@/utils/AppInfo';import { SrpAuthService } from '@/utils/auth/SrpAuthService';import type { VaultResponse, LoginResponse } from '@/utils/dist/shared/models/webapi';+import { EncryptionUtility } from '@/utils/EncryptionUtility';+import SqliteClient from '@/utils/SqliteClient';import { ApiAuthError } from '@/utils/types/errors/ApiAuthError';import type { MobileLoginResult } from '@/utils/types/messaging/MobileLoginResult';@@ -50,6 +53,29 @@ const Login: React.FC = () => {const webApi = useWebApi();const srpUtil = new SrpUtility(webApi);+ /**+ * Helper to persist and load vault after successful authentication.+ * @returns The initialized SqliteClient+ */+ const persistAndLoadVault = async (vaultResponse: VaultResponse, encryptionKey: string): Promise<SqliteClient> => {+ // Persist vault to local storage (fresh from server, no pending sync)+ await sendMessage('STORE_ENCRYPTED_VAULT', {+ vaultBlob: vaultResponse.vault.blob,+ hasPendingSync: false,+ vaultRevisionNumber: vaultResponse.vault.currentRevisionNumber,+ }, 'background');++ await sendMessage('STORE_VAULT_METADATA', {+ publicEmailDomainList: vaultResponse.vault.publicEmailDomainList,+ privateEmailDomainList: vaultResponse.vault.privateEmailDomainList,+ hiddenPrivateEmailDomainList: vaultResponse.vault.hiddenPrivateEmailDomainList,+ }, 'background');++ // Decrypt and load the vault into memory+ const decryptedVault = await EncryptionUtility.symmetricDecrypt(vaultResponse.vault.blob, encryptionKey);+ return dbContext.loadDatabase(decryptedVault);+ };+/*** Handle successful authentication by storing tokens and initializing the database*/@@ -76,8 +102,8 @@ const Login: React.FC = () => {encryptionSettings: loginResponse.encryptionSettings});- // Initialize the SQLite context with the new vault data.- const sqliteClient = await dbContext.initializeDatabase(vaultResponseJson, passwordHashBase64);+ // Persist and load the vault+ const sqliteClient = await persistAndLoadVault(vaultResponseJson, passwordHashBase64);// If there are pending migrations, redirect to the upgrade page.try {@@ -153,12 +179,6 @@ const Login: React.FC = () => {const normalizedUsername = SrpAuthService.normalizeUsername(credentials.username);const loginResponse = await srpUtil.initiateLogin(normalizedUsername);- console.debug('[LOGIN DEBUG] Login params from server:', {- salt: loginResponse.salt,- encryptionType: loginResponse.encryptionType,- encryptionSettings: loginResponse.encryptionSettings,- });-// Derive key from password using Argon2id and prepare credentialsconst { passwordHashString, passwordHashBase64 } = await SrpAuthService.prepareCredentials(credentials.password,@@ -167,8 +187,6 @@ const Login: React.FC = () => {loginResponse.encryptionSettings);- console.debug('[LOGIN DEBUG] Derived key (first 20 chars):', passwordHashBase64.substring(0, 20));-// Validate login with SRP protocolconst validationResponse = await srpUtil.validateLogin(normalizedUsername,@@ -304,8 +322,8 @@ const Login: React.FC = () => {encryptionSettings: result.encryptionSettings,});- // Initialize the database with the vault data- const sqliteClient = await dbContext.initializeDatabase(vaultResponse, result.decryptionKey);+ // Persist and load the vault+ const sqliteClient = await persistAndLoadVault(vaultResponse, result.decryptionKey);// Check for pending migrationstry {diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsxindex 9ca4a1de3..cb8a27090 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx@@ -219,12 +219,6 @@ const Unlock: React.FC = () => {// Online mode: get encryption params from server for key derivationconst loginResponse = await srpUtil.initiateLogin(authContext.username!);- console.debug('[UNLOCK DEBUG] Online mode - params from server:', {- salt: loginResponse.salt,- encryptionType: loginResponse.encryptionType,- encryptionSettings: loginResponse.encryptionSettings,- });-// Derive key from password using user's encryption settingsconst credentials = await SrpAuthService.prepareCredentials(password,@@ -234,8 +228,6 @@ const Unlock: React.FC = () => {);passwordHashBase64 = credentials.passwordHashBase64;- console.debug('[UNLOCK DEBUG] Online mode - derived key (first 20 chars):', passwordHashBase64.substring(0, 20));-// Store encryption params for future offline unlockawait dbContext.storeEncryptionKeyDerivationParams({salt: loginResponse.salt,@@ -246,9 +238,6 @@ const Unlock: React.FC = () => {// Offline mode: use stored encryption params to derive keyconst storedParams = await sendMessage('GET_ENCRYPTION_KEY_DERIVATION_PARAMS', {}, 'background') as EncryptionKeyDerivationParams | null;- console.debug('[UNLOCK DEBUG] Offline mode - stored params:', storedParams);- console.debug('[UNLOCK DEBUG] Offline mode - password length:', password.length);-if (!storedParams) {// No stored params - can't unlock offline without having logged in beforesetError(t('common.errors.serverNotAvailable'));@@ -256,13 +245,6 @@ const Unlock: React.FC = () => {return;}- console.debug('[UNLOCK DEBUG] Calling prepareCredentials with:', {- passwordLength: password.length,- salt: storedParams.salt,- encryptionType: storedParams.encryptionType,- encryptionSettings: storedParams.encryptionSettings,- });-// Derive key from password using stored encryption settingsconst credentials = await SrpAuthService.prepareCredentials(password,@@ -272,34 +254,23 @@ const Unlock: React.FC = () => {);passwordHashBase64 = credentials.passwordHashBase64;- console.debug('[UNLOCK DEBUG] Offline mode - derived key (first 20 chars):', passwordHashBase64.substring(0, 20));-// Set offline modeawait dbContext.setIsOffline(true);}// Store the encryption key in session storage.await dbContext.storeEncryptionKey(passwordHashBase64);- console.debug('[UNLOCK DEBUG] Stored encryption key, attempting to get vault...');- /*- * Always unlock from local vault first.- * The /reinitialize page will call syncVault which handles:- * - Checking if server has newer version- * - Merging local changes with server if hasPendingSync is true- * - Overwriting local with server if no local changes- */- const vaultResponse = await sendMessage('GET_VAULT', {}, 'background') as { success: boolean; vault?: string; error?: string };-- if (!vaultResponse.success || !vaultResponse.vault) {+ // Load the stored vault from background (decrypts using stored encryption key)+ const loaded = await dbContext.loadStoredDatabase();+ if (!loaded) {// Decryption failed - likely wrong passwordsetError(t('auth.errors.wrongPassword'));hideLoading();return;}- // Initialize SQLite client with the decrypted local vault- const sqliteClient = await dbContext.initializeDatabaseFromDecryptedVault(vaultResponse.vault);+ const sqliteClient = dbContext.sqliteClient!;// Check if there are pending migrationsif (await sqliteClient.hasPendingMigrations()) {@@ -393,15 +364,13 @@ const Unlock: React.FC = () => {* - Merging local changes with server if hasPendingSync is true* - Overwriting local with server if no local changes*/- const vaultResponse = await sendMessage('GET_VAULT', {}, 'background') as { success: boolean; vault?: string; error?: string };-- if (!vaultResponse.success || !vaultResponse.vault) {+ const loaded = await dbContext.loadStoredDatabase();+ if (!loaded) {// Decryption failed - likely wrong PINthrow new IncorrectPinError(3);}- // Initialize SQLite client with the decrypted local vault- const sqliteClient = await dbContext.initializeDatabaseFromDecryptedVault(vaultResponse.vault);+ const sqliteClient = dbContext.sqliteClient!;// Check if there are pending migrationsif (await sqliteClient.hasPendingMigrations()) {@@ -476,17 +445,15 @@ const Unlock: React.FC = () => {* - Merging local changes with server if hasPendingSync is true* - Overwriting local with server if no local changes*/- const vaultResponse = await sendMessage('GET_VAULT', {}, 'background') as { success: boolean; vault?: string; error?: string };-- if (!vaultResponse.success || !vaultResponse.vault) {+ const loaded = await dbContext.loadStoredDatabase();+ if (!loaded) {// Decryption failedsetError(t('common.errors.unknownErrorTryAgain'));hideLoading();return;}- // Initialize SQLite client with the decrypted local vault- const sqliteClient = await dbContext.initializeDatabaseFromDecryptedVault(vaultResponse.vault);+ const sqliteClient = dbContext.sqliteClient!;// Check if there are pending migrationsif (await sqliteClient.hasPendingMigrations()) {diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Upgrade.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Upgrade.tsxindex c1817fcb4..dc0691fd7 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Upgrade.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Upgrade.tsx@@ -161,7 +161,6 @@ const Upgrade: React.FC = () => {setError(error.message);}});- console.debug('executeVaultMutation done?');} catch (error) {console.error('Upgrade failed:', error);setError(error instanceof Error ? error.message : t('common.errors.unknownError'));diff --git a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsxindex 656730c74..3c387c57f 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx@@ -2,19 +2,19 @@ import React, { useState, useEffect, useCallback } from 'react';import { useTranslation } from 'react-i18next';import { useNavigate } from 'react-router-dom';-import ItemCard from '@/entrypoints/popup/components/Items/ItemCard';-import FolderCard from '@/entrypoints/popup/components/Items/FolderCard';import FolderModal from '@/entrypoints/popup/components/Folders/FolderModal';import HeaderButton from '@/entrypoints/popup/components/HeaderButton';import { HeaderIconType } from '@/entrypoints/popup/components/Icons/HeaderIcons';+import FolderCard from '@/entrypoints/popup/components/Items/FolderCard';+import ItemCard from '@/entrypoints/popup/components/Items/ItemCard';import LoadingSpinner from '@/entrypoints/popup/components/LoadingSpinner';import ReloadButton from '@/entrypoints/popup/components/ReloadButton';import { useApp } from '@/entrypoints/popup/context/AppContext';import { useDb } from '@/entrypoints/popup/context/DbContext';import { useHeaderButtons } from '@/entrypoints/popup/context/HeaderButtonsContext';import { useLoading } from '@/entrypoints/popup/context/LoadingContext';-import { useVaultSync } from '@/entrypoints/popup/hooks/useVaultSync';import { useVaultMutate } from '@/entrypoints/popup/hooks/useVaultMutate';+import { useVaultSync } from '@/entrypoints/popup/hooks/useVaultSync';import { PopoutUtility } from '@/entrypoints/popup/utils/PopoutUtility';import type { Item } from '@/utils/dist/shared/models/vault';diff --git a/apps/browser-extension/src/utils/SqliteClient.ts b/apps/browser-extension/src/utils/SqliteClient.tsindex 88111c047..d39e6d64c 100644--- a/apps/browser-extension/src/utils/SqliteClient.ts+++ b/apps/browser-extension/src/utils/SqliteClient.ts@@ -3,7 +3,7 @@ import initSqlJs, { Database } from 'sql.js';import * as dateFormatter from '@/utils/dateFormatter';import type { Credential, EncryptionKey, PasswordSettings, TotpCode, Passkey, Item, ItemField, ItemTagRef, FieldType, FieldHistory } from '@/utils/dist/shared/models/vault';import type { Attachment } from '@/utils/dist/shared/models/vault';-import { FieldKey, SystemFieldRegistry, getSystemField, MAX_FIELD_HISTORY_RECORDS } from '@/utils/dist/shared/models/vault';+import { FieldKey, getSystemField, MAX_FIELD_HISTORY_RECORDS } from '@/utils/dist/shared/models/vault';import type { VaultVersion } from '@/utils/dist/shared/vault-sql';import { VaultSqlGenerator, checkVersionCompatibility, extractVersionFromMigrationId } from '@/utils/dist/shared/vault-sql';import { VaultVersionIncompatibleError } from '@/utils/types/errors/VaultVersionIncompatibleError';@@ -2134,8 +2134,10 @@ export class SqliteClient {Value: string;}>(existingFieldValuesQuery, [item.Id]);- // Build a map of existing FieldValues by their effective key (FieldKey for system fields, FieldDefinitionId for custom)- // Key format: "fieldKey:index" to handle multi-value fields+ /*+ * Build a map of existing FieldValues by their effective key (FieldKey for system fields, FieldDefinitionId for custom)+ * Key format: "fieldKey:index" to handle multi-value fields+ */const existingByKey = new Map<string, { Id: string; Value: string }>();const fieldValueCounts = new Map<string, number>();diff --git a/apps/browser-extension/src/utils/types/messaging/StoreVaultRequest.ts b/apps/browser-extension/src/utils/types/messaging/StoreVaultRequest.tsdeleted file mode 100644index 195cac212..000000000--- a/apps/browser-extension/src/utils/types/messaging/StoreVaultRequest.ts+++ /dev/null@@ -1,12 +0,0 @@-export type StoreVaultRequest = {- vaultBlob: string;- publicEmailDomainList?: string[];- privateEmailDomainList?: string[];- hiddenPrivateEmailDomainList?: string[];- vaultRevisionNumber?: number;- /**- * Whether this vault has local changes not yet synced to server.- * Required to ensure callers explicitly track sync state.- */- hasPendingSync: boolean;-}