Something went wrong. Try again.
A free, open-source, self-hosted place to write a novel.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286/** * The zip reader behind every import. Unlike `unzipSync`, it refuses a forged * size or checksum instead of returning truncated data, and a caller expands * only the entries its format names. */import { Inflate, strFromU8 } from 'fflate';import { MAX_MANUSCRIPT_BYTES } from '#lib/import-limits.ts';import { ImportError } from './import-error.ts';
const EOCD_SIGNATURE = 0x06054b50;const CENTRAL_SIGNATURE = 0x02014b50;const LOCAL_SIGNATURE = 0x04034b50;const EOCD_MAX_COMMENT = 0xffff;const CHUNK = 16 * 1024;const MAX_LABEL = `${MAX_MANUSCRIPT_BYTES / 1024 / 1024} MB`;
function damaged(message: string): never { throw new ImportError(message);}
/** * The eight steps per byte this replaces cost about 23 ms per mebibyte, which is * three seconds of a stalled event loop for the 128 MiB manuscript the import * limit allows. A table makes it one lookup per byte. */const CRC_TABLE = (() => { const table = new Uint32Array(256);
for (let index = 0; index < 256; index += 1) { let value = index;
for (let bit = 0; bit < 8; bit += 1) { value = value & 1 ? 0xedb88320 ^ (value >>> 1) : value >>> 1; }
table[index] = value >>> 0; }
return table;})();
function crc32(bytes: Uint8Array): number { let crc = 0xffffffff;
for (let index = 0; index < bytes.length; index += 1) { crc = CRC_TABLE[(crc ^ bytes[index]) & 0xff] ^ (crc >>> 8); }
return (crc ^ 0xffffffff) >>> 0;}
export type ZipEntry = { compression: number; crc: number; compressedSize: number; uncompressedSize: number; localOffset: number; flags?: number; attributes?: number;};
export function listZipEntries(bytes: Uint8Array): Map<string, ZipEntry> { const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); const floor = Math.max(0, bytes.length - EOCD_MAX_COMMENT - 22); let eocd = -1;
for (let at = bytes.length - 22; at >= floor; at -= 1) { if ( view.getUint32(at, true) === EOCD_SIGNATURE && at + 22 + view.getUint16(at + 20, true) === bytes.length ) { eocd = at; break; } }
if (eocd === -1) damaged('That file is not a readable zip. It can be damaged.'); if (bytes.length < eocd + 22) damaged('That file is not a readable zip. It can be damaged.'); if (view.getUint16(eocd + 4, true) !== 0 || view.getUint16(eocd + 6, true) !== 0) { damaged('That zip spans more than one file, which this instance cannot read.'); }
const count = view.getUint16(eocd + 10, true); const size = view.getUint32(eocd + 12, true); const start = view.getUint32(eocd + 16, true); if (start + size > eocd) damaged('That file is not a readable zip. It can be damaged.');
const entries = new Map<string, ZipEntry>(); let at = start;
for (let index = 0; index < count; index += 1) { if (at + 46 > start + size) damaged('That file is not a readable zip. It can be damaged.'); if (view.getUint32(at, true) !== CENTRAL_SIGNATURE) { damaged('That file is not a readable zip. It can be damaged.'); }
const nameLength = view.getUint16(at + 28, true); const extraLength = view.getUint16(at + 30, true); const commentLength = view.getUint16(at + 32, true);
if (at + 46 + nameLength + extraLength + commentLength > start + size) { damaged('That file is not a readable zip. It can be damaged.'); }
const entryName = strFromU8(bytes.subarray(at + 46, at + 46 + nameLength)); if (entries.has(entryName)) { if (entryName === 'manuscript.json') { damaged('That zip contains more than one manuscript.json. Import an unchanged export.'); } damaged(`That zip contains more than one ${entryName}.`); }
const entry: ZipEntry = { compression: view.getUint16(at + 10, true), crc: view.getUint32(at + 16, true), compressedSize: view.getUint32(at + 20, true), uncompressedSize: view.getUint32(at + 24, true), localOffset: view.getUint32(at + 42, true), flags: view.getUint16(at + 8, true), attributes: view.getUint32(at + 38, true) };
if ( entry.compressedSize === 0xffffffff || entry.uncompressedSize === 0xffffffff || entry.localOffset === 0xffffffff ) { damaged('That zip is larger than this instance can read.'); } entries.set(entryName, entry);
at += 46 + nameLength + extraLength + commentLength; }
return entries;}
/** * `directory` lets a caller read many entries of one archive from a single index. * Building that index costs a walk of every central-directory record, so an import * that looks up a picture per note would otherwise pay for the whole archive per * lookup. */export function findOptionalEntry( bytes: Uint8Array, entryName: string, maxBytes = MAX_MANUSCRIPT_BYTES, directory?: Map<string, ZipEntry>): ZipEntry | null { const entry = (directory ?? listZipEntries(bytes)).get(entryName) ?? null; if (entry && entry.uncompressedSize > maxBytes) { if (entryName === 'manuscript.json') { damaged( `The manuscript inside that zip exceeds ${MAX_LABEL}. Keep the archive and restore a database backup instead.` ); } damaged( `The entry ${entryName} inside that zip exceeds ${Math.round(maxBytes / 1024 / 1024)} MB.` ); } return entry;}
export function findEntry( bytes: Uint8Array, entryName = 'manuscript.json', maxBytes = MAX_MANUSCRIPT_BYTES, directory?: Map<string, ZipEntry>): ZipEntry { const entry = findOptionalEntry(bytes, entryName, maxBytes, directory); if (!entry) { if (entryName === 'manuscript.json') { damaged('That zip has no manuscript.json in it. Choose a Fablewright export.'); } damaged(`That zip has no ${entryName} in it.`); } return entry;}
/** The stored bytes of an entry, with the declared size and CRC checked. */export function readEntry(bytes: Uint8Array, entry: ZipEntry): Uint8Array { if ((entry.flags ?? 0) & 1) damaged('Encrypted zip entries cannot be imported. Make an unencrypted copy.'); const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
if (entry.localOffset + 30 > bytes.length) { damaged('That file is not a readable zip. It can be damaged.'); } if (view.getUint32(entry.localOffset, true) !== LOCAL_SIGNATURE) { damaged('That file is not a readable zip. It can be damaged.'); }
const nameLength = view.getUint16(entry.localOffset + 26, true); const extraLength = view.getUint16(entry.localOffset + 28, true); const dataStart = entry.localOffset + 30 + nameLength + extraLength; const dataEnd = dataStart + entry.compressedSize;
if (dataEnd > bytes.length) damaged('That file is not a readable zip. It can be damaged.');
const data = bytes.subarray(dataStart, dataEnd);
if (entry.compression === 0) { if (entry.compressedSize !== entry.uncompressedSize) { damaged('That zip is damaged: the stored manuscript does not match its stated size.'); } if (crc32(data) !== entry.crc) { damaged('That zip is damaged: the stored manuscript fails its checksum.'); } return data; }
if (entry.compression !== 8) { damaged('That zip uses a compression method this instance cannot read.'); }
/* * The output buffer is exactly the size the header claims. Anything longer is * refused rather than dropped, and the check runs on every chunk, so a forged * small size cannot force an unbounded expansion. */ const output = new Uint8Array(entry.uncompressedSize); let written = 0; let overflow = false;
const inflate = new Inflate((chunk) => { if (written + chunk.length > output.length) { overflow = true; return; } output.set(chunk, written); written += chunk.length; });
try { for (let at = 0; at < data.length; at += CHUNK) { inflate.push(data.subarray(at, Math.min(at + CHUNK, data.length)), at + CHUNK >= data.length); if (overflow) break; } } catch { damaged('That zip is damaged: the manuscript could not be expanded.'); }
if (overflow || written !== entry.uncompressedSize) { damaged('That zip is damaged: the manuscript is larger than its stated size.'); } if (crc32(output) !== entry.crc) { damaged('That zip is damaged: the manuscript fails its checksum.'); }
return output;}
export function readZipEntry( bytes: Uint8Array, entryName: string, maxBytes = MAX_MANUSCRIPT_BYTES, directory?: Map<string, ZipEntry>): Uint8Array { try { return readEntry(bytes, findEntry(bytes, entryName, maxBytes, directory)); } catch (cause) { if (cause instanceof ImportError) throw cause; throw new ImportError('That file is not a readable zip. It can be damaged.'); }}
export function readOptionalZipEntry( bytes: Uint8Array, entryName: string, maxBytes = MAX_MANUSCRIPT_BYTES, directory?: Map<string, ZipEntry>): Uint8Array | null { try { const entry = findOptionalEntry(bytes, entryName, maxBytes, directory); if (!entry) return null; return readEntry(bytes, entry); } catch (cause) { if (cause instanceof ImportError) throw cause; throw new ImportError('That file is not a readable zip. It can be damaged.'); }}
export function readManuscriptZip(bytes: Uint8Array, directory?: Map<string, ZipEntry>): string { return strFromU8(readZipEntry(bytes, 'manuscript.json', MAX_MANUSCRIPT_BYTES, directory));}