Skip to content

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

Add trusted extension sender check for sensitive operations (#2159)

Leendert de Borstf36cad06c3ca
diff --git a/apps/browser-extension/src/entrypoints/background.ts b/apps/browser-extension/src/entrypoints/background.ts
index 168801f8a..a9745108f 100644
--- a/apps/browser-extension/src/entrypoints/background.ts
+++ b/apps/browser-extension/src/entrypoints/background.ts
@@ -83,6 +83,24 @@ function withTrustedWebAuthnSender<T, U>(
return handle(senderContext);
}
+/**
+ * True when the message comes from one of the extension's own pages (popup, expanded tab, passkey window),
+ * false for content scripts running inside web pages. Sensitive operations must never be serviceable from a content-script context.
+ */
+function isTrustedExtensionSender(sender: WebAuthnMessageSender): boolean {
+ const senderOrigin = typeof sender.origin === 'string' && sender.origin !== 'null' ? sender.origin : undefined;
+ const senderUrl = senderOrigin ?? sender.url ?? sender.tab?.url;
+ if (!senderUrl) {
+ return false;
+ }
+
+ try {
+ return new URL(senderUrl).origin === new URL(browser.runtime.getURL('')).origin;
+ } catch {
+ return false;
+ }
+}
+
/**
* Notify content scripts in all tabs that the vault was unlocked, so any conditional passkey
* request parked while the vault was locked can re-query and surface its passkeys.
@@ -214,7 +232,7 @@ export default defineBackground({
// Listen for messages via @webext-core/messaging
onMessage('CHECK_AUTH_STATUS', () => handleCheckAuthStatus());
- onMessage('GET_ENCRYPTION_KEY', () => handleGetEncryptionKey());
+ onMessage('GET_ENCRYPTION_KEY', ({ sender }) => isTrustedExtensionSender(sender) ? handleGetEncryptionKey() : null);
onMessage('GET_ENCRYPTION_KEY_DERIVATION_PARAMS', () => handleGetEncryptionKeyDerivationParams());
onMessage('GET_VAULT', () => handleGetVault());
onMessage('GET_FILTERED_ITEMS', ({ data }) => handleGetFilteredItems(data));
@@ -226,7 +244,11 @@ export default defineBackground({
onMessage('GENERATE_PASSWORD', ({ data }) => handleGeneratePassword(data.settings));
onMessage('STORE_VAULT_METADATA', ({ data }) => handleStoreVaultMetadata(data));
- onMessage('STORE_ENCRYPTION_KEY', async ({ data }) => {
+ onMessage('STORE_ENCRYPTION_KEY', async ({ data, sender }) => {
+ if (!isTrustedExtensionSender(sender)) {
+ return { success: false };
+ }
+
const result = await handleStoreEncryptionKey(data);
/*
* Storing the encryption key means the vault just became unlocked; let content scripts
@@ -259,7 +281,7 @@ export default defineBackground({
onMessage('TOGGLE_CONTEXT_MENU', ({ data }) => handleToggleContextMenu(data));
onMessage('PERSIST_FORM_VALUES', ({ data }) => handlePersistFormValues(data));
- onMessage('GET_PERSISTED_FORM_VALUES', () => handleGetPersistedFormValues());
+ onMessage('GET_PERSISTED_FORM_VALUES', ({ sender }) => isTrustedExtensionSender(sender) ? handleGetPersistedFormValues() : null);
onMessage('CLEAR_PERSISTED_FORM_VALUES', () => handleClearPersistedFormValues());
// Remember login save messages
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
index c58f14d05..79a2d1888 100644
--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
@@ -56,15 +56,23 @@ import { t } from '@/i18n/StandaloneI18n';
*
* Cache Strategy:
* - Local mutations (createCredential, etc.): Work directly on cachedSqliteClient, no cache clearing
- * - New vault from remote (login, sync): Clear cache by setting both to null
- * - Logout/clear vault: Clear cache by setting both to null
- *
- * The cache is cleared by setting cachedSqliteClient and cachedVaultBlob to null directly
- * in the functions that receive new vault data from external sources.
+ * - New vault from remote (login, sync): Clear cache by setting both to null, WITHOUT closing — an in-flight
+ * flow (e.g. a push holding the client across an HTTP await, or persistLocalVaultMutation re-adopting the
+ * client it just stored) may legitimately still use the detached instance.
+ * - Lock/logout/clear vault: cleanupCachedSqliteClient().
*/
let cachedSqliteClient: SqliteClient | null = null;
let cachedVaultBlob: string | null = null;
+/**
+ * Cleanup the cached decrypted vault database and drop the cache.
+ */
+function cleanupCachedSqliteClient(): void {
+ cachedSqliteClient?.close();
+ cachedSqliteClient = null;
+ cachedVaultBlob = null;
+}
+
/**
* Whether the client has to pull and re-materialize, i.e. whether any manifest's local state no longer matches what
* the server reports.
@@ -358,6 +366,7 @@ export async function handleGetVault(
*/
export async function handleLockVault(): Promise<messageBoolResponse> {
await storage.removeItems([...VAULT_LOCK_STORAGE_KEYS]);
+ cleanupCachedSqliteClient();
return { success: true };
}
@@ -378,9 +387,8 @@ export async function handleClearSession(): Promise<messageBoolResponse> {
// Reset password unlock failed attempts counter on logout
await LocalPreferencesService.resetPasswordUnlockFailedAttempts();
- // Clear cached client since session ended
- cachedSqliteClient = null;
- cachedVaultBlob = null;
+ // Cleanup cached sqlite client
+ cleanupCachedSqliteClient();
return { success: true };
}
@@ -399,6 +407,9 @@ export async function handleClearVaultData(): Promise<messageBoolResponse> {
// Clear all local preferences (site settings, login save settings, etc.)
await LocalPreferencesService.clearAll();
+ // Free the decrypted vault held in service-worker memory alongside the persisted data it came from.
+ closeCachedSqliteClient();
+
return { success: true };
}