Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update tests (#2484)
Leendert de Borst36cbc67409fe
diff --git a/apps/browser-extension/tests/e2e/90-vault-upgrade.spec.ts b/apps/browser-extension/tests/e2e/90-vault-upgrade.spec.tsindex 0793139f4..2ea847e7e 100644--- a/apps/browser-extension/tests/e2e/90-vault-upgrade.spec.ts+++ b/apps/browser-extension/tests/e2e/90-vault-upgrade.spec.ts@@ -7,6 +7,7 @@ import { readFileSync } from 'node:fs';import * as OTPAuth from 'otpauth';import { test, expect, TestClient, Timeouts } from '../fixtures';+import { getVaultSnapshot } from '../helpers/manifest-v2-api';import { readLegacyVaultFixture, restoreLegacyVault, type LegacyVaultFixture, type LegacyVaultItem } from '../helpers/test-api';/**@@ -224,8 +225,7 @@ test.describe.serial('90. Vault upgrades', () => {expect(accessToken, 'the test user was registered with a session').toBeTruthy();// The migration push may still be on its way when the vault list shows.await expect.poll(async () => {- const response = await fetch(`${apiUrl.replace(/\/$/, '')}/v2/Vault`, { headers: { Authorization: `Bearer ${accessToken}` } });- return response.ok ? ((await response.json()) as { storageFormat?: string }).storageFormat : `HTTP ${response.status}`;+ return getVaultSnapshot(apiUrl, accessToken ?? '').then((snapshot) => snapshot.storageFormat, (error: Error) => error.message);}, { timeout: Timeouts.LONG }).toBe('manifest');});});diff --git a/apps/browser-extension/tests/helpers/manifest-v2-api.ts b/apps/browser-extension/tests/helpers/manifest-v2-api.tsindex 016be4054..49c464e13 100644--- a/apps/browser-extension/tests/helpers/manifest-v2-api.ts+++ b/apps/browser-extension/tests/helpers/manifest-v2-api.ts@@ -17,6 +17,7 @@ import { bucketAad, manifestAad, symmetricDecryptBytes, symmetricEncryptBytes }/** One manifest entry in the v2 GET snapshot. */export type SnapshotManifest = {manifestId: string;+ size: number;blob: string;ciphertextHash: string;revision: number;@@ -29,7 +30,7 @@ export type VaultSnapshot = {manifests?: SnapshotManifest[];/** The manifest owned by the caller's personal group; every other entry is a shared one. */personalManifestId?: string | null;- buckets?: Array<{ category: string; blob: string; ciphertextHash: string; revision: number }>;+ buckets?: Array<{ category: string; size: number; blob: string; ciphertextHash: string; revision: number }>;};/** The account-key unlock chain from GET /v2/VaultKey/{type} (fields relevant to these tests). */@@ -74,7 +75,7 @@ type VaultWriteResult = {};/**- * Fetches the v2 vault snapshot for the authenticated user.+ * Fetches the v2 vault snapshot for the authenticated user, with each manifest and bucket ciphertext as base64 `blob`.** @param apiBaseUrl - The base URL of the API* @param token - Bearer token@@ -82,12 +83,22 @@ type VaultWriteResult = {*/export async function getVaultSnapshot(apiBaseUrl: string, token: string): Promise<VaultSnapshot> {const response = await fetch(`${apiBaseUrl.replace(/\/$/, '')}/v2/Vault`, {- headers: { Authorization: `Bearer ${token}` },+ headers: { Authorization: `Bearer ${token}`, Accept: 'application/octet-stream' },});if (!response.ok) {throw new Error(`GET /v2/Vault failed with status ${response.status}: ${await response.text()}`);}- return (await response.json()) as VaultSnapshot;++ // The body is a 4-byte big-endian header length, the JSON header, then the manifest and bucket ciphertexts in header order.+ const body = Buffer.from(await response.arrayBuffer());+ const headerLength = body.readUInt32BE(0);+ const snapshot = JSON.parse(body.subarray(4, 4 + headerLength).toString('utf8')) as VaultSnapshot;+ let offset = 4 + headerLength;+ for (const entry of [...(snapshot.manifests ?? []), ...(snapshot.buckets ?? [])]) {+ entry.blob = body.subarray(offset, offset + entry.size).toString('base64');+ offset += entry.size;+ }+ return snapshot;}/**diff --git a/apps/web/tests/e2e/90-vault-upgrade.spec.ts b/apps/web/tests/e2e/90-vault-upgrade.spec.tsindex a2b6d9110..cd042227c 100644--- a/apps/web/tests/e2e/90-vault-upgrade.spec.ts+++ b/apps/web/tests/e2e/90-vault-upgrade.spec.ts@@ -8,7 +8,7 @@ import * as OTPAuth from 'otpauth';import { test, expect, type WebApp } from '../fixtures';import { requireSmtp, sendMail } from '../helpers/smtp';-import { readLegacyVaultFixture, restoreLegacyVault, type LegacyVaultFixture, type LegacyVaultItem, type TestUser } from '../helpers/test-api';+import { getVaultSnapshotHeader, readLegacyVaultFixture, restoreLegacyVault, type LegacyVaultFixture, type LegacyVaultItem, type TestUser } from '../helpers/test-api';/*** Log in from the start page to an account whose vault still needs an upgrade, ending on the sync page.@@ -223,9 +223,8 @@ test.describe('90. Vault upgrades', () => {});await test.step('the server holds the vault in the manifest storage format', async () => {- const response = await fetch(`${apiUrl}/v2/Vault`, { headers: { Authorization: `Bearer ${testUser.token}` } });- expect(response.ok).toBe(true);- expect(((await response.json()) as { storageFormat?: string }).storageFormat).toBe('manifest');+ const snapshot = await getVaultSnapshotHeader<{ storageFormat?: string }>(apiUrl, testUser.token);+ expect(snapshot.storageFormat).toBe('manifest');});await test.step('the upgraded vault opens again after a reload', async () => {diff --git a/apps/web/tests/helpers/test-api.ts b/apps/web/tests/helpers/test-api.tsindex baad133f9..0d1b14e3f 100644--- a/apps/web/tests/helpers/test-api.ts+++ b/apps/web/tests/helpers/test-api.ts@@ -211,3 +211,16 @@ export async function enableCapability(apiUrl: string, username: string, key: stthrow new Error(`Enabling capability "${key}" via ${url} failed with status ${response.status}: ${await response.text()}`);}}++/**+ * The JSON header of the binary GET /v2/Vault snapshot (the manifest and bucket ciphertexts follow it).+ */+export async function getVaultSnapshotHeader<T>(apiUrl: string, token: string): Promise<T> {+ const response = await fetch(`${apiUrl}/v2/Vault`, { headers: { Authorization: `Bearer ${token}`, Accept: 'application/octet-stream' } });+ if (!response.ok) {+ throw new Error(`GET /v2/Vault failed with status ${response.status}: ${await response.text()}`);+ }+ const body = new Uint8Array(await response.arrayBuffer());+ const headerLength = new DataView(body.buffer, body.byteOffset).getUint32(0);+ return JSON.parse(new TextDecoder().decode(body.subarray(4, 4 + headerLength))) as T;+}diff --git a/apps/web/tests/helpers/vault-tamper.ts b/apps/web/tests/helpers/vault-tamper.tsindex 049906527..1edecdd03 100644--- a/apps/web/tests/helpers/vault-tamper.ts+++ b/apps/web/tests/helpers/vault-tamper.ts@@ -10,7 +10,7 @@ import { getSyncableTableNames, vaultCodecCanonicalizeFromSqlite, vaultCodecGeneimport './client-platform';-import type { TestUser } from './test-api';+import { getVaultSnapshotHeader, type TestUser } from './test-api';/*** The part of GET /v2/Vault these helpers read.@@ -60,11 +60,7 @@ async function encryptManifest(packed: Uint8Array, base64Key: string, manifestId* The account's personal manifest id and current revision.*/async function getPersonalManifest(apiUrl: string, user: TestUser): Promise<{ manifestId: string; revision: number }> {- const response = await fetch(`${apiUrl}/v2/Vault`, { headers: { Authorization: `Bearer ${user.token}` } });- if (!response.ok) {- throw new Error(`GET /v2/Vault failed with status ${response.status}: ${await response.text()}`);- }- const snapshot = (await response.json()) as VaultSnapshot;+ const snapshot = await getVaultSnapshotHeader<VaultSnapshot>(apiUrl, user.token);const current = snapshot.manifests?.find((m) => m.manifestId === snapshot.personalManifestId);if (!current) {throw new Error('Test account has no personal manifest to overwrite.');diff --git a/core/rust/src/vault_sync/tests/blobs.rs b/core/rust/src/vault_sync/tests/blobs.rsindex 48c43dcf7..c656c2983 100644--- a/core/rust/src/vault_sync/tests/blobs.rs+++ b/core/rust/src/vault_sync/tests/blobs.rs@@ -5,11 +5,12 @@use serde_json::{json, Value};use super::fake_server::FakeServer;-use super::test_host::{open_schema_db, query, TestHost};+use super::test_host::{encode_blob_download, encode_snapshot, open_schema_db, query, TestHost};use super::{insert_attachment, insert_delivery_key, insert_item, insert_logo, rename_item, synced, Synced, ITEM_A, ITEM_B, PERSONAL_MANIFEST_ID};use crate::crypto;use crate::vault_codec;use crate::vault_sync::blob_keys;+use crate::vault_sync::pull::{decode_blob_download, decode_snapshot};/// A server vault holding one item with a logo and an attachment.fn vault_with_blobs(db: &rusqlite::Connection) {@@ -176,3 +177,39 @@ fn an_attachment_row_that_references_no_blob_does_not_fail_the_pull() {assert_eq!(rows.len(), 1);assert_eq!((&rows[0]["Blob"], &rows[0]["BlobHash"]), (&Value::Null, &Value::Null));}++#[test]+fn a_binary_blob_download_splits_into_its_blobs() {+ let frame = encode_blob_download(&json!({ "blobs": [+ { "hash": "a", "category": "favicon", "encryptedDataBase64": "AQID", "encryptedBlobKey": "ka" },+ { "hash": "b", "category": "attachment", "encryptedDataBase64": "", "encryptedBlobKey": "kb" },+ { "hash": "c", "category": "attachment", "encryptedDataBase64": "BAUGBw==", "encryptedBlobKey": "kc" },+ ] }));+ let blobs = decode_blob_download(&frame).unwrap();+ let decoded: Vec<(&str, &str, &[u8])> = blobs.iter().map(|(entry, bytes)| (entry.hash.as_str(), entry.encrypted_blob_key.as_str(), *bytes)).collect();+ assert_eq!(decoded, vec![("a", "ka", &[1u8, 2, 3][..]), ("b", "kb", &[][..]), ("c", "kc", &[4u8, 5, 6, 7][..])]);+ assert!(decode_blob_download(&encode_blob_download(&json!({ "blobs": [] }))).unwrap().is_empty());+}++#[test]+fn a_malformed_binary_blob_download_is_refused() {+ let frame = encode_blob_download(&json!({ "blobs": [{ "hash": "a", "category": "favicon", "encryptedDataBase64": "AQID", "encryptedBlobKey": "ka" }] }));+ assert!(decode_blob_download(&[]).is_err());+ assert!(decode_blob_download(&frame[..frame.len() - 1]).is_err(), "a truncated blob");+ assert!(decode_blob_download(&[frame.as_slice(), &[0]].concat()).is_err(), "trailing bytes");+ assert!(decode_blob_download(&[0, 0, 1, 0, b'{']).is_err(), "a header longer than the body");+}++#[test]+fn a_binary_snapshot_fills_each_manifest_and_bucket_with_its_ciphertext() {+ let frame = encode_snapshot(&json!({+ "storageFormat": "manifest",+ "manifests": [{ "manifestId": "m1", "blob": "AQID", "revision": 3 }, { "manifestId": "m2", "blob": null, "revision": 0 }],+ "buckets": [{ "manifestId": "m1", "category": "settings", "blob": "BAU=", "revision": 1 }],+ }));+ let snapshot = decode_snapshot(&frame).unwrap();+ assert_eq!(snapshot.manifests.iter().map(|m| m.blob.clone()).collect::<Vec<_>>(), vec![vec![1u8, 2, 3], vec![]]);+ assert!(!snapshot.manifests[1].has_content());+ assert_eq!(snapshot.buckets[0].blob, vec![4u8, 5]);+ assert!(decode_snapshot(&frame[..frame.len() - 1]).is_err(), "a truncated bucket");+}diff --git a/core/rust/src/vault_sync/tests/test_host.rs b/core/rust/src/vault_sync/tests/test_host.rsindex fae5d9fb2..aaefd4151 100644--- a/core/rust/src/vault_sync/tests/test_host.rs+++ b/core/rust/src/vault_sync/tests/test_host.rs@@ -201,11 +201,17 @@ impl TestHost {let command: Command = serde_json::from_str(&session.next_command().unwrap()).unwrap();let response = match command {Command::Done { result } => return result,- Command::Http { method, path, body, .. } => {+ Command::Http { method, path, body, binary_response, .. } => {let body_json = body.as_deref().map(|b| serde_json::from_str(b).unwrap_or(Value::String(b.to_string())));let method = serde_json::to_value(method).unwrap().as_str().expect("http method serializes as text").to_string();self.requests.push(RecordedRequest { method: method.clone(), path: path.clone(), body: body_json.clone() });match self.responders.iter().find_map(|r| r(&method, &path, body_json.as_ref())) {+ Some((status, body)) if binary_response && (200..300).contains(&status) => {+ // Responders script binary downloads as JSON; the host hands them over in the server's binary frame.+ let frame = if path == "Vault/blobs/download" { encode_blob_download(&body) } else { encode_snapshot(&body) };+ session.resume(&json!({ "status": status }).to_string(), Some(frame)).unwrap();+ continue;+ }Some((status, body)) => json!({ "status": status, "body": body.to_string() }),None => json!({ "status": 0, "transportError": format!("no scripted response for {} {}", method, path) }),}@@ -348,3 +354,38 @@ pub fn query(conn: &Connection, sql: &str, params: &[Value]) -> Result<Vec<Map<Spub fn exec(conn: &Connection, statements: &[SqlStatement]) -> Result<(), String> {sqlite_host::exec(conn, statements).map_err(|e| e.to_string())}++/// A binary vault download: 4-byte big-endian header length, the JSON header, then the parts.+fn encode_frame(header: &Value, parts: Vec<Vec<u8>>) -> Vec<u8> {+ let header = serde_json::to_vec(header).unwrap();+ let mut frame = (header.len() as u32).to_be_bytes().to_vec();+ frame.extend(header);+ frame.extend(parts.into_iter().flatten());+ frame+}++/// Take the base64 `blob` out of a JSON entry, leaving its `size`.+fn take_blob(entry: &mut Value, field: &str) -> Vec<u8> {+ let bytes = entry.as_object_mut().unwrap().remove(field).and_then(|b| b.as_str().map(str::to_string)).map(|b| crate::common::encoding::base64_decode(&b).unwrap()).unwrap_or_default();+ entry["size"] = json!(bytes.len());+ bytes+}++/// The server's binary snapshot frame for a `GET v2/Vault` JSON body that carries base64 `blob`s.+pub fn encode_snapshot(body: &Value) -> Vec<u8> {+ let mut header = body.clone();+ let mut parts = Vec::new();+ for list in ["manifests", "buckets"] {+ for entry in header.get_mut(list).and_then(Value::as_array_mut).map(|a| a.iter_mut().collect::<Vec<_>>()).unwrap_or_default() {+ parts.push(take_blob(entry, "blob"));+ }+ }+ encode_frame(&header, parts)+}++/// The server's binary blob download frame for a `{ "blobs": [{ hash, category, encryptedDataBase64, encryptedBlobKey }] }` JSON body.+pub fn encode_blob_download(body: &Value) -> Vec<u8> {+ let mut header = body.clone();+ let parts = header.get_mut("blobs").and_then(Value::as_array_mut).map(|a| a.iter_mut().map(|blob| take_blob(blob, "encryptedDataBase64")).collect()).unwrap_or_default();+ encode_frame(&header, parts)+}