Skip to content

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

Add background worker ping check and increase actual vault get timeout (#2407)

Leendert de Borst5c1a246ad8ae
diff --git a/apps/browser-extension/src/entrypoints/background.ts b/apps/browser-extension/src/entrypoints/background.ts
index e46ac74a3..5116721cf 100644
--- a/apps/browser-extension/src/entrypoints/background.ts
+++ b/apps/browser-extension/src/entrypoints/background.ts
@@ -211,7 +211,10 @@ export default defineBackground({
}
});
- // Listen for messages via @webext-core/messaging
+ /*
+ * Listen for messages via @webext-core/messaging.
+ */
+ onMessage('PING', () => true);
onMessage('CHECK_AUTH_STATUS', () => handleCheckAuthStatus());
onMessage('GET_ENCRYPTION_KEY', () => handleGetEncryptionKey());
diff --git a/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx b/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx
index 014906f1e..0aa112bee 100644
--- a/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/context/DbContext.tsx
@@ -6,17 +6,35 @@ import { sendMessage } from '@/utils/messaging/ExtensionMessaging';
import SqliteClient from '@/utils/SqliteClient';
import { getItemWithFallback } from '@/utils/StorageUtility';
import { AppErrorCode, formatErrorWithCode } from '@/utils/types/errors/AppErrorCodes';
-import type { VaultResponse as messageVaultResponse } from '@/utils/types/messaging/VaultResponse';
import { markOwnEncryptionKey, vaultStateEvents } from '@/events/VaultStateEvents';
+import { t } from '@/i18n/StandaloneI18n';
import { storage } from '#imports';
/**
- * Maximum time to wait for the background service worker to respond to GET_VAULT.
- * If exceeded, the popup falls back to the unlock screen.
+ * Maximum time to wait for the background service worker to answer a PING before treating it as unresponsive.
*/
-const GET_VAULT_TIMEOUT_MS = 3500;
+const BACKGROUND_PING_TIMEOUT_MS = 5000;
+
+/**
+ * Maximum time to wait for GET_VAULT once the background is responsive (large vaults take a while to decrypt and transfer).
+ */
+const GET_VAULT_TIMEOUT_MS = 30000;
+
+/**
+ * Wrap a promise in a timeout that returns a rejected promise with a translated error carrying the given code.
+ */
+function withTimeout<T>(promise: Promise<T>, timeoutMs: number, code: AppErrorCode, translationKey: string): Promise<T> {
+ let timeoutId: ReturnType<typeof setTimeout> | undefined;
+ const timeoutPromise = new Promise<never>((_, reject) => {
+ timeoutId = setTimeout(() => {
+ void t(translationKey).then(message => reject(new Error(formatErrorWithCode(message, code))));
+ }, timeoutMs);
+ });
+
+ return Promise.race([promise, timeoutPromise]).finally(() => clearTimeout(timeoutId));
+}
/**
* Vault metadata including the server revision.
@@ -245,27 +263,13 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }
*/
const loadStoredDatabase = useCallback(async (): Promise<SqliteClient | null> => {
try {
- // Use timeout to prevent the popup from spinning indefinitely.
- let timeoutId: ReturnType<typeof setTimeout> | undefined;
- const timeoutPromise = new Promise<never>((_, reject) => {
- timeoutId = setTimeout(() => {
- reject(new Error(formatErrorWithCode('Background service worker timed out', AppErrorCode.UNKNOWN_ERROR)));
- }, GET_VAULT_TIMEOUT_MS);
- });
-
- let response: messageVaultResponse;
- try {
- response = await Promise.race([
- sendMessage('GET_VAULT'),
- timeoutPromise,
- ]);
- } finally {
- if (timeoutId !== undefined) {
- clearTimeout(timeoutId);
- }
- }
+ // Ping service worker with a short timeout so an unresponsive service worker fails fast instead of keeping the popup open indefinitely.
+ await withTimeout(sendMessage('PING'), BACKGROUND_PING_TIMEOUT_MS, AppErrorCode.BACKGROUND_UNRESPONSIVE, 'common.errors.backgroundUnresponsive');
+
+ // Get vault from background with a 30sec timeout as decrypting and transferring a large vault can take several seconds depending on the device hardware.
+ const response = await withTimeout(sendMessage('GET_VAULT'), GET_VAULT_TIMEOUT_MS, AppErrorCode.VAULT_LOAD_TIMEOUT, 'common.errors.vaultLoadTimeout');
- // Check if response contains an error - throw it so callers can handle
+ // Check if response contains an error, if so, throw.
if (!response?.success && response?.error) {
throw new Error(response.error);
}
diff --git a/apps/browser-extension/src/i18n/locales/en.json b/apps/browser-extension/src/i18n/locales/en.json
index a76b8e909..c04e44355 100644
--- a/apps/browser-extension/src/i18n/locales/en.json
+++ b/apps/browser-extension/src/i18n/locales/en.json
@@ -113,6 +113,8 @@
"mergeFailed": "Failed to merge vault changes. Please try again.",
"vaultTooLarge": "The vault is too large for the server to accept. Try to remove some items or attachments to reduce the size and try again.",
"vaultSyncTimeout": "Vault sync timed out. This can happen when the vault is large or the internet connection is slow. Please try again.",
+ "backgroundUnresponsive": "The extension background service is not responding. Please close and reopen the extension, or restart your browser if the problem persists.",
+ "vaultLoadTimeout": "Loading the vault took too long. Please close and reopen the extension and try again, or restart your browser if the problem persists.",
"invalidCode": "Please enter a valid 6-digit authentication code.",
"serverError": "Could not reach AliasVault server. Please try again later or contact support if the problem persists.",
"wrongPassword": "Incorrect password. Please try again.",
diff --git a/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts b/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts
index 445525bf2..59dcd342b 100644
--- a/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts
+++ b/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts
@@ -85,6 +85,7 @@ export interface IExtensionMessageProtocol {
OPEN_POPUP_WITH_ITEM(data: any): BoolResponse;
PASSKEY_POPUP_RESPONSE(data: any): { success: boolean };
PERSIST_FORM_VALUES(data: any): void;
+ PING(): boolean;
POPUP_HEARTBEAT(): void;
RESET_AUTO_LOCK_TIMER(): void;
SAVE_LOGIN_CREDENTIAL(data: { serviceName: string; username: string; password: string; url: string; domain: string; logoBase64?: string; faviconUrl?: string }): SaveLoginResponse;
diff --git a/apps/browser-extension/src/utils/types/errors/AppErrorCodes.ts b/apps/browser-extension/src/utils/types/errors/AppErrorCodes.ts
index d90813b5b..192fb3b61 100644
--- a/apps/browser-extension/src/utils/types/errors/AppErrorCodes.ts
+++ b/apps/browser-extension/src/utils/types/errors/AppErrorCodes.ts
@@ -23,6 +23,7 @@
export enum AppErrorCode {
// Generic errors (E-0xx)
UNKNOWN_ERROR = 'E-001',
+ BACKGROUND_UNRESPONSIVE = 'E-002', // Background service worker did not answer a PING in time
// Auth status check errors (E-1xx) - handleCheckAuthStatus
AUTH_STATUS_CHECK_FAILED = 'E-101',
@@ -34,6 +35,7 @@ export enum AppErrorCode {
VAULT_LOCKED = 'E-202', // No encryption key available
VAULT_DECRYPT_FAILED = 'E-203', // Decryption failed
VAULT_METADATA_READ_FAILED = 'E-204', // Failed to read vault metadata
+ VAULT_LOAD_TIMEOUT = 'E-205', // Background did not return the vault in time
// Item/credential operations (E-3xx) - handleCreateItem, handleUpdateItem, etc.
ITEM_CREATE_FAILED = 'E-301',
@@ -118,6 +120,7 @@ export function getErrorTranslationKey(code: AppErrorCode): string {
const codeToKeyMap: Record<AppErrorCode, string> = {
// Generic errors (E-0xx)
[AppErrorCode.UNKNOWN_ERROR]: 'common.errors.unknownErrorTryAgain',
+ [AppErrorCode.BACKGROUND_UNRESPONSIVE]: 'common.errors.backgroundUnresponsive',
// Auth status check errors (E-1xx)
[AppErrorCode.AUTH_STATUS_CHECK_FAILED]: 'common.errors.unknownErrorTryAgain',
@@ -129,6 +132,7 @@ export function getErrorTranslationKey(code: AppErrorCode): string {
[AppErrorCode.VAULT_LOCKED]: 'common.errors.vaultIsLocked',
[AppErrorCode.VAULT_DECRYPT_FAILED]: 'common.errors.unknownErrorTryAgain',
[AppErrorCode.VAULT_METADATA_READ_FAILED]: 'common.errors.unknownErrorTryAgain',
+ [AppErrorCode.VAULT_LOAD_TIMEOUT]: 'common.errors.vaultLoadTimeout',
// Item/credential operations (E-3xx)
[AppErrorCode.ITEM_CREATE_FAILED]: 'common.errors.unknownErrorTryAgain',