Study/server/documents/zip.ts
Elijah 3e74ecd843 Start Study Desk V2: remove V1 sources, add backend foundation
V1 remains intact on main and in history. This branch replaces it with a new
application: node:sqlite storage with migrations, scrypt auth and sessions,
content/SRS/attempt/stats services, document extraction and chunking, a
provider-neutral AI layer (OpenAI, Anthropic, Gemini, OpenAI-compatible,
Ollama), the generation pipeline and the tool-using assistant.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 08:16:41 -07:00

213 lines
7.6 KiB
TypeScript

/**
* Minimal, defensive ZIP reader for OOXML packages (DOCX / PPTX).
*
* Reads the central directory itself and inflates entries through fflate's
* streaming inflater in small input slices, so both memory and CPU are bounded
* by the limits below even when the archive lies about its sizes (zip bombs).
*/
import { Inflate } from 'fflate';
export interface ZipEntry {
name: string;
method: number;
compressedSize: number;
size: number;
offset: number;
encrypted: boolean;
}
export interface ZipLimits {
/** Largest uncompressed size accepted for a single entry. */
maxEntryBytes: number;
/** Largest total of uncompressed bytes read through one reader. */
maxTotalBytes: number;
}
export class ZipError extends Error {
code: 'corrupt' | 'too_large';
constructor(code: ZipError['code'], message: string) {
super(message);
this.name = 'ZipError';
this.code = code;
}
}
const EOCD_SIG = 0x06054b50;
const ZIP64_LOCATOR_SIG = 0x07064b50;
const ZIP64_EOCD_SIG = 0x06064b50;
const CENTRAL_SIG = 0x02014b50;
const LOCAL_SIG = 0x04034b50;
const MAX_ENTRIES = 20_000;
const INFLATE_STEP = 64 * 1024;
const nameDecoder = new TextDecoder('utf-8');
function u16(b: Uint8Array, o: number): number {
return b[o] | (b[o + 1] << 8);
}
function u32(b: Uint8Array, o: number): number {
return (b[o] | (b[o + 1] << 8) | (b[o + 2] << 16) | (b[o + 3] << 24)) >>> 0;
}
function u64(b: Uint8Array, o: number): number {
return u32(b, o) + u32(b, o + 4) * 0x1_0000_0000;
}
/** Parses the central directory. Throws ZipError('corrupt') when it cannot be read. */
export function listZip(bytes: Uint8Array): ZipEntry[] {
const len = bytes.length;
if (len < 22) throw new ZipError('corrupt', 'Not a zip archive');
let eocd = -1;
const stop = Math.max(0, len - 22 - 0xffff);
for (let i = len - 22; i >= stop; i--) {
if (bytes[i] === 0x50 && u32(bytes, i) === EOCD_SIG) {
eocd = i;
break;
}
}
if (eocd < 0) throw new ZipError('corrupt', 'Zip directory not found');
let count = u16(bytes, eocd + 10);
let dirOffset = u32(bytes, eocd + 16);
if (count === 0xffff || dirOffset === 0xffffffff) {
const locator = eocd - 20;
if (locator < 0 || u32(bytes, locator) !== ZIP64_LOCATOR_SIG) throw new ZipError('corrupt', 'Bad zip64 locator');
const z = u64(bytes, locator + 8);
if (z + 56 > len || u32(bytes, z) !== ZIP64_EOCD_SIG) throw new ZipError('corrupt', 'Bad zip64 directory');
count = u64(bytes, z + 32);
dirOffset = u64(bytes, z + 48);
}
if (count > MAX_ENTRIES) throw new ZipError('too_large', 'Too many files in archive');
const entries: ZipEntry[] = [];
let p = dirOffset;
for (let i = 0; i < count; i++) {
if (p + 46 > len || u32(bytes, p) !== CENTRAL_SIG) throw new ZipError('corrupt', 'Bad zip directory entry');
const flags = u16(bytes, p + 8);
const method = u16(bytes, p + 10);
let compressedSize = u32(bytes, p + 20);
let size = u32(bytes, p + 24);
const nameLen = u16(bytes, p + 28);
const extraLen = u16(bytes, p + 30);
const commentLen = u16(bytes, p + 32);
let offset = u32(bytes, p + 42);
const nameStart = p + 46;
const extraStart = nameStart + nameLen;
const next = extraStart + extraLen + commentLen;
if (next > len) throw new ZipError('corrupt', 'Truncated zip directory');
if (size === 0xffffffff || compressedSize === 0xffffffff || offset === 0xffffffff) {
// zip64 extended information: only the saturated fields are present, in this order.
let e = extraStart;
const extraEnd = extraStart + extraLen;
while (e + 4 <= extraEnd) {
const id = u16(bytes, e);
const dataLen = u16(bytes, e + 2);
if (id === 1) {
let q = e + 4;
if (size === 0xffffffff && q + 8 <= extraEnd) (size = u64(bytes, q)), (q += 8);
if (compressedSize === 0xffffffff && q + 8 <= extraEnd) (compressedSize = u64(bytes, q)), (q += 8);
if (offset === 0xffffffff && q + 8 <= extraEnd) offset = u64(bytes, q);
break;
}
e += 4 + dataLen;
}
}
const name = nameDecoder.decode(bytes.subarray(nameStart, extraStart)).replace(/\\/g, '/');
entries.push({ name, method, compressedSize, size, offset, encrypted: (flags & 1) === 1 });
p = next;
}
return entries;
}
/** Random-access reader over a zip held in memory, with per-entry and total size limits. */
export class ZipReader {
private readonly bytes: Uint8Array;
private readonly limits: ZipLimits;
private readonly byName = new Map<string, ZipEntry>();
private totalRead = 0;
readonly entries: ZipEntry[];
constructor(bytes: Uint8Array, limits: ZipLimits) {
this.bytes = bytes;
this.limits = limits;
this.entries = listZip(bytes);
for (const entry of this.entries) if (!this.byName.has(entry.name)) this.byName.set(entry.name, entry);
}
has(name: string): boolean {
return this.byName.has(name);
}
names(): string[] {
return this.entries.map((entry) => entry.name);
}
/** Uncompressed bytes of an entry, or null when it does not exist. */
read(name: string): Uint8Array | null {
const entry = this.byName.get(name);
if (!entry) return null;
if (entry.encrypted) throw new ZipError('corrupt', 'Encrypted zip entry');
if (entry.size > this.limits.maxEntryBytes) throw new ZipError('too_large', `Entry too large: ${name}`);
if (this.totalRead + entry.size > this.limits.maxTotalBytes) throw new ZipError('too_large', 'Archive too large');
const b = this.bytes;
const o = entry.offset;
if (o + 30 > b.length || u32(b, o) !== LOCAL_SIG) throw new ZipError('corrupt', 'Bad zip file header');
const dataStart = o + 30 + u16(b, o + 26) + u16(b, o + 28);
const dataEnd = dataStart + entry.compressedSize;
if (dataEnd > b.length) throw new ZipError('corrupt', 'Truncated zip entry');
const compressed = b.subarray(dataStart, dataEnd);
let out: Uint8Array;
if (entry.method === 0) {
if (compressed.length !== entry.size) throw new ZipError('corrupt', 'Zip entry size mismatch');
out = compressed;
} else if (entry.method === 8) {
out = this.inflate(compressed, entry.size);
} else {
throw new ZipError('corrupt', `Unsupported zip compression method ${entry.method}`);
}
this.totalRead += out.length;
return out;
}
readText(name: string): string | null {
const data = this.read(name);
if (!data) return null;
let text = new TextDecoder('utf-8').decode(data);
if (text.charCodeAt(0) === 0xfeff) text = text.slice(1);
return text;
}
private inflate(compressed: Uint8Array, expected: number): Uint8Array {
const out = new Uint8Array(expected);
let written = 0;
let overflow = false;
const inflater = new Inflate((chunk) => {
if (overflow) return;
if (written + chunk.length > expected) {
overflow = true;
return;
}
out.set(chunk, written);
written += chunk.length;
});
try {
if (compressed.length === 0) {
inflater.push(compressed, true);
} else {
for (let p = 0; p < compressed.length && !overflow; p += INFLATE_STEP) {
const end = Math.min(p + INFLATE_STEP, compressed.length);
inflater.push(compressed.subarray(p, end), end === compressed.length);
}
}
} catch {
throw new ZipError('corrupt', 'Zip entry could not be decompressed');
}
// More data than the directory declared is the signature of a zip bomb.
if (overflow) throw new ZipError('too_large', 'Zip entry larger than declared');
if (written !== expected) throw new ZipError('corrupt', 'Zip entry size mismatch');
return out;
}
}