Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

add web limit for gzip

AmanRajSinghMouryae41b620da786
diff --git a/web/apps/memories/src/services/public-memory.ts b/web/apps/memories/src/services/public-memory.ts
index b44eb8776e..e7dd272d9c 100644
--- a/web/apps/memories/src/services/public-memory.ts
+++ b/web/apps/memories/src/services/public-memory.ts
@@ -7,11 +7,13 @@ import {
RemoteEnteFile,
type EnteFile,
} from "ente-media/file";
-import { gunzip } from "ente-new/photos/utils/gzip";
+import { gunzipWithLimit } from "ente-new/photos/utils/gzip";
import { z } from "zod";
export type { PublicMemoryCredentials } from "ente-base/public-memory";
+const maxMemoryShareMetadataBytes = 1024 * 1024; // 1 MiB
+
/**
* Information about a public memory share fetched from remote.
*/
@@ -166,12 +168,21 @@ const decryptMemoryShareMetadataJSON = async (
{ encryptedData: metadataCipher, nonce: metadataNonce },
shareKey,
);
+ // These bytes are decrypted but not decompressed: plain JSON for regular
+ // memories and gzip input for lanes. Bound them before parsing or
+ // decompression; gunzipWithLimit separately caps expanded lane metadata.
+ if (metadataBytes.byteLength > maxMemoryShareMetadataBytes) {
+ throw new Error("Memory share metadata exceeds the allowed size");
+ }
let metadataJson: string;
try {
metadataJson = new TextDecoder().decode(metadataBytes);
JSON.parse(metadataJson);
} catch {
- metadataJson = await gunzip(metadataBytes);
+ metadataJson = await gunzipWithLimit(
+ metadataBytes,
+ maxMemoryShareMetadataBytes,
+ );
}
return metadataJson;
};
diff --git a/web/apps/photos/tests/gzip.test.ts b/web/apps/photos/tests/gzip.test.ts
new file mode 100644
index 0000000000..a1d241d255
--- /dev/null
+++ b/web/apps/photos/tests/gzip.test.ts
@@ -0,0 +1,21 @@
+import { gunzipWithLimit, gzip } from "ente-new/photos/utils/gzip";
+import { describe, expect, test } from "vitest";
+
+describe("gunzipWithLimit", () => {
+ test("decompresses data within the output limit", async () => {
+ const value = "memory metadata";
+ const compressed = await gzip(value);
+
+ await expect(gunzipWithLimit(compressed, value.length)).resolves.toBe(
+ value,
+ );
+ });
+
+ test("rejects data that expands beyond the output limit", async () => {
+ const compressed = await gzip("a".repeat(64 * 1024));
+
+ await expect(gunzipWithLimit(compressed, 1024)).rejects.toThrow(
+ "Decompressed data exceeds the allowed size",
+ );
+ });
+});
diff --git a/web/packages/new/photos/utils/gzip.ts b/web/packages/new/photos/utils/gzip.ts
index ece2c0d8ef..c9446ce4f2 100644
--- a/web/packages/new/photos/utils/gzip.ts
+++ b/web/packages/new/photos/utils/gzip.ts
@@ -30,3 +30,43 @@ export const gunzip = async (data: Uint8Array<ArrayBuffer>) => {
.pipeThrough(new DecompressionStream("gzip"));
return new Response(decompressedStream).text();
};
+
+/**
+ * Decompress the given "gzip" compressed {@link data}, rejecting it if the
+ * decompressed output exceeds {@link maxOutputBytes}.
+ */
+export const gunzipWithLimit = async (
+ data: Uint8Array<ArrayBuffer>,
+ maxOutputBytes: number,
+) => {
+ const reader = new Blob([data])
+ .stream()
+ .pipeThrough(new DecompressionStream("gzip"))
+ .getReader();
+ const chunks: Uint8Array<ArrayBuffer>[] = [];
+ let outputSize = 0;
+
+ try {
+ while (true) {
+ const { done, value } = await reader.read();
+ if (done) break;
+
+ outputSize += value.byteLength;
+ if (outputSize > maxOutputBytes) {
+ await reader.cancel();
+ throw new Error("Decompressed data exceeds the allowed size");
+ }
+ chunks.push(value);
+ }
+ } finally {
+ reader.releaseLock();
+ }
+
+ const output = new Uint8Array(outputSize);
+ let offset = 0;
+ for (const chunk of chunks) {
+ output.set(chunk, offset);
+ offset += chunk.byteLength;
+ }
+ return new TextDecoder("utf-8", { fatal: true }).decode(output);
+};