Check downloaded originals against their recorded content hash (closes #68)
check / check (push) Successful in 58s
check / check (push) Successful in 58s
downloadFile, shared by quak get, the content cache and backup, hashes the decrypted bytes (unkeyed BLAKE2b-512, standard base64) and stores nothing on a mismatch, failing with an error naming the file ID. A live photo ZIP is unpacked as it streams with fflate's Unzip, in small slices so memory stays bounded however far an entry expands, and its image and video hashed separately as <imageHash>:<videoHash>. decryptFile reads the older imageHash/videoHash fields for live photos. A file with no recorded hash is stored unchecked. Model: opus-5-5
This commit was merged in pull request #98.
This commit is contained in:
@@ -61,6 +61,7 @@ import { dirname, join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { createHash } from "node:crypto";
|
||||
import sodium from "libsodium-wrappers-sumo";
|
||||
import { zipSync } from "fflate";
|
||||
import {
|
||||
beforeAll,
|
||||
beforeEach,
|
||||
@@ -188,7 +189,31 @@ vi.mock("node:fs/promises", async (importOriginal) => {
|
||||
};
|
||||
});
|
||||
|
||||
/**
|
||||
* `chunkHashUpdate` is wrapped to record the length of every piece hashed, so
|
||||
* a test can show that a live photo entry reaches the hash in pieces far
|
||||
* smaller than the entry, rather than decompressed whole first.
|
||||
*/
|
||||
const hashHook = vi.hoisted(() => ({
|
||||
lengths: [] as number[],
|
||||
}));
|
||||
|
||||
vi.mock("../../src/crypto/index.js", async (importOriginal) => {
|
||||
const actual =
|
||||
await importOriginal<typeof import("../../src/crypto/index.js")>();
|
||||
return {
|
||||
...actual,
|
||||
chunkHashUpdate: (
|
||||
...args: Parameters<typeof actual.chunkHashUpdate>
|
||||
): void => {
|
||||
hashHook.lengths.push(args[1].length);
|
||||
actual.chunkHashUpdate(...args);
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
hashHook.lengths.length = 0;
|
||||
renameHook.calls.length = 0;
|
||||
renameHook.failWith = null;
|
||||
durabilityHook.events.length = 0;
|
||||
@@ -1613,3 +1638,113 @@ describe.each(entryPoints)("$name progress", ({ name, download }) => {
|
||||
expectSameBytes(readFileSync(outPath), plaintext);
|
||||
});
|
||||
});
|
||||
|
||||
describe("downloadFile content hash", () => {
|
||||
// Node's own BLAKE2b-512 is the reference, so these tests do not depend
|
||||
// on the code under test to compute what they expect.
|
||||
const blake2b = (bytes: Uint8Array): string =>
|
||||
createHash("blake2b512").update(bytes).digest("base64");
|
||||
|
||||
// Serve `plaintext` encrypted as file 999 with the given metadata. Four
|
||||
// responses are scripted so a retried mismatch would show in `requests`.
|
||||
const setup = (plaintext: Uint8Array, metadata: Partial<FileMetadata>) => {
|
||||
const key = sodium.crypto_secretstream_xchacha20poly1305_keygen();
|
||||
const { header, ciphertext } = encryptFileBody(plaintext, key);
|
||||
const file = buildMockEnteFile(key, header, header);
|
||||
file.metadata = { ...file.metadata, ...metadata };
|
||||
const body = { kind: "body", bytes: ciphertext } as const;
|
||||
const { fetch, requests } = scriptedCdnFetch(body, body, body, body);
|
||||
const api = new ApiClient({ fetch, retry: { ...noWait, attempts: 4 } });
|
||||
const dir = mkdtempSync(join(testDir, "hash-"));
|
||||
const outPath = join(dir, "f.bin");
|
||||
return {
|
||||
run: () => downloadFile(api, file, outPath),
|
||||
dir,
|
||||
outPath,
|
||||
requests,
|
||||
};
|
||||
};
|
||||
|
||||
const livePhotoZip = zipSync({
|
||||
"image.heic": patternBytes(500, 81),
|
||||
"video.mov": patternBytes(900, 82),
|
||||
});
|
||||
const livePhotoHash = `${blake2b(patternBytes(500, 81))}:${blake2b(patternBytes(900, 82))}`;
|
||||
|
||||
it("stores a file whose hash matches", async () => {
|
||||
const plaintext = patternBytes(700, 80);
|
||||
const t = setup(plaintext, { hash: blake2b(plaintext) });
|
||||
|
||||
await t.run();
|
||||
|
||||
expectSameBytes(readFileSync(t.outPath), plaintext);
|
||||
});
|
||||
|
||||
it("rejects a mismatch, stores nothing, names the file and does not retry", async () => {
|
||||
const t = setup(patternBytes(700, 80), {
|
||||
hash: blake2b(patternBytes(700, 79)),
|
||||
});
|
||||
|
||||
await expect(t.run()).rejects.toThrow(
|
||||
/file 999: content hash .* does not match/,
|
||||
);
|
||||
|
||||
expect(readdirSync(t.dir)).toEqual([]);
|
||||
expect(t.requests()).toBe(1);
|
||||
});
|
||||
|
||||
it("stores a file with no recorded hash unchecked", async () => {
|
||||
const plaintext = patternBytes(700, 80);
|
||||
const t = setup(plaintext, { hash: undefined });
|
||||
|
||||
await t.run();
|
||||
|
||||
expectSameBytes(readFileSync(t.outPath), plaintext);
|
||||
});
|
||||
|
||||
it("stores a live photo whose image and video hashes match", async () => {
|
||||
const t = setup(livePhotoZip, {
|
||||
fileType: "livePhoto",
|
||||
hash: livePhotoHash,
|
||||
});
|
||||
|
||||
await t.run();
|
||||
|
||||
expectSameBytes(readFileSync(t.outPath), livePhotoZip);
|
||||
});
|
||||
|
||||
it("hashes a large live photo entry as it decompresses, never whole", async () => {
|
||||
// 64 MiB of zeros deflates to a few kilobytes, the shape of a ZIP
|
||||
// that would exhaust memory if expanded whole.
|
||||
const image = new Uint8Array(64 * 1024 * 1024);
|
||||
const video = patternBytes(900, 83);
|
||||
const zip = zipSync({ "image.heic": image, "video.mov": video });
|
||||
const t = setup(zip, {
|
||||
fileType: "livePhoto",
|
||||
hash: `${blake2b(image)}:${blake2b(video)}`,
|
||||
});
|
||||
|
||||
await t.run();
|
||||
|
||||
expectSameBytes(readFileSync(t.outPath), zip);
|
||||
const hashed = hashHook.lengths.reduce((a, b) => a + b, 0);
|
||||
expect(hashed).toBe(image.length + video.length);
|
||||
expect(Math.max(...hashHook.lengths)).toBeLessThanOrEqual(
|
||||
2 * STREAM_CHUNK_SIZE,
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects a live photo whose hash does not match", async () => {
|
||||
// The whole ZIP's hash is not the recorded one: each part is hashed.
|
||||
const t = setup(livePhotoZip, {
|
||||
fileType: "livePhoto",
|
||||
hash: blake2b(livePhotoZip),
|
||||
});
|
||||
|
||||
await expect(t.run()).rejects.toThrow(
|
||||
/file 999: content hash .* does not match/,
|
||||
);
|
||||
|
||||
expect(readdirSync(t.dir)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user