Skip to content

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

Update upload tests (#2484)

Leendert de Borst7fed4aeb0f28
diff --git a/apps/browser-extension/tests/helpers/manifest-v2-api.ts b/apps/browser-extension/tests/helpers/manifest-v2-api.ts
index 49c464e13..fac0f97b1 100644
--- a/apps/browser-extension/tests/helpers/manifest-v2-api.ts
+++ b/apps/browser-extension/tests/helpers/manifest-v2-api.ts
@@ -334,18 +334,21 @@ async function postVaultWrite(
manifests: ManifestWrite[],
buckets: BucketWrite[]
): Promise<VaultWriteResult> {
- const payload = {
+ // The body is a 4-byte big-endian header length, the JSON header (sizes in place of the base64 ciphertexts), then the ciphertexts.
+ const ciphertexts = [...manifests.map((m) => Buffer.from(m.manifestBlob, 'base64')), ...buckets.map((b) => Buffer.from(b.blob, 'base64'))];
+ const header = Buffer.from(JSON.stringify({
username: SrpAuthService.normalizeUsername(username),
- manifests,
- buckets,
- newBlobs: [],
+ manifests: manifests.map(({ manifestBlob, ...rest }) => ({ ...rest, size: Buffer.from(manifestBlob, 'base64').length })),
+ buckets: buckets.map(({ blob, ...rest }) => ({ ...rest, size: Buffer.from(blob, 'base64').length })),
emailRouting: null,
- };
+ }));
+ const headerLength = Buffer.alloc(4);
+ headerLength.writeUInt32BE(header.length);
const response = await fetch(`${apiBaseUrl.replace(/\/$/, '')}/v2/Vault`, {
method: 'POST',
- headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
- body: JSON.stringify(payload),
+ headers: { 'Content-Type': 'application/octet-stream', Authorization: `Bearer ${token}` },
+ body: Buffer.concat([headerLength, header, ...ciphertexts]),
});
if (!response.ok) {
throw new Error(`POST /v2/Vault failed with status ${response.status}: ${await response.text()}`);
diff --git a/apps/web/tests/helpers/vault-tamper.ts b/apps/web/tests/helpers/vault-tamper.ts
index 1edecdd03..d3e21e97d 100644
--- a/apps/web/tests/helpers/vault-tamper.ts
+++ b/apps/web/tests/helpers/vault-tamper.ts
@@ -73,23 +73,27 @@ async function getPersonalManifest(apiUrl: string, user: TestUser): Promise<{ ma
*/
async function writePersonalManifest(apiUrl: string, user: TestUser, blob: string): Promise<void> {
const { manifestId, revision } = await getPersonalManifest(apiUrl, user);
+ // The body is a 4-byte big-endian header length, the JSON header, then the manifest ciphertext.
+ const ciphertext = Buffer.from(blob, 'base64');
+ const header = Buffer.from(JSON.stringify({
+ username: SrpAuthService.normalizeUsername(user.username),
+ manifests: [{
+ manifestId,
+ size: ciphertext.length,
+ manifestCiphertextHash: createHash('sha256').update(ciphertext).digest('hex'),
+ currentRevision: revision,
+ credentialsCount: 0,
+ blobReferences: [],
+ }],
+ buckets: [],
+ emailRouting: null,
+ }));
+ const headerLength = Buffer.alloc(4);
+ headerLength.writeUInt32BE(header.length);
const response = await fetch(`${apiUrl}/v2/Vault`, {
method: 'POST',
- headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${user.token}` },
- body: JSON.stringify({
- username: SrpAuthService.normalizeUsername(user.username),
- manifests: [{
- manifestId,
- manifestBlob: blob,
- manifestCiphertextHash: createHash('sha256').update(Buffer.from(blob, 'base64')).digest('hex'),
- currentRevision: revision,
- credentialsCount: 0,
- blobReferences: [],
- }],
- buckets: [],
- newBlobs: [],
- emailRouting: null,
- }),
+ headers: { 'Content-Type': 'application/octet-stream', Authorization: `Bearer ${user.token}` },
+ body: Buffer.concat([headerLength, header, ciphertext]),
});
const result = response.ok ? (await response.json()) as { status: string } : null;
if (!result || result.status !== 'ok') {
diff --git a/core/rust/src/vault_sync/tests/blobs.rs b/core/rust/src/vault_sync/tests/blobs.rs
index c656c2983..ef38509cb 100644
--- a/core/rust/src/vault_sync/tests/blobs.rs
+++ b/core/rust/src/vault_sync/tests/blobs.rs
@@ -5,7 +5,7 @@
use serde_json::{json, Value};
use super::fake_server::FakeServer;
-use super::test_host::{encode_blob_download, encode_snapshot, open_schema_db, query, TestHost};
+use super::test_host::{decode_upload_frame, 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;
@@ -213,3 +213,20 @@ fn a_binary_snapshot_fills_each_manifest_and_bucket_with_its_ciphertext() {
assert_eq!(snapshot.buckets[0].blob, vec![4u8, 5]);
assert!(decode_snapshot(&frame[..frame.len() - 1]).is_err(), "a truncated bucket");
}
+
+#[test]
+fn a_binary_upload_keeps_each_ciphertext_out_of_the_json_header() {
+ use crate::vault_sync::http::encode_frame;
+ use crate::vault_sync::types::{BlobUpload, BlobUploadRequest};
+
+ let request = BlobUploadRequest { manifest_id: "m1".to_string(), overwrite: false, blobs: vec![
+ BlobUpload { hash: "a".to_string(), category: "favicon".to_string(), encrypted_data: vec![1, 2, 3], encrypted_blob_key: "ka".to_string() },
+ BlobUpload { hash: "b".to_string(), category: "attachment".to_string(), encrypted_data: vec![4], encrypted_blob_key: "kb".to_string() },
+ ] };
+ let frame = encode_frame(&request).unwrap();
+ let header_length = u32::from_be_bytes(frame[..4].try_into().unwrap()) as usize;
+ let header: Value = serde_json::from_slice(&frame[4..4 + header_length]).unwrap();
+ assert_eq!(header["blobs"][0], json!({ "hash": "a", "category": "favicon", "size": 3, "encryptedBlobKey": "ka" }));
+ assert_eq!(&frame[4 + header_length..], &[1, 2, 3, 4]);
+ assert_eq!(decode_upload_frame("Vault/blobs", &frame)["blobs"][1]["encryptedDataBase64"], json!("BA=="));
+}
diff --git a/core/rust/src/vault_sync/tests/fake_server.rs b/core/rust/src/vault_sync/tests/fake_server.rs
index c7f3f4df2..e6c8789e6 100644
--- a/core/rust/src/vault_sync/tests/fake_server.rs
+++ b/core/rust/src/vault_sync/tests/fake_server.rs
@@ -124,7 +124,7 @@ impl FakeServer {
for (blob_hash, blob_entry) in &entry.blobs {
let bytes = crate::common::encoding::base64_decode(&blob_entry.bytes_base64).unwrap();
let encrypted = blob_keys::encrypt_blob(&bytes, blob_key, &personal, blob_hash).unwrap();
- store.insert(blob_hash.clone(), json!({ "hash": blob_hash, "category": blob_entry.kind, "encryptedDataBase64": encrypted.encrypted_data_base64, "encryptedBlobKey": encrypted.encrypted_blob_key }));
+ store.insert(blob_hash.clone(), json!({ "hash": blob_hash, "category": blob_entry.kind, "encryptedDataBase64": crate::common::encoding::base64_encode(&encrypted.encrypted_data), "encryptedBlobKey": encrypted.encrypted_blob_key }));
references.push(json!({ "hash": blob_hash, "category": blob_entry.kind, "sizeBytes": bytes.len() }));
}
let access = self.manifests.get(&personal).map(|m| m.access.clone()).unwrap_or_else(|| json!({ "canAdminister": true, "keyType": "account-key" }));
diff --git a/core/rust/src/vault_sync/tests/test_host.rs b/core/rust/src/vault_sync/tests/test_host.rs
index aaefd4151..a75573086 100644
--- a/core/rust/src/vault_sync/tests/test_host.rs
+++ b/core/rust/src/vault_sync/tests/test_host.rs
@@ -201,8 +201,13 @@ 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, binary_response, .. } => {
- let body_json = body.as_deref().map(|b| serde_json::from_str(b).unwrap_or(Value::String(b.to_string())));
+ Command::Http { method, path, body, binary_response, binary_body, .. } => {
+ let body_json = if binary_body {
+ // Upload frames reach the responders as the JSON they stand for, each ciphertext back in its base64 field.
+ Some(decode_upload_frame(&path, &session.command_bytes().unwrap().expect("a binary body carries bytes")))
+ } else {
+ 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())) {
@@ -389,3 +394,22 @@ pub fn encode_blob_download(body: &Value) -> Vec<u8> {
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)
}
+
+/// The JSON a binary upload frame stands for: each listed `size` replaced by its ciphertext as base64 in the named field.
+pub fn decode_upload_frame(path: &str, frame: &[u8]) -> Value {
+ let header_length = u32::from_be_bytes(frame[..4].try_into().unwrap()) as usize;
+ let mut header: Value = serde_json::from_slice(&frame[4..4 + header_length]).unwrap();
+ let mut data = &frame[4 + header_length..];
+ let lists: &[(&str, &str)] = if path == "Vault/blobs" { &[("blobs", "encryptedDataBase64")] } else { &[("manifests", "manifestBlob"), ("buckets", "blob")] };
+ for (list, field) in lists {
+ for entry in header.get_mut(*list).and_then(Value::as_array_mut).map(|a| a.iter_mut().collect::<Vec<_>>()).unwrap_or_default() {
+ let entry = entry.as_object_mut().unwrap();
+ let size = entry.remove("size").and_then(|s| s.as_u64()).expect("each listed ciphertext has a size") as usize;
+ let (ciphertext, rest) = data.split_at(size);
+ entry.insert(field.to_string(), json!(crate::common::encoding::base64_encode(ciphertext)));
+ data = rest;
+ }
+ }
+ assert!(data.is_empty(), "the frame carries only the ciphertexts its header lists");
+ header
+}