π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[photos][web] Reject truncated streaming downloads
AswinAsok62e4721ded1e
diff --git a/web/apps/photos/tests/download-core.test.ts b/web/apps/photos/tests/download-core.test.tsindex d1c872438f..63441134b0 100644--- a/web/apps/photos/tests/download-core.test.ts+++ b/web/apps/photos/tests/download-core.test.ts@@ -267,9 +267,15 @@ describe("download byte progress", () => {describe("download consumer compatibility", () => {test.each([["fragmented chunks", [[1], [2], [3], [4], [5]]],+ [+ "full-size final chunk",+ [+ [1, 2],+ [3, 4],+ ],+ ],["multiple chunks per read", [[1, 2, 3, 4, 5]]],["empty network chunks", [[], [1, 2], [], [3, 4, 5]]],- ["empty stream", []],] as const)("emits decrypted video bytes with %s", async (_, chunks) => {vi.mocked(decryptStreamChunk).mockClear();vi.mocked(initChunkDecryption).mockClear();@@ -282,7 +288,11 @@ describe("download consumer compatibility", () => {const expectedChunks = [];for (let i = 0; i < encryptedBytes.length; i += 2) {const chunk = encryptedBytes.slice(i, i + 2);- expectedChunks.push([chunk, pullState]);+ expectedChunks.push([+ chunk,+ pullState,+ i + 2 >= encryptedBytes.length,+ ]);vi.mocked(decryptStreamChunk).mockResolvedValueOnce(chunk.map((byte) => byte + 10),);diff --git a/web/apps/photos/tests/download-integrity.test.ts b/web/apps/photos/tests/download-integrity.test.tsnew file mode 100644index 0000000000..4c4bc89120--- /dev/null+++ b/web/apps/photos/tests/download-integrity.test.ts@@ -0,0 +1,121 @@+import {+ encryptStreamBytes,+ generateBlobOrStreamKey,+} from "ente-base/crypto/libsodium";+import {+ streamEncryptionChunkOverhead,+ streamEncryptionChunkSize,+ type EncryptedFile,+} from "ente-base/crypto/types";+import { createDownloadManager } from "ente-gallery/services/download-core";+import type { EnteFile } from "ente-media/file";+import { FileType } from "ente-media/file-type";+import { Buffer } from "node:buffer";+import { beforeAll, describe, expect, test, vi } from "vitest";++// Exercise the real cryptography without needing a browser Comlink worker.+vi.mock("ente-base/crypto", () => import("ente-base/crypto/libsodium"));+vi.mock("ente-base/log", () => ({+ default: { info: vi.fn(), error: vi.fn() },+}));++let key: string;+let encrypted: EncryptedFile;+const original = new Uint8Array(streamEncryptionChunkSize * 2 + 123).fill(42);+const encryptedChunkSize =+ streamEncryptionChunkSize + streamEncryptionChunkOverhead;++beforeAll(async () => {+ key = await generateBlobOrStreamKey();+ encrypted = await encryptStreamBytes(original, key);+});++const download = (+ data: Uint8Array<ArrayBuffer> | null,+ decryptionHeader = encrypted.decryptionHeader,+) => {+ const manager = createDownloadManager({+ downloadFile: () => Promise.resolve(new Response(data)),+ downloadThumbnail: vi.fn(),+ renderableImageBlob: vi.fn(),+ playableVideoURL: vi.fn(),+ });+ const file = {+ id: 42,+ metadata: { fileType: FileType.video },+ file: { decryptionHeader },+ key,+ } as EnteFile;+ return {+ manager,+ bytes: manager+ .fileStream(file)+ .then((stream) => new Response(stream).arrayBuffer()),+ };+};++describe("streaming video download integrity", () => {+ test.each([0, 123, streamEncryptionChunkSize, original.length])(+ "accepts a complete file with %i plaintext bytes",+ async (size) => {+ const plaintext = original.slice(0, size);+ const file = await encryptStreamBytes(plaintext, key);+ const { bytes, manager } = download(+ file.encryptedData,+ file.decryptionHeader,+ );+ expect(+ Buffer.from(await bytes).equals(Buffer.from(plaintext)),+ ).toBe(true);+ expect(manager.fileDownloadProgressSnapshot().size).toBe(0);+ },+ );++ test.each([0, encryptedChunkSize, encryptedChunkSize * 2])(+ "rejects EOF after %i encrypted bytes without a final tag",+ async (length) => {+ const { bytes, manager } = download(+ encrypted.encryptedData.slice(0, length),+ );+ await expect(bytes).rejects.toThrow(+ length === 0+ ? "Missing final encrypted chunk"+ : "Unexpected encrypted stream final tag",+ );+ expect(manager.fileDownloadProgressSnapshot().size).toBe(0);+ },+ );++ test("rejects a missing response body", async () => {+ await expect(download(null).bytes).rejects.toThrow(+ "Missing final encrypted chunk",+ );+ });++ test("rejects truncation inside a chunk", async () => {+ await expect(+ download(encrypted.encryptedData.slice(0, encryptedChunkSize + 100))+ .bytes,+ ).rejects.toThrow("Invalid encrypted chunk");+ });++ test("rejects ciphertext appended after a full-size final chunk", async () => {+ const complete = await encryptStreamBytes(+ original.slice(0, streamEncryptionChunkSize),+ key,+ );+ const appended = new Uint8Array(complete.encryptedData.length + 1);+ appended.set(complete.encryptedData);+ await expect(+ download(appended, complete.decryptionHeader).bytes,+ ).rejects.toThrow("Unexpected encrypted stream final tag");+ });++ test("rejects a modified encrypted chunk", async () => {+ const modified = encrypted.encryptedData.slice();+ modified[0] = modified[0]! ^ 1;+ await expect(download(modified).bytes).rejects.toThrow(+ "Invalid encrypted chunk",+ );+ });+});diff --git a/web/packages/base/crypto/index.ts b/web/packages/base/crypto/index.tsindex c29df722a9..1c2d94065b 100644--- a/web/packages/base/crypto/index.ts+++ b/web/packages/base/crypto/index.ts@@ -131,10 +131,13 @@ export const initChunkDecryption = (export const decryptStreamChunk = (data: Uint8Array,state: SodiumStateAddress,+ isFinalChunk?: boolean,): Promise<Uint8Array<ArrayBuffer>> =>inWorker()- ? libsodium.decryptStreamChunk(data, state)- : sharedWorker().then((w) => w.decryptStreamChunk(data, state));+ ? libsodium.decryptStreamChunk(data, state, isFinalChunk)+ : sharedWorker().then((w) =>+ w.decryptStreamChunk(data, state, isFinalChunk),+ );export const decryptMetadataJSON = (blob: EncryptedBlob,diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.tsindex 2ac8a5f0bc..63a19e8987 100644--- a/web/packages/base/crypto/libsodium.ts+++ b/web/packages/base/crypto/libsodium.ts@@ -1,6 +1,6 @@import { mergeUint8Arrays } from "ente-utils/array";import { ensureArrayBufferBacked } from "ente-utils/bytes";-import sodium from "libsodium-wrappers-sumo";+import sodium, { type MessageTag } from "libsodium-wrappers-sumo";import {streamEncryptionChunkSize,type BytesOrB64,@@ -282,15 +282,27 @@ export const initChunkDecryption = async (return { pullState, decryptionChunkSize };};+/** If supplied, isFinalChunk must match the authenticated final tag. */export const decryptStreamChunk = async (data: Uint8Array,pullState: SodiumStateAddress,+ isFinalChunk?: boolean,): Promise<Uint8Array<ArrayBuffer>> => {await sodium.ready;+ // The sodium typings omit the false returned on authentication failure.const pullResult = sodium.crypto_secretstream_xchacha20poly1305_pull(pullState,data,- );+ ) as MessageTag | false;+ if (!pullResult) throw new Error("Invalid encrypted chunk");+ if (+ isFinalChunk !== undefined &&+ (pullResult.tag ===+ sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL) !==+ isFinalChunk+ ) {+ throw new Error("Unexpected encrypted stream final tag");+ }return ensureArrayBufferBacked(pullResult.message);};diff --git a/web/packages/gallery/services/download-core.ts b/web/packages/gallery/services/download-core.tsindex a89a25c132..48e700861a 100644--- a/web/packages/gallery/services/download-core.ts+++ b/web/packages/gallery/services/download-core.ts@@ -310,7 +310,7 @@ class DownloadManagerCore {}const body = res.body;- if (!body) return null;+ if (!body) throw new Error("Missing final encrypted chunk");const reader = body.getReader();const { pullState, decryptionChunkSize } =await initChunkDecryption(file.file.decryptionHeader, file.key);@@ -353,11 +353,13 @@ class DownloadManagerCore {);}- // A network read can contain several encrypted chunks.- while (data.length >= decryptionChunkSize) {+ // Keep the last chunk until EOF, even if it is full-size,+ // so its authenticated final tag can be checked.+ while (data.length > decryptionChunkSize) {const decryptedData = await decryptStreamChunk(data.slice(0, decryptionChunkSize),pullState,+ false,);if (isCancelled()) return;controller.enqueue(decryptedData);@@ -366,16 +368,17 @@ class DownloadManagerCore {}if (done) {- // Only EOF proves that a short remainder is the final chunk.- if (data.length) {- const decryptedData =- await decryptStreamChunk(- data,- pullState,- );- if (isCancelled()) return;- controller.enqueue(decryptedData);- }+ if (!data.length)+ throw new Error(+ "Missing final encrypted chunk",+ );+ const decryptedData = await decryptStreamChunk(+ data,+ pullState,+ true,+ );+ if (isCancelled()) return;+ controller.enqueue(decryptedData);didEnqueue = true;controller.close();this.setFileDownloadProgress(