Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Improve vault upgrade unknown vault version flow in browser extension (#1274)
Leendert de Borstcad95e779db3
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.tsindex 9139c00af..1f0700adb 100644--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts@@ -4,6 +4,7 @@ import { storage } from 'wxt/utils/storage';import type { EncryptionKeyDerivationParams } from '@/utils/dist/shared/models/metadata';import type { Vault, VaultResponse, VaultPostResponse } from '@/utils/dist/shared/models/webapi';import { EncryptionUtility } from '@/utils/EncryptionUtility';+import { VaultVersionIncompatibleError } from '@/utils/errors/VaultVersionError';import { SqliteClient } from '@/utils/SqliteClient';import { BoolResponse as messageBoolResponse } from '@/utils/types/messaging/BoolResponse';import { CredentialsResponse as messageCredentialsResponse } from '@/utils/types/messaging/CredentialsResponse';@@ -57,6 +58,18 @@ export async function handleCheckAuthStatus() : Promise<{ isLoggedIn: boolean, i};} catch (error) {console.error('Error checking pending migrations:', error);++ // If it's a version incompatibility error, we need to handle it specially+ if (error instanceof VaultVersionIncompatibleError) {+ // Return the error so the UI can handle it appropriately (logout user)+ return {+ isLoggedIn,+ isVaultLocked,+ hasPendingMigrations: false,+ error: error.message+ };+ }+return {isLoggedIn,isVaultLocked,@@ -489,7 +502,7 @@ async function uploadNewVaultToServer(sqliteClient: SqliteClient) : Promise<Vaulclient: '', // Empty on purpose, API will not use this for vault updates.updatedAt: new Date().toISOString(),username: username,- version: sqliteClient.getDatabaseVersion().version+ version: (await sqliteClient.getDatabaseVersion()).version};const webApi = new WebApiService(() => {});diff --git a/apps/browser-extension/src/entrypoints/popup/hooks/useVaultSync.ts b/apps/browser-extension/src/entrypoints/popup/hooks/useVaultSync.tsindex 1c15cd72d..f0b598174 100644--- a/apps/browser-extension/src/entrypoints/popup/hooks/useVaultSync.ts+++ b/apps/browser-extension/src/entrypoints/popup/hooks/useVaultSync.ts@@ -8,6 +8,7 @@ import { useWebApi } from '@/entrypoints/popup/context/WebApiContext';import type { EncryptionKeyDerivationParams } from '@/utils/dist/shared/models/metadata';import type { VaultResponse } from '@/utils/dist/shared/models/webapi';+import { VaultVersionIncompatibleError } from '@/utils/errors/VaultVersionError';/*** Utility function to ensure a minimum time has elapsed for an operation@@ -142,8 +143,9 @@ export const useVaultSync = () : {return true;} catch (error) {// Check if it's a version-related error (app needs to be updated)- if (error instanceof Error && error.message.includes('This browser extension is outdated')) {+ if (error instanceof VaultVersionIncompatibleError) {await webApi.logout(error.message);+ // Redirect to logoutonError?.(error.message);return false;}@@ -165,7 +167,7 @@ export const useVaultSync = () : {console.error('Vault sync error:', err);// Check if it's a version-related error (app needs to be updated)- if (errorMessage.includes('This browser extension is outdated')) {+ if (err instanceof VaultVersionIncompatibleError) {await webApi.logout(errorMessage);onError?.(errorMessage);return false;diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsxindex 74978d485..9fa9f08e2 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx@@ -18,6 +18,7 @@ import SrpUtility from '@/entrypoints/popup/utils/SrpUtility';import { VAULT_LOCKED_DISMISS_UNTIL_KEY } from '@/utils/Constants';import type { VaultResponse } from '@/utils/dist/shared/models/webapi';import EncryptionUtility from '@/utils/EncryptionUtility';+import { VaultVersionIncompatibleError } from '@/utils/errors/VaultVersionError';import { storage } from '#imports';@@ -42,6 +43,7 @@ const Unlock: React.FC = () => {useEffect(() => {/*** Make status call to API which acts as health check.+ * This runs only once during component mount.*/const checkStatus = async () : Promise<void> => {const statusResponse = await webApi.getStatus();@@ -54,7 +56,8 @@ const Unlock: React.FC = () => {};checkStatus();- }, [webApi, authContext, setIsInitialLoading, navigate, t]);+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, []); // Only run once on mount// Set header buttons on mount and clear on unmountuseEffect((): (() => void) => {@@ -110,7 +113,14 @@ const Unlock: React.FC = () => {await dbContext.storeEncryptionKey(passwordHashBase64);// Initialize the SQLite context with the new vault data.- await dbContext.initializeDatabase(vaultResponseJson, passwordHashBase64);+ const sqliteClient = await dbContext.initializeDatabase(vaultResponseJson, passwordHashBase64);++ // Check if there are pending migrations+ if (await sqliteClient.hasPendingMigrations()) {+ navigate('/upgrade', { replace: true });+ hideLoading();+ return;+ }// Clear dismiss until (which can be enabled after user has dimissed vault is locked popup) to ensure popup is shown.await storage.setItem(VAULT_LOCKED_DISMISS_UNTIL_KEY, 0);@@ -118,7 +128,13 @@ const Unlock: React.FC = () => {// Redirect to reinitialize pagenavigate('/reinitialize', { replace: true });} catch (err) {- setError(t('auth.errors.wrongPassword'));+ // Check if it's a version incompatibility error+ if (err instanceof VaultVersionIncompatibleError) {+ await webApi.logout(err.message);+ navigate('/logout');+ } else {+ setError(t('auth.errors.wrongPassword'));+ }console.error('Unlock error:', err);} finally {hideLoading();diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Upgrade.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Upgrade.tsxindex 4762f4084..44e1653e0 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Upgrade.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Upgrade.tsx@@ -65,7 +65,7 @@ const Upgrade: React.FC = () => {const loadVersionInfo = useCallback(async () => {try {if (sqliteClient) {- const current = sqliteClient.getDatabaseVersion();+ const current = await sqliteClient.getDatabaseVersion();const latest = await sqliteClient.getLatestDatabaseVersion();setCurrentVersion(current);setLatestVersion(latest);diff --git a/apps/browser-extension/src/utils/SqliteClient.ts b/apps/browser-extension/src/utils/SqliteClient.tsindex 5ff80e52f..cd5029e50 100644--- a/apps/browser-extension/src/utils/SqliteClient.ts+++ b/apps/browser-extension/src/utils/SqliteClient.ts@@ -4,6 +4,9 @@ import type { Credential, EncryptionKey, PasswordSettings, TotpCode } from '@/utimport type { Attachment } from '@/utils/dist/shared/models/vault';import type { VaultVersion } from '@/utils/dist/shared/vault-sql';import { VaultSqlGenerator } from '@/utils/dist/shared/vault-sql';+import { VaultVersionIncompatibleError } from '@/utils/errors/VaultVersionError';++import { t } from '@/i18n/StandaloneI18n';/*** Placeholder base64 image for credentials without a logo.@@ -558,7 +561,7 @@ export class SqliteClient {* Returns the semantic version (e.g., "1.4.1") from the latest migration.* Returns null if no migrations are found.*/- public getDatabaseVersion(): VaultVersion {+ public async getDatabaseVersion(): Promise<VaultVersion> {if (!this.db) {throw new Error('Database not initialized');}@@ -591,7 +594,8 @@ export class SqliteClient {const currentVersionRevision = allVersions.find(v => v.version === currentVersion);if (!currentVersionRevision) {- throw new Error('This browser extension is outdated and cannot be used to access this vault. Please update this browser extension to continue.');+ const errorMessage = await t('common.errors.browserExtensionOutdated');+ throw new VaultVersionIncompatibleError(errorMessage);}return currentVersionRevision;@@ -617,7 +621,7 @@ export class SqliteClient {*/public async hasPendingMigrations(): Promise<boolean> {try {- const currentVersion = this.getDatabaseVersion();+ const currentVersion = await this.getDatabaseVersion();const latestVersion = await this.getLatestDatabaseVersion();return currentVersion.revision < latestVersion.revision;diff --git a/apps/browser-extension/src/utils/errors/VaultVersionError.ts b/apps/browser-extension/src/utils/errors/VaultVersionError.tsnew file mode 100644index 000000000..5dceb0865--- /dev/null+++ b/apps/browser-extension/src/utils/errors/VaultVersionError.ts@@ -0,0 +1,15 @@+/**+ * Custom error class for vault version incompatibility issues.+ * Thrown when the browser extension is outdated and cannot handle the vault version.+ */+export class VaultVersionIncompatibleError extends Error {+ /**+ * Creates a new instance of the VaultVersionIncompatibleError class.+ * @param message - The error message.+ */+ public constructor(message: string) {+ super(message);+ this.name = 'VaultVersionIncompatibleError';+ Object.setPrototypeOf(this, VaultVersionIncompatibleError.prototype);+ }+}