Skip to content

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

Refactor (#2159)

Leendert de Borstcfe286f1b3e6
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
index 6bdf8db4e..c7c6e3351 100644
--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
@@ -1320,7 +1320,7 @@ async function resealSharedManifestRecords(newEncryptionKey: string): Promise<vo
/**
* Adopt a server-side vault key this device does not know about yet.
*
- * A missing local encrypted-VEK cache means one of two things: this user is genuinely still legacy (their next full
+ * A missing local key-chain cache means one of two things: this user is genuinely still legacy (their next full
* push performs the KEK/VEK migration), or another device migrated while this one held the old password-derived key.
* Every path that adopts a VEK as the session key writes the encrypted-VEK cache first, so a session key that is
* already the VEK without a cache is not a reachable state -- it can only come from torn storage, which a re-login fixes.
@@ -1356,12 +1356,18 @@ async function adoptRemoteVaultKeyIfNeeded(): Promise<boolean> {
return true;
}
+ const encryptedVek = fetchResult.vaultKey.encryptedVek;
+ if (!encryptedVek) {
+ devWarn('[VaultSync] Error: vault key chain has no VEK.');
+ return true;
+ }
+
const encryptedVault = await storage.getItem(StorageKeys.ENCRYPTED_VAULT) as string | null;
try {
// Decrypt the encrypted Account Key and VEK.
const accountKey = await EncryptionUtility.decryptVaultEncryptionKey(fetchResult.vaultKey.encryptedAccountKey, sessionKey);
- const vek = fetchResult.vaultKey.encryptedVek ? await EncryptionUtility.decryptVaultEncryptionKey(fetchResult.vaultKey.encryptedVek, accountKey) : accountKey;
+ const vek = await EncryptionUtility.decryptVaultEncryptionKey(encryptedVek, accountKey);
// Re-encrypt the locally persisted vault with the VEK before swapping the session key.
if (encryptedVault) {
diff --git a/apps/browser-extension/src/utils/VaultKeyService.ts b/apps/browser-extension/src/utils/VaultKeyService.ts
index cca08e054..c5bee67f8 100644
--- a/apps/browser-extension/src/utils/VaultKeyService.ts
+++ b/apps/browser-extension/src/utils/VaultKeyService.ts
@@ -85,11 +85,7 @@ export class VaultKeyService {
* since the last login), which the background sync resolves by adopting the remote key before any vault work happens.
*/
public static async hasLocalVaultKey(): Promise<boolean> {
- if ((await storage.getItem(StorageKeys.ENCRYPTED_ACCOUNT_KEY) as string | null) !== null) {
- return true;
- }
- // Pre-encrypted VEK cache shape (encrypted VEK only), left behind by an older build of this extension.
- return (await storage.getItem(StorageKeys.ENCRYPTED_VEK) as string | null) !== null;
+ return (await storage.getItem(StorageKeys.ENCRYPTED_ACCOUNT_KEY) as string | null) !== null;
}
/**
@@ -181,45 +177,42 @@ export class VaultKeyService {
/**
* Decrypt the locally cached blob chain with the given KEK, staging the session private key when the chain
- * carries one. Falls back to the pre-encrypted VEK cache shape (a encrypted VEK alone), and to the derived key itself
- * when nothing is cached (legacy account).
+ * carries one. Falls back to the derived key itself when nothing is cached (legacy account).
* @param derivedKeyBase64 - the password-derived key (the KEK)
*/
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;
-
- if (encryptedAccountKey) {
- 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 vek;
+ if (!encryptedAccountKey) {
+ return derivedKeyBase64;
}
- if (encryptedVek) {
- // Pre-encrypted VEK cache: the VEK was encrypted directly with the KEK.
- return VaultKeyService.decryptKeyOrThrow(encryptedVek, derivedKeyBase64);
+ const encryptedVek = (await storage.getItem(StorageKeys.ENCRYPTED_VEK)) as string | null;
+ if (!encryptedVek) {
+ throw new Error('Cached vault key chain is missing the encrypted VEK');
}
- return derivedKeyBase64;
+ const accountKey = await VaultKeyService.decryptKeyOrThrow(encryptedAccountKey, derivedKeyBase64);
+ const vek = await VaultKeyService.decryptKeyOrThrow(encryptedVek, accountKey);
+ await VaultKeyService.stageSessionPrivateKey(accountKey, (await storage.getItem(StorageKeys.ENCRYPTED_ACCOUNT_PRIVATE_KEY)) as string | null);
+ return vek;
}
/**
- * Decrypt a fresh server response's chain with the KEK: AK first, then the VEK (or the AK itself for a
- * transitional account where AK ≡ VEK), staging the session private key when the account has a keypair.
+ * Decrypt a fresh server response's chain with the KEK: AK first, then the VEK, staging the session private
+ * key when the account has a keypair.
* @param vaultKey - the server's vault key response
* @param derivedKeyBase64 - the password-derived KEK
*/
private static async decryptKeyChainOrThrow(vaultKey: VaultKeyResponse, derivedKeyBase64: string): Promise<string> {
const accountKey = await VaultKeyService.decryptKeyOrThrow(vaultKey.encryptedAccountKey, derivedKeyBase64);
- const vek = vaultKey.encryptedVek ? await VaultKeyService.decryptKeyOrThrow(vaultKey.encryptedVek, accountKey) : accountKey;
+ const vek = await VaultKeyService.decryptKeyOrThrow(vaultKey.encryptedVek!, accountKey);
await VaultKeyService.stageSessionPrivateKey(accountKey, vaultKey.encryptedAccountPrivateKey ?? null);
return vek;
}
/**
* Decrypt the account private key with the AK into session storage, so pull/grant flows can decrypt shared
- * VEKs. A chain without a keypair (transitional account) clears any stale session copy instead.
+ * VEKs. A chain without a keypair clears any stale session copy instead.
* @param accountKeyBase64 - the encrypted Account Key
* @param encryptedAccountPrivateKey - the account private key encrypted with the Account Key, or null when the account has none yet
*/
@@ -242,12 +235,11 @@ export class VaultKeyService {
* @param vaultKey - the server's vault key response
*/
public static async cacheVaultKeyBlobs(vaultKey: VaultKeyResponse): Promise<void> {
- await storage.setItem(StorageKeys.ENCRYPTED_ACCOUNT_KEY, vaultKey.encryptedAccountKey);
- if (vaultKey.encryptedVek) {
- await storage.setItem(StorageKeys.ENCRYPTED_VEK, vaultKey.encryptedVek);
- } else {
- await storage.removeItem(StorageKeys.ENCRYPTED_VEK);
- }
+ await storage.setItems([
+ { key: StorageKeys.ENCRYPTED_ACCOUNT_KEY, value: vaultKey.encryptedAccountKey },
+ { key: StorageKeys.ENCRYPTED_VEK, value: vaultKey.encryptedVek },
+ ]);
+
if (vaultKey.accountPublicKey && vaultKey.encryptedAccountPrivateKey) {
await storage.setItem(StorageKeys.ACCOUNT_PUBLIC_KEY, vaultKey.accountPublicKey);
await storage.setItem(StorageKeys.ENCRYPTED_ACCOUNT_PRIVATE_KEY, vaultKey.encryptedAccountPrivateKey);
diff --git a/apps/browser-extension/tests/helpers/manifest-v2-api.ts b/apps/browser-extension/tests/helpers/manifest-v2-api.ts
index 5198d3b26..9238a181c 100644
--- a/apps/browser-extension/tests/helpers/manifest-v2-api.ts
+++ b/apps/browser-extension/tests/helpers/manifest-v2-api.ts
@@ -102,8 +102,12 @@ export async function resolveVaultEncryptionKey(apiBaseUrl: string, token: strin
return derivedKey;
}
+ if (!vaultKey.encryptedVek) {
+ throw new Error('Vault key chain is missing the encrypted VEK');
+ }
+
const accountKey = await symmetricDecryptBytes(vaultKey.encryptedAccountKey, derivedKey);
- return vaultKey.encryptedVek ? symmetricDecryptBytes(vaultKey.encryptedVek, accountKey) : accountKey;
+ return symmetricDecryptBytes(vaultKey.encryptedVek, accountKey);
}
/**
diff --git a/apps/server/AliasVault.Client/Services/VaultSync/VaultKeyService.cs b/apps/server/AliasVault.Client/Services/VaultSync/VaultKeyService.cs
index 1ce52e8ba..9f8a6d582 100644
--- a/apps/server/AliasVault.Client/Services/VaultSync/VaultKeyService.cs
+++ b/apps/server/AliasVault.Client/Services/VaultSync/VaultKeyService.cs
@@ -257,17 +257,17 @@ public sealed class VaultKeyService(HttpClient httpClient, ILocalStorageService
}
/// <summary>
- /// Walk the chain: AK first, then the VEK (or the AK itself for a transitional account where AK equals VEK), then the account private key.
+ /// Walk the chain: AK first, then the VEK, then the account private key.
/// </summary>
/// <param name="encryptedAccountKey">The AK encrypted with the KEK.</param>
- /// <param name="encryptedVek">The VEK encrypted with the AK, or null for a transitional account.</param>
+ /// <param name="encryptedVek">The VEK encrypted with the AK.</param>
/// <param name="encryptedAccountPrivateKey">The account private key encrypted with the AK, or null when the account has no keypair.</param>
/// <param name="derivedKeyBase64">The password-derived KEK.</param>
/// <returns>The resolved keys.</returns>
private async Task<ResolvedVaultKey> DecryptKeyChainAsync(string encryptedAccountKey, string? encryptedVek, string? encryptedAccountPrivateKey, string derivedKeyBase64)
{
var accountKey = await DecryptKeyOrThrowAsync(encryptedAccountKey, derivedKeyBase64);
- var vek = string.IsNullOrEmpty(encryptedVek) ? accountKey : await DecryptKeyOrThrowAsync(encryptedVek, accountKey);
+ var vek = await DecryptKeyOrThrowAsync(encryptedVek!, accountKey);
return new ResolvedVaultKey(vek, await DecryptPrivateKeyAsync(encryptedAccountPrivateKey, accountKey), false);
}