// Hostile PDF bytes through the read commands (inspect, extract-text, // merge): an xref /Prev chain past MAX_XREF_CHAIN, a /Prev cycle, nesting // past MAX_PARSE_DEPTH, an inflate bomb under --max-inflate-size and forty // random mutations of a valid file all end in a CliError with a stable // code — never a RangeError, never a hang. The engine's own limits are // exercised here through the CLI, the untrusted-input boundary its fuzz // tests never cross. import { describe, it, expect, afterAll, afterEach, beforeAll, vi } from 'vitest'; import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { inspect } from '../../src/commands/inspect.js'; import { extractTextCmd as extractText } from '../../src/commands/extract-text.js'; import { merge } from '../../src/commands/merge.js'; import { parseArgs } from '../../src/utils/args.js'; import { ErrorCode } from '../../src/utils/error.js'; import { MAX_XREF_CHAIN, MAX_PARSE_DEPTH, setMaxInflateOutputSize, DEFAULT_MAX_INFLATE_OUTPUT } from '../../src/core-bridge/index.js'; import { captured, tmpRoot } from '../helpers/cli-harness.js'; import { forEachCaseAsync, onlyCliErrorAsync, assertPrototypeClean, mutatePdf, minimalPdf, buildChainedXrefPdf, buildCyclicXrefPdf, buildDeepNestingPdf, buildFlatePdf, textBombPayload, buildObjectsPdf, } from '../helpers/fuzz.js'; const dir = mkdtempSync(join(tmpRoot(), 'pdfcli-fuzz-pdf-')); afterAll(() => { assertPrototypeClean(); rmSync(dir, { recursive: true, force: true }); }); afterEach(() => { vi.restoreAllMocks(); setMaxInflateOutputSize(DEFAULT_MAX_INFLATE_OUTPUT); }); const READ_CODES = [ErrorCode.PARSE, ErrorCode.PASSWORD, ErrorCode.UNSUPPORTED, ErrorCode.INPUT, ErrorCode.USAGE]; function file(name: string, bytes: Uint8Array): string { const p = join(dir, name); writeFileSync(p, bytes); return p; } const inspectFile = (p: string): Promise => captured(() => inspect(parseArgs(['--input', p, '--format', 'json']))); const extractFile = (p: string): Promise => captured(() => extractText(parseArgs(['--input', p]))); const mergeFiles = (a: string, b: string, out: string): Promise => captured(() => merge(parseArgs([a, b, '--output', out]))); describe('fuzz: PDF bytes through the read commands', { timeout: 60_000 }, () => { let valid = ''; beforeAll(() => { valid = file('valid.pdf', minimalPdf()); }); it('the engine exposes its two parser limits', () => { expect(Number.isInteger(MAX_XREF_CHAIN) && MAX_XREF_CHAIN > 0).toBe(true); expect(Number.isInteger(MAX_PARSE_DEPTH) && MAX_PARSE_DEPTH > 0).toBe(true); }); it('a /Prev chain just below the limit is read; past the limit inspect answers E_PARSE', async () => { const below = file('chain-ok.pdf', buildChainedXrefPdf(Math.min(10, MAX_XREF_CHAIN - 1))); expect((await onlyCliErrorAsync(() => inspectFile(below), READ_CODES)).outcome).toBe('ok'); const past = file('chain-bad.pdf', buildChainedXrefPdf(MAX_XREF_CHAIN + 5)); const r = await onlyCliErrorAsync(() => inspectFile(past), [ErrorCode.PARSE]); expect(r.outcome).toBe('error'); if (r.outcome === 'error') expect(r.error.message).toMatch(/Prev chain|maximum depth|depth/i); }); it('a self-referential /Prev is E_PARSE (cycle), not a hang', async () => { const p = file('cycle.pdf', buildCyclicXrefPdf()); const r = await onlyCliErrorAsync(() => inspectFile(p), [ErrorCode.PARSE]); expect(r.outcome).toBe('error'); if (r.outcome === 'error') expect(r.error.message).toMatch(/cycle/i); }); it.each([ ['arrays', MAX_PARSE_DEPTH + 50, 'array'], ['dictionaries', MAX_PARSE_DEPTH + 10, 'dict'], ['10 000 arrays', 10_000, 'array'], ] as const)('a catalog nesting %s past MAX_PARSE_DEPTH is E_PARSE, never a stack overflow', async (_label, depth, kind) => { const p = file(`deep-${kind}-${depth}.pdf`, buildDeepNestingPdf(depth, kind)); const r = await onlyCliErrorAsync(() => inspectFile(p), [ErrorCode.PARSE]); expect(r.outcome).toBe('error'); if (r.outcome === 'error') expect(r.error.message).toMatch(/depth|nested|recursion/i); }); it('nesting below the cap is read normally', async () => { const p = file('deep-ok.pdf', buildDeepNestingPdf(Math.min(100, MAX_PARSE_DEPTH - 1), 'array')); expect((await onlyCliErrorAsync(() => inspectFile(p), READ_CODES)).outcome).toBe('ok'); }); it('an inflate bomb under --max-inflate-size is never materialised: E_INPUT by default, an empty page under --on-stream-error skip; above the cap its text comes back', async () => { // ~1 MiB of text operators deflating to ~3 KB. Under the cap the // engine's inflate throws PdfStreamLimitError (pdfnative 1.9.0, code // PDF_STREAM_LIMIT); extractText rethrows it by default — the 1.8.0 // engine dropped the stream silently (audit V-01) — and the CLI // answers E_INPUT, naming the two remedies. --on-stream-error skip is // the 1.8.0 lenience. What the CLI guarantees either way is the bound // itself: nothing past the cap is ever allocated. const p = file('bomb.pdf', buildFlatePdf(textBombPayload(30_000))); setMaxInflateOutputSize(1024); const capped = await onlyCliErrorAsync(() => captured(() => extractText(parseArgs(['--input', p]))), READ_CODES); expect(capped.outcome).toBe('error'); if (capped.outcome === 'error') { expect(capped.error.code).toBe(ErrorCode.INPUT); expect(capped.error.message).toMatch(/--on-stream-error skip/); } const skipped = await onlyCliErrorAsync(() => captured(() => extractText(parseArgs(['--input', p, '--on-stream-error', 'skip']))), READ_CODES); expect(skipped.outcome).toBe('ok'); if (skipped.outcome === 'ok') expect(skipped.value.stdout).not.toContain('bomb'); setMaxInflateOutputSize(2 * 1024 * 1024); const open = await onlyCliErrorAsync(() => captured(() => extractText(parseArgs(['--input', p]))), READ_CODES); expect(open.outcome).toBe('ok'); if (open.outcome === 'ok') expect(open.value.stdout).toContain('bomb'); // inspect never touches content streams: identical under both caps. setMaxInflateOutputSize(1024); expect((await onlyCliErrorAsync(() => inspectFile(p), READ_CODES)).outcome).toBe('ok'); }); // pdfnative 1.9.0 (CWE-835): a stray ), { or } made the tokenizer return an // empty keyword without advancing, so every token loop — the ToUnicode CMap // reader among them — spun forever on a one-byte corruption. The suite's // 60 s timeout is the hang detector; the assertion is that the command answers. it('a stray ), { or } in a content stream and in a ToUnicode CMap ends in a value or a stable code, never a hang (CWE-835)', async () => { const stream = (content: string): string => `<< /Length ${content.length} >>\nstream\n${content}\nendstream`; for (const [name, stray] of [['paren', ')'], ['brace-open', '{'], ['brace-close', '}']] as const) { const p = file(`stray-${name}.pdf`, buildObjectsPdf([ '<< /Type /Catalog /Pages 2 0 R >>', '<< /Type /Pages /Kids [3 0 R] /Count 1 >>', '<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >>', stream(`BT /F1 12 Tf 10 100 Td (ok) Tj ${stray} ET`), '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /ToUnicode 6 0 R >>', stream(`/CIDInit /ProcSet findresource begin ${stray} 1 begincodespacerange <00> endcodespacerange 1 beginbfchar <6F> <006F> endbfchar endcmap`), ])); const text = await onlyCliErrorAsync(() => captured(() => extractText(parseArgs(['--input', p]))), READ_CODES); expect(['ok', 'error']).toContain(text.outcome); const meta = await onlyCliErrorAsync(() => inspectFile(p), READ_CODES); expect(['ok', 'error']).toContain(meta.outcome); } }); it('40 random mutations of a valid PDF end in a value or a stable code for inspect, extract-text and merge', async () => { await forEachCaseAsync('mutatePdf', 40, async (rng, i) => { const mutated = file(`m${i}.pdf`, mutatePdf(minimalPdf(), rng, 1 + rng.int(12))); await onlyCliErrorAsync(() => inspectFile(mutated), READ_CODES); await onlyCliErrorAsync(() => extractFile(mutated), READ_CODES); await onlyCliErrorAsync(() => mergeFiles(valid, mutated, join(dir, `merged${i}.pdf`)), READ_CODES); }); }); });