Skip to content

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

Make vault upload call be in bytes to avoid json and base64 overhead (#2484)

Leendert de Borstbb45a5b6ce7f
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.kt
index ef9a8e9ae..a1f691a67 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultSyncEngine.kt
@@ -66,7 +66,8 @@ class VaultSyncEngine(
return result
}
val startNanos = System.nanoTime()
- val (response, bytes) = respond(kind, command)
+ val commandBytes = if (kind == "http" && command.optBoolean("binaryBody", false)) log.engine { session.commandBytes() } else null
+ val (response, bytes) = respond(kind, command, commandBytes)
log.recordCommand(kind, command, response, bytes, System.nanoTime() - startNanos)
val responseJson = log.json { response.toString() }
log.engine { session.resume(responseJson, bytes) }
@@ -111,9 +112,9 @@ class VaultSyncEngine(
* The JSON response to one command.
*/
@Suppress("TooGenericExceptionCaught")
- private suspend fun respond(kind: String, command: JSONObject): Pair<JSONObject, ByteArray?> {
+ private suspend fun respond(kind: String, command: JSONObject, commandBytes: ByteArray?): Pair<JSONObject, ByteArray?> {
if (kind == "http") {
- return http(command)
+ return http(command, commandBytes)
}
if (kind != "dbExport") {
return Pair(handle(kind, command), null)
@@ -192,11 +193,14 @@ class VaultSyncEngine(
* An API request. On success a binary response returns its body as raw bytes instead of as `body` text in the JSON.
*/
@Suppress("TooGenericExceptionCaught")
- private suspend fun http(command: JSONObject): Pair<JSONObject, ByteArray?> = withContext(Dispatchers.IO) {
+ private suspend fun http(command: JSONObject, commandBytes: ByteArray?): Pair<JSONObject, ByteArray?> = withContext(Dispatchers.IO) {
val body = if (command.has("body") && !command.isNull("body")) command.getString("body") else null
val binaryResponse = command.optBoolean("binaryResponse", false)
+ val binaryBody = command.optBoolean("binaryBody", false)
val headers = mutableMapOf("Accept" to if (binaryResponse) "application/octet-stream" else "application/json")
- if (body != null) {
+ if (binaryBody) {
+ headers["Content-Type"] = "application/octet-stream"
+ } else if (body != null) {
headers["Content-Type"] = "application/json"
}
try {
@@ -207,6 +211,8 @@ class VaultSyncEngine(
headers = headers,
requiresAuth = command.optBoolean("auth", true),
largeTransfer = command.optBoolean("largeTransfer", false),
+ bodyBytes = if (binaryBody) commandBytes ?: ByteArray(0) else null,
+ rawBinaryResponse = binaryResponse,
)
val bytes = response.bodyBytes
if (binaryResponse && bytes != null) {
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/webapi/WebApiService.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/webapi/WebApiService.kt
index 65e9ad79a..3ee6dda1d 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/webapi/WebApiService.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/webapi/WebApiService.kt
@@ -24,7 +24,7 @@ data class WebApiResponse(
val body: String,
/** The response headers as a map of key-value pairs. */
val headers: Map<String, String>,
- /** The body as raw bytes, set on success when the request accepted `application/octet-stream`. */
+ /** The body as raw bytes, set on a successful binary response when the request asked for raw bytes. */
val bodyBytes: ByteArray? = null,
)
@@ -218,6 +218,8 @@ class WebApiService(private val context: Context) {
/**
* Execute a WebAPI request with support for authentication and token refresh.
* Pass largeTransfer for a request that carries vault ciphertext, which gets the longer transfer timeout.
+ * Pass bodyBytes to send raw bytes instead of the text body, and rawBinaryResponse to get a binary
+ * response as `bodyBytes` instead of as base64 `body` text.
*/
@Suppress("LongParameterList") // One argument per part of the request the callers vary
suspend fun executeRequest(
@@ -227,6 +229,8 @@ class WebApiService(private val context: Context) {
headers: Map<String, String>,
requiresAuth: Boolean,
largeTransfer: Boolean = false,
+ bodyBytes: ByteArray? = null,
+ rawBinaryResponse: Boolean = false,
): WebApiResponse = withContext(Dispatchers.IO) {
val requestHeaders = headers.toMutableMap()
@@ -248,6 +252,8 @@ class WebApiService(private val context: Context) {
body = body,
headers = requestHeaders,
largeTransfer = largeTransfer,
+ bodyBytes = bodyBytes,
+ rawBinaryResponse = rawBinaryResponse,
)
// Handle 401 Unauthorized - attempt token refresh
@@ -268,6 +274,8 @@ class WebApiService(private val context: Context) {
body = body,
headers = retryHeaders,
largeTransfer = largeTransfer,
+ bodyBytes = bodyBytes,
+ rawBinaryResponse = rawBinaryResponse,
)
return@withContext retryResponse
@@ -284,12 +292,15 @@ class WebApiService(private val context: Context) {
/**
* Execute a raw HTTP request without token refresh logic.
*/
+ @Suppress("LongParameterList") // One argument per part of the request the callers vary
private suspend fun executeRawRequest(
method: String,
endpoint: String,
body: String?,
headers: Map<String, String>,
largeTransfer: Boolean = false,
+ bodyBytes: ByteArray? = null,
+ rawBinaryResponse: Boolean = false,
): WebApiResponse = withContext(Dispatchers.IO) {
val urlString = resolveUrl(endpoint)
@@ -312,7 +323,12 @@ class WebApiService(private val context: Context) {
}
// Set body if present
- if (body != null && (method.uppercase() == "POST" || method.uppercase() == "PUT" || method.uppercase() == "PATCH")) {
+ val sendsBody = method.uppercase() == "POST" || method.uppercase() == "PUT" || method.uppercase() == "PATCH"
+ if (bodyBytes != null && sendsBody) {
+ connection.doOutput = true
+ connection.setFixedLengthStreamingMode(bodyBytes.size)
+ connection.outputStream.use { it.write(bodyBytes) }
+ } else if (body != null && sendsBody) {
connection.doOutput = true
OutputStreamWriter(connection.outputStream).use { writer ->
writer.write(body)
@@ -324,18 +340,20 @@ class WebApiService(private val context: Context) {
val statusCode = connection.responseCode
// Read response body
- var bodyBytes: ByteArray? = null
+ var responseBytes: ByteArray? = null
val responseBody = try {
val acceptHeader = headers.entries.firstOrNull { it.key.equals("Accept", ignoreCase = true) }?.value ?: ""
val isBinary = acceptHeader.contains("application/octet-stream", ignoreCase = true)
if (statusCode in 200..299) {
- if (isBinary) {
+ if (isBinary && rawBinaryResponse) {
+ // The caller takes the bytes from bodyBytes
+ responseBytes = connection.inputStream.use { it.readBytes() }
+ ""
+ } else if (isBinary) {
// Read binary data and encode as base64
connection.inputStream.use { inputStream ->
- val bytes = inputStream.readBytes()
- bodyBytes = bytes
- android.util.Base64.encodeToString(bytes, android.util.Base64.NO_WRAP)
+ android.util.Base64.encodeToString(inputStream.readBytes(), android.util.Base64.NO_WRAP)
}
} else {
// Read text data
@@ -365,7 +383,7 @@ class WebApiService(private val context: Context) {
statusCode = statusCode,
body = responseBody,
headers = responseHeaders,
- bodyBytes = bodyBytes,
+ bodyBytes = responseBytes,
)
} catch (e: Exception) {
Log.e(TAG, "Error executing request", e)
diff --git a/apps/mobile-app/ios/VaultStoreKit/Models/WebApiResponse.swift b/apps/mobile-app/ios/VaultStoreKit/Models/WebApiResponse.swift
index 345f6bcc3..34ecc2c61 100644
--- a/apps/mobile-app/ios/VaultStoreKit/Models/WebApiResponse.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/Models/WebApiResponse.swift
@@ -8,7 +8,7 @@ public struct WebApiResponse {
public let body: String
/// The headers of the response
public let headers: [String: String]
- /// The body as raw bytes, set on success when the request accepted `application/octet-stream`
+ /// The body as raw bytes, set on a successful binary response when the request asked for raw bytes
public let bodyData: Data?
/// Initialize a new WebApiResponse
diff --git a/apps/mobile-app/ios/VaultStoreKit/Services/VaultSyncEngine.swift b/apps/mobile-app/ios/VaultStoreKit/Services/VaultSyncEngine.swift
index 8d86217f1..03017b624 100644
--- a/apps/mobile-app/ios/VaultStoreKit/Services/VaultSyncEngine.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/Services/VaultSyncEngine.swift
@@ -63,7 +63,11 @@ public final class VaultSyncEngine {
return result
}
let commandStartedAt = Date()
- let (response, bytes) = await respond(kind: kind, command: command)
+ var commandBytes: Data?
+ if kind == "http", command["binaryBody"] as? Bool == true {
+ commandBytes = try log.engine { try session.commandBytes() }
+ }
+ let (response, bytes) = await respond(kind: kind, command: command, commandBytes: commandBytes)
log.recordCommand(kind, command: command, response: response, responseBytes: bytes, since: commandStartedAt)
let responseJson = try log.json { try Self.serializeJson(response) }
try log.engine { try session.resume(responseJson: responseJson, bytes: bytes) }
@@ -106,10 +110,10 @@ public final class VaultSyncEngine {
// MARK: - Commands
- /// The JSON response to one command.
- private func respond(kind: String, command: [String: Any]) async -> (json: [String: Any], bytes: Data?) {
+ /// The JSON response to one command; `commandBytes` is the body of a binary `http` request.
+ private func respond(kind: String, command: [String: Any], commandBytes: Data?) async -> (json: [String: Any], bytes: Data?) {
if kind == "http" {
- return await http(command)
+ return await http(command, commandBytes: commandBytes)
}
guard kind == "dbExport" else {
return (await handle(kind: kind, command: command), nil)
@@ -185,19 +189,23 @@ public final class VaultSyncEngine {
}
/// An API request. On success a binary response returns its body as raw bytes instead of as `body` text in the JSON.
- private func http(_ command: [String: Any]) async -> (json: [String: Any], bytes: Data?) {
+ private func http(_ command: [String: Any], commandBytes: Data?) async -> (json: [String: Any], bytes: Data?) {
let method = command["method"] as? String ?? "GET"
let path = command["path"] as? String ?? ""
let body = command["body"] as? String
let auth = command["auth"] as? Bool ?? true
let largeTransfer = command["largeTransfer"] as? Bool ?? false
let binaryResponse = command["binaryResponse"] as? Bool ?? false
+ let binaryBody = command["binaryBody"] as? Bool ?? false
var headers: [String: String] = ["Accept": binaryResponse ? "application/octet-stream" : "application/json"]
- if body != nil {
+ if binaryBody {
+ headers["Content-Type"] = "application/octet-stream"
+ } else if body != nil {
headers["Content-Type"] = "application/json"
}
do {
- let response = try await webApiService.executeRequest(method: method, endpoint: path, body: body, headers: headers, requiresAuth: auth, largeTransfer: largeTransfer)
+ let bodyData = binaryBody ? commandBytes ?? Data() : nil
+ let response = try await webApiService.executeRequest(method: method, endpoint: path, body: body, headers: headers, requiresAuth: auth, largeTransfer: largeTransfer, bodyData: bodyData, rawBinaryResponse: binaryResponse)
if binaryResponse, let data = response.bodyData {
return (["status": response.statusCode], data)
}
diff --git a/apps/mobile-app/ios/VaultStoreKit/Services/WebApiService.swift b/apps/mobile-app/ios/VaultStoreKit/Services/WebApiService.swift
index f8550b5df..f8dfe7f82 100644
--- a/apps/mobile-app/ios/VaultStoreKit/Services/WebApiService.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/Services/WebApiService.swift
@@ -184,6 +184,8 @@ public class WebApiService {
/**
* Execute a WebAPI request with support for authentication and token refresh.
* Pass largeTransfer for a request that carries vault ciphertext, which gets the longer transfer timeout.
+ * Pass bodyData to send raw bytes instead of the text body, and rawBinaryResponse to get a binary
+ * response as `bodyData` instead of as base64 `body` text.
*/
public func executeRequest(
method: String,
@@ -191,7 +193,9 @@ public class WebApiService {
body: String?,
headers: [String: String],
requiresAuth: Bool,
- largeTransfer: Bool = false
+ largeTransfer: Bool = false,
+ bodyData: Data? = nil,
+ rawBinaryResponse: Bool = false
) async throws -> WebApiResponse {
var requestHeaders = headers
@@ -209,7 +213,9 @@ public class WebApiService {
endpoint: endpoint,
body: body,
headers: requestHeaders,
- largeTransfer: largeTransfer
+ largeTransfer: largeTransfer,
+ bodyData: bodyData,
+ rawBinaryResponse: rawBinaryResponse
)
// Handle 401 Unauthorized - attempt token refresh
@@ -226,7 +232,9 @@ public class WebApiService {
endpoint: endpoint,
body: body,
headers: retryHeaders,
- largeTransfer: largeTransfer
+ largeTransfer: largeTransfer,
+ bodyData: bodyData,
+ rawBinaryResponse: rawBinaryResponse
)
return retryResponse
@@ -248,7 +256,9 @@ public class WebApiService {
endpoint: String,
body: String?,
headers: [String: String],
- largeTransfer: Bool = false
+ largeTransfer: Bool = false,
+ bodyData: Data? = nil,
+ rawBinaryResponse: Bool = false
) async throws -> WebApiResponse {
let urlString = resolveUrl(endpoint)
@@ -275,7 +285,9 @@ public class WebApiService {
}
// Set body if present
- if let bodyString = body {
+ if let bodyData = bodyData {
+ request.httpBody = bodyData
+ } else if let bodyString = body {
request.httpBody = bodyString.data(using: .utf8)
}
@@ -304,7 +316,10 @@ public class WebApiService {
// Parse response body
let responseBody: String
- if isBinary {
+ if isBinary && rawBinaryResponse {
+ // The caller takes the bytes from bodyData
+ responseBody = ""
+ } else if isBinary {
// Encode binary data as base64
responseBody = data.base64EncodedString()
} else {
@@ -316,7 +331,7 @@ public class WebApiService {
statusCode: httpResponse.statusCode,
body: responseBody,
headers: responseHeaders,
- bodyData: isBinary ? data : nil
+ bodyData: isBinary && rawBinaryResponse ? data : nil
)
}
diff --git a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs b/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs
index 530226565..5a114963b 100644
--- a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs
+++ b/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs
@@ -332,27 +332,27 @@ public class VaultController(
}
}
- // Decode base64-encoded ciphertexts into raw bytes.
+ // Check each ciphertext against the hash the client computed for it.
var manifestBlobs = new Dictionary<Guid, byte[]>();
foreach (var mw in model.Manifests)
{
- if (!CiphertextHelper.TryDecode(mw.ManifestBlob, out var manifestBlob) || !CiphertextHelper.MatchesHash(manifestBlob, mw.ManifestCiphertextHash))
+ if (!CiphertextHelper.IsCiphertext(mw.Data) || !CiphertextHelper.MatchesHash(mw.Data, mw.ManifestCiphertextHash))
{
return ApiError.Result(ApiErrorCode.INVALID_REQUEST, 400);
}
- manifestBlobs[mw.ManifestId] = manifestBlob;
+ manifestBlobs[mw.ManifestId] = mw.Data;
}
var bucketBlobs = new Dictionary<(Guid ManifestId, VaultDataBucketCategory Category), byte[]>();
- foreach (var bucket in model.Buckets.Where(b => !string.IsNullOrEmpty(b.Blob)))
+ foreach (var bucket in model.Buckets.Where(b => b.Data.Length > 0))
{
- if (!CiphertextHelper.TryDecode(bucket.Blob, out var bucketBlob) || !CiphertextHelper.MatchesHash(bucketBlob, bucket.CiphertextHash))
+ if (!CiphertextHelper.IsCiphertext(bucket.Data) || !CiphertextHelper.MatchesHash(bucket.Data, bucket.CiphertextHash))
{
return ApiError.Result(ApiErrorCode.INVALID_REQUEST, 400);
}
- bucketBlobs[(bucket.ManifestId, bucket.Category)] = bucketBlob;
+ bucketBlobs[(bucket.ManifestId, bucket.Category)] = bucket.Data;
}
var accessScope = await ManifestAccessHelper.ResolveScopeAsync(context, user.Id, user.PersonalGroupId);
@@ -801,7 +801,7 @@ public class VaultController(
.Select(b => new { b.Hash, b.Category, b.EncryptedBlobKey, b.EncryptedData })
.ToListAsync();
- var header = new BlobDownloadResponse { Blobs = rows.Select(r => new BlobDownloadEntry { Hash = r.Hash, Category = r.Category, EncryptedBlobKey = r.EncryptedBlobKey, Size = r.EncryptedData.Length }).ToList() };
+ var header = new BlobDownloadResponse { Blobs = rows.Select(r => new BlobEntry { Hash = r.Hash, Category = r.Category, EncryptedBlobKey = r.EncryptedBlobKey, Size = r.EncryptedData.Length }).ToList() };
return File(BinaryFrame.Write(header, rows.Select(r => r.EncryptedData).ToList()), BinaryFrame.ContentType);
}
@@ -1081,7 +1081,7 @@ public class VaultController(
/// <param name="blobs">Blobs to upsert.</param>
/// <param name="overwrite">When true, existing blobs with the same hash get their ciphertext replaced.</param>
/// <returns>True when every payload is structurally valid; false when any is malformed (caller should 400).</returns>
- private async Task<bool> TryUpsertBlobObjectsAsync(AliasServerDbContext context, Guid manifestId, List<Blob> blobs, bool overwrite = false)
+ private async Task<bool> TryUpsertBlobObjectsAsync(AliasServerDbContext context, Guid manifestId, List<BlobEntry> blobs, bool overwrite = false)
{
var nowUtc = timeProvider.GetUtcNow().UtcDateTime;
var hashes = blobs.Select(b => b.Hash).Distinct().ToList();
@@ -1094,10 +1094,12 @@ public class VaultController(
byte[]? data = null;
if (!existing.TryGetValue(dto.Hash, out var row) || overwrite)
{
- if (!CiphertextHelper.TryDecode(dto.EncryptedDataBase64, out data))
+ if (!CiphertextHelper.IsCiphertext(dto.Data))
{
return false;
}
+
+ data = dto.Data;
}
if (row != null)
diff --git a/apps/server/AliasVault.Api/Helpers/BinaryFrameInputFormatter.cs b/apps/server/AliasVault.Api/Helpers/BinaryFrameInputFormatter.cs
new file mode 100644
index 000000000..b61c5da2b
--- /dev/null
+++ b/apps/server/AliasVault.Api/Helpers/BinaryFrameInputFormatter.cs
@@ -0,0 +1,88 @@
+//-----------------------------------------------------------------------
+// <copyright file="BinaryFrameInputFormatter.cs" company="aliasvault">
+// Copyright (c) aliasvault. All rights reserved.
+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.Api.Helpers;
+
+using System.Buffers.Binary;
+using System.Text.Json;
+using AliasVault.Shared.Models.WebApi.V2.Vault;
+using Microsoft.AspNetCore.Mvc.Formatters;
+
+/// <summary>
+/// Reads an <see cref="IFrameBody"/> request sent as a <see cref="BinaryFrame"/>, so it binds and validates like a JSON body.
+/// </summary>
+public class BinaryFrameInputFormatter : InputFormatter
+{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="BinaryFrameInputFormatter"/> class.
+ /// </summary>
+ public BinaryFrameInputFormatter()
+ {
+ SupportedMediaTypes.Add(BinaryFrame.ContentType);
+ }
+
+ /// <inheritdoc/>
+ public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
+ {
+ using var buffer = new MemoryStream();
+ await context.HttpContext.Request.Body.CopyToAsync(buffer, context.HttpContext.RequestAborted);
+ var body = buffer.GetBuffer().AsMemory(0, (int)buffer.Length);
+
+ if (body.Length < 4 || BinaryPrimitives.ReadUInt32BigEndian(body.Span) > (uint)(body.Length - 4))
+ {
+ return Malformed(context, "The frame header length exceeds the body.");
+ }
+
+ var headerLength = (int)BinaryPrimitives.ReadUInt32BigEndian(body.Span);
+ IFrameBody? model;
+ try
+ {
+ model = JsonSerializer.Deserialize(body.Span.Slice(4, headerLength), context.ModelType, JsonSerializerOptions.Web) as IFrameBody;
+ }
+ catch (JsonException ex)
+ {
+ return Malformed(context, ex.Message);
+ }
+
+ if (model == null)
+ {
+ return Malformed(context, "The frame header is empty.");
+ }
+
+ var offset = 4 + headerLength;
+ foreach (var part in model.FrameParts)
+ {
+ if (part.Size < 0 || part.Size > body.Length - offset)
+ {
+ return Malformed(context, "A ciphertext exceeds the body.");
+ }
+
+ part.Data = body.Slice(offset, part.Size).ToArray();
+ offset += part.Size;
+ }
+
+ return offset == body.Length ? await InputFormatterResult.SuccessAsync(model) : Malformed(context, "The body carries bytes the header does not list.");
+ }
+
+ /// <inheritdoc/>
+ protected override bool CanReadType(Type type)
+ {
+ return typeof(IFrameBody).IsAssignableFrom(type);
+ }
+
+ /// <summary>
+ /// Records why the frame was refused, which turns the request into a 400.
+ /// </summary>
+ /// <param name="context">The formatter context.</param>
+ /// <param name="reason">Why the frame was refused.</param>
+ /// <returns>The failure result.</returns>
+ private static InputFormatterResult Malformed(InputFormatterContext context, string reason)
+ {
+ context.ModelState.TryAddModelError(context.ModelName, reason);
+ return InputFormatterResult.Failure();
+ }
+}
diff --git a/apps/server/AliasVault.Api/Helpers/CiphertextHelper.cs b/apps/server/AliasVault.Api/Helpers/CiphertextHelper.cs
index 9658814f0..3691e1ea1 100644
--- a/apps/server/AliasVault.Api/Helpers/CiphertextHelper.cs
+++ b/apps/server/AliasVault.Api/Helpers/CiphertextHelper.cs
@@ -18,23 +18,12 @@ public static class CiphertextHelper
private const int MinCiphertextLength = 16;
/// <summary>
- /// Decodes a base64-encoded ciphertext into the raw ciphertext bytes.
+ /// Checks that the bytes are long enough to be AES-GCM ciphertext.
/// </summary>
- /// <param name="base64">The base64-encoded ciphertext as it arrived on the request.</param>
- /// <param name="bytes">The decoded ciphertext, empty when the ciphertext is malformed.</param>
- /// <returns>True when the ciphertext decodes to something that can be AES-GCM ciphertext; false when the caller should reject the request.</returns>
- public static bool TryDecode(string base64, out byte[] bytes)
+ /// <param name="bytes">The ciphertext as it arrived on the request.</param>
+ /// <returns>True when the bytes can be AES-GCM ciphertext; false when the caller should reject the request.</returns>
+ public static bool IsCiphertext(byte[] bytes)
{
- try
- {
- bytes = Convert.FromBase64String(base64);
- }
- catch (FormatException)
- {
- bytes = [];
- return false;
- }
-
return bytes.Length >= MinCiphertextLength;
}
diff --git a/apps/server/AliasVault.Api/Program.cs b/apps/server/AliasVault.Api/Program.cs
index 31dc9af2f..4bc61deed 100644
--- a/apps/server/AliasVault.Api/Program.cs
+++ b/apps/server/AliasVault.Api/Program.cs
@@ -164,7 +164,7 @@ builder.Services.AddCors(options =>
.AllowAnyHeader());
});
-builder.Services.AddControllers()
+builder.Services.AddControllers(options => options.InputFormatters.Insert(0, new BinaryFrameInputFormatter()))
.AddJsonOptions(options =>
{
// Ensure consistent date formatting regardless of server locale
diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobDownloadEntry.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobDownloadEntry.cs
deleted file mode 100644
index 4022e0dc1..000000000
--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobDownloadEntry.cs
+++ /dev/null
@@ -1,26 +0,0 @@
-//-----------------------------------------------------------------------
-// <copyright file="BlobDownloadEntry.cs" company="aliasvault">
-// Copyright (c) aliasvault. All rights reserved.
-// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace AliasVault.Shared.Models.WebApi.V2.Vault;
-
-/// <summary>
-/// One blob in the header of the binary blob download response.
-/// </summary>
-public class BlobDownloadEntry
-{
- /// <summary>Gets or sets the per-manifest salted SHA-256 hex of the plaintext.</summary>
- public required string Hash { get; set; }
-
- /// <summary>Gets or sets the blob category ("favicon" or "attachment").</summary>
- public required string Category { get; set; }
-
- /// <summary>Gets or sets the blob's own key, encrypted with the manifest's VEK.</summary>
- public required string EncryptedBlobKey { get; set; }
-
- /// <summary>Gets or sets the length in bytes of the blob's ciphertext in the response body.</summary>
- public int Size { get; set; }
-}
diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobDownloadResponse.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobDownloadResponse.cs
index ddccfa011..da5464a0c 100644
--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobDownloadResponse.cs
+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobDownloadResponse.cs
@@ -14,5 +14,5 @@ namespace AliasVault.Shared.Models.WebApi.V2.Vault;
public class BlobDownloadResponse
{
/// <summary>Gets or sets the requested blobs the server has stored, in the order their bytes follow the header.</summary>
- public List<BlobDownloadEntry> Blobs { get; set; } = [];
+ public List<BlobEntry> Blobs { get; set; } = [];
}
diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/Blob.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobEntry.cs
similarity index 65%
rename from apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/Blob.cs
rename to apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobEntry.cs
index fffb5792e..540ff5ae0 100644
--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/Blob.cs
+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobEntry.cs
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------
-// <copyright file="Blob.cs" company="aliasvault">
+// <copyright file="BlobEntry.cs" company="aliasvault">
// Copyright (c) aliasvault. All rights reserved.
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
// </copyright>
@@ -8,11 +8,12 @@
namespace AliasVault.Shared.Models.WebApi.V2.Vault;
using System.ComponentModel.DataAnnotations;
+using System.Text.Json.Serialization;
/// <summary>
-/// Single encrypted blob payload.
+/// One blob in the header of a binary blob upload or download; its ciphertext follows the header.
/// </summary>
-public class Blob
+public class BlobEntry : IFramePart
{
/// <summary>Gets or sets the per-manifest salted SHA-256 hex of the plaintext.</summary>
[RegularExpression(VaultWriteLimits.HashPattern)]
@@ -22,10 +23,14 @@ public class Blob
[StringLength(VaultWriteLimits.MaxCategoryLength, MinimumLength = 1)]
public required string Category { get; set; }
- /// <summary>Gets or sets the bytes encrypted with the blob's own key, base64-encoded for transport.</summary>
- public required string EncryptedDataBase64 { get; set; }
-
/// <summary>Gets or sets the blob's own key, encrypted with the manifest's VEK.</summary>
[StringLength(255, MinimumLength = 1)]
public required string EncryptedBlobKey { get; set; }
+
+ /// <summary>Gets or sets the length in bytes of the blob's ciphertext in the frame.</summary>
+ public int Size { get; set; }
+
+ /// <summary>Gets or sets the blob's ciphertext (encrypted with the blob's own key). Sent after the JSON header, not inside it.</summary>
+ [JsonIgnore]
+ public byte[] Data { get; set; } = [];
}
diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobUploadRequest.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobUploadRequest.cs
index 1d3a57479..3481f6cd1 100644
--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobUploadRequest.cs
+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BlobUploadRequest.cs
@@ -8,22 +8,27 @@
namespace AliasVault.Shared.Models.WebApi.V2.Vault;
using System.ComponentModel.DataAnnotations;
+using System.Text.Json.Serialization;
/// <summary>
/// POST /v2/Vault/blobs. Batch-upload encrypted blobs ahead of a manifest upload. Idempotent per blob on
/// (manifest, hash). Clients chunk large blob sets across multiple calls to keep request bodies within server limits.
/// </summary>
-public class BlobUploadRequest
+public class BlobUploadRequest : IFrameBody
{
/// <summary>Gets or sets the manifest the blobs belong to.</summary>
public required Guid ManifestId { get; set; }
/// <summary>Gets or sets the encrypted blobs to store.</summary>
[MaxLength(VaultWriteLimits.MaxBlobsPerUpload)]
- public required List<Blob> Blobs { get; set; }
+ public required List<BlobEntry> Blobs { get; set; }
/// <summary>Gets or sets a value indicating whether existing blobs with the same hash must have their ciphertext
/// replaced instead of being skipped. Used during the KEK/VEK migration where every blob is re-encrypted with the
/// new VEK while keeping its (plaintext-based) content hash.</summary>
public bool Overwrite { get; set; }
+
+ /// <inheritdoc/>
+ [JsonIgnore]
+ public IEnumerable<IFramePart> FrameParts => Blobs;
}
diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BucketWrite.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BucketWrite.cs
index fdd3e4523..0fc68ac3e 100644
--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BucketWrite.cs
+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/BucketWrite.cs
@@ -7,11 +7,13 @@
namespace AliasVault.Shared.Models.WebApi.V2.Vault;
+using System.Text.Json.Serialization;
+
/// <summary>
/// A single data bucket to write within a <see cref="VaultWriteRequest"/> batch. Carries the client's believed-current
/// revision so the bucket participates in the same optimistic all-or-nothing gate as the manifests.
/// </summary>
-public class BucketWrite
+public class BucketWrite : IFramePart
{
/// <summary>Gets or sets the id of the manifest that owns this bucket. The caller must have write access to it.</summary>
public required Guid ManifestId { get; set; }
@@ -19,8 +21,12 @@ public class BucketWrite
/// <summary>Gets or sets the bucket kind discriminator.</summary>
public required VaultDataBucketCategory Category { get; set; }
- /// <summary>Gets or sets the encrypted bucket blob (base64 of AES-GCM ciphertext).</summary>
- public required string Blob { get; set; }
+ /// <summary>Gets or sets the length of the bucket ciphertext in the frame.</summary>
+ public int Size { get; set; }
+
+ /// <summary>Gets or sets the bucket ciphertext (AES-GCM). Sent after the JSON header, not inside it.</summary>
+ [JsonIgnore]
+ public byte[] Data { get; set; } = [];
/// <summary>Gets or sets the SHA-256 (hex) of the bucket ciphertext.</summary>
public required string CiphertextHash { get; set; }
diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/IFrameBody.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/IFrameBody.cs
new file mode 100644
index 000000000..121a63484
--- /dev/null
+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/IFrameBody.cs
@@ -0,0 +1,18 @@
+//-----------------------------------------------------------------------
+// <copyright file="IFrameBody.cs" company="aliasvault">
+// Copyright (c) aliasvault. All rights reserved.
+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.Shared.Models.WebApi.V2.Vault;
+
+/// <summary>
+/// A request body that arrives as a binary frame: a 4-byte big-endian header length, this object as JSON, then the
+/// ciphertexts of its <see cref="FrameParts"/> back to back.
+/// </summary>
+public interface IFrameBody
+{
+ /// <summary>Gets the entries that carry a ciphertext, in the order their bytes follow the header.</summary>
+ IEnumerable<IFramePart> FrameParts { get; }
+}
diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/IFramePart.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/IFramePart.cs
new file mode 100644
index 000000000..f6c6a6ac0
--- /dev/null
+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/IFramePart.cs
@@ -0,0 +1,20 @@
+//-----------------------------------------------------------------------
+// <copyright file="IFramePart.cs" company="aliasvault">
+// Copyright (c) aliasvault. All rights reserved.
+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.Shared.Models.WebApi.V2.Vault;
+
+/// <summary>
+/// An entry of a binary frame header whose ciphertext follows the header.
+/// </summary>
+public interface IFramePart
+{
+ /// <summary>Gets the length of the ciphertext in the frame.</summary>
+ int Size { get; }
+
+ /// <summary>Gets or sets the ciphertext, read from the frame after the header.</summary>
+ byte[] Data { get; set; }
+}
diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/ManifestWrite.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/ManifestWrite.cs
index 24f0703e5..9ddfa3610 100644
--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/ManifestWrite.cs
+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/ManifestWrite.cs
@@ -8,17 +8,22 @@
namespace AliasVault.Shared.Models.WebApi.V2.Vault;
using System.ComponentModel.DataAnnotations;
+using System.Text.Json.Serialization;
/// <summary>
/// A single manifest to write within a <see cref="VaultWriteRequest"/> batch.
/// </summary>
-public class ManifestWrite
+public class ManifestWrite : IFramePart
{
/// <summary>Gets or sets the manifest this write targets.</summary>
public required Guid ManifestId { get; set; }
- /// <summary>Gets or sets the encrypted manifest blob.</summary>
- public required string ManifestBlob { get; set; }
+ /// <summary>Gets or sets the length of the manifest ciphertext in the frame.</summary>
+ public int Size { get; set; }
+
+ /// <summary>Gets or sets the manifest ciphertext (AES-GCM). Sent after the JSON header, not inside it.</summary>
+ [JsonIgnore]
+ public byte[] Data { get; set; } = [];
/// <summary>Gets or sets the SHA-256 (hex) of the manifest ciphertext.</summary>
public required string ManifestCiphertextHash { get; set; }
diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteRequest.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteRequest.cs
index f72860cf4..778f48782 100644
--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteRequest.cs
+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteRequest.cs
@@ -11,9 +11,10 @@ using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
/// <summary>
-/// Unified atomic write for POST /v2/Vault.
+/// Unified atomic write for POST /v2/Vault, sent as a binary frame: the manifest ciphertexts follow this JSON header in
+/// <see cref="Manifests"/> order, then the bucket ciphertexts in <see cref="Buckets"/> order.
/// </summary>
-public class VaultWriteRequest
+public class VaultWriteRequest : IFrameBody
{
/// <summary>Gets or sets the username.</summary>
public required string Username { get; set; }
@@ -33,4 +34,8 @@ public class VaultWriteRequest
/// <summary>Gets or sets the one-time migrations to apply atomically with this write.</summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public VaultWriteMigration? Migration { get; set; }
+
+ /// <inheritdoc/>
+ [JsonIgnore]
+ public IEnumerable<IFramePart> FrameParts => Manifests.Cast<IFramePart>().Concat(Buckets);
}
diff --git a/core/client/src/api/WebApiService.ts b/core/client/src/api/WebApiService.ts
index 1ef5d1fa1..5138215f2 100644
--- a/core/client/src/api/WebApiService.ts
+++ b/core/client/src/api/WebApiService.ts
@@ -213,24 +213,25 @@ export class WebApiService {
/**
* Run a request on behalf of the Rust sync engine.
*/
- public async engineRequest(method: string, path: string, body: string | undefined, requiresAuth: boolean, largeTransfer: boolean, binaryResponse = false): Promise<EngineHttpResponse> {
+ public async engineRequest(method: string, path: string, body: string | Uint8Array | undefined, requiresAuth: boolean, largeTransfer: boolean, binaryResponse = false): Promise<EngineHttpResponse> {
const url = await this.resolveUrl(path);
const headers = new Headers({ Accept: binaryResponse ? 'application/octet-stream' : 'application/json' });
if (body !== undefined) {
- headers.set('Content-Type', 'application/json');
+ headers.set('Content-Type', body instanceof Uint8Array ? 'application/octet-stream' : 'application/json');
}
+ const requestBody = body as string | Uint8Array<ArrayBuffer> | undefined;
const accessToken = requiresAuth ? await this.getAccessToken() : null;
if (accessToken) {
headers.set('Authorization', `Bearer ${accessToken}`);
}
try {
- let response = await this.performFetch(url, path, { method, headers, body }, largeTransfer);
+ let response = await this.performFetch(url, path, { method, headers, body: requestBody }, largeTransfer);
if (response.status === 401 && requiresAuth) {
const refreshResult = await this.refreshAccessToken();
if (refreshResult.token) {
headers.set('Authorization', `Bearer ${refreshResult.token}`);
- response = await this.performFetch(url, path, { method, headers, body }, largeTransfer);
+ response = await this.performFetch(url, path, { method, headers, body: requestBody }, largeTransfer);
} else if (refreshResult.isAuthError) {
// The session is truly expired; the engine turns the 401 into its logout outcome.
logoutEventEmitter.emit('common.errors.sessionExpired');
diff --git a/core/client/src/rust/RustCoreBinding.ts b/core/client/src/rust/RustCoreBinding.ts
index d9d925fd9..29acc1928 100644
--- a/core/client/src/rust/RustCoreBinding.ts
+++ b/core/client/src/rust/RustCoreBinding.ts
@@ -7,6 +7,9 @@ export interface IVaultSyncSession {
/** The next command for the host, as JSON. */
nextCommand(): Promise<string>;
+ /** Take the raw bytes attached to the last command (the body of a binary `http` request), if any. */
+ commandBytes(): Promise<Uint8Array | null>;
+
/** Hand the host's response to the last command back, as JSON, with raw bytes for a `dbExport`. */
resume(responseJson: string, bytes: Uint8Array | null): Promise<void>;
diff --git a/core/client/src/rust/WasmRustCore.ts b/core/client/src/rust/WasmRustCore.ts
index 9e5b4c551..5a63d395c 100644
--- a/core/client/src/rust/WasmRustCore.ts
+++ b/core/client/src/rust/WasmRustCore.ts
@@ -105,6 +105,7 @@ export function createWasmRustCore(loadWasm: WasmLoader): IRustCore {
const session = new core.VaultSyncSession(requestJson);
return {
nextCommand: async (): Promise<string> => session.nextCommand(),
+ commandBytes: async (): Promise<Uint8Array | null> => session.commandBytes() ?? null,
resume: async (responseJson: string, bytes: Uint8Array | null): Promise<void> => session.resume(responseJson, bytes),
free: (): void => session.free(),
};
diff --git a/core/client/src/sync/VaultSyncEngine.ts b/core/client/src/sync/VaultSyncEngine.ts
index be6882d4e..64d287a72 100644
--- a/core/client/src/sync/VaultSyncEngine.ts
+++ b/core/client/src/sync/VaultSyncEngine.ts
@@ -174,7 +174,7 @@ type EngineSqlStatement = { sql: string; params?: JsonValue[] };
/** The engine's commands; see the Rust `Command` enum. */
type EngineCommand =
- | { kind: 'http'; method: string; path: string; body?: string; auth: boolean; largeTransfer: boolean; binaryResponse?: boolean }
+ | { kind: 'http'; method: string; path: string; body?: string; auth: boolean; largeTransfer: boolean; binaryResponse?: boolean; binaryBody?: boolean }
| { kind: 'stateGet'; key: string }
| { kind: 'stateSet'; key: string; value: JsonValue }
| { kind: 'stateRemove'; key: string }
@@ -310,12 +310,13 @@ class EngineRun {
/**
* Carry out one command and return the host's response.
* @param command - the command
+ * @param commandBytes - the raw bytes the engine attached to the command (the body of a binary `http` request)
*/
- public async handle(command: Exclude<EngineCommand, { kind: 'done' }>): Promise<HostResponse> {
+ public async handle(command: Exclude<EngineCommand, { kind: 'done' }>, commandBytes: Uint8Array | null = null): Promise<HostResponse> {
const started = now();
try {
if (command.kind === 'http') {
- return await this.http(command);
+ return await this.http(command, commandBytes);
}
const response = await this.dispatch(command);
return response instanceof Uint8Array ? { json: {}, bytes: response } : { json: response, bytes: null };
@@ -401,10 +402,12 @@ class EngineRun {
/**
* An API request. On success a binary response returns its body as raw bytes instead of as `body` text in the JSON.
* @param command - the command
+ * @param commandBytes - the request body of a binary request
*/
- private async http(command: Extract<EngineCommand, { kind: 'http' }>): Promise<HostResponse> {
+ private async http(command: Extract<EngineCommand, { kind: 'http' }>, commandBytes: Uint8Array | null): Promise<HostResponse> {
try {
- const response = await this.webApi.engineRequest(command.method, command.path, command.body, command.auth, command.largeTransfer, command.binaryResponse);
+ const body = command.binaryBody ? commandBytes ?? new Uint8Array() : command.body;
+ const response = await this.webApi.engineRequest(command.method, command.path, body, command.auth, command.largeTransfer, command.binaryResponse);
return { json: { status: response.status, body: response.body }, bytes: response.bytes ?? null };
} catch (error) {
if (error instanceof NetworkError) {
@@ -503,7 +506,8 @@ export async function runVaultSyncEngine<T extends VaultSyncEngineResultBase>(ho
devLog(run.summarize(request.operation, now() - started));
return command.result as T;
}
- const response = await run.handle(command);
+ const commandBytes = command.kind === 'http' && command.binaryBody ? await session.commandBytes() : null;
+ const response = await run.handle(command, commandBytes);
await session.resume(JSON.stringify(response.json), response.bytes);
}
} finally {
diff --git a/core/rust/src/bindings/uniffi_api.rs b/core/rust/src/bindings/uniffi_api.rs
index 8d5e3b5b1..8fbb24265 100644
--- a/core/rust/src/bindings/uniffi_api.rs
+++ b/core/rust/src/bindings/uniffi_api.rs
@@ -330,6 +330,11 @@ impl VaultSyncSession {
self.inner.next_command()
}
+ /// Take the raw bytes attached to the last command (the body of a binary `http` request), if any.
+ pub fn command_bytes(&self) -> Result<Option<Vec<u8>>, VaultError> {
+ self.inner.command_bytes()
+ }
+
/// Hand the host's response to the last command back, as JSON, with raw bytes for a `dbExport`.
pub fn resume(&self, response_json: String, bytes: Option<Vec<u8>>) -> Result<(), VaultError> {
self.inner.resume(&response_json, bytes)
diff --git a/core/rust/src/bindings/wasm.rs b/core/rust/src/bindings/wasm.rs
index 53faeee7e..a5a928c2a 100644
--- a/core/rust/src/bindings/wasm.rs
+++ b/core/rust/src/bindings/wasm.rs
@@ -325,6 +325,12 @@ impl VaultSyncSessionJs {
self.inner.next_command().map_err(js_err)
}
+ /// Take the raw bytes attached to the last command (the body of a binary `http` request), if any.
+ #[wasm_bindgen(js_name = commandBytes)]
+ pub fn command_bytes(&self) -> Result<Option<Vec<u8>>, JsValue> {
+ self.inner.command_bytes().map_err(js_err)
+ }
+
/// Hand the host's response to the last command back, as JSON, with raw bytes for a `dbExport`.
pub fn resume(&self, response_json: &str, bytes: Option<Vec<u8>>) -> Result<(), JsValue> {
self.inner.resume(response_json, bytes).map_err(js_err)
diff --git a/core/rust/src/crypto/aes_gcm.rs b/core/rust/src/crypto/aes_gcm.rs
index a8f50812a..8b0b682c8 100644
--- a/core/rust/src/crypto/aes_gcm.rs
+++ b/core/rust/src/crypto/aes_gcm.rs
@@ -26,6 +26,11 @@ pub fn symmetric_encrypt_bytes(plaintext: &[u8], key_base64: &str) -> VaultResul
/// Encrypt bytes bound to `aad`, which decryption must present as-is. Returns base64 of `IV | ciphertext | tag`.
pub fn symmetric_encrypt_bytes_with_aad(plaintext: &[u8], key_base64: &str, aad: &[u8]) -> VaultResult<String> {
+ Ok(base64_encode(&symmetric_encrypt_raw_with_aad(plaintext, key_base64, aad)?))
+}
+
+/// Encrypt bytes bound to `aad`, which decryption must present as-is. Returns the raw `IV | ciphertext | tag`.
+pub fn symmetric_encrypt_raw_with_aad(plaintext: &[u8], key_base64: &str, aad: &[u8]) -> VaultResult<Vec<u8>> {
let cipher = cipher_for(key_base64)?;
let mut iv = [0u8; IV_LENGTH];
fill_random(&mut iv);
@@ -37,7 +42,7 @@ pub fn symmetric_encrypt_bytes_with_aad(plaintext: &[u8], key_base64: &str, aad:
let mut combined = Vec::with_capacity(IV_LENGTH + ciphertext.len());
combined.extend_from_slice(&iv);
combined.extend_from_slice(&ciphertext);
- Ok(base64_encode(&combined))
+ Ok(combined)
}
/// Decrypt `IV | ciphertext | tag` bytes with a base64 key and no associated data.
diff --git a/core/rust/src/crypto/mod.rs b/core/rust/src/crypto/mod.rs
index b88f9aba3..edf0eeca7 100644
--- a/core/rust/src/crypto/mod.rs
+++ b/core/rust/src/crypto/mod.rs
@@ -10,7 +10,7 @@ pub mod srp;
pub use aes_gcm::{
generate_key_base64, symmetric_decrypt, symmetric_decrypt_bytes, symmetric_decrypt_bytes_with_aad, symmetric_decrypt_with_aad, symmetric_encrypt, symmetric_encrypt_bytes, symmetric_encrypt_bytes_with_aad,
- symmetric_encrypt_with_aad,
+ symmetric_encrypt_raw_with_aad, symmetric_encrypt_with_aad,
};
pub use key_chain::{
create_account_key_hierarchy, create_account_key_hierarchy_with_key_pair, derive_kek, derive_kek_base64, derive_srp_password_hash, open_account_key_chain, open_account_private_key, open_account_signing_private_key, reencrypt_account_key, unwrap_account_key, unwrap_key, wrap_account_key, wrap_key, AccountKeyBlobs,
diff --git a/core/rust/src/vault_sync/blob_keys.rs b/core/rust/src/vault_sync/blob_keys.rs
index 02e2ea774..7d9658dff 100644
--- a/core/rust/src/vault_sync/blob_keys.rs
+++ b/core/rust/src/vault_sync/blob_keys.rs
@@ -3,15 +3,16 @@
use serde::{Deserialize, Serialize};
use crate::crypto::{self, aad};
-use crate::common::encoding::base64_decode;
+use crate::common::encoding::{base64_decode, base64_encode};
use crate::common::error::VaultResult;
/// A blob as the server stores it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct EncryptedBlob {
- /// Base64 of the bytes, encrypted with the blob key.
- pub encrypted_data_base64: String,
+ /// The bytes, encrypted with the blob key. Kept as base64 in the persisted cache (the field name predates raw transport).
+ #[serde(rename = "encryptedDataBase64", with = "base64_bytes")]
+ pub encrypted_data: Vec<u8>,
/// The blob key, encrypted with the manifest's VEK.
pub encrypted_blob_key: String,
}
@@ -20,7 +21,7 @@ pub(crate) struct EncryptedBlob {
pub(crate) fn encrypt_blob(bytes: &[u8], vek: &str, manifest_id: &str, hash: &str) -> VaultResult<EncryptedBlob> {
let blob_key = zeroize::Zeroizing::new(crypto::generate_key_base64());
Ok(EncryptedBlob {
- encrypted_data_base64: crypto::symmetric_encrypt_bytes_with_aad(bytes, &blob_key, &aad::blob_data(manifest_id, hash))?,
+ encrypted_data: crypto::symmetric_encrypt_raw_with_aad(bytes, &blob_key, &aad::blob_data(manifest_id, hash))?,
encrypted_blob_key: crypto::wrap_key(&blob_key, vek, &aad::blob_key(manifest_id, hash))?,
})
}
@@ -28,7 +29,20 @@ pub(crate) fn encrypt_blob(bytes: &[u8], vek: &str, manifest_id: &str, hash: &st
/// Decrypt the blob stored at `(manifest_id, hash)` with the manifest's VEK.
pub(crate) fn decrypt_blob(blob: &EncryptedBlob, vek: &str, manifest_id: &str, hash: &str) -> VaultResult<Vec<u8>> {
let blob_key = crypto::unwrap_key(&blob.encrypted_blob_key, vek, &aad::blob_key(manifest_id, hash))?;
- crypto::symmetric_decrypt_bytes_with_aad(&base64_decode(&blob.encrypted_data_base64)?, &blob_key, &aad::blob_data(manifest_id, hash))
+ crypto::symmetric_decrypt_bytes_with_aad(&blob.encrypted_data, &blob_key, &aad::blob_data(manifest_id, hash))
+}
+
+/// Serde for bytes stored as a standard base64 string.
+mod base64_bytes {
+ use serde::{Deserialize, Deserializer, Serializer};
+
+ pub fn serialize<S: Serializer>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error> {
+ serializer.serialize_str(&super::base64_encode(bytes))
+ }
+
+ pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Vec<u8>, D::Error> {
+ super::base64_decode(&String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
+ }
}
#[cfg(test)]
@@ -44,7 +58,7 @@ mod tests {
let blob = encrypt_blob(&[1, 2, 3, 4], &vek, MANIFEST, HASH).unwrap();
assert_eq!(decrypt_blob(&blob, &vek, MANIFEST, HASH).unwrap(), vec![1, 2, 3, 4]);
- assert!(crypto::symmetric_decrypt_bytes(&base64_decode(&blob.encrypted_data_base64).unwrap(), &vek).is_err(), "the bytes are not encrypted with the VEK itself");
+ assert!(crypto::symmetric_decrypt_bytes(&blob.encrypted_data, &vek).is_err(), "the bytes are not encrypted with the VEK itself");
assert!(decrypt_blob(&blob, &crypto::generate_key_base64(), MANIFEST, HASH).is_err());
}
@@ -57,7 +71,7 @@ mod tests {
// The blob key of one blob next to the bytes of another: neither half opens the other.
let other = encrypt_blob(&[5, 6], &vek, MANIFEST, "bb22").unwrap();
- let mixed = EncryptedBlob { encrypted_data_base64: other.encrypted_data_base64, encrypted_blob_key: blob.encrypted_blob_key };
+ let mixed = EncryptedBlob { encrypted_data: other.encrypted_data, encrypted_blob_key: blob.encrypted_blob_key };
assert!(decrypt_blob(&mixed, &vek, MANIFEST, HASH).is_err());
}
@@ -68,7 +82,7 @@ mod tests {
let key_aad = aad::blob_key(MANIFEST, HASH);
let blob_key = crypto::unwrap_key(&blob.encrypted_blob_key, &old_vek, &key_aad).unwrap();
- let rekeyed = EncryptedBlob { encrypted_data_base64: blob.encrypted_data_base64.clone(), encrypted_blob_key: crypto::wrap_key(&blob_key, &new_vek, &key_aad).unwrap() };
+ let rekeyed = EncryptedBlob { encrypted_data: blob.encrypted_data.clone(), encrypted_blob_key: crypto::wrap_key(&blob_key, &new_vek, &key_aad).unwrap() };
assert_eq!(decrypt_blob(&rekeyed, &new_vek, MANIFEST, HASH).unwrap(), vec![9; 64]);
assert!(decrypt_blob(&rekeyed, &old_vek, MANIFEST, HASH).is_err());
diff --git a/core/rust/src/vault_sync/http.rs b/core/rust/src/vault_sync/http.rs
index d91a32a28..b55aaaf38 100644
--- a/core/rust/src/vault_sync/http.rs
+++ b/core/rust/src/vault_sync/http.rs
@@ -7,12 +7,26 @@ use super::errors::{SyncError, SyncResult};
use super::session::Host;
use super::types::{Command, HttpMethod, HttpResponse, LogLevel, StatusResponse};
+/// The body of a request: JSON text, or raw bytes the host sends as `application/octet-stream`.
+enum Body {
+ None,
+ Json(String),
+ Binary(Vec<u8>),
+}
+
async fn send(host: &Host, method: HttpMethod, path: &str, body: Option<String>, auth: bool, large_transfer: bool) -> SyncResult<HttpResponse> {
+ let body = body.map_or(Body::None, Body::Json);
Ok(send_with_bytes(host, method, path, body, auth, large_transfer, false).await?.0)
}
-async fn send_with_bytes(host: &Host, method: HttpMethod, path: &str, body: Option<String>, auth: bool, large_transfer: bool, binary_response: bool) -> SyncResult<(HttpResponse, Option<Vec<u8>>)> {
- let (response, bytes): (HttpResponse, _) = host.call_with_bytes(Command::Http { method, path: path.to_string(), body, auth, large_transfer, binary_response }).await?;
+async fn send_with_bytes(host: &Host, method: HttpMethod, path: &str, body: Body, auth: bool, large_transfer: bool, binary_response: bool) -> SyncResult<(HttpResponse, Option<Vec<u8>>)> {
+ let (body, binary_body, command_bytes) = match body {
+ Body::None => (None, false, None),
+ Body::Json(json) => (Some(json), false, None),
+ Body::Binary(bytes) => (None, true, Some(bytes)),
+ };
+ let command = Command::Http { method, path: path.to_string(), body, auth, large_transfer, binary_response, binary_body };
+ let (response, bytes): (HttpResponse, _) = host.exchange(command, command_bytes).await?;
if response.status == 0 {
let message = response.transport_error.unwrap_or_else(|| "request failed".to_string());
return Err(if response.timed_out { SyncError::Timeout(message) } else { SyncError::Network(message) });
@@ -64,18 +78,51 @@ pub(crate) async fn post<B: Serialize, T: DeserializeOwned>(host: &Host, path: &
/// Authenticated GET of a v2 endpoint that answers with raw bytes.
pub(crate) async fn get_binary(host: &Host, path: &str, large_transfer: bool) -> SyncResult<Vec<u8>> {
- let (response, bytes) = send_with_bytes(host, HttpMethod::Get, path, None, true, large_transfer, true).await?;
+ let (response, bytes) = send_with_bytes(host, HttpMethod::Get, path, Body::None, true, large_transfer, true).await?;
check(response)?;
Ok(bytes.unwrap_or_default())
}
/// Authenticated POST of a v2 endpoint with a JSON body that answers with raw bytes.
pub(crate) async fn post_binary<B: Serialize>(host: &Host, path: &str, body: &B, large_transfer: bool) -> SyncResult<Vec<u8>> {
- let (response, bytes) = send_with_bytes(host, HttpMethod::Post, path, Some(serde_json::to_string(body)?), true, large_transfer, true).await?;
+ let (response, bytes) = send_with_bytes(host, HttpMethod::Post, path, Body::Json(serde_json::to_string(body)?), true, large_transfer, true).await?;
check(response)?;
Ok(bytes.unwrap_or_default())
}
+/// A request body sent as a binary frame: its JSON header (which lists each ciphertext's size), then the ciphertexts.
+pub(crate) trait FrameBody: Serialize {
+ /// The ciphertexts, in the order the header lists their sizes.
+ fn parts(&self) -> Vec<&[u8]>;
+}
+
+/// Authenticated POST of a v2 endpoint with a binary frame body and a JSON response.
+pub(crate) async fn post_frame<B: FrameBody, T: DeserializeOwned>(host: &Host, path: &str, body: &B) -> SyncResult<T> {
+ let (response, _) = send_with_bytes(host, HttpMethod::Post, path, Body::Binary(encode_frame(body)?), true, true, false).await?;
+ parse(&check(response)?)
+}
+
+/// Authenticated POST of a v2 endpoint with a binary frame body and no response body.
+pub(crate) async fn post_frame_no_content<B: FrameBody>(host: &Host, path: &str, body: &B) -> SyncResult<()> {
+ let (response, _) = send_with_bytes(host, HttpMethod::Post, path, Body::Binary(encode_frame(body)?), true, true, false).await?;
+ check(response)?;
+ Ok(())
+}
+
+/// A binary frame: 4-byte big-endian header length, the JSON header, then the parts back to back.
+pub(crate) fn encode_frame<B: FrameBody>(body: &B) -> SyncResult<Vec<u8>> {
+ let header = serde_json::to_vec(body)?;
+ let parts = body.parts();
+ let header_length = u32::try_from(header.len()).map_err(|_| SyncError::Other("binary frame header exceeds 4 GiB".to_string()))?;
+ let mut frame = Vec::with_capacity(4 + header.len() + parts.iter().map(|p| p.len()).sum::<usize>());
+ frame.extend_from_slice(&header_length.to_be_bytes());
+ frame.extend_from_slice(&header);
+ for part in parts {
+ frame.extend_from_slice(part);
+ }
+ Ok(frame)
+}
+
/// The ciphertexts of a binary vault download, after its JSON header, taken in the order the header lists their sizes.
pub(crate) struct Frame<'a> {
data: &'a [u8],
diff --git a/core/rust/src/vault_sync/pull.rs b/core/rust/src/vault_sync/pull.rs
index b5a7962a5..65a1f673d 100644
--- a/core/rust/src/vault_sync/pull.rs
+++ b/core/rust/src/vault_sync/pull.rs
@@ -9,7 +9,6 @@ use super::state::{self, Ctx};
use super::types::{self, BlobDownloadEntry, BlobDownloadResponse, BlobHashesRequest, Db, EmailRoutingDto, GetResponse, ManifestDto, SharedManifestDto, StoredBlobRef};
use super::blob_keys::{self, EncryptedBlob};
use super::{db, http, keys, legacy};
-use crate::common::encoding::base64_encode;
use crate::crypto;
use crate::vault_codec::{self, CanonicalizeInput, CodecTableData, DataBucket, Manifest, ManifestSpec, MaterializeInput, UnpackedPayload};
use crate::vault_model::SYNCABLE_TABLE_NAMES;
@@ -333,7 +332,7 @@ async fn download_referenced_blobs(ctx: &Ctx, resolved: &[ResolvedManifest]) ->
let blobs = decode_blob_download(&body)?;
ctx.log(format!("[Pull] Downloaded blob batch {}/{}: requested {}, received {} ({} bytes).", index + 1, batch_count, chunk.len(), blobs.len(), body.len())).await;
for (entry, ciphertext) in blobs {
- cache.insert(entry.hash, EncryptedBlob { encrypted_data_base64: base64_encode(ciphertext), encrypted_blob_key: entry.encrypted_blob_key });
+ cache.insert(entry.hash, EncryptedBlob { encrypted_data: ciphertext.to_vec(), encrypted_blob_key: entry.encrypted_blob_key });
}
}
diff --git a/core/rust/src/vault_sync/push.rs b/core/rust/src/vault_sync/push.rs
index 5981d5095..dcd73d559 100644
--- a/core/rust/src/vault_sync/push.rs
+++ b/core/rust/src/vault_sync/push.rs
@@ -8,7 +8,7 @@ use crate::vault_model::{id_key, OVERFLOW_TABLE, TRASH_RETENTION_DEFAULT_DAYS};
use super::email_routing::build_email_routing;
use super::errors::{SyncError, SyncResult};
use super::state::{self, Ctx};
-use super::types::{BlobDto, BlobHashesRequest, BlobRef, BlobUploadRequest, BucketRevision, BucketWrite, Db, ManifestRevision, ManifestWrite, MissingBlobsResponse, VaultWriteMigration, VaultWriteRequest, VaultWriteResponse, VaultWriteStatus, ALGORITHM_RSA_OAEP_SHA256};
+use super::types::{BlobHashesRequest, BlobUpload, BlobRef, BlobUploadRequest, BucketRevision, BucketWrite, Db, ManifestRevision, ManifestWrite, MissingBlobsResponse, VaultWriteMigration, VaultWriteRequest, VaultWriteResponse, VaultWriteStatus, ALGORITHM_RSA_OAEP_SHA256};
use super::blob_keys::{self, EncryptedBlob};
use super::write_set::{self, ManifestRecord, SkipReason};
use super::{db, http, keys};
@@ -222,7 +222,7 @@ async fn write_dirty_buckets(ctx: &Ctx, categories: &[String]) -> SyncResult<Pus
return Ok(PushStatus::Ok);
}
let payload = VaultWriteRequest { username: ctx.request.username.clone(), manifests: Vec::new(), buckets: writes, email_routing: None, migration: None };
- let response: VaultWriteResponse = http::with_outdated_server_guard(http::post(&ctx.host, http::VAULT_ENDPOINT, &payload, true).await)?;
+ let response: VaultWriteResponse = http::with_outdated_server_guard(http::post_frame(&ctx.host, http::VAULT_ENDPOINT, &payload).await)?;
if response.status != VaultWriteStatus::Ok {
ctx.warn("[Push] Bucket-only write outdated; pulling and merging before the next attempt.").await;
return Ok(PushStatus::Outdated);
@@ -397,16 +397,16 @@ fn fingerprinted<T: serde::Serialize>(payload: &T) -> SyncResult<(String, String
/// A payload packed and encrypted for the write, with the hash the server verifies it by.
struct EncryptedPayload {
- ciphertext: String,
+ ciphertext: Vec<u8>,
hash: String,
}
/// Pack and encrypt a JSON payload under `key` bound to `aad`, logging its size at every stage.
async fn encrypt_payload(ctx: &Ctx, label: &str, plaintext: &str, key: &str, aad: &[u8]) -> SyncResult<EncryptedPayload> {
let packed = vault_codec::pack_payload(plaintext)?;
- let ciphertext = crypto::symmetric_encrypt_bytes_with_aad(&packed, key, aad)?;
+ let ciphertext = crypto::symmetric_encrypt_raw_with_aad(&packed, key, aad)?;
ctx.log(format!("[Push] {}: raw {} > compressed {} > encrypted {}.", label, format_kb(plaintext.len()), format_kb(packed.len()), format_kb(ciphertext.len()))).await;
- let hash = vault_codec::compute_ciphertext_hash(&ciphertext);
+ let hash = vault_codec::compute_ciphertext_hash_bytes(&ciphertext);
Ok(EncryptedPayload { ciphertext, hash })
}
@@ -646,7 +646,7 @@ async fn missing_on_server(ctx: &Ctx, blobs: &UploadBlobs, hashes: &[String]) ->
/// `POST v2/Vault`. When the server reports blobs it lacks (stale local knowledge of its blob set), upload
/// them and retry the identical write once; blobs this client cannot supply fail the push.
async fn write_vault(ctx: &Ctx, payload: &VaultWriteRequest, blobs: &UploadBlobs, overwrite_personal_blobs: bool, uploaded: &mut HashMap<String, EncryptedBlob>) -> SyncResult<VaultWriteResponse> {
- let response: VaultWriteResponse = http::post(&ctx.host, http::VAULT_ENDPOINT, payload, true).await?;
+ let response: VaultWriteResponse = http::post_frame(&ctx.host, http::VAULT_ENDPOINT, payload).await?;
if response.missing_blob_hashes.is_empty() {
return Ok(response);
}
@@ -658,7 +658,7 @@ async fn write_vault(ctx: &Ctx, payload: &VaultWriteRequest, blobs: &UploadBlobs
let (missing_personal, missing_shared): (Vec<String>, Vec<String>) = response.missing_blob_hashes.iter().cloned().partition(|h| blobs.entries[h].from_personal);
uploaded.extend(upload_blobs(ctx, blobs, &missing_personal, overwrite_personal_blobs).await?);
uploaded.extend(upload_blobs(ctx, blobs, &missing_shared, false).await?);
- let response: VaultWriteResponse = http::post(&ctx.host, http::VAULT_ENDPOINT, payload, true).await?;
+ let response: VaultWriteResponse = http::post_frame(&ctx.host, http::VAULT_ENDPOINT, payload).await?;
if !response.missing_blob_hashes.is_empty() {
return Err(SyncError::MissingBlobs(response.missing_blob_hashes));
}
@@ -722,12 +722,12 @@ async fn upload_blobs(ctx: &Ctx, blobs: &UploadBlobs, hashes: &[String], overwri
let entry = &blobs.entries[&hash];
let encrypted = blob_keys::encrypt_blob(&entry.bytes, &entry.vek, &manifest_id, &hash)?;
encrypted_blobs.insert(hash.clone(), encrypted.clone());
- dtos.push(BlobDto { hash, category: entry.kind.clone(), encrypted_data_base64: encrypted.encrypted_data_base64, encrypted_blob_key: encrypted.encrypted_blob_key });
+ dtos.push(BlobUpload { hash, category: entry.kind.clone(), encrypted_data: encrypted.encrypted_data, encrypted_blob_key: encrypted.encrypted_blob_key });
}
- for batch in http::batch_by_transfer_cost(dtos, |dto| dto.encrypted_data_base64.len()) {
- let chars: usize = batch.iter().map(|dto| dto.encrypted_data_base64.len()).sum();
- ctx.log(format!("[Push] Uploading blob batch: {} blobs, {}.", batch.len(), format_kb(chars))).await;
- http::post_no_content(&ctx.host, BLOBS_ENDPOINT, &BlobUploadRequest { manifest_id: manifest_id.clone(), blobs: batch, overwrite }).await?;
+ for batch in http::batch_by_transfer_cost(dtos, |dto| dto.encrypted_data.len()) {
+ let bytes: usize = batch.iter().map(|dto| dto.encrypted_data.len()).sum();
+ ctx.log(format!("[Push] Uploading blob batch: {} blobs, {}.", batch.len(), format_kb(bytes))).await;
+ http::post_frame_no_content(&ctx.host, BLOBS_ENDPOINT, &BlobUploadRequest { manifest_id: manifest_id.clone(), blobs: batch, overwrite }).await?;
}
}
Ok(encrypted_blobs)
diff --git a/core/rust/src/vault_sync/session.rs b/core/rust/src/vault_sync/session.rs
index c767c3915..7c135e588 100644
--- a/core/rust/src/vault_sync/session.rs
+++ b/core/rust/src/vault_sync/session.rs
@@ -17,8 +17,8 @@ use crate::common::error::{VaultError, VaultResult};
#[derive(Default)]
pub(crate) struct Slot {
pub pending: Option<Command>,
+ pub pending_bytes: Option<Vec<u8>>,
pub response: Option<Value>,
- /// Raw bytes the host handed back with the response (the SQLite file of a `dbExport`).
pub response_bytes: Option<Vec<u8>>,
}
@@ -40,8 +40,13 @@ impl Host {
/// Like [`Host::call`], also returning the raw bytes the host attached to its response, if any.
pub async fn call_with_bytes<R: DeserializeOwned>(&self, command: Command) -> SyncResult<(R, Option<Vec<u8>>)> {
+ self.exchange(command, None).await
+ }
+
+ /// Send a command with raw bytes attached for the host, returning the typed response and the bytes the host attached to it.
+ pub async fn exchange<R: DeserializeOwned>(&self, command: Command, command_bytes: Option<Vec<u8>>) -> SyncResult<(R, Option<Vec<u8>>)> {
let kind = command.name();
- let (response, bytes) = CommandFuture { slot: self.slot.clone(), command: Some(command), sent: false }.await;
+ let (response, bytes) = CommandFuture { slot: self.slot.clone(), command: Some(command), command_bytes, sent: false }.await;
if let Some(error) = response.get("error").and_then(Value::as_str) {
return Err(SyncError::Host { command: kind, message: error.to_string() });
}
@@ -59,6 +64,7 @@ impl Host {
struct CommandFuture {
slot: Arc<Mutex<Slot>>,
command: Option<Command>,
+ command_bytes: Option<Vec<u8>>,
sent: bool,
}
@@ -68,7 +74,10 @@ impl Future for CommandFuture {
fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = &mut *self;
if !this.sent {
- this.slot.lock().expect("sync host slot poisoned").pending = this.command.take();
+ let mut slot = this.slot.lock().expect("sync host slot poisoned");
+ slot.pending = this.command.take();
+ slot.pending_bytes = this.command_bytes.take();
+ drop(slot);
this.sent = true;
return Poll::Pending;
}
@@ -89,6 +98,8 @@ struct SessionInner {
slot: Arc<Mutex<Slot>>,
future: Option<Pin<Box<dyn Future<Output = Value> + Send>>>,
finished: Option<Value>,
+ /// Raw bytes attached to the last command handed out, until the host takes them.
+ command_bytes: Option<Vec<u8>>,
}
impl SyncSession {
@@ -98,7 +109,7 @@ impl SyncSession {
let slot = Arc::new(Mutex::new(Slot::default()));
let host = Host::new(slot.clone());
let future: Pin<Box<dyn Future<Output = Value> + Send>> = Box::pin(engine::run(host, request));
- Ok(Self { inner: Mutex::new(SessionInner { slot, future: Some(future), finished: None }) })
+ Ok(Self { inner: Mutex::new(SessionInner { slot, future: Some(future), finished: None, command_bytes: None }) })
}
/// The next command for the host, as JSON.
@@ -117,7 +128,11 @@ impl SyncSession {
Ok(serde_json::to_string(&Command::Done { result })?)
}
Poll::Pending => {
- let pending = inner.slot.lock().map_err(|_| VaultError::General("sync host slot poisoned".to_string()))?.pending.take();
+ let (pending, pending_bytes) = {
+ let mut slot = inner.slot.lock().map_err(|_| VaultError::General("sync host slot poisoned".to_string()))?;
+ (slot.pending.take(), slot.pending_bytes.take())
+ };
+ inner.command_bytes = pending_bytes;
match pending {
Some(command) => Ok(serde_json::to_string(&command)?),
None => Err(VaultError::General("sync session is waiting for a response to its last command".to_string())),
@@ -126,6 +141,12 @@ impl SyncSession {
}
}
+ /// Take the raw bytes attached to the last command (the body of a binary `http` request), if any.
+ pub fn command_bytes(&self) -> VaultResult<Option<Vec<u8>>> {
+ let mut inner = self.inner.lock().map_err(|_| VaultError::General("sync session poisoned".to_string()))?;
+ Ok(inner.command_bytes.take())
+ }
+
/// Hand the host's response to the last command back, with raw bytes for a `dbExport` (the SQLite file).
pub fn resume(&self, response_json: &str, bytes: Option<Vec<u8>>) -> VaultResult<()> {
let response: Value = if response_json.trim().is_empty() {
diff --git a/core/rust/src/vault_sync/types.rs b/core/rust/src/vault_sync/types.rs
index c8a83c610..019ba7975 100644
--- a/core/rust/src/vault_sync/types.rs
+++ b/core/rust/src/vault_sync/types.rs
@@ -260,6 +260,9 @@ pub enum Command {
/// On success the host returns the body as raw bytes (the `bytes` argument of `SyncSession::resume`), not as `body` text.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
binary_response: bool,
+ /// The request body is raw bytes, which the host takes with `SyncSession::command_bytes` and sends as `application/octet-stream`.
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ binary_body: bool,
},
/// Read an engine-owned persisted value; response [`StateValue`].
StateGet { key: String },
@@ -547,7 +550,9 @@ pub const ALGORITHM_AES256_GCM: &str = crate::crypto::key_chain::ACCOUNT_KEY_WRA
#[serde(rename_all = "camelCase")]
pub struct ManifestWrite {
pub manifest_id: String,
- pub manifest_blob: String,
+ /// The manifest ciphertext; the JSON header carries its length as `size`, the bytes follow the header.
+ #[serde(rename = "size", serialize_with = "serialize_len")]
+ pub manifest_blob: Vec<u8>,
pub manifest_ciphertext_hash: String,
pub current_revision: i64,
pub credentials_count: usize,
@@ -572,20 +577,30 @@ pub struct BlobRef {
pub struct BucketWrite {
pub manifest_id: String,
pub category: String,
- pub blob: String,
+ /// The bucket ciphertext; the JSON header carries its length as `size`, the bytes follow the header.
+ #[serde(rename = "size", serialize_with = "serialize_len")]
+ pub blob: Vec<u8>,
pub ciphertext_hash: String,
pub current_revision: i64,
}
-#[derive(Debug, Clone, Serialize, Deserialize)]
+/// One blob of a binary `POST v2/Vault/blobs` upload.
+#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
-pub struct BlobDto {
+pub struct BlobUpload {
pub hash: String,
pub category: String,
- pub encrypted_data_base64: String,
+ /// The blob ciphertext; the JSON header carries its length as `size`, the bytes follow the header.
+ #[serde(rename = "size", serialize_with = "serialize_len")]
+ pub encrypted_data: Vec<u8>,
pub encrypted_blob_key: String,
}
+/// Serialize a ciphertext field as its length: the bytes travel after the JSON header of a binary frame.
+fn serialize_len<S: serde::Serializer>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error> {
+ serializer.serialize_u64(bytes.len() as u64)
+}
+
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ClaimedEmailAddress {
@@ -614,6 +629,12 @@ pub struct VaultWriteRequest {
pub migration: Option<VaultWriteMigration>,
}
+impl super::http::FrameBody for VaultWriteRequest {
+ fn parts(&self) -> Vec<&[u8]> {
+ self.manifests.iter().map(|m| m.manifest_blob.as_slice()).chain(self.buckets.iter().map(|b| b.blob.as_slice())).collect()
+ }
+}
+
/// One-time migrations applied atomically with a vault write (optional field).
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
@@ -652,10 +673,16 @@ pub enum VaultWriteStatus {
#[serde(rename_all = "camelCase")]
pub struct BlobUploadRequest {
pub manifest_id: String,
- pub blobs: Vec<BlobDto>,
+ pub blobs: Vec<BlobUpload>,
pub overwrite: bool,
}
+impl super::http::FrameBody for BlobUploadRequest {
+ fn parts(&self) -> Vec<&[u8]> {
+ self.blobs.iter().map(|b| b.encrypted_data.as_slice()).collect()
+ }
+}
+
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BlobHashesRequest {