@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 77 lines 3.41 KB
Raw
1 import { ZipError } from "./zipError.ts";
2
3 export function checkZipDirectory(bytes: ArrayBuffer, entryLimit = 2048) {
4 // Reject oversized directories before JSZip allocates objects for their entries.
5 const view = new DataView(bytes);
6 let directoryEnd = -1;
7 let directorySize = 0;
8 for (
9 let offset = bytes.byteLength - 22;
10 offset >= Math.max(0, bytes.byteLength - 65557);
11 offset--
12 ) {
13 if (view.getUint32(offset, true) !== 0x06054b50) continue;
14 if (offset + 22 + view.getUint16(offset + 20, true) !== bytes.byteLength)
15 throw new ZipError({ code: "zip-unreadable" });
16 let count = view.getUint16(offset + 10, true);
17 directorySize = view.getUint32(offset + 12, true);
18 let directoryOffset = view.getUint32(offset + 16, true);
19 directoryEnd = offset;
20 if (
21 [4, 6, 8, 10].some((field) => view.getUint16(offset + field, true) === 0xffff) ||
22 directorySize === 0xffffffff ||
23 directoryOffset === 0xffffffff
24 ) {
25 const locator = offset - 20;
26 const record = locator - 56;
27 // JSZip uses the last locator, including signatures inside the archive comment.
28 for (let candidate = bytes.byteLength - 4; candidate > locator; candidate--)
29 if (view.getUint32(candidate, true) === 0x07064b50)
30 throw new ZipError({ code: "zip64-unsupported" });
31 if (
32 record < 0 ||
33 view.getUint32(locator, true) !== 0x07064b50 ||
34 view.getUint32(record, true) !== 0x06064b50 ||
35 view.getBigUint64(record + 4, true) !== 44n ||
36 view.getUint32(record + 16, true) !== 0 ||
37 view.getUint32(record + 20, true) !== 0 ||
38 view.getUint32(locator + 4, true) !== 0 ||
39 view.getUint32(locator + 16, true) !== 1 ||
40 view.getBigUint64(locator + 8, true) !== BigInt(record)
41 )
42 throw new ZipError({ code: "zip64-unsupported" });
43 const records = view.getBigUint64(record + 32, true);
44 const size = view.getBigUint64(record + 40, true);
45 const start = view.getBigUint64(record + 48, true);
46 if (
47 records > BigInt(entryLimit) ||
48 size > BigInt(record) ||
49 start > BigInt(record) ||
50 view.getBigUint64(locator + 8, true) !== start + size ||
51 view.getBigUint64(record + 24, true) !== records
52 )
53 throw new ZipError({ code: "zip64-damaged" });
54 count = Number(records);
55 directorySize = Number(size);
56 directoryOffset = Number(start);
57 directoryEnd = record;
58 }
59 if (count > entryLimit) throw new ZipError({ code: "archive-too-many-entries" });
60 if (directoryOffset > directoryEnd - directorySize)
61 throw new ZipError({ code: "zip-directory-damaged" });
62 break;
63 }
64 if (directoryEnd < 0 || directorySize > directoryEnd)
65 throw new ZipError({ code: "zip-unreadable" });
66 let records = 0;
67 for (let offset = directoryEnd - directorySize; offset < directoryEnd;) {
68 if (offset + 46 > directoryEnd || view.getUint32(offset, true) !== 0x02014b50)
69 throw new ZipError({ code: "zip-directory-damaged" });
70 if (++records > entryLimit) throw new ZipError({ code: "archive-too-many-entries" });
71 const nameLength = view.getUint16(offset + 28, true);
72 if (nameLength > 1024) throw new ZipError({ code: "archive-path-too-long" });
73 offset +=
74 46 + nameLength + view.getUint16(offset + 30, true) + view.getUint16(offset + 32, true);
75 if (offset > directoryEnd) throw new ZipError({ code: "zip-directory-damaged" });
76 }
77 }