Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

fix(transfer): bound what an imported file can unpack

davbioc3612ff8bc39
diff --git a/src/core/transfer/__tests__/parse.test.ts b/src/core/transfer/__tests__/parse.test.ts
index 1de967c..4e6acbf 100644
--- a/src/core/transfer/__tests__/parse.test.ts
+++ b/src/core/transfer/__tests__/parse.test.ts
@@ -272,3 +272,12 @@ describe('readBundle', () => {
expect((await caught(bundleFile({ [MANIFEST_PATH]: strToU8('{ nope') }))).kind).toBe('not-a-bundle');
});
});
+
+describe('readBundle against a crafted archive', () => {
+ it('refuses an archive with implausibly many entries', async () => {
+ const files: Record<string, Uint8Array> = { [MANIFEST_PATH]: strToU8(JSON.stringify(manifest())) };
+ for (let i = 0; i < 5_001; i++) files[`screenshots/${i}.webp`] = new Uint8Array(1);
+
+ expect((await caught(bundleFile(files))).kind).toBe('unreadable');
+ });
+});
diff --git a/src/core/transfer/parse.ts b/src/core/transfer/parse.ts
index 554e382..bccc86a 100644
--- a/src/core/transfer/parse.ts
+++ b/src/core/transfer/parse.ts
@@ -10,15 +10,21 @@ const MAX_UNPACKED_BYTES = 200 * 1024 * 1024;
const MAX_FILE_BYTES = 100 * 1024 * 1024;
+const MAX_ENTRIES = 5_000;
+
export async function readBundle(file: Blob): Promise<ParsedBundle> {
if (file.size > MAX_FILE_BYTES) throw new BundleError('unreadable', 'bundle is implausibly large');
let entries: Record<string, Uint8Array>;
try {
let unpacked = 0;
+ const seen = new Set<string>();
entries = unzipSync(new Uint8Array(await file.arrayBuffer()), {
filter: (entry) => {
- unpacked += entry.originalSize;
+ if (seen.has(entry.name)) throw new Error('bundle repeats an entry');
+ seen.add(entry.name);
+ if (seen.size > MAX_ENTRIES) throw new Error('bundle has implausibly many entries');
+ unpacked += Math.max(entry.originalSize, entry.size);
if (unpacked > MAX_UNPACKED_BYTES) throw new Error('bundle is implausibly large');
return true;
},