Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update browser extension tests (#2469)
Leendert de Borst32b2eb2ba016
diff --git a/apps/browser-extension/tests/e2e/33-forward-compat-overflow.spec.ts b/apps/browser-extension/tests/e2e/33-forward-compat-overflow.spec.tsindex f8df46397..c1fe2de4e 100644--- a/apps/browser-extension/tests/e2e/33-forward-compat-overflow.spec.ts+++ b/apps/browser-extension/tests/e2e/33-forward-compat-overflow.spec.ts@@ -134,6 +134,6 @@ async function openLatestManifest(apiUrl: string, token: string, derivedKey: Uinconst snapshot = await getVaultSnapshot(apiUrl, token);const personal = requirePersonalManifest(snapshot);const vaultKey = await resolveVaultEncryptionKey(apiUrl, token, derivedKey);- return { manifest: await openManifest(personal.blob, vaultKey), personal, vaultKey };+ return { manifest: await openManifest(personal.manifestId, personal.blob, vaultKey), personal, vaultKey };});}diff --git a/apps/browser-extension/tests/helpers/manifest-v2-api.ts b/apps/browser-extension/tests/helpers/manifest-v2-api.tsindex 19a0b4165..9d1e4fdc2 100644--- a/apps/browser-extension/tests/helpers/manifest-v2-api.ts+++ b/apps/browser-extension/tests/helpers/manifest-v2-api.ts@@ -12,7 +12,7 @@ import { createHash } from 'crypto';import { SrpAuthService } from '@aliasvault/client/auth/SrpAuthService';import { getSyncableTableNames, openAccountKeyChain, vaultCodecCanonicalizeFromSqlite, vaultCodecGenerateManifestSalt, vaultCodecPackPayload, vaultCodecUnpackPayload } from '@aliasvault/client/rust/RustCore';-import { symmetricDecryptBytes, symmetricEncryptBytes } from './vault-crypto';+import { bucketAad, manifestAad, symmetricDecryptBytes, symmetricEncryptBytes } from './vault-crypto';/** One manifest entry in the v2 GET snapshot. */export type SnapshotManifest = {@@ -141,12 +141,13 @@ export async function resolveVaultEncryptionKey(apiBaseUrl: string, token: strin/*** Decrypts and unpacks a manifest blob into its JSON object (content hash verified by the codec).*+ * @param manifestId - The manifest the blob belongs to* @param blobBase64 - The encrypted manifest blob from the snapshot* @param encryptionKey - The key the vault content is encrypted with, from `resolveVaultEncryptionKey`* @returns The decrypted manifest object*/-export async function openManifest(blobBase64: string, encryptionKey: Uint8Array): Promise<DecryptedManifest> {- const packedBytes = await symmetricDecryptBytes(blobBase64, encryptionKey);+export async function openManifest(manifestId: string, blobBase64: string, encryptionKey: Uint8Array): Promise<DecryptedManifest> {+ const packedBytes = await symmetricDecryptBytes(blobBase64, encryptionKey, manifestAad(manifestId));return JSON.parse(await vaultCodecUnpackPayload(packedBytes)) as DecryptedManifest;}@@ -232,7 +233,7 @@ export async function pushInitialVault(apiBaseUrl: string, token: string, usernaconst bucketWrites: BucketWrite[] = [];for (const bucket of canonicalized.dataBuckets) {- const { blob, ciphertextHash } = await packEncrypt(JSON.stringify(bucket), encryptionKey);+ const { blob, ciphertextHash } = await packEncrypt(JSON.stringify(bucket), encryptionKey, bucketAad(bucket.manifestId, bucket.category));bucketWrites.push({ manifestId: bucket.manifestId, category: bucket.category, blob, ciphertextHash, currentRevision: 0 });}@@ -270,11 +271,12 @@ export async function pollUntil<T>(predicate: () => Promise<T | undefined | fals** @param payloadJson - The payload to pack* @param encryptionKey - The key the vault content is encrypted with+ * @param aad - The associated data the ciphertext is bound to* @returns The encrypted blob and the ciphertext hash the server verifies it against*/-async function packEncrypt(payloadJson: string, encryptionKey: Uint8Array): Promise<{ blob: string; ciphertextHash: string }> {+async function packEncrypt(payloadJson: string, encryptionKey: Uint8Array, aad: Uint8Array): Promise<{ blob: string; ciphertextHash: string }> {const packedBytes = await vaultCodecPackPayload(payloadJson);- const blob = await symmetricEncryptBytes(packedBytes, encryptionKey);+ const blob = await symmetricEncryptBytes(packedBytes, encryptionKey, aad);return { blob, ciphertextHash: createHash('sha256').update(Buffer.from(blob, 'base64')).digest('hex') };}@@ -295,7 +297,7 @@ async function buildManifestWrite(blobReferences: Array<{ hash: string; category: string }>,encryptionKey: Uint8Array): Promise<ManifestWrite> {- const { blob, ciphertextHash } = await packEncrypt(JSON.stringify(manifest), encryptionKey);+ const { blob, ciphertextHash } = await packEncrypt(JSON.stringify(manifest), encryptionKey, manifestAad(manifestId));return {manifestId,manifestBlob: blob,diff --git a/apps/browser-extension/tests/helpers/vault-crypto.ts b/apps/browser-extension/tests/helpers/vault-crypto.tsindex 4679b8705..a2cbd3566 100644--- a/apps/browser-extension/tests/helpers/vault-crypto.ts+++ b/apps/browser-extension/tests/helpers/vault-crypto.ts@@ -1,30 +1,59 @@/**- * AES-GCM helpers for tests that read or write vault ciphertext API-side.+ * AES-GCM helpers for tests that read or write vault ciphertext API-side, bound to the same associated data the+ * Rust core uses (`core/rust/src/crypto/aad.rs`).*/-import { EncryptionUtility } from '@aliasvault/client/crypto/EncryptionUtility';+import { randomBytes, webcrypto } from 'crypto';+import { base64ToBytes, bytesToBase64 } from '@aliasvault/client/utilities/Base64';-import './client-platform';+/**+ * Associated data of a manifest payload under its manifest's VEK.+ *+ * @param manifestId - The manifest the payload belongs to+ * @returns The AAD bytes+ */+export function manifestAad(manifestId: string): Uint8Array {+ return new TextEncoder().encode(`aliasvault/v1/manifest/${manifestId.toLowerCase()}`);+}++/**+ * Associated data of a data bucket payload under its manifest's VEK.+ *+ * @param manifestId - The manifest the bucket belongs to+ * @param category - The bucket category+ * @returns The AAD bytes+ */+export function bucketAad(manifestId: string, category: string): Uint8Array {+ return new TextEncoder().encode(`aliasvault/v1/bucket/${manifestId.toLowerCase()}/${category}`);+}/**- * Encrypts raw bytes, returning base64(IV | ciphertext | authTag).+ * Encrypts raw bytes bound to `aad`, returning base64(IV | ciphertext | authTag).** @param plaintextBytes - The plaintext bytes to encrypt* @param keyBytes - The 256-bit encryption key+ * @param aad - The associated data the ciphertext is bound to* @returns Base64-encoded ciphertext*/-export async function symmetricEncryptBytes(plaintextBytes: Uint8Array, keyBytes: Uint8Array): Promise<string> {- return EncryptionUtility.symmetricEncryptBytes(plaintextBytes, bytesToBase64(keyBytes));+export async function symmetricEncryptBytes(plaintextBytes: Uint8Array, keyBytes: Uint8Array, aad: Uint8Array): Promise<string> {+ const key = await webcrypto.subtle.importKey('raw', keyBytes, 'AES-GCM', false, ['encrypt']);+ const iv = randomBytes(12);+ const ciphertext = await webcrypto.subtle.encrypt({ name: 'AES-GCM', iv, additionalData: aad }, key, plaintextBytes);+ return bytesToBase64(new Uint8Array(Buffer.concat([iv, Buffer.from(ciphertext)])));}/**- * Decrypts a ciphertext produced by {@link symmetricEncryptBytes}.+ * Decrypts a ciphertext produced by {@link symmetricEncryptBytes} with the same `aad`.** @param base64Ciphertext - Base64-encoded ciphertext (12-byte IV prepended)* @param keyBytes - The 256-bit encryption key+ * @param aad - The associated data the ciphertext is bound to* @returns The decrypted plaintext bytes*/-export async function symmetricDecryptBytes(base64Ciphertext: string, keyBytes: Uint8Array): Promise<Uint8Array> {- return EncryptionUtility.symmetricDecryptBytes(base64ToBytes(base64Ciphertext), bytesToBase64(keyBytes));+export async function symmetricDecryptBytes(base64Ciphertext: string, keyBytes: Uint8Array, aad: Uint8Array): Promise<Uint8Array> {+ const bytes = base64ToBytes(base64Ciphertext);+ const key = await webcrypto.subtle.importKey('raw', keyBytes, 'AES-GCM', false, ['decrypt']);+ const plaintext = await webcrypto.subtle.decrypt({ name: 'AES-GCM', iv: bytes.subarray(0, 12), additionalData: aad }, key, bytes.subarray(12));+ return new Uint8Array(plaintext);}