Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update VaultKeyService.ts (#2159)
Leendert de Borst45bae16560bc
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsxindex b319e588c..828560f8b 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx@@ -103,7 +103,7 @@ const Login: React.FC = () => {* KEK/VEK: for migrated accounts the derived key is only the KEK; fetch the vault key and decrypt the VEK,* which becomes the session encryption key. Legacy accounts keep using the derived key directly.*/- const { encryptionKey } = await VaultKeyService.resolveEncryptionKey(passwordHashBase64, webApi);+ const encryptionKey = await VaultKeyService.resolveEncryptionKey(passwordHashBase64, webApi);// Store the encryption key and derivation params separatelyawait dbContext.storeEncryptionKey(encryptionKey);diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsxindex e27869b87..a06b2fc12 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx@@ -297,7 +297,7 @@ const Unlock: React.FC = () => {* vault encryption key. Throws a decrypt-failed (E-203) error on a wrong password. Legacy accounts keep* using the derived key directly.*/- passwordHashBase64 = (await VaultKeyService.resolveEncryptionKey(credentials.passwordHashBase64, webApi)).encryptionKey;+ passwordHashBase64 = await VaultKeyService.resolveEncryptionKey(credentials.passwordHashBase64, webApi);} else {// Offline mode: use stored encryption params to derive keyconst storedParams = await sendMessage('GET_ENCRYPTION_KEY_DERIVATION_PARAMS');diff --git a/apps/browser-extension/src/utils/VaultKeyService.ts b/apps/browser-extension/src/utils/VaultKeyService.tsindex eea414c5e..745d39c8b 100644--- a/apps/browser-extension/src/utils/VaultKeyService.ts+++ b/apps/browser-extension/src/utils/VaultKeyService.ts@@ -11,14 +11,6 @@ import { ApiRequestError } from '@/utils/types/errors/ApiRequestError';import { AppErrorCode, formatErrorWithCode } from '@/utils/types/errors/AppErrorCodes';import { WebApiService } from '@/utils/WebApiService';-/**- * Result of resolving the vault encryption key after deriving the password key.- */-export type ResolvedEncryptionKey = {- encryptionKey: string;- encryptedVek: string | null;-};-/*** Result of fetching the vault key from the server.*/@@ -56,7 +48,7 @@ export class VaultKeyService {* private key. All encrypted blobs are cached for offline unlock. For legacy users (server explicitly* reports no vault key) the derived key itself is the encryption key and any stale cached chain is cleared.*/- public static async resolveEncryptionKey(derivedKeyBase64: string, webApi?: WebApiService): Promise<ResolvedEncryptionKey> {+ public static async resolveEncryptionKey(derivedKeyBase64: string, webApi?: WebApiService): Promise<string> {const result = await VaultKeyService.fetchVaultKey(webApi);if (!result.supported) {@@ -66,12 +58,12 @@ export class VaultKeyService {if (!result.vaultKey) {await storage.removeItems([StorageKeys.ENCRYPTED_VEK, StorageKeys.ENCRYPTED_ACCOUNT_KEY, StorageKeys.ACCOUNT_PUBLIC_KEY, StorageKeys.ENCRYPTED_ACCOUNT_PRIVATE_KEY]);- return { encryptionKey: derivedKeyBase64, encryptedVek: null };+ return derivedKeyBase64;}const vek = await VaultKeyService.decryptKeyChainOrThrow(result.vaultKey, derivedKeyBase64);await VaultKeyService.cacheVaultKeyBlobs(result.vaultKey);- return { encryptionKey: vek, encryptedVek: result.vaultKey.encryptedAccountKey };+ return vek;}/**@@ -81,7 +73,7 @@ export class VaultKeyService {* @throws Error with {@link AppErrorCode.VAULT_DECRYPT_FAILED} when decryption fails (wrong password).*/public static async resolveEncryptionKeyOffline(derivedKeyBase64: string): Promise<string> {- return (await VaultKeyService.resolveFromLocalCache(derivedKeyBase64)).encryptionKey;+ return VaultKeyService.resolveFromLocalCache(derivedKeyBase64);}/**@@ -164,9 +156,9 @@ export class VaultKeyService {*/private static async upgradeStoredKeyIfNeeded(storedKey: string): Promise<{ key: string; upgraded: boolean }> {try {- const resolved = await VaultKeyService.resolveFromLocalCache(storedKey);- if (resolved.encryptedVek !== null && resolved.encryptionKey !== storedKey) {- return { key: resolved.encryptionKey, upgraded: true };+ const resolvedKey = await VaultKeyService.resolveFromLocalCache(storedKey);+ if (resolvedKey !== storedKey) {+ return { key: resolvedKey, upgraded: true };}return { key: storedKey, upgraded: false };} catch {@@ -180,7 +172,7 @@ export class VaultKeyService {* when nothing is cached (legacy account).* @param derivedKeyBase64 - the password-derived key (the KEK)*/- private static async resolveFromLocalCache(derivedKeyBase64: string): Promise<ResolvedEncryptionKey> {+ private static async resolveFromLocalCache(derivedKeyBase64: string): Promise<string> {const encryptedAccountKey = (await storage.getItem(StorageKeys.ENCRYPTED_ACCOUNT_KEY)) as string | null;const encryptedVek = (await storage.getItem(StorageKeys.ENCRYPTED_VEK)) as string | null;@@ -188,15 +180,15 @@ export class VaultKeyService {const accountKey = await VaultKeyService.decryptKeyOrThrow(encryptedAccountKey, derivedKeyBase64);const vek = encryptedVek ? await VaultKeyService.decryptKeyOrThrow(encryptedVek, accountKey) : accountKey;await VaultKeyService.stageSessionPrivateKey(accountKey, (await storage.getItem(StorageKeys.ENCRYPTED_ACCOUNT_PRIVATE_KEY)) as string | null);- return { encryptionKey: vek, encryptedVek: encryptedAccountKey };+ return vek;}if (encryptedVek) {// Pre-encrypted VEK cache: the VEK was encrypted directly with the KEK.- return { encryptionKey: await VaultKeyService.decryptKeyOrThrow(encryptedVek, derivedKeyBase64), encryptedVek };+ return VaultKeyService.decryptKeyOrThrow(encryptedVek, derivedKeyBase64);}- return { encryptionKey: derivedKeyBase64, encryptedVek: null };+ return derivedKeyBase64;}/**diff --git a/apps/browser-extension/src/utils/constants/legacyStorageKeys.ts b/apps/browser-extension/src/utils/constants/legacyStorageKeys.tsindex a3e1874ea..09bfd4344 100644--- a/apps/browser-extension/src/utils/constants/legacyStorageKeys.ts+++ b/apps/browser-extension/src/utils/constants/legacyStorageKeys.ts@@ -13,14 +13,15 @@ import type { StorageKey } from '@/utils/constants/storageKeys';export const LegacyStorageKeys = {/** Pre-v0.22 encryption key location; read as a fallback for `StorageKeys.ENCRYPTION_KEY`. */DERIVED_KEY: 'session:derivedKey',- /** Pre-v0.31 single server revision, superseded by the per-manifest `StorageKeys.SERVER_MANIFEST_REVISIONS`. */- SERVER_REVISION: 'local:serverRevision',/** Pre-v0.26 Session locations migrated to local (see readLegacyStorageFallback) */SESSION_PUBLIC_EMAIL_DOMAINS: 'session:publicEmailDomains',SESSION_PRIVATE_EMAIL_DOMAINS: 'session:privateEmailDomains',SESSION_HIDDEN_PRIVATE_EMAIL_DOMAINS: 'session:hiddenPrivateEmailDomains',SESSION_ENCRYPTION_KEY_DERIVATION_PARAMS: 'session:encryptionKeyDerivationParams',++ /** Pre-v0.31 single server revision, superseded by the per-manifest `StorageKeys.SERVER_MANIFEST_REVISIONS`. */+ SERVER_REVISION: 'local:serverRevision',} as const satisfies Record<string, StorageKey>;/**