From 0ca9b48b7c7fd74242b235f7bbbc175919d8dd9c Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Mon, 16 Nov 2026 11:15:45 +0000 Subject: [PATCH 1/3] feat(pdf-codec): bound a document's stream decoding with maxDecodedBytes readPdf, renderPdfPage and openPdfDocument take maxDecodedBytes, one budget for every decode the document reaches: cross-reference and object streams (recovery included), content streams, form XObjects, font programs, ToUnicode and CIDToGIDMap streams, images, inline images, soft masks, Indexed lookups, metadata and embedded files. Every reader now decodes through the resolver, so no site can decode outside it. The first decode that would pass the budget throws PdfDecodeBudgetError, a PdfParseError carrying the budget, the bytes already spent, the stream's object number and its filters. Flate inflates in slices sized to what the remainder can absorb, LZW, RunLength and ASCII85 stop at the first byte past it, and the whole-image decoders (CCITTFax, JBIG2, JPEG 2000, a rasterised JPEG) are charged their declared output before they decode. decodeStream takes the budget directly. Without the option nothing is charged and every decoder runs its existing code. --- packages/pdf-codec/README.md | 4 +- packages/pdf-codec/src/attachments.ts | 3 +- packages/pdf-codec/src/bytes/flate.ts | 125 ++++- packages/pdf-codec/src/bytes/output-limit.ts | 14 + .../src/core-font-numbered-glyphs.test.ts | 6 +- .../pdf-codec/src/decode-budget-sites.test.ts | 508 ++++++++++++++++++ packages/pdf-codec/src/decode-budget.test.ts | 334 ++++++++++++ packages/pdf-codec/src/decode-budget.ts | 104 ++++ packages/pdf-codec/src/document.ts | 64 ++- packages/pdf-codec/src/filters.test.ts | 7 +- packages/pdf-codec/src/filters.ts | 170 ++++-- .../pdf-codec/src/font-read-type0.test.ts | 3 +- packages/pdf-codec/src/font-read.test.ts | 3 +- packages/pdf-codec/src/font-read.ts | 11 +- packages/pdf-codec/src/image/ccitt.ts | 4 +- packages/pdf-codec/src/image/jpeg2000.ts | 5 + .../images-read-decode-differential.test.ts | 9 +- .../pdf-codec/src/images-read-filters.test.ts | 3 +- .../pdf-codec/src/images-read-jpx-b.test.ts | 3 +- .../pdf-codec/src/images-read-jpx.test.ts | 3 +- packages/pdf-codec/src/images-read.test.ts | 3 +- packages/pdf-codec/src/images-read.ts | 42 +- packages/pdf-codec/src/index.ts | 3 + .../pdf-codec/src/interpret-colour.test.ts | 3 +- .../pdf-codec/src/interpret-operators.test.ts | 3 +- .../interpret-run-gaps-differential.test.ts | 5 +- .../pdf-codec/src/interpret-shapes.test.ts | 3 +- .../pdf-codec/src/interpret-text-runs.test.ts | 3 +- packages/pdf-codec/src/interpret-types.ts | 7 +- .../pdf-codec/src/interpret-xobjects.test.ts | 3 +- packages/pdf-codec/src/interpret.test.ts | 3 +- packages/pdf-codec/src/interpret.ts | 3 +- packages/pdf-codec/src/names.test.ts | 13 +- packages/pdf-codec/src/navigation.test.ts | 3 +- .../pdf-codec/src/optional-content.test.ts | 3 +- packages/pdf-codec/src/raster-text.ts | 12 +- packages/pdf-codec/src/raster.ts | 20 +- packages/pdf-codec/src/read.ts | 17 +- .../src/symbol-font-private-use.test.ts | 3 +- .../src/test-support/decode-budget.ts | 427 +++++++++++++++ .../pdf-codec/src/test-support/resolver.ts | 19 + .../src/verified-glyph-tables.test.ts | 3 +- .../pdf-codec/src/write-passthrough.test.ts | 9 +- packages/pdf-codec/src/xref.ts | 36 +- 44 files changed, 1903 insertions(+), 126 deletions(-) create mode 100644 packages/pdf-codec/src/bytes/output-limit.ts create mode 100644 packages/pdf-codec/src/decode-budget-sites.test.ts create mode 100644 packages/pdf-codec/src/decode-budget.test.ts create mode 100644 packages/pdf-codec/src/decode-budget.ts create mode 100644 packages/pdf-codec/src/test-support/decode-budget.ts create mode 100644 packages/pdf-codec/src/test-support/resolver.ts diff --git a/packages/pdf-codec/README.md b/packages/pdf-codec/README.md index 78a967458e..94e54c1f7e 100644 --- a/packages/pdf-codec/README.md +++ b/packages/pdf-codec/README.md @@ -117,6 +117,8 @@ A `LayoutDocument` read with any surface skipped is a reading with parts of the **Page boundaries: the crop box is the visible region** (ISO 32000-1 14.11.2). A page's reported `widthPt`/`heightPt` and coordinate frame come from its effective `/CropBox` — the rectangle a viewer displays and prints, inherited through the page tree like `/MediaBox` and defaulting to it — not from the media box: content an author placed outside the crop box (printer's marks, bleed, off-page slugs) is not visible and does not extract. Content wholly outside the crop box is dropped; content straddling the boundary keeps its original unclipped geometry (the item layer records what the file states — a viewer's clipping is a rendering fact, not source data); link annotations are anchored constructs rather than painted content and are never filtered, the same line the optional-content filter draws. The declared boundary rectangles a distinct crop box hides — plus `/BleedBox`/`/TrimBox`/`/ArtBox`, print-production facts with no field in the layout model — are quarantined verbatim as the `page-boxes` package-level residue row rather than silently dropped. A degenerate `/CropBox` (zero width or height) falls back to the media box with a `pdf/invalid-crop-box` warning. +**Bounding decoding: a document-wide decode budget.** `readPdf(bytes, { maxDecodedBytes })` and `renderPdfPage(bytes, page, { maxDecodedBytes }, rasteriser)` (and `openPdfDocument(bytes, sink, { maxDecodedBytes })` underneath them) charge every byte stream decoding produces against one budget for the whole document: cross-reference and object streams (including those cross-reference recovery finds), page content streams and form XObjects, every embedded font program each time it is decoded (for its encoding, its PostScript name, its digest, or its outlines when rendering), `/ToUnicode` CMaps, `/CIDToGIDMap` streams, image XObjects and inline images, soft masks, `/Indexed` lookup streams, the catalog's `/Metadata` and embedded files. Each filter stage's output counts, a stream decoded twice (one program two fonts share) is charged twice, and an encrypted stream is charged for its decrypted plaintext's decoding. The first decode that would pass the budget throws `PdfDecodeBudgetError` (a `PdfParseError` with code `pdf/decode-budget-exceeded`, carrying `maxDecodedBytes`, the bytes already `decodedBytes`, the stream's `objectNumber`, undefined for an inline image, and its `filters`), so a caller can tell "too much work" from a malformed file, and once one decode has been refused every later one is too. The decode stops as soon as its output crosses what is left rather than after materialising it: Flate inflates in input slices sized to what the remainder can absorb at DEFLATE's maximum expansion, LZW, RunLength and ASCII85 stop at the first byte past it, ASCIIHex is refused from its exact size before allocating, CCITTFax is refused from its declared rows (or, with none, the most rows its input bits can encode), JBIG2 from its `/Width` and `/Height` (refused without them), JPEG 2000 from its sample planes before allocating them, and a JPEG drawn by `renderPdfPage` from its declared samples before the rasteriser gets it; `readPdf` passes a JPEG through undecoded, so it costs nothing there. Absent, nothing is charged and every decode runs the same unbounded code it always has. A caller decoding a stream itself passes a budget straight to `decodeStream` (`pdf-codec/filters`): `decodeStream(raw, dict, sink, { budget: new PdfDecodeBudget(n), objectNumber })`. What the budget does not bound is work that produces no decoded bytes: interpreting content, laying out text and encoding PNGs, which scale with page and content-stream sizes a caller can measure up front. The read-only entry does not re-export the error class; a consumer avoiding the root barrel imports it from `pdf-codec/decode-budget`. + **Cancellation granularity and cost, for CPU-metered runtimes.** Both pipelines are synchronous end to end — there is no `await` point for cancellation to hook into implicitly — so the `signal` is checked explicitly, once per page-loop iteration (and once before `readPdf`'s document-open phase begins). A signal aborted mid-parse therefore takes effect at the next page boundary, not instantly: `readPdf`'s document-open phase (cross-reference resolution, object parsing) and a single page's content-stream interpretation are the two spans that cannot be interrupted, and a document consisting of one enormous page is effectively uninterruptible however many pages it claims. Cost is roughly linear in decompressed content length, so budget for the worst single page, not the page count. On Cloudflare Workers this is the honest shape of the trade: the parse holds the isolate for its whole duration with no opportunity to yield or report progress, and an `AbortSignal` shared with whatever can abort concurrently (a binding, another context) makes a deadline enforceable at page granularity — but it cannot convert a synchronous parse into a resumable one. An async page-at-a-time API is a deliberate non-goal of this package's current surface. The same round trip is also available as a schema-validated [`z.codec()`](https://zod.dev) pair: @@ -297,7 +299,7 @@ Dependency direction is strictly downward and checkable: `layout` imports only ` - **`PdfObject` has no Zod schema at all**, deliberately: it never crosses a public boundary or round-trips through JSON, and is constructed exclusively by this package's own parser. It narrows natively on its own `kind` discriminant. - **The `MathBox`/`MathFontMetrics` family is structurally typed on purpose, not validated by Zod** — the mechanism that lets a caller (`documents.js`) hand this package a real value produced by a completely independent module, with zero cast, zero wrapper, and no shared class or branded type. - **No type assertions anywhere.** Every third-party or loosely-typed value is narrowed through a type guard or a Zod parse at the boundary. -- **A three-tier PDF-read failure policy**: throw a typed `PdfParseError`/`PdfEncryptedError`/`PdfPasswordRequiredError` for a file that cannot be meaningfully processed at all (or `PdfPageSelectionError` for a page selection naming no page of it); recover with a `PdfDiagnostic` (`severity: 'warning'`) for something malformed but salvageable; degrade with a diagnostic for an individual unsupported feature while the rest of the document still reads. +- **A three-tier PDF-read failure policy**: throw a typed `PdfParseError`/`PdfEncryptedError`/`PdfPasswordRequiredError` for a file that cannot be meaningfully processed at all (or `PdfPageSelectionError` for a page selection naming no page of it), or `PdfDecodeBudgetError` for a read that would decode more than its `maxDecodedBytes`; recover with a `PdfDiagnostic` (`severity: 'warning'`) for something malformed but salvageable; degrade with a diagnostic for an individual unsupported feature while the rest of the document still reads. - **Conventional commits**, enforced via commitlint + husky. ## Gotchas and quirks diff --git a/packages/pdf-codec/src/attachments.ts b/packages/pdf-codec/src/attachments.ts index f8783f23b1..66e1c992a9 100644 --- a/packages/pdf-codec/src/attachments.ts +++ b/packages/pdf-codec/src/attachments.ts @@ -1,6 +1,5 @@ import type { PdfDiagnosticSink } from "./diagnostics"; import type { PdfObjectResolver } from "./interpret-types"; -import { decodeStream } from "./filters"; import type { LayoutAttachment } from "./layout"; import { walkNameTree } from "./names"; import type { PdfDict, PdfObject } from "./objects"; @@ -100,7 +99,7 @@ function readFilespec( if (streamObj?.kind !== "stream") { continue; } - const decoded = decodeStream(streamObj.raw, streamObj.dict, sink); + const decoded = resolver.decodeStream(streamObj, sink); const descObj = dictGet(filespec, "Desc"); const subtype = asName(dictGet(streamObj.dict, "Subtype")); return { diff --git a/packages/pdf-codec/src/bytes/flate.ts b/packages/pdf-codec/src/bytes/flate.ts index 57551c1d48..61de88a016 100644 --- a/packages/pdf-codec/src/bytes/flate.ts +++ b/packages/pdf-codec/src/bytes/flate.ts @@ -1,4 +1,5 @@ -import { Unzlib, inflateSync, unzlibSync, zlibSync } from "fflate"; +import { Inflate, Unzlib, inflateSync, unzlibSync, zlibSync } from "fflate"; +import { OutputLimitExceededError } from "./output-limit"; import { isAsciiWhitespace } from "./reader"; import { concatBytes } from "./writer"; @@ -80,3 +81,125 @@ export function inflateTolerant(data: Uint8Array): InflateResult { } return { bytes: concatBytes(chunks), recovered: true }; } + +// DEFLATE's largest expansion per input byte: a dynamic-Huffman block can give one length symbol (a 258-byte match) and its distance one bit each, so two bits of input yield 258 bytes of output, 1032 bytes per input byte (RFC 1951 3.2.5). +const MAX_DEFLATE_EXPANSION = 1032; +// The smallest input slice fed to the streaming inflater, so a nearly spent limit does not cost one push per input byte: one slice can then overshoot the limit by at most this many bytes times MAX_DEFLATE_EXPANSION (258 KiB), plus whatever a few buffered header bytes expand to. +const MIN_BOUNDED_SLICE_BYTES = 256; + +type StreamingInflater = Inflate | Unzlib; + +interface BoundedAttempt { + readonly chunks: Uint8Array[]; + // Set when the inflater threw part-way (corrupt or truncated data), with `chunks` holding what it emitted before then. + readonly failed: boolean; +} + +// Feeds `data` to a streaming inflater in slices no larger than the remaining limit can absorb at DEFLATE's maximum expansion, so output is checked as it is produced and the limit stops the decode before it can materialise much more than the limit: an output that would pass `maxOutputBytes` throws OutputLimitExceededError out of the ondata callback, through the push. `final` is passed with the last slice, which is what makes the inflater verify the end of the stream (see inflateTolerant's last tier for why recovery passes false). +function boundedInflateAttempt( + inflater: StreamingInflater, + data: Uint8Array, + final: boolean, + maxOutputBytes: number, +): BoundedAttempt { + const chunks: Uint8Array[] = []; + let produced = 0; + inflater.ondata = (chunk) => { + produced += chunk.length; + if (produced > maxOutputBytes) { + throw new OutputLimitExceededError(maxOutputBytes); + } + chunks.push(chunk); + }; + let offset = 0; + try { + do { + const slice = Math.max( + MIN_BOUNDED_SLICE_BYTES, + Math.floor((maxOutputBytes - produced) / MAX_DEFLATE_EXPANSION), + ); + const end = Math.min(data.length, offset + slice); + inflater.push(data.subarray(offset, end), final && end === data.length); + offset = end; + } while (offset < data.length); + } catch (error) { + if (error instanceof OutputLimitExceededError) { + throw error; + } + return { chunks, failed: true }; + } + return { chunks, failed: false }; +} + +// One of inflateTolerant's whole-stream tiers under a limit: the streaming inflater decodes it with the limit enforced as output appears; if that fails for any other reason, the tier's own synchronous function decides, as it does in inflateTolerant, since fflate's synchronous inflaters accept some ends of stream (a stream cut off inside its header or its final block) that the streaming ones reject. The streaming attempt failed having produced no more than the limit, and both decode the same bits up to where it failed, so the synchronous decode does a bounded amount of work, and its output is checked against the limit too. Undefined when the tier fails. +function boundedTier( + inflater: StreamingInflater, + inflateWhole: (data: Uint8Array) => Uint8Array, + data: Uint8Array, + maxOutputBytes: number, +): Uint8Array | undefined { + const attempt = boundedInflateAttempt(inflater, data, true, maxOutputBytes); + if (!attempt.failed) { + return concatBytes(attempt.chunks); + } + let whole: Uint8Array; + try { + whole = inflateWhole(data); + } catch { + return undefined; + } + if (whole.length > maxOutputBytes) { + throw new OutputLimitExceededError(maxOutputBytes); + } + return whole; +} + +/** + * inflateTolerant's recovery ladder with an output limit: the same four tiers in the same order (zlib, zlib after leading whitespace, raw DEFLATE, then whatever a truncated zlib stream yields), each decoding incrementally so it stops as soon as its output would pass `maxOutputBytes` rather than after materialising it. A tier that fails for any other reason falls through exactly as inflateTolerant's does, and its output is discarded. A tier whose output passes the limit refuses the stream there, even when it would have failed later and a later tier would have produced less: whether it succeeds is only known by finishing it, which is the work the limit exists to prevent, so the refusal errs on the side of the bound. The last tier feeds the stream in slices, so on a truncated stream it can recover a longer prefix of the decoded data than inflateTolerant's single push does; that output is still a prefix of the stream's own decoding, within the limit. + * @param data - the zlib-framed (or mislabelled raw) DEFLATE stream + * @param maxOutputBytes - the most bytes the decoded output may hold + * @returns the decoded bytes, and whether any tier past the first produced them + * @throws OutputLimitExceededError as soon as a tier's output would pass `maxOutputBytes` + * @throws Error when no tier recovers any data, as inflateTolerant does + */ +export function inflateTolerantBounded( + data: Uint8Array, + maxOutputBytes: number, +): InflateResult { + const plain = boundedTier(new Unzlib(), unzlibSync, data, maxOutputBytes); + if (plain !== undefined) { + return { bytes: plain, recovered: false }; + } + + let offset = 0; + while (offset < data.length && isAsciiWhitespace(data[offset])) { + offset++; + } + if (offset > 0) { + const skipped = boundedTier( + new Unzlib(), + unzlibSync, + data.subarray(offset), + maxOutputBytes, + ); + if (skipped !== undefined) { + return { bytes: skipped, recovered: true }; + } + } + + const raw = boundedTier(new Inflate(), inflateSync, data, maxOutputBytes); + if (raw !== undefined) { + return { bytes: raw, recovered: true }; + } + + const partial = boundedInflateAttempt( + new Unzlib(), + data, + false, + maxOutputBytes, + ); + if (partial.chunks.length === 0) { + throw new Error("unable to inflate stream: no data could be recovered"); + } + return { bytes: concatBytes(partial.chunks), recovered: true }; +} diff --git a/packages/pdf-codec/src/bytes/output-limit.ts b/packages/pdf-codec/src/bytes/output-limit.ts new file mode 100644 index 0000000000..8e90bbcdeb --- /dev/null +++ b/packages/pdf-codec/src/bytes/output-limit.ts @@ -0,0 +1,14 @@ +/** + * Thrown by a bounded decoder the moment its output would grow past the limit its caller set, before that output is materialised. It names only the limit, because a byte-level decoder knows nothing about the document the bytes came from: `decodeStream` (src/filters.ts) catches it at the one place a decode stage runs and reports the refusal as a `PdfDecodeBudgetError` carrying the stream's object number and filters. + */ +export class OutputLimitExceededError extends Error { + readonly maxOutputBytes: number; + + constructor(maxOutputBytes: number) { + super( + `decoded output would exceed the ${String(maxOutputBytes)}-byte limit`, + ); + this.name = "OutputLimitExceededError"; + this.maxOutputBytes = maxOutputBytes; + } +} diff --git a/packages/pdf-codec/src/core-font-numbered-glyphs.test.ts b/packages/pdf-codec/src/core-font-numbered-glyphs.test.ts index 6abb356935..4313516d1f 100644 --- a/packages/pdf-codec/src/core-font-numbered-glyphs.test.ts +++ b/packages/pdf-codec/src/core-font-numbered-glyphs.test.ts @@ -8,6 +8,7 @@ import { createFontResolver } from "./font-read"; import type { PdfObjectResolver } from "./interpret-types"; import type { PdfDict, PdfObject } from "./objects"; import { asDict, pdfArray, pdfDict, pdfName, pdfNum } from "./objects"; +import { unbudgetedResolver } from "./test-support/resolver"; const NO_DIAGNOSTICS: PdfDiagnosticSink = () => undefined; @@ -89,7 +90,10 @@ function makeResolver(objects: Map): PdfObjectResolver { const resolve = (obj: PdfObject | undefined): PdfObject | undefined => obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict: (obj) => resolveDict(obj ?? objects.get(0)) }; + return unbudgetedResolver({ + resolve, + resolveDict: (obj) => resolveDict(obj ?? objects.get(0)), + }); } describe("a CFF subset that numbers its glyphs after a core face", () => { diff --git a/packages/pdf-codec/src/decode-budget-sites.test.ts b/packages/pdf-codec/src/decode-budget-sites.test.ts new file mode 100644 index 0000000000..5fe6029713 --- /dev/null +++ b/packages/pdf-codec/src/decode-budget-sites.test.ts @@ -0,0 +1,508 @@ +import { describe, expect, it } from "vitest"; +import { PdfDecodeBudget } from "./decode-budget"; +import { NOOP_DIAGNOSTIC_SINK } from "./diagnostics"; +import { readImageXObject } from "./images-read"; +import type { PdfObject } from "./objects"; +import { pdfDict, pdfName, pdfStream } from "./objects"; +import type { PageRasteriser } from "./raster"; +import { renderPdfPage } from "./raster"; +import { readPdf } from "./read"; +import { + BIG, + BIG_OBJ, + bigStream, + BUDGET, + budgetedResolver, + contentSitePdf, + expectBudgetError, + fillUnused, + FLATE_ZEROS, + flateZeros, + jpegHeader, + LARGE, + OBJECT_STREAM_OBJ, + objectStreamPdf, + runLengthZeros, + SHOW_TEXT, + sitePdf, + TO_UNICODE_PRIVATE_USE, + XREF_STREAM_OBJ, +} from "./test-support/decode-budget"; +import * as encryptedFixtures from "./test-support/encrypted-pdfs"; +import { carlitoRegularBytes } from "./test-support/fonts"; +import { + JPEG2000_FIXTURES, + jpeg2000FixtureBytes, +} from "./test-support/jpeg2000"; +import { enc, FixtureBuilder } from "./test-support/pdf"; + +// Every site a document decodes a stream at, each reached by a document whose only large stream sits there: reverting a site's charge lets its read decode to the end, so its test fails. + +// Each site, the document that reaches it, and the stream its budget error names. +const SITES: readonly { + readonly site: string; + readonly pdf: () => Uint8Array; + readonly objectNumber: number | undefined; +}[] = [ + { + site: "a page content stream, as an element of a /Contents array", + pdf: () => contentSitePdf(), + objectNumber: 6, + }, + { + site: "a form XObject", + pdf: () => + sitePdf({ + pageResources: "/XObject << /Fm1 9 0 R >>", + content: "/Fm1 Do", + objects: (b) => { + b.stream( + BIG_OBJ, + "<< /Type /XObject /Subtype /Form /BBox [0 0 10 10] /Filter /FlateDecode >>", + FLATE_ZEROS, + ); + }, + maxObj: BIG_OBJ, + }), + objectNumber: BIG_OBJ, + }, + { + site: "an image XObject", + pdf: () => + sitePdf({ + pageResources: "/XObject << /Im1 9 0 R >>", + content: "q 10 0 0 10 0 0 cm /Im1 Do Q", + objects: (b) => { + b.stream( + BIG_OBJ, + "<< /Type /XObject /Subtype /Image /Width 1024 /Height 1024 /BitsPerComponent 8 /ColorSpace /DeviceGray /Filter /FlateDecode >>", + FLATE_ZEROS, + ); + }, + maxObj: BIG_OBJ, + }), + objectNumber: BIG_OBJ, + }, + { + site: "an inline image", + pdf: () => { + const data = runLengthZeros(LARGE); + const b = new FixtureBuilder().header("1.7"); + b.object(1, "<< /Type /Catalog /Pages 2 0 R >>"); + b.object(2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>"); + b.object( + 3, + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 100] /Resources << >> /Contents 5 0 R >>", + ); + const content = new Uint8Array([ + ...enc( + "q 10 0 0 10 0 0 cm BI /W 1024 /H 1024 /BPC 8 /CS /G /F /RL ID ", + ), + ...data, + ...enc(" EI Q"), + ]); + b.stream(5, "<< >>", content); + fillUnused(b, 5); + b.classicXrefAndTrailer(5, "/Root 1 0 R"); + return b.bytes(); + }, + objectNumber: undefined, + }, + { + site: "an image's /SMask", + pdf: () => + sitePdf({ + pageResources: "/XObject << /Im1 6 0 R >>", + content: "q 10 0 0 10 0 0 cm /Im1 Do Q", + objects: (b) => { + b.stream( + 6, + "<< /Type /XObject /Subtype /Image /Width 1 /Height 1 /BitsPerComponent 8 /ColorSpace /DeviceGray /SMask 9 0 R >>", + new Uint8Array([0x80]), + ); + b.object(7, "null"); + b.object(8, "null"); + b.stream( + BIG_OBJ, + "<< /Type /XObject /Subtype /Image /Width 1 /Height 1 /BitsPerComponent 8 /ColorSpace /DeviceGray /Filter /FlateDecode >>", + FLATE_ZEROS, + ); + }, + maxObj: BIG_OBJ, + }), + objectNumber: BIG_OBJ, + }, + { + site: "an /Indexed colour space's lookup stream", + pdf: () => + sitePdf({ + pageResources: "/XObject << /Im1 6 0 R >>", + content: "q 10 0 0 10 0 0 cm /Im1 Do Q", + objects: (b) => { + b.stream( + 6, + "<< /Type /XObject /Subtype /Image /Width 1 /Height 1 /BitsPerComponent 8 /ColorSpace [/Indexed /DeviceRGB 255 9 0 R] >>", + new Uint8Array([0]), + ); + b.object(7, "null"); + b.object(8, "null"); + bigStream(b); + }, + maxObj: BIG_OBJ, + }), + objectNumber: BIG_OBJ, + }, + { + site: "the catalog's /Metadata", + pdf: () => + sitePdf({ + catalogExtra: "/Metadata 9 0 R", + objects: (b) => { + for (let n = 6; n < BIG_OBJ; n++) b.object(n, "null"); + b.stream( + BIG_OBJ, + "<< /Type /Metadata /Subtype /XML /Filter /FlateDecode >>", + FLATE_ZEROS, + ); + }, + maxObj: BIG_OBJ, + }), + objectNumber: BIG_OBJ, + }, + { + site: "an embedded file", + pdf: () => + sitePdf({ + catalogExtra: + "/Names << /EmbeddedFiles << /Names [(a.bin) 6 0 R] >> >>", + objects: (b) => { + b.object(6, "<< /Type /Filespec /F (a.bin) /EF << /F 9 0 R >> >>"); + b.object(7, "null"); + b.object(8, "null"); + b.stream( + BIG_OBJ, + "<< /Type /EmbeddedFile /Filter /FlateDecode >>", + FLATE_ZEROS, + ); + }, + maxObj: BIG_OBJ, + }), + objectNumber: BIG_OBJ, + }, + { + site: "a font's /ToUnicode CMap", + pdf: () => + sitePdf({ + pageResources: "/Font << /F1 6 0 R >>", + content: SHOW_TEXT, + objects: (b) => { + b.object( + 6, + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /ToUnicode 9 0 R >>", + ); + b.object(7, "null"); + b.object(8, "null"); + bigStream(b); + }, + maxObj: BIG_OBJ, + }), + objectNumber: BIG_OBJ, + }, + { + site: "a symbolic font's embedded program, read for its encoding", + pdf: () => + sitePdf({ + pageResources: "/Font << /F1 6 0 R >>", + content: SHOW_TEXT, + objects: (b) => { + b.object( + 6, + "<< /Type /Font /Subtype /TrueType /BaseFont /Glyphs /FirstChar 65 /LastChar 65 /Widths [500] /FontDescriptor 7 0 R >>", + ); + b.object( + 7, + "<< /Type /FontDescriptor /FontName /Glyphs /Flags 4 /FontFile2 9 0 R >>", + ); + b.object(8, "null"); + bigStream(b); + }, + maxObj: BIG_OBJ, + }), + objectNumber: BIG_OBJ, + }, + { + site: "a font's embedded program, read for its PostScript name", + pdf: () => + sitePdf({ + pageResources: "/Font << /F1 6 0 R >>", + content: SHOW_TEXT, + objects: (b) => { + b.object( + 6, + "<< /Type /Font /Subtype /TrueType /BaseFont /Glyphs /FirstChar 65 /LastChar 65 /Widths [500] /FontDescriptor 7 0 R /ToUnicode 8 0 R >>", + ); + b.object( + 7, + "<< /Type /FontDescriptor /FontName /Glyphs /Flags 32 /FontFile2 9 0 R >>", + ); + b.stream(8, "<< >>", TO_UNICODE_PRIVATE_USE); + bigStream(b); + }, + maxObj: BIG_OBJ, + }), + objectNumber: BIG_OBJ, + }, + { + site: "a font's embedded program, read for its digest", + pdf: () => + sitePdf({ + pageResources: "/Font << /F1 6 0 R >>", + content: SHOW_TEXT, + objects: (b) => { + b.object( + 6, + "<< /Type /Font /Subtype /Type1 /BaseFont /AdvP4C4E74 /FirstChar 65 /LastChar 65 /Widths [500] /FontDescriptor 7 0 R >>", + ); + b.object( + 7, + "<< /Type /FontDescriptor /FontName /AdvP4C4E74 /Flags 32 /FontFile3 9 0 R >>", + ); + b.object(8, "null"); + bigStream(b); + }, + maxObj: BIG_OBJ, + }), + objectNumber: BIG_OBJ, + }, + { + site: "a composite font's /CIDToGIDMap", + pdf: () => + sitePdf({ + pageResources: "/Font << /F1 6 0 R >>", + content: "BT /F1 12 Tf 10 50 Td <0041> Tj ET", + objects: (b) => { + b.object( + 6, + "<< /Type /Font /Subtype /Type0 /BaseFont /Glyphs /Encoding /Identity-H /DescendantFonts [7 0 R] >>", + ); + b.object( + 7, + "<< /Type /Font /Subtype /CIDFontType2 /BaseFont /Glyphs /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> /CIDToGIDMap 9 0 R >>", + ); + b.object(8, "null"); + bigStream(b); + }, + maxObj: BIG_OBJ, + }), + objectNumber: BIG_OBJ, + }, + { + site: "a cross-reference stream", + pdf: () => objectStreamPdf(0, LARGE), + objectNumber: XREF_STREAM_OBJ, + }, + { + site: "an object stream", + pdf: () => objectStreamPdf(LARGE, 0), + objectNumber: OBJECT_STREAM_OBJ, + }, + { + site: "an object stream found by cross-reference recovery", + pdf: () => objectStreamPdf(LARGE, 0, true), + objectNumber: OBJECT_STREAM_OBJ, + }, +]; + +describe("readPdf with maxDecodedBytes", () => { + for (const { site, pdf, objectNumber } of SITES) { + it(`charges ${site}`, () => { + const document = pdf(); + // The same document reads without a budget, so the refusal below is the budget's and not a malformed file's. + expect(() => readPdf(document)).not.toThrow(); + expectBudgetError( + () => readPdf(document, { maxDecodedBytes: BUDGET }), + objectNumber, + ); + expect(() => + readPdf(document, { maxDecodedBytes: 4 * LARGE }), + ).not.toThrow(); + }); + } + + it("charges a font program two font dictionaries share once for each", () => { + const document = sitePdf({ + pageResources: "/Font << /F1 6 0 R /F2 8 0 R >>", + content: `${SHOW_TEXT} BT /F2 12 Tf 10 30 Td (A) Tj ET`, + objects: (b) => { + const font = (name: string): string => + `<< /Type /Font /Subtype /TrueType /BaseFont /${name} /FirstChar 65 /LastChar 65 /Widths [500] /FontDescriptor 7 0 R >>`; + b.object(6, font("GlyphsA")); + b.object( + 7, + "<< /Type /FontDescriptor /FontName /Glyphs /Flags 4 /FontFile2 9 0 R >>", + ); + b.object(8, font("GlyphsB")); + bigStream(b); + }, + maxObj: BIG_OBJ, + }); + expect(() => + readPdf(document, { maxDecodedBytes: 2 * LARGE }), + ).not.toThrow(); + expectBudgetError( + () => readPdf(document, { maxDecodedBytes: 2 * LARGE - 1 }), + BIG_OBJ, + ); + }); + + it("charges a JPEG 2000 image's samples before decoding them, and stops the read rather than skipping the image", () => { + const fixture = JPEG2000_FIXTURES.find((f) => !f.undecodable); + if (fixture === undefined) { + throw new Error("no decodable JPEG 2000 fixture"); + } + const samples = fixture.width * fixture.height * fixture.componentCount * 4; + const dict = pdfDict({ + Subtype: pdfName("Image"), + Filter: pdfName("JPXDecode"), + }); + const raw = jpeg2000FixtureBytes(fixture.codestream); + const objects = new Map([ + [BIG_OBJ, pdfStream(dict, raw)], + ]); + const fits = new PdfDecodeBudget(samples); + expect( + readImageXObject( + dict, + raw, + budgetedResolver(objects, fits), + NOOP_DIAGNOSTIC_SINK, + ), + ).toBeDefined(); + expect(fits.decodedBytes).toBe(samples); + expectBudgetError( + () => + readImageXObject( + dict, + raw, + budgetedResolver(objects, new PdfDecodeBudget(samples - 1)), + NOOP_DIAGNOSTIC_SINK, + ), + BIG_OBJ, + ); + }); + + it("reads an encrypted document with object streams under a budget exactly as without one", () => { + const document = encryptedFixtures.aes256ObjectStreamsPdf(); + expect(readPdf(document, { maxDecodedBytes: LARGE })).toEqual( + readPdf(document), + ); + }); +}); + +// A rasteriser that records nothing: the decode budget is what these tests watch. +const SILENT_RASTERISER: PageRasteriser = { + beginPage: () => undefined, + draw: () => undefined, + finish: () => new Uint8Array(0), +}; + +function render( + document: Uint8Array, + maxDecodedBytes?: number, +): unknown { + return renderPdfPage( + document, + 0, + maxDecodedBytes === undefined ? {} : { maxDecodedBytes }, + SILENT_RASTERISER, + ); +} + +function carlitoType0Pdf( + cidToGidMap: Uint8Array, +): Uint8Array { + return sitePdf({ + pageResources: "/Font << /F1 6 0 R >>", + content: "BT /F1 12 Tf 10 50 Td <0041> Tj ET", + objects: (b) => { + b.object( + 6, + "<< /Type /Font /Subtype /Type0 /BaseFont /Carlito /Encoding /Identity-H /DescendantFonts [7 0 R] >>", + ); + b.object( + 7, + "<< /Type /Font /Subtype /CIDFontType2 /BaseFont /Carlito /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> /FontDescriptor 8 0 R /CIDToGIDMap 9 0 R >>", + ); + b.stream( + 8, + "<< /Type /FontDescriptor /FontName /Carlito /Flags 32 /FontFile2 10 0 R >>", + new Uint8Array(0), + ); + b.stream(BIG_OBJ, BIG, cidToGidMap); + b.stream(10, "<< >>", carlitoRegularBytes()); + }, + maxObj: 10, + }); +} + +describe("renderPdfPage with maxDecodedBytes", () => { + it("charges a page content stream", () => { + const document = contentSitePdf(); + expect(() => render(document)).not.toThrow(); + expectBudgetError(() => render(document, BUDGET), 6); + }); + + it("charges a TrueType program the text walk decodes for its outlines", () => { + const document = sitePdf({ + pageResources: "/Font << /F1 6 0 R >>", + content: SHOW_TEXT, + objects: (b) => { + b.object( + 6, + "<< /Type /Font /Subtype /TrueType /BaseFont /Glyphs /FirstChar 65 /LastChar 65 /Widths [500] /FontDescriptor 7 0 R >>", + ); + b.object( + 7, + "<< /Type /FontDescriptor /FontName /Glyphs /Flags 32 /FontFile2 9 0 R >>", + ); + b.object(8, "null"); + bigStream(b); + }, + maxObj: BIG_OBJ, + }); + expect(() => render(document)).not.toThrow(); + expectBudgetError(() => render(document, BUDGET), BIG_OBJ); + }); + + it("charges the /CIDToGIDMap the text walk decodes, beside the one the font read decodes", () => { + const map = flateZeros(1000); + const document = carlitoType0Pdf(map); + expect(() => render(document)).not.toThrow(); + // Read once by the font reader and once by the text walk: one charge fits, the second does not. + expectBudgetError(() => render(document, 1500), BIG_OBJ); + expect(() => render(document, 2000)).not.toThrow(); + }); + + it("charges a JPEG its decoded samples before handing it to the rasteriser", () => { + const width = 1024; + const height = 1024; + const document = sitePdf({ + pageResources: "/XObject << /Im1 9 0 R >>", + content: "q 10 0 0 10 0 0 cm /Im1 Do Q", + objects: (b) => { + for (let n = 6; n < BIG_OBJ; n++) b.object(n, "null"); + b.stream( + BIG_OBJ, + `<< /Type /XObject /Subtype /Image /Width ${String(width)} /Height ${String(height)} /BitsPerComponent 8 /ColorSpace /DeviceRGB /Filter /DCTDecode >>`, + jpegHeader(width, height, 3), + ); + }, + maxObj: BIG_OBJ, + }); + expect(() => render(document)).not.toThrow(); + expectBudgetError(() => render(document, width * height * 3 - 1), BIG_OBJ); + expect(() => render(document, width * height * 3)).not.toThrow(); + // readPdf passes the JPEG through undecoded, so it costs nothing there. + expect(() => readPdf(document, { maxDecodedBytes: 0 })).not.toThrow(); + }); +}); diff --git a/packages/pdf-codec/src/decode-budget.test.ts b/packages/pdf-codec/src/decode-budget.test.ts new file mode 100644 index 0000000000..9ff779273e --- /dev/null +++ b/packages/pdf-codec/src/decode-budget.test.ts @@ -0,0 +1,334 @@ +import { readdirSync, readFileSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { zlibSync } from "fflate"; +import { describe, expect, it } from "vitest"; +import { inflateTolerant, inflateTolerantBounded } from "./bytes/flate"; +import { OutputLimitExceededError } from "./bytes/output-limit"; +import { PdfDecodeBudget } from "./decode-budget"; +import { NOOP_DIAGNOSTIC_SINK, PdfParseError } from "./diagnostics"; +import { openPdfDocument } from "./document"; +import { decodeStream } from "./filters"; +import { pdfArray, pdfDict, pdfName, pdfNum, pdfRef } from "./objects"; +import { readPdf } from "./read"; +import { + ascii85Zeros, + asciiHexZeros, + BIG, + BUDGET, + expectBudgetError, + flateZeros, + LARGE, + lzwEncode, + runLengthZeros, + sitePdf, +} from "./test-support/decode-budget"; +import * as encryptedFixtures from "./test-support/encrypted-pdfs"; +import { enc } from "./test-support/pdf"; +import * as pdfFixtures from "./test-support/pdf"; +import { randomBytes, seededRandom } from "./test-support/seeded-random"; + +// The decode budget's mechanics: each filter's bounded decode, the bounded inflate ladder, a document's running total, the unchanged default, and the one decode path every reader goes through. Each site a document decodes at is tested in decode-budget-sites.test.ts. + +describe("decodeStream with a budget", () => { + const filters: readonly { + readonly name: string; + readonly encode: (size: number) => Uint8Array; + }[] = [ + { name: "FlateDecode", encode: flateZeros }, + { name: "LZWDecode", encode: (size) => lzwEncode(new Uint8Array(size)) }, + { name: "RunLengthDecode", encode: runLengthZeros }, + { name: "ASCII85Decode", encode: ascii85Zeros }, + { name: "ASCIIHexDecode", encode: asciiHexZeros }, + ]; + + for (const { name, encode } of filters) { + describe(name, () => { + const dict = pdfDict({ Filter: pdfName(name) }); + + it("refuses a stream that would expand past the budget, naming it", () => { + const budget = new PdfDecodeBudget(BUDGET); + const error = expectBudgetError( + () => + decodeStream(encode(LARGE), dict, NOOP_DIAGNOSTIC_SINK, { + budget, + objectNumber: 7, + }), + 7, + [name], + ); + expect(error).toBeInstanceOf(PdfParseError); + expect(error.maxDecodedBytes).toBe(BUDGET); + expect(error.decodedBytes).toBe(0); + expect(budget.remainingBytes).toBe(0); + }); + + it("decodes what fits exactly as without a budget, and charges its length", () => { + const random = seededRandom(name.length); + for (let trial = 0; trial < 8; trial++) { + const size = Math.floor(random() * 4096) + 1; + const encoded = encode(size); + const unbounded = decodeStream(encoded, dict, NOOP_DIAGNOSTIC_SINK); + expect(unbounded.bytes.length).toBe(size); + const exact = new PdfDecodeBudget(size); + expect( + decodeStream(encoded, dict, NOOP_DIAGNOSTIC_SINK, { + budget: exact, + }).bytes, + ).toEqual(unbounded.bytes); + expect(exact.decodedBytes).toBe(size); + expectBudgetError( + () => + decodeStream(encoded, dict, NOOP_DIAGNOSTIC_SINK, { + budget: new PdfDecodeBudget(size - 1), + }), + undefined, + ); + } + }); + }); + } + + it("charges every stage of a filter chain", () => { + const dict = pdfDict({ + Filter: pdfArray([pdfName("ASCIIHexDecode"), pdfName("FlateDecode")]), + }); + const flate = flateZeros(1000); + const hex = enc( + `${Array.from(flate, (byte) => byte.toString(16).padStart(2, "0")).join("")}>`, + ); + const budget = new PdfDecodeBudget(LARGE); + decodeStream(hex, dict, NOOP_DIAGNOSTIC_SINK, { budget }); + expect(budget.decodedBytes).toBe(flate.length + 1000); + }); + + it("refuses every later decode once one has been refused", () => { + const budget = new PdfDecodeBudget(BUDGET); + const dict = pdfDict({ Filter: pdfName("FlateDecode") }); + expectBudgetError( + () => + decodeStream(flateZeros(LARGE), dict, NOOP_DIAGNOSTIC_SINK, { budget }), + undefined, + ); + expectBudgetError( + () => decodeStream(flateZeros(1), dict, NOOP_DIAGNOSTIC_SINK, { budget }), + undefined, + ); + }); + + it("charges nothing for an unfiltered stream, or the undecoded DCT and JPX stages", () => { + const budget = new PdfDecodeBudget(0); + for (const dict of [ + pdfDict({}), + pdfDict({ Filter: pdfName("DCTDecode") }), + pdfDict({ Filter: pdfName("JPXDecode") }), + ]) { + decodeStream(new Uint8Array(LARGE), dict, NOOP_DIAGNOSTIC_SINK, { + budget, + }); + } + expect(budget.decodedBytes).toBe(0); + }); + + it("refuses a CCITTFax stream from its declared rows before decoding, and bounds one with none by its input", () => { + const declared = pdfDict({ + Filter: pdfName("CCITTFaxDecode"), + DecodeParms: pdfDict({ + K: pdfNum(-1), + Columns: pdfNum(8192), + Rows: pdfNum(8192), + }), + }); + expectBudgetError( + () => + decodeStream(new Uint8Array(1), declared, NOOP_DIAGNOSTIC_SINK, { + budget: new PdfDecodeBudget(BUDGET), + }), + undefined, + ["CCITTFaxDecode"], + ); + // No /Rows: every row costs at least one input bit, so a 1-byte input bounds the output at 8 rows of 1024 bytes. + const undeclared = pdfDict({ + Filter: pdfName("CCITTFaxDecode"), + DecodeParms: pdfDict({ K: pdfNum(-1), Columns: pdfNum(8192) }), + }); + expectBudgetError( + () => + decodeStream(new Uint8Array(1), undeclared, NOOP_DIAGNOSTIC_SINK, { + budget: new PdfDecodeBudget(8 * 1024 - 1), + }), + undefined, + ); + const fits = new PdfDecodeBudget(8 * 1024); + decodeStream(new Uint8Array(1), undeclared, NOOP_DIAGNOSTIC_SINK, { + budget: fits, + }); + }); + + it("charges a JBIG2 image its declared size before decoding, and refuses one with no declared size", () => { + const sized = pdfDict({ + Filter: pdfName("JBIG2Decode"), + Width: pdfNum(8192), + Height: pdfNum(8192), + }); + expectBudgetError( + () => + decodeStream(new Uint8Array(0), sized, NOOP_DIAGNOSTIC_SINK, { + budget: new PdfDecodeBudget(BUDGET), + }), + undefined, + ["JBIG2Decode"], + ); + expectBudgetError( + () => + decodeStream( + new Uint8Array(0), + pdfDict({ Filter: pdfName("JBIG2Decode") }), + NOOP_DIAGNOSTIC_SINK, + { budget: new PdfDecodeBudget(LARGE) }, + ), + undefined, + ); + }); + + it("rejects a budget that is not a non-negative safe integer", () => { + for (const bad of [-1, 1.5, Number.NaN, Number.POSITIVE_INFINITY]) { + expect(() => new PdfDecodeBudget(bad)).toThrow(RangeError); + } + }); +}); + +describe("inflateTolerantBounded", () => { + // Valid, truncated and corrupted zlib streams, against limits either side of what inflateTolerant produces: the bounded ladder must refuse whenever the unbounded output does not fit, and otherwise agree with it. + it("refuses every input whose output would not fit, and otherwise agrees with inflateTolerant", () => { + const random = seededRandom(1814); + for (let trial = 0; trial < 200; trial++) { + const plain = randomBytes(random, Math.floor(random() * 20_000)); + const repeated = new Uint8Array(Math.floor(random() * 50_000)); + const source = random() < 0.5 ? plain : repeated; + let data = zlibSync(source); + const mangle = random(); + if (mangle < 0.25) { + data = data.slice(0, Math.floor(random() * data.length)); + } else if (mangle < 0.4) { + data = Uint8Array.from([0x0a, ...data]); + } else if (mangle < 0.5) { + data = data.slice(2); // raw DEFLATE under a zlib label + } + let unbounded: Uint8Array; + let recovered: boolean; + try { + ({ bytes: unbounded, recovered } = inflateTolerant(data)); + } catch { + continue; + } + const limit = Math.floor(random() * 2 * (unbounded.length + 1)); + if (unbounded.length > limit) { + expect(() => inflateTolerantBounded(data, limit)).toThrow( + OutputLimitExceededError, + ); + continue; + } + if (!recovered) { + expect(inflateTolerantBounded(data, limit).bytes).toEqual(unbounded); + continue; + } + // A recovered output may differ: an earlier tier that passes the limit before failing is refused as it happens (whether it would have succeeded is only known by finishing it), and the last tier, fed in slices, can recover a longer prefix than a single push. Whatever it returns is still a prefix of the stream's own decoding, within the limit. + let bounded: Uint8Array; + try { + bounded = inflateTolerantBounded(data, limit).bytes; + } catch (error) { + expect(error).toBeInstanceOf(OutputLimitExceededError); + continue; + } + expect(bounded.length).toBeLessThanOrEqual(limit); + if (mangle >= 0.25) { + expect(bounded).toEqual(unbounded); + } else { + expect(bounded).toEqual(source.subarray(0, bounded.length)); + } + } + }); +}); + +describe("a document opened with a budget", () => { + it("charges a stream each time it is decoded", () => { + const document = sitePdf({ + objects: (b) => { + b.stream(6, BIG, flateZeros(1000)); + }, + maxObj: 6, + }); + const doc = openPdfDocument(document, NOOP_DIAGNOSTIC_SINK, { + maxDecodedBytes: LARGE, + }); + const stream = doc.resolve(pdfRef(6, 0)); + if (stream?.kind !== "stream") { + throw new Error("fixture object 6 is a stream"); + } + doc.decodeStream(stream, NOOP_DIAGNOSTIC_SINK); + doc.decodeStream(stream, NOOP_DIAGNOSTIC_SINK); + expect(doc.decodeBudget?.decodedBytes).toBe(2000); + }); + + it("has no budget, and charges nothing, without maxDecodedBytes", () => { + const doc = openPdfDocument( + pdfFixtures.minimalClassicXrefPdf(), + NOOP_DIAGNOSTIC_SINK, + ); + expect(doc.decodeBudget).toBeUndefined(); + }); +}); + +describe("without a budget", () => { + // Every zero-argument document fixture the read tests use: a read with the largest budget must equal a read with none, error for error. + const fixtures = Object.entries({ ...pdfFixtures, ...encryptedFixtures }) + .filter( + (entry): entry is [string, () => Uint8Array] => + typeof entry[1] === "function" && entry[1].length === 0, + ) + .flatMap(([name, build]) => { + try { + const bytes = build(); + return bytes instanceof Uint8Array ? [{ name, bytes }] : []; + } catch { + return []; + } + }); + + it("finds the fixtures it compares", () => { + expect(fixtures.length).toBeGreaterThan(10); + }); + + for (const { name, bytes } of fixtures) { + it(`reads ${name} the same with an unlimited budget as with none`, () => { + const outcome = (maxDecodedBytes?: number): unknown => { + try { + return readPdf( + bytes, + maxDecodedBytes === undefined ? {} : { maxDecodedBytes }, + ); + } catch (error) { + return error; + } + }; + expect(outcome(Number.MAX_SAFE_INTEGER)).toEqual(outcome()); + }); + } +}); + +// Every decode a document reaches goes through its resolver's decodeStream, which is what charges the budget; a module calling filters.ts's decodeStream itself would decode outside it. Only the modules that build that path (the document, cross-reference reading, and the filters themselves) may. +describe("the decodeStream boundary", () => { + it("is imported only by the modules that charge the budget themselves", () => { + const src = dirname(fileURLToPath(import.meta.url)); + const importers = readdirSync(src) + .filter((file) => file.endsWith(".ts") && !file.endsWith(".test.ts")) + .filter((file) => + /import\s*\{[^}]*\bdecodeStream\b[^}]*\}\s*from\s*"\.\/filters"/u.test( + readFileSync(join(src, file), "utf8"), + ), + ) + .sort(); + expect(importers).toEqual(["document.ts", "xref.ts"]); + }); +}); diff --git a/packages/pdf-codec/src/decode-budget.ts b/packages/pdf-codec/src/decode-budget.ts new file mode 100644 index 0000000000..3e65635f1e --- /dev/null +++ b/packages/pdf-codec/src/decode-budget.ts @@ -0,0 +1,104 @@ +import { PdfParseError } from "./diagnostics"; + +/** + * The stream a decode was charged for: its indirect object number when it has one (undefined for an inline image, or a stream a caller built itself), and its `/Filter` chain in order. + */ +export interface PdfDecodeSource { + readonly objectNumber: number | undefined; + readonly filters: readonly string[]; +} + +/** + * A decode refused because its output would have taken the document past its decode budget (`maxDecodedBytes` on `readPdf`, `renderPdfPage` or `openPdfDocument`, or a `PdfDecodeBudget` handed to `decodeStream`). It is the throw tier for "too much work", not for a malformed file: the stream may be perfectly well formed, so a caller can tell the two apart by class (or by its code, `pdf/decode-budget-exceeded`) and retry with a larger budget, or refuse the file, as it chooses. + * + * `maxDecodedBytes` is the budget, `decodedBytes` what the document had already decoded before this stream, `objectNumber` the refused stream's object number (undefined for an inline image) and `filters` its `/Filter` chain. + */ +export class PdfDecodeBudgetError extends PdfParseError { + readonly maxDecodedBytes: number; + readonly decodedBytes: number; + readonly objectNumber: number | undefined; + readonly filters: readonly string[]; + + constructor( + maxDecodedBytes: number, + decodedBytes: number, + source: PdfDecodeSource, + ) { + const stream = + source.objectNumber === undefined + ? "an inline stream" + : `stream object ${String(source.objectNumber)}`; + const chain = + source.filters.length === 0 ? "no filter" : source.filters.join(", "); + super( + "pdf/decode-budget-exceeded", + `decoding ${stream} (${chain}) would take this document past its ${String(maxDecodedBytes)}-byte decode budget, of which ${String(decodedBytes)} bytes were already spent`, + ); + this.name = "PdfDecodeBudgetError"; + this.maxDecodedBytes = maxDecodedBytes; + this.decodedBytes = decodedBytes; + this.objectNumber = source.objectNumber; + this.filters = source.filters; + } +} + +/** + * A running total of the bytes a document's stream decoding has produced, against a fixed maximum. Every filter stage's output is charged (a stream under `[/ASCII85Decode /FlateDecode]` pays for both stages), and every decode is charged when it happens, so a stream decoded twice (one font program two font dictionaries name) pays twice, while one decoded once and cached (an object stream) pays once. + * + * Once a decode has been refused the budget is spent: every later decode with any output is refused too, so a conversion cannot carry on past the first refusal even through a caller that caught the error. + */ +export class PdfDecodeBudget { + readonly maxDecodedBytes: number; + #decodedBytes = 0; + #spent = false; + + /** + * @param maxDecodedBytes - the most bytes decoding may produce, a non-negative safe integer + * @throws RangeError when `maxDecodedBytes` is negative, fractional, or not a safe integer + */ + constructor(maxDecodedBytes: number) { + if (!Number.isSafeInteger(maxDecodedBytes) || maxDecodedBytes < 0) { + throw new RangeError( + `maxDecodedBytes must be a non-negative safe integer, not ${String(maxDecodedBytes)}`, + ); + } + this.maxDecodedBytes = maxDecodedBytes; + } + + /** The bytes charged so far. */ + get decodedBytes(): number { + return this.#decodedBytes; + } + + /** The bytes the next decode may still produce: zero once a decode has been refused. */ + get remainingBytes(): number { + return this.#spent ? 0 : this.maxDecodedBytes - this.#decodedBytes; + } + + /** + * Charges one decode's output. + * @param bytes - the bytes the decode produced, or is about to produce when its size is known in advance + * @param source - the stream being decoded, named in the error + * @throws PdfDecodeBudgetError when `bytes` is more than `remainingBytes`, after which the budget is spent + */ + charge(bytes: number, source: PdfDecodeSource): void { + if (bytes > this.remainingBytes) { + this.refuse(source); + } + this.#decodedBytes += bytes; + } + + /** + * Refuses a decode whose output is known to pass the budget, or cannot be bounded at all, and spends the budget. + * @param source - the stream being decoded, named in the error + * @throws PdfDecodeBudgetError always + */ + refuse(source: PdfDecodeSource): never { + this.#spent = true; + throw new PdfDecodeBudgetError( + this.maxDecodedBytes, + this.#decodedBytes, + source, + ); + } +} diff --git a/packages/pdf-codec/src/document.ts b/packages/pdf-codec/src/document.ts index 1bf7ad6f45..07aaa3e176 100644 --- a/packages/pdf-codec/src/document.ts +++ b/packages/pdf-codec/src/document.ts @@ -3,9 +3,12 @@ import { PdfEncryptedError, PdfParseError } from "./diagnostics"; import type { PdfDiagnosticSink } from "./diagnostics"; import type { PdfDecryptor } from "./encrypt"; import { createStandardDecryptor } from "./encrypt"; -import { decodeStream } from "./filters"; +import { PdfDecodeBudget } from "./decode-budget"; +import type { DecodedStream } from "./filters"; +import { decodeStream, filterNames } from "./filters"; +import type { PdfObjectResolver } from "./interpret-types"; import { nextToken } from "./lexer"; -import type { PdfDict, PdfObject } from "./objects"; +import type { PdfDict, PdfObject, PdfStream } from "./objects"; import { asArray, asDict, asNumber, dictGet, isName, pdfNull } from "./objects"; import { parseIndirectObject, parseValue } from "./parse"; import type { XrefEntry } from "./xref"; @@ -13,12 +16,12 @@ import { readXref } from "./xref"; // The object store: resolves references (direct offset or ObjStm-compressed) against the cross-reference table xref.ts built, and walks the page tree with ISO 32000-1 7.7.3.4 inheritance (a Page node's /Resources, /MediaBox, /CropBox, /Rotate fall back to the nearest ancestor Pages node that defines them). Every fetched object is cached by object number; every decoded object stream is cached whole, since a single ObjStm is very often referenced by several of its own contained objects in short order. -export interface PdfDocument { +export interface PdfDocument extends PdfObjectResolver { readonly trailer: PdfDict; // The validated /Root catalog, exposed because every document-level reader (destinations, outline, embedded files, optional content, AcroForm) walks catalog keys — re-resolving trailer /Root at each call site would re-narrow a fact openPdfDocument already established. readonly catalog: PdfDict; - resolve: (obj: PdfObject | undefined) => PdfObject | undefined; - resolveDict: (obj: PdfObject | undefined) => PdfDict | undefined; + // The budget every decode reached through this document is charged against, when openPdfDocument was given maxDecodedBytes. + readonly decodeBudget: PdfDecodeBudget | undefined; pages: () => PdfDict[]; // The 0-based position of a page object in pages()' own order — identity-matched against the tree's resolved leaf objects (NOT the inheritance-merged copies pages() returns, which are fresh objects). This is what a destination array's page reference resolves against. pageIndex: (obj: PdfObject | undefined) => number | undefined; @@ -95,11 +98,29 @@ function decryptObject( return value; } +/** + * What opening a document may do beyond reading it. + */ +export interface OpenPdfDocumentOptions { + /** + * The most bytes stream decoding may produce over the document's whole life, charged by every decode reached through it: the cross-reference and object streams read while opening it, then every content stream, form, font program, CMap, image, soft mask, colour lookup, metadata packet and embedded file a reader decodes through `decodeStream`. The first decode that would pass it throws `PdfDecodeBudgetError` as soon as its output crosses what is left, before materialising the rest. Absent, decoding is unbounded. A non-negative safe integer; anything else throws `RangeError`. + */ + readonly maxDecodedBytes?: number; +} + export function openPdfDocument( bytes: Uint8Array, sink: PdfDiagnosticSink, + options?: OpenPdfDocumentOptions, ): PdfDocument { - const xref = readXref(bytes, sink); + const maxDecodedBytes = options?.maxDecodedBytes; + const decodeBudget = + maxDecodedBytes === undefined + ? undefined + : new PdfDecodeBudget(maxDecodedBytes); + const xref = readXref(bytes, sink, decodeBudget); + // Each stream object fetch() hands out, by its object number: a decode reached through decodeStream below names the stream it refused by this, so a budget error says which object it was. Keyed by the stream's dictionary as fetch() returns it (after decryption), which every reader holds even when it passes the dictionary and data on separately (an image XObject does), and held weakly so it goes with the objects. + const streamObjectNumbers = new WeakMap(); const objectCache = new Map(); const objStmCache = new Map(); @@ -147,7 +168,11 @@ export function openPdfDocument( objStmCache.set(streamObjNum, []); return []; } - const decoded = decodeStream(streamObj.raw, streamObj.dict, sink); + // fetchDirect has already decrypted the stream, so what is decoded (and charged) is the object stream's plaintext. + const decoded = decodeStream(streamObj.raw, streamObj.dict, sink, { + budget: decodeBudget, + objectNumber: streamObjNum, + }); const n = asNumber(dictGet(streamObj.dict, "N")) ?? 0; const first = asNumber(dictGet(streamObj.dict, "First")) ?? 0; const headerReader = new ByteReader(decoded.bytes); @@ -194,6 +219,9 @@ export function openPdfDocument( : (decodeObjectStream(entry.streamObjNum)[entry.indexInStream] ?? pdfNull()); objectCache.set(num, value); + if (value.kind === "stream") { + streamObjectNumbers.set(value.dict, num); + } return value; } @@ -220,6 +248,25 @@ export function openPdfDocument( return asDict(resolve(obj)); } + function decodeDocumentStream( + stream: PdfStream, + decodeSink: PdfDiagnosticSink, + ): DecodedStream { + return decodeStream(stream.raw, stream.dict, decodeSink, { + resolve, + budget: decodeBudget, + objectNumber: streamObjectNumbers.get(stream.dict), + objectNumberOf: (resolved) => streamObjectNumbers.get(resolved.dict), + }); + } + + function chargeDecodedBytes(stream: PdfStream, decodedBytes: number): void { + decodeBudget?.charge(decodedBytes, { + objectNumber: streamObjectNumbers.get(stream.dict), + filters: filterNames(stream.dict), + }); + } + // Returns its own type narrowed to PdfDict (never undefined) via the function's declared return type — unlike a bare `if (x === undefined) throw; ` guard on a local, this narrowing is a real declared type, so it still holds inside the nested `pages()` closure below, which TypeScript's control-flow analysis would not otherwise carry across a function boundary. function requireCatalog(dict: PdfDict | undefined): PdfDict { if (dict === undefined || !isName(dictGet(dict, "Type"), "Catalog")) { @@ -326,8 +373,11 @@ export function openPdfDocument( return { trailer: xref.trailer, catalog, + decodeBudget, resolve, resolveDict, + decodeStream: decodeDocumentStream, + chargeDecodedBytes, pages, pageIndex, }; diff --git a/packages/pdf-codec/src/filters.test.ts b/packages/pdf-codec/src/filters.test.ts index 2f5bcc2d22..1b91fcb3ab 100644 --- a/packages/pdf-codec/src/filters.test.ts +++ b/packages/pdf-codec/src/filters.test.ts @@ -485,12 +485,9 @@ describe("decodeStream: JBIG2Decode", () => { BitsPerComponent: pdfNum(1), ColorSpace: pdfName("DeviceGray"), }); - const result = decodeStream( - jbig2FixtureBytes(symbols.stream), - dict, - sink, + const result = decodeStream(jbig2FixtureBytes(symbols.stream), dict, sink, { resolve, - ); + }); expect(diagnostics).toEqual([]); expect(Array.from(result.bytes)).toEqual(expectedFilterBytes(symbols)); }); diff --git a/packages/pdf-codec/src/filters.ts b/packages/pdf-codec/src/filters.ts index 9b961879f7..b9a724bce1 100644 --- a/packages/pdf-codec/src/filters.ts +++ b/packages/pdf-codec/src/filters.ts @@ -1,9 +1,11 @@ -import { inflateTolerant } from "./bytes/flate"; +import { inflateTolerant, inflateTolerantBounded } from "./bytes/flate"; +import { OutputLimitExceededError } from "./bytes/output-limit"; import { isAsciiWhitespace } from "./bytes/reader"; +import type { PdfDecodeBudget, PdfDecodeSource } from "./decode-budget"; import type { PdfDiagnosticSink } from "./diagnostics"; -import { decodeCcittFax } from "./image/ccitt"; +import { CCITT_DEFAULT_COLUMNS, decodeCcittFax } from "./image/ccitt"; import { decodeJbig2Embedded } from "./image/jbig2"; -import type { PdfDict, PdfObject } from "./objects"; +import type { PdfDict, PdfObject, PdfStream } from "./objects"; import { asArray, asBool, asDict, asName, asNumber, dictGet } from "./objects"; import { applyPredictor, readPredictorParams } from "./predictors"; @@ -14,13 +16,23 @@ const BYTE_MASK = 0xff; // an all-ones byte: used both to bit-invert a byte (XOR class GrowableBytes { #buffer: Uint8Array; #length = 0; - - // `capacity` is a first guess at the output's length (at least one byte); a wrong guess costs only doublings. - constructor(capacity: number) { - this.#buffer = new Uint8Array(Math.max(1, capacity)); + readonly #maxLength: number | undefined; + + // `capacity` is a first guess at the output's length (at least one byte); a wrong guess costs only doublings. `maxLength`, when given, is the most bytes the output may hold: the push that would add one more throws OutputLimitExceededError, so a decoder stops at the limit rather than after finishing. + constructor(capacity: number, maxLength: number | undefined) { + this.#buffer = new Uint8Array( + Math.max( + 1, + maxLength === undefined ? capacity : Math.min(capacity, maxLength), + ), + ); + this.#maxLength = maxLength; } push(byte: number): void { + if (this.#length === this.#maxLength) { + throw new OutputLimitExceededError(this.#maxLength); + } if (this.#length === this.#buffer.length) { const grown = new Uint8Array(this.#buffer.length * 2); grown.set(this.#buffer); @@ -51,40 +63,91 @@ export type PdfIndirectResolver = ( obj: PdfObject | undefined, ) => PdfObject | undefined; +/** + * What a decode may consult beyond the stream itself. + */ +export interface DecodeStreamOptions { + /** Follows an indirect /DecodeParms entry (JBIG2Decode's /JBIG2Globals). */ + readonly resolve?: PdfIndirectResolver; + /** Charges every filter stage's output against this budget, refusing with `PdfDecodeBudgetError` the first stage that would pass it. Absent, decoding is unbounded, exactly as before budgets existed. */ + readonly budget?: PdfDecodeBudget; + /** The stream's indirect object number, named in a `PdfDecodeBudgetError`. */ + readonly objectNumber?: number; + /** The object number of a stream `resolve` hands out (the /JBIG2Globals stream), named in a `PdfDecodeBudgetError` refusing it. */ + readonly objectNumberOf?: (stream: PdfStream) => number | undefined; +} + // Runs a stream's raw bytes through its /Filter chain (a single name or an array of names, with /DecodeParms supplying per-filter parameters in the same single-or-array shape). Recoverable per-filter issues (an unresolvable /Predictor, an unimplemented filter) degrade with a diagnostic and stop the chain rather than throwing — the caller decides whether the partially- or un-decoded result is still useful (e.g. a DCTDecode image's bytes are perfectly usable as-is). +// +// With a budget, each stage decodes through a bounded variant that stops as soon as its output would pass what the budget has left (Flate, LZW, RunLength and ASCII85 incrementally; ASCIIHex from its exact output size, counted before allocating it; CCITTFax and JBIG2 from their declared or worst-case output, refused before decoding when that passes the budget or cannot be known), and its output is charged once it exists. A /Predictor applied after Flate or LZW is not charged again: it rewrites the stage's output in place of it, never longer than it. DCTDecode and JPXDecode stop the chain undecoded here, so they cost nothing at this point; whoever decodes their pixels charges for them (src/images-read.ts for JPEG 2000, src/raster.ts for a JPEG handed to a rasteriser). Without a budget every stage runs exactly the unbounded code it always has, so the output is byte-for-byte what it was. export function decodeStream( raw: Uint8Array, dict: PdfDict, sink: PdfDiagnosticSink, - resolve?: PdfIndirectResolver, + options?: DecodeStreamOptions, ): DecodedStream { const filters = filterNames(dict); const parms = decodeParmsList(dict, filters.length); + const budget = options?.budget; + const source: PdfDecodeSource = { + objectNumber: options?.objectNumber, + filters, + }; + // One filter stage under the budget: its decoder is given what the budget has left as its output limit, a decoder that would pass it is refused through the budget (spending it), and the output that stayed within it is charged. + const charged = ( + decode: (maxOutputBytes: number | undefined) => Uint8Array, + ): Uint8Array => { + if (budget === undefined) { + return decode(undefined); + } + let output: Uint8Array; + try { + output = decode(budget.remainingBytes); + } catch (error) { + if (error instanceof OutputLimitExceededError) { + budget.refuse(source); + } + throw error; + } + budget.charge(output.length, source); + return output; + }; let bytes = raw; let jbig2: DecodedStream["jbig2"] | undefined; for (let i = 0; i < filters.length; i++) { const filter = filters[i]!; const parm = parms[i]; + const input = bytes; if (filter === "FlateDecode" || filter === "Fl") { - bytes = applyPredictorIfPresent(inflateTolerant(bytes).bytes, parm, sink); + bytes = applyPredictorIfPresent( + charged( + (max) => + (max === undefined + ? inflateTolerant(input) + : inflateTolerantBounded(input, max) + ).bytes, + ), + parm, + sink, + ); } else if (filter === "LZWDecode" || filter === "LZW") { const earlyChange = (asNumber(parm ? dictGet(parm, "EarlyChange") : undefined) ?? 1) !== 0; bytes = applyPredictorIfPresent( - lzwDecode(bytes, earlyChange, sink), + charged((max) => lzwDecode(input, earlyChange, sink, max)), parm, sink, ); } else if (filter === "ASCII85Decode" || filter === "A85") { - bytes = ascii85Decode(bytes); + bytes = charged((max) => ascii85Decode(input, max)); } else if (filter === "ASCIIHexDecode" || filter === "AHx") { - bytes = asciiHexDecode(bytes); + bytes = charged((max) => asciiHexDecode(input, max)); } else if (filter === "RunLengthDecode" || filter === "RL") { - bytes = runLengthDecode(bytes); + bytes = charged((max) => runLengthDecode(input, max)); } else if (filter === "CCITTFaxDecode" || filter === "CCF") { - bytes = ccittFaxDecode(bytes, parm, dict, sink); + bytes = charged((max) => ccittFaxDecode(input, parm, dict, sink, max)); } else if (filter === "JBIG2Decode") { - const decoded = jbig2Decode(bytes, parm, dict, sink, resolve); + const decoded = jbig2Decode(input, parm, dict, sink, options, source); if (decoded === undefined) { return { bytes, remainingFilter: "JBIG2Decode" }; } @@ -120,20 +183,36 @@ function applyPredictorIfPresent( // /Rows falls back to the stream dictionary's own /Height because Table 11 defaults /Rows to 0 ("decode until the data runs out"), and a real producer very often leaves it there and lets the image dictionary carry the row count — resolving it here means the decoder gets a definite row count and stops on it rather than reading whatever trailing bits an encoder left behind. // // /EndOfLine, /EndOfBlock, and /DamagedRowsBeforeError are deliberately not consulted: the decoder handles an EOL wherever one actually appears rather than being told in advance whether to expect one, stops at an end-of-block marker or at the declared row count whichever comes first, and reports damage through the diagnostic sink rather than switching between "throw" and "keep going" on a per-document count. +// +// With `maxOutputBytes`, the output is bounded before decoding rather than incrementally, since the decoder produces whole rows of a width it knows up front: a declared row count gives the exact output (the decoder pads a short image out to it), and with none every row costs at least one bit of input (a two-dimensional V0 code, or a pass straight to the end of an unchanged row), so the input's bit count bounds the rows. Either bound passing the limit refuses the stream before any row is decoded. function ccittFaxDecode( data: Uint8Array, parm: PdfDict | undefined, dict: PdfDict, sink: PdfDiagnosticSink, + maxOutputBytes: number | undefined, ): Uint8Array { const parmGet = (key: string): PdfObject | undefined => parm !== undefined ? dictGet(parm, key) : undefined; const rows = asNumber(parmGet("Rows")) ?? asNumber(dictGet(dict, "Height") ?? dictGet(dict, "H")); + const columns = asNumber(parmGet("Columns")); + if (maxOutputBytes !== undefined) { + const bytesPerRow = Math.ceil( + Math.max(0, columns ?? CCITT_DEFAULT_COLUMNS) / BITS_PER_BYTE, + ); + const rowBound = + rows !== undefined && rows > 0 + ? Math.ceil(rows) + : data.length * BITS_PER_BYTE; + if (bytesPerRow * rowBound > maxOutputBytes) { + throw new OutputLimitExceededError(maxOutputBytes); + } + } return decodeCcittFax(data, { k: asNumber(parmGet("K")), - columns: asNumber(parmGet("Columns")), + columns, rows, blackIs1: asBool(parmGet("BlackIs1")), encodedByteAlign: asBool(parmGet("EncodedByteAlign")), @@ -148,30 +227,44 @@ function ccittFaxDecode( // Two polarity/sizing details, both of them PDF's rather than JBIG2's, and both handled here so src/image/jbig2.ts stays free of PDF knowledge. First, JBIG2 codes a black pixel as a 1 bit (T.88 3.29) while a PDF 1-bit /DeviceGray image reads 0 as black, so the decoded bitmap is inverted on the way out — exactly the convention CCITTFaxDecode reaches through its own /BlackIs1 defaulting to false. Second, the image dictionary's own /Width and /Height are authoritative over the page information segment's, which is also the only way a JBIG2 page of "unknown" (striped) height resolves at all. // // Returns undefined when the stream uses a JBIG2 feature this decoder does not implement, or is malformed. That degrades exactly like an unimplemented filter: the caller gets the still-encoded bytes back with remainingFilter set, skips the image, and the rest of the page still reads. On success it returns the decoded samples beside the globals segments it consumed, so decodeStream can capture the verbatim-re-embedding pair (see DecodedStream.jbig2). +// +// Under a budget the output is charged before decoding, from the image dictionary's /Width and /Height (the size the decoder produces, packed one bit per pixel), and a stream without them is refused, since only the page information segment inside the data would say how large it is. The /JBIG2Globals stream is decoded through decodeStream with the same budget, so it pays for its own decoding. The decoder's intermediate bitmaps (symbol dictionaries, regions) are bounded by its own per-bitmap ceiling (MAX_JBIG2_BITMAP_PIXELS) rather than charged. function jbig2Decode( data: Uint8Array, parm: PdfDict | undefined, dict: PdfDict, sink: PdfDiagnosticSink, - resolve: PdfIndirectResolver | undefined, + options: DecodeStreamOptions | undefined, + source: PdfDecodeSource, ): | { bytes: Uint8Array; globals: Uint8Array | undefined; } | undefined { + const resolve = options?.resolve; + const budget = options?.budget; + const width = asNumber(dictGet(dict, "Width") ?? dictGet(dict, "W")); + const height = asNumber(dictGet(dict, "Height") ?? dictGet(dict, "H")); + if (budget !== undefined) { + if (width === undefined || height === undefined) { + return budget.refuse(source); + } + budget.charge( + Math.ceil(Math.max(0, width) / BITS_PER_BYTE) * Math.max(0, height), + source, + ); + } const globalsObj = parm !== undefined ? dictGet(parm, "JBIG2Globals") : undefined; const resolvedGlobals = resolve !== undefined ? resolve(globalsObj) : globalsObj; let globals: Uint8Array | undefined; if (resolvedGlobals?.kind === "stream") { - globals = decodeStream( - resolvedGlobals.raw, - resolvedGlobals.dict, - sink, - resolve, - ).bytes; + globals = decodeStream(resolvedGlobals.raw, resolvedGlobals.dict, sink, { + ...options, + objectNumber: options?.objectNumberOf?.(resolvedGlobals), + }).bytes; } else if (globalsObj !== undefined) { sink({ code: "pdf/jbig2-degraded", @@ -184,8 +277,8 @@ function jbig2Decode( try { const image = decodeJbig2Embedded(data, { globals, - width: asNumber(dictGet(dict, "Width") ?? dictGet(dict, "W")), - height: asNumber(dictGet(dict, "Height") ?? dictGet(dict, "H")), + width, + height, onWarning: (message) => { sink({ code: "pdf/jbig2-degraded", severity: "warning", message }); }, @@ -205,7 +298,12 @@ function jbig2Decode( } } -function filterNames(dict: PdfDict): string[] { +/** + * The names of a stream's /Filter chain in order: one name, an array of names, or none. + * @param dict - the stream dictionary + * @returns the filter names, empty for an unfiltered stream + */ +export function filterNames(dict: PdfDict): string[] { const filterObj = dictGet(dict, "Filter") ?? dictGet(dict, "F"); if (filterObj === undefined) { return []; @@ -276,12 +374,15 @@ function concatTwo( } // `earlyChange` mirrors the filter's own /EarlyChange DecodeParms entry (default true): when set, the code width grows one code sooner than the dictionary size alone would demand, matching how essentially every real-world PDF/TIFF encoder actually writes the bitstream. +// +// `maxOutputBytes`, when given, stops the decode with OutputLimitExceededError at the first byte past it (as for ascii85Decode, asciiHexDecode and runLengthDecode below). export function lzwDecode( data: Uint8Array, earlyChange: boolean, sink: PdfDiagnosticSink, + maxOutputBytes?: number, ): Uint8Array { - const out = new GrowableBytes(data.length); + const out = new GrowableBytes(data.length, maxOutputBytes); let dict = initialLzwDictionary(); let nextCode = LZW_FIRST_NEW_CODE; let codeWidth = LZW_INITIAL_CODE_WIDTH; @@ -385,8 +486,9 @@ function ascii85GroupBytes( export function ascii85Decode( data: Uint8Array, + maxOutputBytes?: number, ): Uint8Array { - const out = new GrowableBytes(data.length); + const out = new GrowableBytes(data.length, maxOutputBytes); let tuple: number[] = []; let i = 0; if ( @@ -460,8 +562,9 @@ function hexDigitValue(byte: number): number | undefined { export function asciiHexDecode( data: Uint8Array, + maxOutputBytes?: number, ): Uint8Array { - // Two passes, so the output is sized exactly and no digit is held anywhere but the input: the first counts the digits before the terminator, the second packs them two to a byte (an odd last digit as the high nibble of a byte whose low nibble is 0). + // Two passes, so the output is sized exactly (and refused against `maxOutputBytes` before it is allocated) and no digit is held anywhere but the input: the first counts the digits before the terminator, the second packs them two to a byte (an odd last digit as the high nibble of a byte whose low nibble is 0). let digitCount = 0; for (const byte of data) { if (byte === ASCII_HEX_TERMINATOR) { @@ -471,7 +574,11 @@ export function asciiHexDecode( digitCount++; } } - const out = new Uint8Array(Math.ceil(digitCount / 2)); + const outputLength = Math.ceil(digitCount / 2); + if (maxOutputBytes !== undefined && outputLength > maxOutputBytes) { + throw new OutputLimitExceededError(maxOutputBytes); + } + const out = new Uint8Array(outputLength); let digit = 0; for (const byte of data) { if (byte === ASCII_HEX_TERMINATOR) { @@ -495,8 +602,9 @@ const RUN_LENGTH_REPEAT_COUNT_BASE = 257; // one past the maximum byte value (25 export function runLengthDecode( data: Uint8Array, + maxOutputBytes?: number, ): Uint8Array { - const out = new GrowableBytes(data.length); + const out = new GrowableBytes(data.length, maxOutputBytes); let i = 0; while (i < data.length) { const length = data[i]!; diff --git a/packages/pdf-codec/src/font-read-type0.test.ts b/packages/pdf-codec/src/font-read-type0.test.ts index c4a8b1be54..6d20e2ddee 100644 --- a/packages/pdf-codec/src/font-read-type0.test.ts +++ b/packages/pdf-codec/src/font-read-type0.test.ts @@ -13,6 +13,7 @@ import { pdfStream, } from "./objects"; import { buildCmapTable, buildSfnt } from "./test-support/sfnt"; +import { unbudgetedResolver } from "./test-support/resolver"; function collectDiagnostics(): { sink: PdfDiagnosticSink; @@ -36,7 +37,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } describe("createFontResolver: composite (Type0) fonts", () => { diff --git a/packages/pdf-codec/src/font-read.test.ts b/packages/pdf-codec/src/font-read.test.ts index 05dcb23827..0f2e2754f3 100644 --- a/packages/pdf-codec/src/font-read.test.ts +++ b/packages/pdf-codec/src/font-read.test.ts @@ -20,6 +20,7 @@ import { buildPostV3Table, buildSfnt, } from "./test-support/sfnt"; +import { unbudgetedResolver } from "./test-support/resolver"; function collectDiagnostics(): { sink: PdfDiagnosticSink; @@ -43,7 +44,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } describe("createFontResolver: simple fonts", () => { diff --git a/packages/pdf-codec/src/font-read.ts b/packages/pdf-codec/src/font-read.ts index 703759aa05..06e16df143 100644 --- a/packages/pdf-codec/src/font-read.ts +++ b/packages/pdf-codec/src/font-read.ts @@ -7,7 +7,6 @@ import { import { parseToUnicodeCMap } from "./cmap"; import type { ToUnicodeCMap } from "./cmap"; import type { PdfDiagnosticSink } from "./diagnostics"; -import { decodeStream } from "./filters"; import { namedEncodingGlyphName, winAnsiGlyphName } from "./encoding"; import { symbolGlyphName, @@ -119,7 +118,7 @@ function readFontProgram( if (stream?.kind !== "stream") { continue; } - const decoded = decodeStream(stream.raw, stream.dict, context.sink); + const decoded = context.resolver.decodeStream(stream, context.sink); const encoding = readFontProgramEncoding(decoded.bytes, baseFont); if (encoding !== undefined) { return encoding; @@ -167,7 +166,7 @@ function nameOfFontStream( if (read === undefined) { read = { name: readFontProgramName( - decodeStream(stream.raw, stream.dict, context.sink).bytes, + context.resolver.decodeStream(stream, context.sink).bytes, ), }; PROGRAM_NAMES.set(stream, read); @@ -223,7 +222,7 @@ function digestOfFontStream( let digest = PROGRAM_DIGESTS.get(stream); if (digest === undefined) { digest = programDigest( - decodeStream(stream.raw, stream.dict, context.sink).bytes, + context.resolver.decodeStream(stream, context.sink).bytes, ); PROGRAM_DIGESTS.set(stream, digest); } @@ -276,7 +275,7 @@ function readToUnicodeCMap( if (stream?.kind !== "stream") { return undefined; } - const decoded = decodeStream(stream.raw, stream.dict, context.sink); + const decoded = context.resolver.decodeStream(stream, context.sink); return parseToUnicodeCMap(decoded.bytes, context.sink); } @@ -699,7 +698,7 @@ function cidToGlyphId( if (map.kind !== "stream") { return undefined; } - const bytes = decodeStream(map.raw, map.dict, context.sink).bytes; + const bytes = context.resolver.decodeStream(map, context.sink).bytes; return (cid) => { const at = cid * CID_TO_GID_ENTRY_SIZE; if (at + 1 >= bytes.length) { diff --git a/packages/pdf-codec/src/image/ccitt.ts b/packages/pdf-codec/src/image/ccitt.ts index 61a2d95bc3..889d6e0a4f 100644 --- a/packages/pdf-codec/src/image/ccitt.ts +++ b/packages/pdf-codec/src/image/ccitt.ts @@ -617,13 +617,13 @@ export interface CcittFaxImage { readonly rows: number; } -const DEFAULT_COLUMNS = 1728; // ISO 32000-1 Table 11, and the standard fax scan line width T.4 is written around +export const CCITT_DEFAULT_COLUMNS = 1728; // ISO 32000-1 Table 11, and the standard fax scan line width T.4 is written around export function decodeCcittFax( data: Uint8Array, options: CcittFaxOptions = {}, ): CcittFaxImage { - const columns = options.columns ?? DEFAULT_COLUMNS; + const columns = options.columns ?? CCITT_DEFAULT_COLUMNS; const requestedRows = options.rows ?? 0; const k = options.k ?? 0; const blackBit = options.blackIs1 === true ? 1 : 0; diff --git a/packages/pdf-codec/src/image/jpeg2000.ts b/packages/pdf-codec/src/image/jpeg2000.ts index b43f814d51..8ea16d54e1 100644 --- a/packages/pdf-codec/src/image/jpeg2000.ts +++ b/packages/pdf-codec/src/image/jpeg2000.ts @@ -78,6 +78,8 @@ export interface Jpeg2000DecodeOptions { readonly maxWidth?: number; readonly maxHeight?: number; readonly maxPixels?: number; + // Called once the size bounds above have passed, with the bytes the decoder is about to allocate for its sample planes (one 32-bit sample per pixel per component), before allocating them; a throw from it stops the decode there. How a caller charges a JPEG 2000 image against a budget of its own before paying for it. + readonly onSampleAllocation?: (bytes: number) => void; } export interface Jpeg2000Image { @@ -601,6 +603,9 @@ export function decodeJpeg2000( if (first === undefined) { throw new Jpeg2000ParseError("the codestream declares no components"); } + options.onSampleAllocation?.( + width * height * componentCount * Int32Array.BYTES_PER_ELEMENT, + ); const planes: Int32Array[] = Array.from( { length: componentCount }, () => new Int32Array(width * height), diff --git a/packages/pdf-codec/src/images-read-decode-differential.test.ts b/packages/pdf-codec/src/images-read-decode-differential.test.ts index 1407fac6d6..0505685850 100644 --- a/packages/pdf-codec/src/images-read-decode-differential.test.ts +++ b/packages/pdf-codec/src/images-read-decode-differential.test.ts @@ -28,6 +28,7 @@ import { import { JBIG2_FIXTURES, jbig2FixtureBytes } from "./test-support/jbig2"; import { referenceDecodeImageSamples } from "./test-support/reference-image-decode"; import { randomBytes, seededRandom } from "./test-support/seeded-random"; +import { unbudgetedResolver } from "./test-support/resolver"; // The current sample decode against the per-sample-array decode it replaced (test-support/reference-image-decode.ts): every colour space, bit depth and /Decode setting the package decodes, over row widths that do and do not fill their last byte, and over streams that are exactly as long as the image, shorter (mid-row and mid-pixel included) and longer. Both must produce the same raster and so the same PNG bytes, since an image's id is the crc32 of those bytes. @@ -302,7 +303,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } function asciiHexEncode(bytes: Uint8Array): Uint8Array { @@ -509,9 +510,9 @@ function referenceRead( inverted: boolean, alpha: Uint8Array | undefined, ): Uint8Array { - const decoded = decodeStream(raw, dict, NOOP_SINK, (obj) => - resolver.resolve(obj), - ); + const decoded = decodeStream(raw, dict, NOOP_SINK, { + resolve: (obj) => resolver.resolve(obj), + }); const image = referenceDecodeImageSamples( decoded.bytes, width, diff --git a/packages/pdf-codec/src/images-read-filters.test.ts b/packages/pdf-codec/src/images-read-filters.test.ts index d883054246..e6ef2dfec8 100644 --- a/packages/pdf-codec/src/images-read-filters.test.ts +++ b/packages/pdf-codec/src/images-read-filters.test.ts @@ -19,6 +19,7 @@ import { ccittFixtureBytes, } from "./test-support/ccitt-fax"; import {} from "./test-support/jpeg2000"; +import { unbudgetedResolver } from "./test-support/resolver"; function collectDiagnostics(): { sink: PdfDiagnosticSink; @@ -38,7 +39,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } const EMPTY_RESOLVER = makeResolver(new Map()); diff --git a/packages/pdf-codec/src/images-read-jpx-b.test.ts b/packages/pdf-codec/src/images-read-jpx-b.test.ts index aa823be345..ba934cd4a6 100644 --- a/packages/pdf-codec/src/images-read-jpx-b.test.ts +++ b/packages/pdf-codec/src/images-read-jpx-b.test.ts @@ -18,6 +18,7 @@ import { import {} from "./test-support/ccitt-fax"; import { JBIG2_FIXTURES, jbig2FixtureBytes } from "./test-support/jbig2"; import {} from "./test-support/jpeg2000"; +import { unbudgetedResolver } from "./test-support/resolver"; function collectDiagnostics(): { sink: PdfDiagnosticSink; @@ -37,7 +38,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } function u16be(n: number): number[] { diff --git a/packages/pdf-codec/src/images-read-jpx.test.ts b/packages/pdf-codec/src/images-read-jpx.test.ts index 91580c9b6d..3c039c67c9 100644 --- a/packages/pdf-codec/src/images-read-jpx.test.ts +++ b/packages/pdf-codec/src/images-read-jpx.test.ts @@ -23,6 +23,7 @@ import { jpeg2000FixtureBytes, jpeg2000FixtureSamples, } from "./test-support/jpeg2000"; +import { unbudgetedResolver } from "./test-support/resolver"; function collectDiagnostics(): { sink: PdfDiagnosticSink; @@ -42,7 +43,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } const EMPTY_RESOLVER = makeResolver(new Map()); diff --git a/packages/pdf-codec/src/images-read.test.ts b/packages/pdf-codec/src/images-read.test.ts index 1f697d797a..8526ba64b5 100644 --- a/packages/pdf-codec/src/images-read.test.ts +++ b/packages/pdf-codec/src/images-read.test.ts @@ -18,6 +18,7 @@ import { } from "./objects"; import {} from "./test-support/ccitt-fax"; import {} from "./test-support/jpeg2000"; +import { unbudgetedResolver } from "./test-support/resolver"; function collectDiagnostics(): { sink: PdfDiagnosticSink; @@ -37,7 +38,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } function u16be(n: number): number[] { diff --git a/packages/pdf-codec/src/images-read.ts b/packages/pdf-codec/src/images-read.ts index fa5b2f5f06..218237a7c0 100644 --- a/packages/pdf-codec/src/images-read.ts +++ b/packages/pdf-codec/src/images-read.ts @@ -7,10 +7,10 @@ import { } from "byte-codec"; import type { Jpeg2000Image } from "./image/jpeg2000"; import { decodeJpeg2000 } from "./image/jpeg2000"; +import { PdfDecodeBudgetError } from "./decode-budget"; import type { PdfDiagnosticSink } from "./diagnostics"; -import { decodeStream } from "./filters"; import type { PdfObjectResolver } from "./interpret-types"; -import type { PdfDict, PdfObject } from "./objects"; +import type { PdfDict, PdfObject, PdfStream } from "./objects"; import { asArray, asBool, asName, asNumber, dictGet } from "./objects"; // Turns an Image XObject (or an inline image's dict+data) into bytes ready to store as a LayoutImageAsset. A DCTDecode (JPEG) image passes through completely undecoded — its compressed bytes ARE the deliverable, exactly mirroring the write path's own lossless JPEG passthrough. Everything else is decoded to raw samples and re-encoded as PNG via byte-codec's own encodePng, since LayoutImageAsset only ever stores 'png' or 'jpeg' — byte-codec's encodePng is this package's only PNG encoder: a caller's own imageId is a crc32 of the PNG bytes it originally supplied (see documents.js's registerImageBytes), so this module's re-encode of the same raster data must run through the identical function a caller would have used, not a second, independently-maintained copy that could silently diverge from it. @@ -107,7 +107,7 @@ function resolveColorSpace( const lookupObj = resolver.resolve(arr[3]); let lookup: Uint8Array; if (lookupObj?.kind === "stream") { - lookup = decodeStream(lookupObj.raw, lookupObj.dict, sink).bytes; + lookup = resolver.decodeStream(lookupObj, sink).bytes; } else if (lookupObj?.kind === "string") { lookup = lookupObj.bytes; } else { @@ -399,7 +399,7 @@ function readSoftMaskAlpha( if (smaskObj?.kind !== "stream") { return undefined; } - const decoded = decodeStream(smaskObj.raw, smaskObj.dict, sink); + const decoded = resolver.decodeStream(smaskObj, sink); if (decoded.remainingFilter !== undefined) { return undefined; // an encoded (e.g. DCT) soft mask is out of scope — degrade to no alpha rather than guess } @@ -458,11 +458,12 @@ function jpeg2000ChannelKind( } function readJpeg2000Image( - dict: PdfDict, + stream: PdfStream, raw: Uint8Array, resolver: Readonly, sink: PdfDiagnosticSink, ): ExtractedPdfImage | undefined { + const dict = stream.dict; let image: Jpeg2000Image; try { image = decodeJpeg2000(raw, { @@ -473,8 +474,16 @@ function readJpeg2000Image( maxWidth: PNG_MAX_DIMENSION, maxHeight: PNG_MAX_DIMENSION, maxPixels: PNG_MAX_PIXELS, + // The decoded samples are the JPEG 2000 stage's output, which decodeStream leaves undecoded, so they are charged here, before the decoder allocates them. + onSampleAllocation: (bytes) => { + resolver.chargeDecodedBytes(stream, bytes); + }, }); } catch (error) { + // A budget refusal is the document's, not this image's: it stops the read rather than degrading to "skip this image". + if (error instanceof PdfDecodeBudgetError) { + throw error; + } sink({ code: "image/jpx-undecodable", severity: "warning", @@ -568,6 +577,8 @@ export function readImageXObject( resolver: Readonly, sink: PdfDiagnosticSink, ): ExtractedPdfImage | undefined { + // The resolver knows a stream by its dictionary, so an image XObject's dictionary names its object in a budget error, and an inline image's, which belongs to no object, names it as inline. + const stream: PdfStream = { kind: "stream", dict, raw }; if (asBool(dictGet(dict, "ImageMask") ?? dictGet(dict, "IM")) === true) { sink({ code: "image/mask-unsupported", @@ -578,8 +589,8 @@ export function readImageXObject( return undefined; } - // The resolver is threaded in here, and only here, because JBIG2Decode's own /JBIG2Globals DecodeParms entry is a stream that a producer essentially always writes as an indirect reference — no other filter this codec implements has a parameter that needs dereferencing. - const decoded = decodeStream(raw, dict, sink, (obj) => resolver.resolve(obj)); + // Decoded through the resolver, which also follows JBIG2Decode's own /JBIG2Globals DecodeParms entry, a stream a producer essentially always writes as an indirect reference. + const decoded = resolver.decodeStream(stream, sink); const jbig2Original: ExtractedPdfImage["original"] = decoded.jbig2 === undefined ? undefined @@ -611,7 +622,7 @@ export function readImageXObject( }; } if (decoded.remainingFilter === "JPXDecode") { - return readJpeg2000Image(dict, decoded.bytes, resolver, sink); + return readJpeg2000Image(stream, decoded.bytes, resolver, sink); } if (decoded.remainingFilter !== undefined) { return undefined; // decodeStream already raised 'pdf/unsupported-filter' @@ -698,3 +709,18 @@ export function readImageXObject( ...(jbig2Original !== undefined ? { original: jbig2Original } : {}), }; } + +// A JPEG passes through readImageXObject undecoded (its DCTDecode stage stops the filter chain), so a rasteriser renderPdfPage hands it to is what decodes it: its samples, one byte per component per pixel as the JPEG's own header declares them, are that stage's output, and are charged against the document's decode budget before the image is handed over. A PNG needs no charge, since readImageXObject decoded, and charged, its samples itself. +export function chargeRasterisedJpeg( + image: ExtractedPdfImage, + stream: PdfStream, + resolver: Readonly, +): void { + if (image.format === "jpeg") { + const info = readJpegInfo(image.bytes); + resolver.chargeDecodedBytes( + stream, + info.width * info.height * info.components, + ); + } +} diff --git a/packages/pdf-codec/src/index.ts b/packages/pdf-codec/src/index.ts index eb81bb22d6..7e7adc1f72 100644 --- a/packages/pdf-codec/src/index.ts +++ b/packages/pdf-codec/src/index.ts @@ -25,6 +25,9 @@ export { PdfParseError, PdfPasswordRequiredError, } from "./diagnostics"; +// The decode budget (ReadPdfOptions.maxDecodedBytes, RenderPdfPageOptions.maxDecodedBytes): the error a read throws at the first decode that would pass it, and the budget itself, for a caller decoding a stream through `pdf-codec/filters`' decodeStream. +export type { PdfDecodeSource } from "./decode-budget"; +export { PdfDecodeBudget, PdfDecodeBudgetError } from "./decode-budget"; export type { WinAnsiSubstitution } from "./winansi"; export { PdfBytesSchema, pdfCodec } from "./codec"; diff --git a/packages/pdf-codec/src/interpret-colour.test.ts b/packages/pdf-codec/src/interpret-colour.test.ts index 857cd34f0c..1227e4441e 100644 --- a/packages/pdf-codec/src/interpret-colour.test.ts +++ b/packages/pdf-codec/src/interpret-colour.test.ts @@ -4,6 +4,7 @@ import type { FontMetricsPort, PdfObjectResolver } from "./interpret-types"; import { interpretContentStream } from "./interpret"; import type { PdfDict, PdfObject } from "./objects"; import { asDict, pdfDict, pdfName, pdfNum, pdfRef, pdfStream } from "./objects"; +import { unbudgetedResolver } from "./test-support/resolver"; function collectDiagnostics(): { sink: PdfDiagnosticSink; @@ -37,7 +38,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } const EMPTY_RESOURCES = pdfDict({}); diff --git a/packages/pdf-codec/src/interpret-operators.test.ts b/packages/pdf-codec/src/interpret-operators.test.ts index 43b89bb0d8..5f9bd12bcc 100644 --- a/packages/pdf-codec/src/interpret-operators.test.ts +++ b/packages/pdf-codec/src/interpret-operators.test.ts @@ -4,6 +4,7 @@ import type { FontMetricsPort, PdfObjectResolver } from "./interpret-types"; import { interpretContentStream } from "./interpret"; import type { PdfDict, PdfObject } from "./objects"; import { asDict, pdfDict } from "./objects"; +import { unbudgetedResolver } from "./test-support/resolver"; function collectDiagnostics(): { sink: PdfDiagnosticSink; @@ -37,7 +38,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } const EMPTY_RESOURCES = pdfDict({}); diff --git a/packages/pdf-codec/src/interpret-run-gaps-differential.test.ts b/packages/pdf-codec/src/interpret-run-gaps-differential.test.ts index 4ea5b89103..5a7cd398a2 100644 --- a/packages/pdf-codec/src/interpret-run-gaps-differential.test.ts +++ b/packages/pdf-codec/src/interpret-run-gaps-differential.test.ts @@ -10,6 +10,7 @@ import { groupPdfTextRuns, } from "./text-group"; import { seededRandom } from "./test-support/seeded-random"; +import { unbudgetedResolver } from "./test-support/resolver"; // How a producer chooses to chunk its text-showing operators must not change the words a reader gets back. The reference here places every glyph with an operator of its own at the position the producer's form puts it, so the line grouping alone decides where words and columns break; the form under test sets a whole row as one TJ under a character spacing, with adjustments that take the spacing back inside each figure and leave or widen it between figures. Both must read as the same line. @@ -34,10 +35,10 @@ const FIXED_WIDTH_METRICS: FontMetricsPort = { }), isVertical: () => false, }; -const RESOLVER = { +const RESOLVER = unbudgetedResolver({ resolve: () => undefined, resolveDict: () => undefined, -}; +}); function between(random: () => number, low: number, high: number): number { return low + random() * (high - low); diff --git a/packages/pdf-codec/src/interpret-shapes.test.ts b/packages/pdf-codec/src/interpret-shapes.test.ts index 1bef31eb45..f83794a6ee 100644 --- a/packages/pdf-codec/src/interpret-shapes.test.ts +++ b/packages/pdf-codec/src/interpret-shapes.test.ts @@ -4,6 +4,7 @@ import type { FontMetricsPort, PdfObjectResolver } from "./interpret-types"; import { interpretContentStream } from "./interpret"; import type { PdfDict, PdfObject } from "./objects"; import { asDict, pdfDict } from "./objects"; +import { unbudgetedResolver } from "./test-support/resolver"; function collectDiagnostics(): { sink: PdfDiagnosticSink; @@ -37,7 +38,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } const EMPTY_RESOURCES = pdfDict({}); diff --git a/packages/pdf-codec/src/interpret-text-runs.test.ts b/packages/pdf-codec/src/interpret-text-runs.test.ts index 6f4e1de046..ca1604c0be 100644 --- a/packages/pdf-codec/src/interpret-text-runs.test.ts +++ b/packages/pdf-codec/src/interpret-text-runs.test.ts @@ -4,6 +4,7 @@ import type { FontMetricsPort, PdfObjectResolver } from "./interpret-types"; import { interpretContentStream } from "./interpret"; import type { PdfDict, PdfObject } from "./objects"; import { asDict, pdfDict } from "./objects"; +import { unbudgetedResolver } from "./test-support/resolver"; function collectDiagnostics(): { sink: PdfDiagnosticSink; @@ -37,7 +38,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } const EMPTY_RESOURCES = pdfDict({}); diff --git a/packages/pdf-codec/src/interpret-types.ts b/packages/pdf-codec/src/interpret-types.ts index bb9e01f3e7..35d00e71d5 100644 --- a/packages/pdf-codec/src/interpret-types.ts +++ b/packages/pdf-codec/src/interpret-types.ts @@ -1,6 +1,7 @@ // The result types interpretContentStream produces, split from interpret.ts: the Extracted* paint/text/image item shapes, the glyph-advance port, and the InterpretContext the caller supplies. import type { PdfDiagnosticSink } from "./diagnostics"; -import type { PdfDict, PdfObject } from "./objects"; +import type { DecodedStream } from "./filters"; +import type { PdfDict, PdfObject, PdfStream } from "./objects"; import type { Matrix } from "./matrix"; import type { Color as LayoutColor, @@ -148,6 +149,10 @@ export interface FontMetricsPort { export interface PdfObjectResolver { resolve: (obj: PdfObject | undefined) => PdfObject | undefined; resolveDict: (obj: PdfObject | undefined) => PdfDict | undefined; + // Decodes a stream this resolver handed out (or an inline image's dictionary and data, wrapped as a stream), charging its output against the document's decode budget when it has one. Every reader decodes through this rather than calling decodeStream itself, which is what puts every decode a document reaches under its budget (decode-budget.test.ts checks that only document.ts and xref.ts, which charge the budget themselves, import it). + decodeStream: (stream: PdfStream, sink: PdfDiagnosticSink) => DecodedStream; + // Charges bytes a reader decodes outside decodeStream (a JPEG 2000 codestream's samples, a JPEG handed to a rasteriser) against the document's decode budget, before it decodes them; throws PdfDecodeBudgetError when they would pass it, and does nothing for a document with no budget. + chargeDecodedBytes: (stream: PdfStream, decodedBytes: number) => void; } export interface InterpretContext { diff --git a/packages/pdf-codec/src/interpret-xobjects.test.ts b/packages/pdf-codec/src/interpret-xobjects.test.ts index 2c68ab4965..39be05908b 100644 --- a/packages/pdf-codec/src/interpret-xobjects.test.ts +++ b/packages/pdf-codec/src/interpret-xobjects.test.ts @@ -12,6 +12,7 @@ import { pdfRef, pdfStream, } from "./objects"; +import { unbudgetedResolver } from "./test-support/resolver"; function collectDiagnostics(): { sink: PdfDiagnosticSink; @@ -45,7 +46,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } const EMPTY_RESOURCES = pdfDict({}); diff --git a/packages/pdf-codec/src/interpret.test.ts b/packages/pdf-codec/src/interpret.test.ts index 0b5a53d68f..bb5b9d3680 100644 --- a/packages/pdf-codec/src/interpret.test.ts +++ b/packages/pdf-codec/src/interpret.test.ts @@ -4,6 +4,7 @@ import type { FontMetricsPort, PdfObjectResolver } from "./interpret-types"; import { interpretContentStream } from "./interpret"; import type { PdfDict, PdfObject } from "./objects"; import { asDict, pdfDict } from "./objects"; +import { unbudgetedResolver } from "./test-support/resolver"; function collectDiagnostics(): { sink: PdfDiagnosticSink; @@ -41,7 +42,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } const EMPTY_RESOURCES = pdfDict({}); diff --git a/packages/pdf-codec/src/interpret.ts b/packages/pdf-codec/src/interpret.ts index c7db7a0a6a..ec806f4235 100644 --- a/packages/pdf-codec/src/interpret.ts +++ b/packages/pdf-codec/src/interpret.ts @@ -1,5 +1,4 @@ import { COLOR_BLACK, type Point } from "document-schema.js"; -import { decodeStream } from "./filters"; import type { Matrix } from "./matrix"; import { IDENTITY_MATRIX, @@ -476,7 +475,7 @@ function runContentStream( const formResources = context.resolver.resolveDict(dictGet(xobj.dict, "Resources")) ?? resources; - const decoded = decodeStream(xobj.raw, xobj.dict, context.sink); + const decoded = context.resolver.decodeStream(xobj, context.sink); // ISO 32000-1 8.10.2: the form executes in the graphics state in effect at this Do, as if nested inline inside an implicit q/Q — so the whole state travels inward, the text parameters (a font a preceding Tf already selected, spacing, scaling) among them, and a form whose own content omits a redundant Tf still draws in the caller's font. The recursed call binds its own `gs` local, so nothing the form changes travels back out; the text matrix is not carried because it is text object state the form's own BT resets regardless. const formState: GraphicsState = { ...gs, diff --git a/packages/pdf-codec/src/names.test.ts b/packages/pdf-codec/src/names.test.ts index 4eff47470c..f4258e712e 100644 --- a/packages/pdf-codec/src/names.test.ts +++ b/packages/pdf-codec/src/names.test.ts @@ -4,19 +4,20 @@ import type { PdfDiagnosticSink } from "./diagnostics"; import { walkNameTree } from "./names"; import type { PdfDict, PdfObject } from "./objects"; import { pdfArray, pdfDict, pdfName, pdfNum, pdfRef } from "./objects"; +import { unbudgetedResolver } from "./test-support/resolver"; // The names-tree walker (ISO 32000-1 7.9.6): flattens a name tree's /Names pairs across /Kids recursion into one ordered entry list. This is the hard prerequisite the #721 verdict calls out — one walker serves destinations, embedded files, and any later names-tree tenant (JavaScript names, etc.). Tests build synthetic PdfObject trees with an in-memory resolver so the walker's own structural rules (recursion, ordering, cycle safety, non-dict handling) are pinned independently of any full-document fixture. function resolverOf(objects: Map) { - return { - resolve(obj: PdfObject | undefined): PdfObject | undefined { - return obj?.kind === "ref" ? objects.get(obj.num) : obj; - }, + const resolve = (obj: PdfObject | undefined): PdfObject | undefined => + obj?.kind === "ref" ? objects.get(obj.num) : obj; + return unbudgetedResolver({ + resolve, resolveDict(obj: PdfObject | undefined): PdfDict | undefined { - const resolved = this.resolve(obj); + const resolved = resolve(obj); return resolved?.kind === "dict" ? resolved : undefined; }, - }; + }); } function stringObj(text: string): PdfObject { diff --git a/packages/pdf-codec/src/navigation.test.ts b/packages/pdf-codec/src/navigation.test.ts index 1687269d9c..d53458e651 100644 --- a/packages/pdf-codec/src/navigation.test.ts +++ b/packages/pdf-codec/src/navigation.test.ts @@ -20,6 +20,7 @@ import { } from "./objects"; import { readPdf } from "./read"; import { navigationClusterPdf } from "./test-support/pdf"; +import { unbudgetedResolver } from "./test-support/resolver"; // The navigation cluster (#721's core): named destinations (old-style /Dests dictionary AND /Names /Dests name tree), the /Outlines bookmark tree, and internal link annotations (/Dest direct and named, /A /GoTo actions) — all read into the LayoutDocument's destinations/outline surfaces and the internalLink item kind. A direct destination array names no destination, so the reader mints one (dest1, dest2, ...) so every internal link and outline entry targets a destinations-table name. @@ -150,7 +151,7 @@ function makeResolver( obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } // A page-index lookup that resolves any ref to its own object number — arbitrary but deterministic, and distinct enough from a small page count that a test asserting `pageIndex: N` can't be confused with a coincidental default. diff --git a/packages/pdf-codec/src/optional-content.test.ts b/packages/pdf-codec/src/optional-content.test.ts index 1b55487dc6..689e184943 100644 --- a/packages/pdf-codec/src/optional-content.test.ts +++ b/packages/pdf-codec/src/optional-content.test.ts @@ -7,6 +7,7 @@ import type { PdfDict, PdfObject } from "./objects"; import { asDict, pdfArray, pdfDict, pdfLiteralString, pdfRef } from "./objects"; import { readPdf } from "./read"; import { ocgPdf } from "./test-support/pdf-structures"; +import { unbudgetedResolver } from "./test-support/resolver"; // A resolver over a plain ref-number -> object table, matching interpret.test.ts's/navigation.test.ts's own makeResolver. function makeResolver( @@ -16,7 +17,7 @@ function makeResolver( obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } function collectDiagnostics(): { diff --git a/packages/pdf-codec/src/raster-text.ts b/packages/pdf-codec/src/raster-text.ts index ac360ae161..7413db7aae 100644 --- a/packages/pdf-codec/src/raster-text.ts +++ b/packages/pdf-codec/src/raster-text.ts @@ -31,7 +31,6 @@ import { scaleMatrix, translationMatrix, } from "./matrix"; -import { decodeStream } from "./filters"; import { EXTENT_ZERO_EPSILON, type EmbeddedProgram, @@ -58,11 +57,7 @@ function openEmbeddedProgram( if (stream?.kind !== "stream") { continue; } - const bytes = decodeStream( - stream.raw, - stream.dict, - NOOP_DIAGNOSTIC_SINK, - ).bytes; + const bytes = resolver.decodeStream(stream, NOOP_DIAGNOSTIC_SINK).bytes; // No separate bytes.length >= 3 guard: with noUncheckedIndexedAccess, an out-of-bounds index already reads as undefined, which can never strictly equal any of these three literals — a short stream already fails the chain on its own without a length check duplicating that fact. if ( bytes[0] === 0x01 && @@ -216,9 +211,8 @@ function buildTextOutlineFace( } if (cidToGidMap?.kind === "stream") { // An explicit mapping stream: 2-byte big-endian GID per CID (ISO 32000-1 9.7.4.2). CIDs past the stream's end have no entry, and a missing GID is skipped — never drawn as glyph 0, which would paint .notdef ink the file never stated. - const decodedBytes = decodeStream( - cidToGidMap.raw, - cidToGidMap.dict, + const decodedBytes = resolver.decodeStream( + cidToGidMap, NOOP_DIAGNOSTIC_SINK, ).bytes; const entries: number[] = []; diff --git a/packages/pdf-codec/src/raster.ts b/packages/pdf-codec/src/raster.ts index 7019e63c9c..dfed6bd952 100644 --- a/packages/pdf-codec/src/raster.ts +++ b/packages/pdf-codec/src/raster.ts @@ -8,10 +8,9 @@ import type { SfntFont } from "./sfnt"; import { drawTextRun, type TextOutlineFace } from "./raster-text"; import type { PdfDiagnosticSink } from "./diagnostics"; import { NOOP_DIAGNOSTIC_SINK, PdfParseError } from "./diagnostics"; -import { decodeStream } from "./filters"; import { createFontResolver } from "./font-read"; import type { FontResolverService } from "./font-read"; -import { readImageXObject } from "./images-read"; +import { chargeRasterisedJpeg, readImageXObject } from "./images-read"; import type { ExtractedEllipse, ExtractedImage, @@ -31,7 +30,7 @@ import { translationMatrix, } from "./matrix"; import { normalizeRotation, pageRotationTransform } from "./read"; -import type { PdfDict } from "./objects"; +import type { PdfDict, PdfStream } from "./objects"; import { asArray, asNumber, dictGet } from "./objects"; import { readOptionalContent } from "./optional-content"; import { concatBytes } from "./bytes/writer"; @@ -148,6 +147,8 @@ export interface RenderPdfPageOptions { readonly clipPt?: RasterRegionPt; readonly sink?: PdfDiagnosticSink; readonly signal?: AbortSignal; + // The most bytes decoding may produce while opening the document and rendering the page, as ReadPdfOptions.maxDecodedBytes describes it; a JPEG the page draws is charged its decoded samples here, since the rasteriser decodes it. The first decode that would pass it throws PdfDecodeBudgetError. Absent, decoding is unbounded. + readonly maxDecodedBytes?: number; } // Absorbs float fuzz in regionPixels below (an exact 100pt at scale 2 computing 200.00000000000003 must be 200, not 201). @@ -251,7 +252,7 @@ function readPageContentBytes( ): Uint8Array { const contentsObj = resolver.resolve(dictGet(page, "Contents")); if (contentsObj?.kind === "stream") { - return decodeStream(contentsObj.raw, contentsObj.dict, sink).bytes; + return resolver.decodeStream(contentsObj, sink).bytes; } if (contentsObj?.kind === "array") { const chunks: Uint8Array[] = []; @@ -259,7 +260,7 @@ function readPageContentBytes( const streamObj = resolver.resolve(entry); if (streamObj?.kind === "stream") { chunks.push( - decodeStream(streamObj.raw, streamObj.dict, sink).bytes, + resolver.decodeStream(streamObj, sink).bytes, new Uint8Array([NEWLINE_BYTE]), ); } @@ -305,7 +306,9 @@ export function renderPdfPage( 'no "%PDF-" header found within the first bytes of the file; this does not look like a PDF at all', ); } - const doc = openPdfDocument(pdfBytes, sink); + const doc = openPdfDocument(pdfBytes, sink, { + maxDecodedBytes: options.maxDecodedBytes, + }); const pages = doc.pages(); if (pageIndex < 0 || pageIndex >= pages.length) { throw new PdfParseError( @@ -735,6 +738,7 @@ function drawImageXObjectItem( if (image === undefined) { return; // readImageXObject has reported its own diagnostic for why these pixels are unavailable } + chargeRasterisedJpeg(image, stream, resolver); drawImage(image, item.matrix, interpretToDeviceMatrix, rasteriser); } @@ -745,10 +749,12 @@ function drawInlineImageItem( rasteriser: Readonly, sink: PdfDiagnosticSink, ): void { - const image = readImageXObject(item.dict, item.data, resolver, sink); + const stream: PdfStream = { kind: "stream", dict: item.dict, raw: item.data }; + const image = readImageXObject(stream.dict, stream.raw, resolver, sink); if (image === undefined) { return; } + chargeRasterisedJpeg(image, stream, resolver); drawImage(image, item.matrix, interpretToDeviceMatrix, rasteriser); } diff --git a/packages/pdf-codec/src/read.ts b/packages/pdf-codec/src/read.ts index b46639ac12..883acd47d2 100644 --- a/packages/pdf-codec/src/read.ts +++ b/packages/pdf-codec/src/read.ts @@ -28,7 +28,6 @@ import { PdfPageSelectionError, PdfParseError, } from "./diagnostics"; -import { decodeStream } from "./filters"; import { throwIfAborted } from "./util/abort"; import type { FontResolverService } from "./font-read"; import { createFontResolver } from "./font-read"; @@ -89,6 +88,12 @@ export interface ReadPdfOptions { * Apart from those stated losses and the renumbering a skipped outline causes, every page item is exactly what a read with the surface on produces. */ readonly surfaces?: ReadPdfSurfaces; + /** + * The most bytes stream decoding may produce over the whole read, a non-negative safe integer (anything else throws `RangeError`). Every decode the read reaches is charged against it: cross-reference and object streams while opening the document, then page content streams, form XObjects, font programs (each time one is decoded, so a program two fonts share is charged twice), `/ToUnicode` CMaps, `/CIDToGIDMap` streams, image XObjects and inline images, soft masks, `/Indexed` lookup streams, the `/Metadata` packet and embedded files. Each filter stage's output counts, so a stream under two filters pays for both. + * + * The first decode that would pass the budget throws `PdfDecodeBudgetError` (a `PdfParseError` with code `pdf/decode-budget-exceeded`, carrying `maxDecodedBytes`, the bytes already `decodedBytes`, the stream's `objectNumber` and its `filters`), and stops as soon as its output crosses what is left rather than after materialising it: Flate, LZW, RunLength and ASCII85 stop incrementally, ASCIIHex is refused from its exact size before allocating it, and CCITTFax, JBIG2 and JPEG 2000 are charged their declared output (a CCITTFax stream with no row count its worst case, a JBIG2 stream with no `/Width` and `/Height` refused) before they decode. A JPEG is passed through undecoded and costs nothing. Absent, the read decodes without a bound, exactly as it always has. + */ + readonly maxDecodedBytes?: number; } /** @@ -166,7 +171,9 @@ export function readPdf( } // Checked before openPdfDocument rather than relying solely on the page loop's per-iteration check below: the document-open phase (xref resolution, object parsing) runs before any page, and a document with an empty page tree never enters the loop at all — without this check, an already-aborted signal on such a file would return a parsed document instead of throwing. throwIfAborted(signal); - const doc = openPdfDocument(bytes, sink); + const doc = openPdfDocument(bytes, sink, { + maxDecodedBytes: options?.maxDecodedBytes, + }); const fontResolver = createFontResolver({ resolver: doc, sink }); const images: Record = {}; @@ -418,7 +425,7 @@ function readPageContentBytes( ): Uint8Array { const contentsObj = resolver.resolve(dictGet(page, "Contents")); if (contentsObj?.kind === "stream") { - return decodeStream(contentsObj.raw, contentsObj.dict, sink).bytes; + return resolver.decodeStream(contentsObj, sink).bytes; } if (contentsObj?.kind === "array") { const chunks: Uint8Array[] = []; @@ -426,7 +433,7 @@ function readPageContentBytes( const streamObj = resolver.resolve(item); if (streamObj?.kind === "stream") { chunks.push( - decodeStream(streamObj.raw, streamObj.dict, sink).bytes, + resolver.decodeStream(streamObj, sink).bytes, new Uint8Array([CONTENT_STREAM_JOIN_BYTE]), ); } @@ -752,7 +759,7 @@ function xmpPacket( if (metadataObj?.kind !== "stream") { return undefined; } - const decoded = decodeStream(metadataObj.raw, metadataObj.dict, sink); + const decoded = resolver.decodeStream(metadataObj, sink); return new TextDecoder().decode(decoded.bytes); } diff --git a/packages/pdf-codec/src/symbol-font-private-use.test.ts b/packages/pdf-codec/src/symbol-font-private-use.test.ts index 429a394a65..b0ccc06b08 100644 --- a/packages/pdf-codec/src/symbol-font-private-use.test.ts +++ b/packages/pdf-codec/src/symbol-font-private-use.test.ts @@ -27,6 +27,7 @@ import { import { buildSfnt } from "./test-support/sfnt"; import { buildNameTable } from "./test-support/sfnt-tables"; import { WINGDINGS_CHARACTERS } from "./wingdings-characters"; +import { unbudgetedResolver } from "./test-support/resolver"; const NO_DIAGNOSTICS: PdfDiagnosticSink = () => undefined; @@ -35,7 +36,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } /** A /ToUnicode CMap sending each two-byte code to the code point given. */ diff --git a/packages/pdf-codec/src/test-support/decode-budget.ts b/packages/pdf-codec/src/test-support/decode-budget.ts new file mode 100644 index 0000000000..964eef470d --- /dev/null +++ b/packages/pdf-codec/src/test-support/decode-budget.ts @@ -0,0 +1,427 @@ +import { zlibSync } from "fflate"; +import type { PdfDecodeBudget } from "../decode-budget"; +import { PdfDecodeBudgetError } from "../decode-budget"; +import { decodeStream } from "../filters"; +import type { PdfObjectResolver } from "../interpret-types"; +import type { PdfDict, PdfObject, PdfStream } from "../objects"; +import { asDict } from "../objects"; +import { enc, FixtureBuilder } from "./pdf"; + +// Encoders and fixtures for the decode-budget tests: each filter's own encoding of a run of zero bytes (the cheapest input that decodes to many bytes), and documents whose only large decode sits at one named site. + +/** + * A run of zero bytes, FlateDecode-encoded: a few hundred bytes of input for a megabyte of output. + * @param size - the decoded length + * @returns the zlib-framed stream + */ +export function flateZeros(size: number): Uint8Array { + return zlibSync(new Uint8Array(size)); +} + +// RunLengthDecode's longest repeat run (ISO 32000-1 7.4.5): a length byte of 129 repeats the next byte 257 - 129 = 128 times. +const RUN_LENGTH_LONGEST_REPEAT = 128; +const RUN_LENGTH_REPEAT_BASE = 257; +const RUN_LENGTH_EOD = 128; + +/** + * A run of zero bytes, RunLengthDecode-encoded as repeat runs, then the end-of-data marker. + * @param size - the decoded length + * @returns the encoded bytes + */ +export function runLengthZeros(size: number): Uint8Array { + const out: number[] = []; + for (let left = size; left > 0; left -= RUN_LENGTH_LONGEST_REPEAT) { + const run = Math.min(left, RUN_LENGTH_LONGEST_REPEAT); + if (run === 1) { + out.push(0, 0); // a one-byte literal run: length byte 0, then the byte + } else { + out.push(RUN_LENGTH_REPEAT_BASE - run, 0); + } + } + out.push(RUN_LENGTH_EOD); + return new Uint8Array(out); +} + +// ASCII85's 'z' stands for a whole group of four zero bytes. +const ASCII85_GROUP_BYTES = 4; + +/** + * A run of zero bytes, ASCII85Decode-encoded with 'z' groups (and `!` digits for a final partial group), ending in the "~" end-of-data marker and its closing bracket. + * @param size - the decoded length + * @returns the encoded bytes + */ +export function ascii85Zeros(size: number): Uint8Array { + const groups = Math.floor(size / ASCII85_GROUP_BYTES); + const rest = size % ASCII85_GROUP_BYTES; + // A partial group of n bytes is written as n + 1 digits; zero bytes are all '!' digits. + const tail = rest === 0 ? "" : "!".repeat(rest + 1); + return enc(`${"z".repeat(groups)}${tail}~>`); +} + +/** + * A run of zero bytes, ASCIIHexDecode-encoded, ending in the end-of-data bracket. + * @param size - the decoded length + * @returns the encoded bytes + */ +export function asciiHexZeros(size: number): Uint8Array { + return enc(`${"00".repeat(size)}>`); +} + +const LZW_CLEAR = 256; +const LZW_EOD = 257; +const LZW_FIRST_CODE = 258; +const LZW_MAX_WIDTH = 12; +const LZW_INITIAL_WIDTH = 9; +const BITS_PER_BYTE = 8; +const BYTE_MASK = 0xff; + +/** + * Any bytes, LZWDecode-encoded with /EarlyChange 1 (the default): a clear code, the codes, then end-of-data, with the code width growing one code early as ISO 32000-1 7.4.4 describes and the table cleared before it would pass twelve bits. + * @param data - the bytes to encode + * @returns the encoded bytes + */ +export function lzwEncode(data: Uint8Array): Uint8Array { + const out: number[] = []; + let bitBuffer = 0; + let bitCount = 0; + let width = LZW_INITIAL_WIDTH; + const write = (code: number): void => { + bitBuffer = (bitBuffer << width) | code; + bitCount += width; + while (bitCount >= BITS_PER_BYTE) { + bitCount -= BITS_PER_BYTE; + out.push((bitBuffer >>> bitCount) & BYTE_MASK); + } + bitBuffer &= (1 << bitCount) - 1; + }; + let table = new Map(); + let next = LZW_FIRST_CODE; + const reset = (): void => { + table = new Map(); + for (let i = 0; i < LZW_FIRST_CODE - 2; i++) { + table.set(String.fromCharCode(i), i); + } + next = LZW_FIRST_CODE; + width = LZW_INITIAL_WIDTH; + }; + write(LZW_CLEAR); + reset(); + let current = ""; + for (const byte of data) { + const extended = current + String.fromCharCode(byte); + if (table.has(extended)) { + current = extended; + continue; + } + write(table.get(current)!); + table.set(extended, next); + next++; + // The decoder's table runs one entry behind the encoder's, and with EarlyChange it widens once its own next code is 2^width - 1, so the encoder widens once its next code reaches 2^width: both then switch before the same code. + if (next >= 1 << width) { + if (width === LZW_MAX_WIDTH) { + write(LZW_CLEAR); + reset(); + } else { + width++; + } + } + current = String.fromCharCode(byte); + } + if (current !== "") { + write(table.get(current)!); + } + write(LZW_EOD); + if (bitCount > 0) { + out.push((bitBuffer << (BITS_PER_BYTE - bitCount)) & BYTE_MASK); + } + return new Uint8Array(out); +} + +/** + * A full `PdfObjectResolver` over hand-built objects whose decodeStream charges `budget`, naming each stream by the object number `objects` holds it under: what a document opened with `maxDecodedBytes` does, for a test that calls one reader directly. + * @param objects - the document's objects by number + * @param budget - the budget every decode is charged against + * @returns the resolver + */ +export function budgetedResolver( + objects: ReadonlyMap, + budget: PdfDecodeBudget, +): PdfObjectResolver { + const numbers = new Map(); + for (const [num, obj] of objects) { + if (obj.kind === "stream") { + numbers.set(obj.dict, num); + } + } + const resolve = (obj: PdfObject | undefined): PdfObject | undefined => + obj?.kind === "ref" ? objects.get(obj.num) : obj; + const objectNumberOf = (stream: PdfStream): number | undefined => + numbers.get(stream.dict); + return { + resolve, + resolveDict: (obj) => asDict(resolve(obj)), + decodeStream: (stream, sink) => + decodeStream(stream.raw, stream.dict, sink, { + resolve, + budget, + objectNumber: objectNumberOf(stream), + objectNumberOf, + }), + chargeDecodedBytes: (stream, bytes) => { + budget.charge(bytes, { + objectNumber: objectNumberOf(stream), + filters: [], + }); + }, + }; +} + +// A cross-reference stream row's fixed width here, /W [1 4 2]: a type byte, a four-byte offset or object stream number, and a two-byte generation or index. +const XREF_ROW_BYTES = 7; +const XREF_OFFSET_BYTES = 4; +const XREF_TYPE_OFFSET = 1; +const XREF_TYPE_COMPRESSED = 2; +/** The object stream's object number in the documents objectStreamPdf builds. */ +export const OBJECT_STREAM_OBJ = 10; +/** The cross-reference stream's object number in the documents objectStreamPdf builds. */ +export const XREF_STREAM_OBJ = 11; +const CONTENT_STREAM_OBJ = 4; + +function bigEndian(value: number, width: number): number[] { + return Array.from( + { length: width }, + (_, i) => (value >>> ((width - 1 - i) * BITS_PER_BYTE)) & BYTE_MASK, + ); +} + +/** + * A one-page document whose catalog, page tree and page live in a FlateDecode object stream, located by a cross-reference stream, with optional zero padding decoded after each: the object stream's padding follows its objects, so the objects still parse, and the cross-reference stream's follows its rows, which the reader never reaches. With `brokenStartxref` the startxref offset points nowhere, so the reader recovers the table by scanning for objects, which decodes the object stream during the scan. + * @param objectStreamPadding - the zero bytes the object stream decodes to after its objects + * @param xrefStreamPadding - the zero bytes the cross-reference stream decodes to after its rows + * @param brokenStartxref - whether startxref names a nonsense offset + * @returns the document + */ +export function objectStreamPdf( + objectStreamPadding: number, + xrefStreamPadding: number, + brokenStartxref = false, +): Uint8Array { + const bodies = [ + { num: 1, body: "<< /Type /Catalog /Pages 2 0 R >>" }, + { num: 2, body: "<< /Type /Pages /Kids [3 0 R] /Count 1 >>" }, + { + num: 3, + body: `<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 100] /Contents ${String(CONTENT_STREAM_OBJ)} 0 R >>`, + }, + ]; + let data = ""; + const offsets: number[] = []; + for (const { body } of bodies) { + offsets.push(data.length); + data += `${body} `; + } + const header = bodies + .map(({ num }, i) => `${String(num)} ${String(offsets[i])}`) + .join(" "); + const text = enc(`${header}\n${data}`); + const decoded = new Uint8Array(text.length + objectStreamPadding); + decoded.set(text); + + const b = new FixtureBuilder().header("1.5"); + b.stream( + OBJECT_STREAM_OBJ, + `<< /Type /ObjStm /N ${String(bodies.length)} /First ${String(header.length + 1)} /Filter /FlateDecode >>`, + zlibSync(decoded), + ); + b.stream(CONTENT_STREAM_OBJ, "<< >>", enc("0 0 1 1 re f")); + const rows: number[][] = [new Array(XREF_ROW_BYTES).fill(0)]; + for (let num = 1; num <= XREF_STREAM_OBJ; num++) { + const inStream = bodies.findIndex((entry) => entry.num === num); + if (inStream >= 0) { + rows.push([ + XREF_TYPE_COMPRESSED, + ...bigEndian(OBJECT_STREAM_OBJ, XREF_OFFSET_BYTES), + 0, + inStream, + ]); + } else if (num === OBJECT_STREAM_OBJ || num === CONTENT_STREAM_OBJ) { + rows.push([ + XREF_TYPE_OFFSET, + ...bigEndian(b.offsetOf(num), XREF_OFFSET_BYTES), + 0, + 0, + ]); + } else if (num === XREF_STREAM_OBJ) { + rows.push([ + XREF_TYPE_OFFSET, + ...bigEndian(b.length, XREF_OFFSET_BYTES), + 0, + 0, + ]); + } else { + rows.push(new Array(XREF_ROW_BYTES).fill(0)); + } + } + const xrefOffset = b.length; + const table = new Uint8Array( + rows.length * XREF_ROW_BYTES + xrefStreamPadding, + ); + rows.forEach((row, i) => { + table.set(row, i * XREF_ROW_BYTES); + }); + b.stream( + XREF_STREAM_OBJ, + `<< /Type /XRef /Size ${String(rows.length)} /W [1 4 2] /Root 1 0 R /Filter /FlateDecode >>`, + zlibSync(table), + ); + b.raw(`startxref\n${String(brokenStartxref ? 999_999 : xrefOffset)}\n%%EOF`); + return b.bytes(); +} + +// JPEG markers (ITU-T T.81 Table B.1): start of image, a baseline start-of-frame, end of image. +const JPEG_SOI = [0xff, 0xd8]; +const JPEG_SOF0 = [0xff, 0xc0]; +const JPEG_EOI = [0xff, 0xd9]; +const JPEG_PRECISION = 8; +const JPEG_COMPONENT_SPEC_BYTES = 3; +const JPEG_SOF_FIXED_BYTES = 8; +const JPEG_SAMPLING_1X1 = 0x11; + +/** + * The header of a baseline JPEG declaring a frame size, with no scan data: enough for a reader that only reads the frame header, which is all a JPEG passed through undecoded gets. + * @param width - the declared width in pixels + * @param height - the declared height in pixels + * @param components - the declared component count + * @returns the bytes + */ +export function jpegHeader( + width: number, + height: number, + components: number, +): Uint8Array { + const length = JPEG_SOF_FIXED_BYTES + components * JPEG_COMPONENT_SPEC_BYTES; + const specs: number[] = []; + for (let id = 1; id <= components; id++) { + specs.push(id, JPEG_SAMPLING_1X1, 0); + } + return new Uint8Array([ + ...JPEG_SOI, + ...JPEG_SOF0, + ...bigEndian(length, 2), + JPEG_PRECISION, + ...bigEndian(height, 2), + ...bigEndian(width, 2), + components, + ...specs, + ...JPEG_EOI, + ]); +} + +/** The decoded size the decode-budget tests' large streams expand to. */ +export const LARGE = 1 << 20; +/** The budget the decode-budget tests set: a sixteenth of LARGE, so a site that decodes without charging reads to the end instead of throwing. */ +export const BUDGET = LARGE / 16; + +/** + * Runs `run` and returns the PdfDecodeBudgetError it throws, checking it names `objectNumber` (and `filters`, when given). + * @param run - the read or decode expected to be refused + * @param objectNumber - the object number the error must name + * @param filters - the filter chain the error must name + * @returns the error + * @throws Error when `run` throws anything else, throws nothing, or the error names another stream + */ +export function expectBudgetError( + run: () => unknown, + objectNumber: number | undefined, + filters?: readonly string[], +): PdfDecodeBudgetError { + let caught: unknown; + try { + run(); + } catch (error) { + caught = error; + } + if (!(caught instanceof PdfDecodeBudgetError)) { + throw new Error(`expected a PdfDecodeBudgetError, got ${String(caught)}`); + } + if (caught.code !== "pdf/decode-budget-exceeded") { + throw new Error(`unexpected code ${caught.code}`); + } + if (caught.objectNumber !== objectNumber) { + throw new Error( + `expected object ${String(objectNumber)}, got ${String(caught.objectNumber)}`, + ); + } + if (filters !== undefined && caught.filters.join() !== filters.join()) { + throw new Error( + `expected filters ${filters.join()}, got ${caught.filters.join()}`, + ); + } + return caught; +} + +// --- Whole documents: each fixture puts its one large decode at a single site. --- + +export const FLATE_ZEROS = flateZeros(LARGE); +export const BIG = "<< /Filter /FlateDecode >>"; +export const BIG_OBJ = 9; + +export interface SiteFixture { + readonly pageResources?: string; + readonly content?: string; + readonly catalogExtra?: string; + readonly objects?: (b: FixtureBuilder) => void; + readonly maxObj?: number; +} + +// Writes a null object for every number up to `maxObj` a fixture left unused, so its classic cross-reference table has an entry for each. +export function fillUnused(b: FixtureBuilder, maxObj: number): void { + for (let num = 1; num <= maxObj; num++) { + try { + b.offsetOf(num); + } catch { + b.object(num, "null"); + } + } +} + +// Catalog 1, page tree 2, page 3, content 5, then whatever the site adds (object 9 is the large stream in every fixture that has one). +export function sitePdf(site: SiteFixture): Uint8Array { + const b = new FixtureBuilder().header("1.7"); + b.object(1, `<< /Type /Catalog /Pages 2 0 R ${site.catalogExtra ?? ""} >>`); + b.object(2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>"); + b.object( + 3, + `<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 100] /Resources << ${site.pageResources ?? ""} >> /Contents 5 0 R >>`, + ); + b.stream(5, "<< >>", enc(site.content ?? "")); + site.objects?.(b); + fillUnused(b, site.maxObj ?? 5); + b.classicXrefAndTrailer(site.maxObj ?? 5, "/Root 1 0 R"); + return b.bytes(); +} + +export function bigStream(b: FixtureBuilder, num = BIG_OBJ): void { + b.stream(num, BIG, FLATE_ZEROS); +} + +export const SHOW_TEXT = "BT /F1 12 Tf 10 50 Td (A) Tj ET"; +export const TO_UNICODE_PRIVATE_USE = enc( + "/CIDInit /ProcSet findresource begin begincmap 1 begincodespacerange <00> endcodespacerange 1 beginbfchar <41> endbfchar endcmap end", +); + +// A page whose /Contents array holds a small stream and a large one (object 6). +export function contentSitePdf(): Uint8Array { + const b = new FixtureBuilder().header("1.7"); + b.object(1, "<< /Type /Catalog /Pages 2 0 R >>"); + b.object(2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>"); + b.object( + 3, + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 100] /Resources << >> /Contents [5 0 R 6 0 R] >>", + ); + b.stream(5, "<< >>", enc("0 0 1 1 re f")); + bigStream(b, 6); + fillUnused(b, 6); + b.classicXrefAndTrailer(6, "/Root 1 0 R"); + return b.bytes(); +} diff --git a/packages/pdf-codec/src/test-support/resolver.ts b/packages/pdf-codec/src/test-support/resolver.ts new file mode 100644 index 0000000000..4c167123b6 --- /dev/null +++ b/packages/pdf-codec/src/test-support/resolver.ts @@ -0,0 +1,19 @@ +import { decodeStream } from "../filters"; +import type { PdfObjectResolver } from "../interpret-types"; + +/** + * A full `PdfObjectResolver` over a test's own `resolve` and `resolveDict`, decoding with no budget: what a document opened without `maxDecodedBytes` does, for a test that builds its objects by hand rather than opening a file. + * @param base - the test's reference-following functions + * @returns the resolver, its decodeStream following references through `base.resolve` and its chargeDecodedBytes charging nothing + */ +export function unbudgetedResolver( + base: Readonly>, +): PdfObjectResolver { + return { + resolve: base.resolve, + resolveDict: base.resolveDict, + decodeStream: (stream, sink) => + decodeStream(stream.raw, stream.dict, sink, { resolve: base.resolve }), + chargeDecodedBytes: () => undefined, + }; +} diff --git a/packages/pdf-codec/src/verified-glyph-tables.test.ts b/packages/pdf-codec/src/verified-glyph-tables.test.ts index c7f3f577d5..11ca79f093 100644 --- a/packages/pdf-codec/src/verified-glyph-tables.test.ts +++ b/packages/pdf-codec/src/verified-glyph-tables.test.ts @@ -20,6 +20,7 @@ import { verifiedGlyphTableFor, type VerifiedGlyphTableSource, } from "./verified-glyph-tables"; +import { unbudgetedResolver } from "./test-support/resolver"; const NO_DIAGNOSTICS: PdfDiagnosticSink = () => undefined; @@ -28,7 +29,7 @@ function makeResolver(objects: Map): PdfObjectResolver { obj?.kind === "ref" ? objects.get(obj.num) : obj; const resolveDict = (obj: PdfObject | undefined): PdfDict | undefined => asDict(resolve(obj)); - return { resolve, resolveDict }; + return unbudgetedResolver({ resolve, resolveDict }); } function programBytes(label: string): Uint8Array { diff --git a/packages/pdf-codec/src/write-passthrough.test.ts b/packages/pdf-codec/src/write-passthrough.test.ts index 63f695e066..b1e5ca0220 100644 --- a/packages/pdf-codec/src/write-passthrough.test.ts +++ b/packages/pdf-codec/src/write-passthrough.test.ts @@ -14,6 +14,7 @@ import { } from "./test-support/jpeg2000"; import type { PdfObject } from "./objects"; import { asDict, pdfDict, pdfName, pdfNum, pdfStream } from "./objects"; +import { unbudgetedResolver } from "./test-support/resolver"; // The write side's verbatim passthrough of a no-encoder filter's original stream: an asset carrying `original` (which only the read side of a JBIG2/JPX source mints) must re-embed those bytes under their own filter rather than re-encoding the decoded pixels through Flate or Group 4, so a pdf-to-pdf round trip pays zero generation loss for exactly the two filters this package cannot re-encode. Each test builds the asset the way the reader really does — through readImageXObject over the fixture, so the decoded canonical and the original come from one genuine extraction rather than a hand-assembled pair — then writes, re-reads, and checks both halves: the written bytes carry the original stream (and the /JBIG2Globals object a symbol-dictionary stream needs), and the re-read image decodes back to the same pixels. @@ -56,12 +57,12 @@ function jbig2Asset( ], ]) : undefined; - const resolver = { + const resolver = unbudgetedResolver({ resolve: (obj: PdfObject | undefined) => obj?.kind === "ref" ? (globalsObjects?.get(obj.num) ?? obj) : obj, resolveDict: (obj: PdfObject | undefined) => asDict(obj?.kind === "ref" ? (globalsObjects?.get(obj.num) ?? obj) : obj), - }; + }); const extracted = readImageXObject( dict, jbig2FixtureBytes(fixture.stream), @@ -112,10 +113,10 @@ function jpeg2000Asset( const extracted = readImageXObject( dict, jpeg2000FixtureBytes(fixture.codestream), - { + unbudgetedResolver({ resolve: (obj: PdfObject | undefined) => obj, resolveDict: (obj: PdfObject | undefined) => asDict(obj), - }, + }), emptySink, ); if (extracted === undefined) { diff --git a/packages/pdf-codec/src/xref.ts b/packages/pdf-codec/src/xref.ts index b4b1f7fc0f..cfba033b11 100644 --- a/packages/pdf-codec/src/xref.ts +++ b/packages/pdf-codec/src/xref.ts @@ -1,4 +1,5 @@ import { ByteReader, isAsciiWhitespace } from "./bytes/reader"; +import type { PdfDecodeBudget } from "./decode-budget"; import type { PdfDiagnosticSink } from "./diagnostics"; import { decodeStream } from "./filters"; import { nextToken } from "./lexer"; @@ -67,13 +68,20 @@ const DELIMITER_BYTES = new Set([ // Guards a looping /Prev chain (a corrupt or adversarial file pointing back at an already-visited offset) — generous for any real-world incremental-update history, which rarely exceeds single digits of revisions. const MAX_XREF_SECTIONS = 64; +// `budget`, when given, is charged for every cross-reference stream and recovered object stream decoded here, the same budget openPdfDocument charges every later decode against. A cross-reference stream is never encrypted (ISO 32000-1 7.5.8.2), so its bytes are its plaintext; a recovered object stream is decoded as found, before the document's decryptor exists, so in an encrypted file it is charged for whatever decoding its ciphertext produces. export function readXref( bytes: Uint8Array, sink: PdfDiagnosticSink, + budget?: PdfDecodeBudget, ): XrefTable { const startxrefOffset = findStartxrefOffset(bytes); if (startxrefOffset !== undefined) { - const { entries, trailer } = walkXrefChain(bytes, startxrefOffset, sink); + const { entries, trailer } = walkXrefChain( + bytes, + startxrefOffset, + sink, + budget, + ); if (isTableUsable(bytes, entries, trailer)) { return { entries, trailer }; } @@ -84,7 +92,7 @@ export function readXref( message: 'the document\'s own cross-reference data was missing, unreadable, or did not check out against the bytes it points at; rebuilt the table by scanning the file for "N G obj" headers', }); - return recoverXrefByLinearScan(bytes, sink); + return recoverXrefByLinearScan(bytes, sink, budget); } function findLastOccurrence( @@ -167,6 +175,7 @@ function walkXrefChain( bytes: Uint8Array, startOffset: number, sink: PdfDiagnosticSink, + budget: PdfDecodeBudget | undefined, ): { entries: Map; trailer: PdfDict } { const merged = new Map(); const mergedTrailerEntries = new Map(); @@ -180,7 +189,7 @@ function walkXrefChain( ) { visited.add(offset); sections++; - const section = readXrefSection(bytes, offset, sink); + const section = readXrefSection(bytes, offset, sink, budget); if (section === undefined) { sink({ code: "pdf/xref-entry-invalid", @@ -208,6 +217,7 @@ function readXrefSection( bytes: Uint8Array, offset: number, sink: PdfDiagnosticSink, + budget: PdfDecodeBudget | undefined, ): XrefSection | undefined { const reader = new ByteReader(bytes); reader.seek(offset); @@ -219,7 +229,13 @@ function readXrefSection( reader.reset(mark); const indirect = parseIndirectObject(reader, sink); if (indirect?.value.kind === "stream") { - return readXrefStreamSection(indirect.value.dict, indirect.value.raw, sink); + return readXrefStreamSection( + indirect.value.dict, + indirect.value.raw, + sink, + budget, + indirect.num, + ); } return undefined; } @@ -291,8 +307,10 @@ function readXrefStreamSection( dict: PdfDict, raw: Uint8Array, sink: PdfDiagnosticSink, + budget: PdfDecodeBudget | undefined, + objectNumber: number, ): XrefSection { - const decoded = decodeStream(raw, dict, sink); + const decoded = decodeStream(raw, dict, sink, { budget, objectNumber }); const widths = asArray(dictGet(dict, "W")); const w0 = widths !== undefined ? (asNumber(widths[0]) ?? 0) : 1; const w1 = widths !== undefined ? (asNumber(widths[1]) ?? 0) : 1; @@ -448,6 +466,7 @@ function scanObjectHeaderBackward( function recoverXrefByLinearScan( bytes: Uint8Array, sink: PdfDiagnosticSink, + budget: PdfDecodeBudget | undefined, ): XrefTable { const entries = new Map(); for (const objPos of findAllKeywordPositions(bytes, OBJ_BYTES)) { @@ -494,6 +513,7 @@ function recoverXrefByLinearScan( indirect.value.raw, entries, sink, + budget, ); } } @@ -513,8 +533,12 @@ function registerObjStmContents( raw: Uint8Array, entries: Map, sink: PdfDiagnosticSink, + budget: PdfDecodeBudget | undefined, ): void { - const decoded = decodeStream(raw, dict, sink); + const decoded = decodeStream(raw, dict, sink, { + budget, + objectNumber: streamObjNum, + }); const n = asNumber(dictGet(dict, "N")) ?? 0; const reader = new ByteReader(decoded.bytes); let index = 0; From ff7912825c246ca7e7f6aa31840883ade12da7c2 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Mon, 16 Nov 2026 11:16:08 +0000 Subject: [PATCH 2/3] test(pdf-codec): refuse a decode past maxDecodedBytes inside workerd --- .../pdf-codec/test/workers/pdf-codec.test.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/pdf-codec/test/workers/pdf-codec.test.ts b/packages/pdf-codec/test/workers/pdf-codec.test.ts index c0d8154363..9c000ddda5 100644 --- a/packages/pdf-codec/test/workers/pdf-codec.test.ts +++ b/packages/pdf-codec/test/workers/pdf-codec.test.ts @@ -1,11 +1,12 @@ import { describe, expect, it } from 'vitest'; import { readPdf } from '../../src/read'; -import { createFontRegistry, writePdf } from '../../src'; +import { createFontRegistry, PdfDecodeBudgetError, writePdf } from '../../src'; // Proves pdf-codec's read path executes inside a Cloudflare Workers isolate (workerd, via @cloudflare/vitest-pool-workers) with no Node-only APIs. The read test imports through src/read.ts — the module behind the package.json `./read` entry point — so one test proves both that the read pipeline executes in the isolate and that the read-only entry itself works; the module-graph-width half of that entry's guarantee is held separately by src/read-graph.test.ts. The write test below exercises the opposite end: a real font registry resolving Calibri onto the vendored, metric-compatible Carlito face, which decodes the embedded asset, subsets it, and embeds a genuine TrueType font program — exactly the modules the read entry exists to exclude, proven to run in the isolate when a caller does want them. Fixtures are built inline rather than read from disk because workerd exposes no node:fs — and building them from the package's own format rules (object table, classic cross-reference, parenthesised content-stream string) is itself a check that nothing in the construction path needs Node either. This is the runtime complement to attw's static module-resolution check. // A minimal, structurally ordinary single-page PDF built by literal ASCII concatenation with inline byte-offset tracking — the same construction idea src/test-support/pdf.ts uses for the node fixtures, reimplemented here so the workerd test stays self-contained and pulls in no test-support code (only the package src barrel). Produces: Catalog -> Pages -> one Page (MediaBox [0 0 200 100]) with a Helvetica /F1 font and a content stream drawing "(Hello)", plus a classic (ISO 32000-1 7.5.4) cross-reference table. Each xref entry is padded to the mandatory fixed 20 bytes. -function minimalClassicXrefPdf(): Uint8Array { +// `contentFilter`, when given, is the content stream's /Filter and `content` its already-encoded bytes; otherwise the stream is the plain "(Hello)" text. +function minimalClassicXrefPdf(contentFilter?: string, content?: Uint8Array): Uint8Array { const enc = new TextEncoder(); const chunks: Uint8Array[] = []; const offsets = new Map(); @@ -37,7 +38,7 @@ function minimalClassicXrefPdf(): Uint8Array { object(2, '<< /Type /Pages /Kids [3 0 R] /Count 1 >>'); object(3, '<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 100] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>'); object(4, '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>'); - stream(5, '<< >>', enc.encode('BT /F1 12 Tf 10 50 Td (Hello) Tj ET')); + stream(5, contentFilter === undefined ? '<< >>' : `<< /Filter /${contentFilter} >>`, content ?? enc.encode('BT /F1 12 Tf 10 50 Td (Hello) Tj ET')); const xrefOffset = length; ascii(`xref\n0 6\n`); @@ -67,6 +68,18 @@ describe('pdf-codec under the Cloudflare Workers runtime', () => { expect(textItems[0]).toMatchObject({ kind: 'text', text: 'Hello' }); }); + it('readPdf stops a content stream that would decode past maxDecodedBytes inside a workerd isolate, before decoding the rest', () => { + // 64 KiB of RunLength-encoded zero bytes, written as 512 repeat runs of 128 (length byte 129, then the byte) and an end-of-data marker: a small stream that expands far past the budget. + const runs = new Uint8Array(512 * 2 + 1); + for (let i = 0; i < 512; i++) { + runs[i * 2] = 129; + } + runs[runs.length - 1] = 128; + const pdf = minimalClassicXrefPdf('RunLengthDecode', runs); + expect(() => readPdf(pdf)).not.toThrow(); + expect(() => readPdf(pdf, { maxDecodedBytes: 1024 })).toThrow(PdfDecodeBudgetError); + }); + it('writePdf embeds the vendored Carlito face through a font registry inside a workerd isolate', () => { // Read a real document back, relabel its one text run's family as Calibri, and write it through a registry: the registry resolves Calibri onto the vendored Carlito face, so this exercises the asset modules the read-only entry exists to exclude — font-registry.ts's module-scope asset imports, the sfnt parse, the glyph subsetting, and the /Type0 + /CIDFontType2 + /FontFile2 embedding group — inside the isolate. const doc = readPdf(minimalClassicXrefPdf()); From ffeadca427a291965b0d20db9e93ffe3e816c3ee Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Mon, 16 Nov 2026 11:16:10 +0000 Subject: [PATCH 3/3] feat(documents.js): forward maxDecodedBytes to the pdf read of every conversion UnifiedConversionOptions, PdfToDocumentOptions, ReadNativeDocumentTreeOptions and ReadDocumentMetadataOptions take maxDecodedBytes and hand it to readPdf, so a pdf conversion that would decode too much throws PdfDecodeBudgetError, now re-exported, rather than a generic failure. Like pages and surfaces it reaches only the hop that reads the caller's own pdf, never an intermediate pdf a route renders. --- .../documents.js/src/convert/composition.ts | 10 +- .../convert/from-pdf-decode-budget.test.ts | 131 ++++++++++++++++++ packages/documents.js/src/convert/from-pdf.ts | 12 ++ packages/documents.js/src/index.ts | 1 + 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 packages/documents.js/src/convert/from-pdf-decode-budget.test.ts diff --git a/packages/documents.js/src/convert/composition.ts b/packages/documents.js/src/convert/composition.ts index 5ab3094060..4f279d259c 100644 --- a/packages/documents.js/src/convert/composition.ts +++ b/packages/documents.js/src/convert/composition.ts @@ -329,6 +329,10 @@ export interface UnifiedConversionOptions { * The document-level surfaces of a pdf source to read, as pdf-codec's `ReadPdfOptions.surfaces` takes them: one boolean per surface (`outline`, `destinations`, `form`, `layers`, `structure`, `residue`, `images`), each read unless set to `false`, so omitting it reads everything as before. Independent of `pages`, and handed to the same hop: only the plan's first hop, and only when the conversion's source is pdf, so an intermediate pdf a route renders internally is always read whole. A skipped surface is never parsed and is absent from the read, so what the conversion builds from it is absent too: no `outline-N` entries in the tree's `destinations` table without `outline`, no destinations and no internal link constructs without `destinations` (a link to a URI is kept, and a read outline's `outline-N` entries stay, each without a `destination`), no form `contentControl` constructs without `form`, no layers table without `layers`, no structure entries in `definitions` without `structure`, no residue without `residue`, no images without `images` (no image stream is decoded, so a text-only conversion of an image-heavy page costs no image memory); skipping the outline also renumbers the reader-minted `destN` names internal link anchors carry, since the outline mints its direct destinations first. Two skips also change reconstruction: without `structure` a tagged PDF's headings, table cells and divisions are inferred from geometry as for an untagged file, and without `layers` content the default view hides cannot be told apart and is kept as if visible. See `ReadPdfOptions.surfaces` for exactly what each covers. */ readonly surfaces?: ReadPdfSurfaces; + /** + * The most bytes a pdf source's stream decoding may produce, as pdf-codec's `ReadPdfOptions.maxDecodedBytes` takes it: every content stream, font program, image, metadata packet and embedded file the read decodes is charged against it, and the first decode that would pass it throws `PdfDecodeBudgetError` (a `PdfParseError` with code `pdf/decode-budget-exceeded`, carrying the budget, the stream's object number and its filters) before materialising the rest, so a caller can tell a conversion that would cost too much from a malformed file. Handed to the same hop as `pages` and `surfaces`: only the plan's first hop, and only when the conversion's source is pdf, since an intermediate pdf a route renders internally is this package's own output rather than untrusted input. Absent, decoding is unbounded. + */ + readonly maxDecodedBytes?: number; } // What the fromPdf executor knows about a conversion's pages that a text build needs and the caller's options do not carry, passed to a text node's build alongside those options: the original one-based page number of each page the read returned, set only for a page-selected read (see UnifiedConversionOptions.pages), where a page's position in the output no longer says which page it is. Internal to the executors, never caller-supplied. @@ -657,6 +661,7 @@ export function executeFromPdf( sink: options?.sink, pages: options?.pages, surfaces: options?.surfaces, + maxDecodedBytes: options?.maxDecodedBytes, }); const content = RECONSTRUCTORS[node.variant](layout, { signal: options?.signal, @@ -726,7 +731,7 @@ export function runCompositionPlan( } // onDocument fires on the last hop only: null out onDocument for every hop before the last, then pass the caller's options through unchanged on the final hop so the callback receives the package that actually produced the output bytes. This mirrors the convention the original hand-written composed functions (xlsxToPdf/pdfToXlsx/xlsxToMarkdown/markdownToXlsx) already followed — each forwarded onDocument to its own last hop, never the first — so a caller of xlsxToPdf still receives the odsToPdf hop's content+layout package rather than the intermediate xlsx->ods bridge's content-only one. const isLastHop = i === plan.hops.length - 1; - // pages and surfaces select what to read of the caller's own pdf source, so only the hop that reads that source receives them: the first hop of a plan whose source is pdf. Every other hop's pdf (an intermediate rendering, as on a docx -> ods route through toPdf then fromPdf) is not the caller's document, and selecting its pages would silently drop content or throw for an index the caller never meant against it, and skipping its surfaces would drop what the caller's own source carried through it. + // pages, surfaces and maxDecodedBytes govern how the caller's own pdf source is read, so only the hop that reads that source receives them: the first hop of a plan whose source is pdf. Every other hop's pdf (an intermediate rendering, as on a docx -> ods route through toPdf then fromPdf) is not the caller's document, and selecting its pages would silently drop content or throw for an index the caller never meant against it, and skipping its surfaces would drop what the caller's own source carried through it. const readsCallerPdf = i === 0 && hop.from === "pdf"; const hopOptions: UnifiedConversionOptions | undefined = options === undefined @@ -736,6 +741,9 @@ export function runCompositionPlan( onDocument: isLastHop ? options.onDocument : undefined, pages: readsCallerPdf ? options.pages : undefined, surfaces: readsCallerPdf ? options.surfaces : undefined, + maxDecodedBytes: readsCallerPdf + ? options.maxDecodedBytes + : undefined, }; if (hop.executor === "toPdf") { if (!isSourceContentFormat(hop.from)) { diff --git a/packages/documents.js/src/convert/from-pdf-decode-budget.test.ts b/packages/documents.js/src/convert/from-pdf-decode-budget.test.ts new file mode 100644 index 0000000000..8210312e37 --- /dev/null +++ b/packages/documents.js/src/convert/from-pdf-decode-budget.test.ts @@ -0,0 +1,131 @@ +import { DEFAULT_LAYOUT_FONT } from "document-schema.js"; +import { bytesToBase64, encodePng } from "byte-codec"; +import { LAYOUT_FORMAT_VERSION, writePdf } from "pdf-codec"; +import { describe, expect, it } from "vitest"; +import { PdfDecodeBudgetError, PdfParseError } from "../index"; +import { convertDocument } from "./composition-to-pdf"; +import { + pdfToDocx, + pdfToMarkdown, + readContentDocument, + readNativeDocumentTree, +} from "./from-pdf"; +import { encodeMarkdownText } from "../markdown/text"; + +const IMAGE_SIDE_PX = 64; +const RGB_CHANNELS = 3; +// A budget far beyond what the fixture decodes (its image's samples, some kilobytes). +const AMPLE_BUDGET = 1 << 24; + +// One page of text and one image, which writePdf stores FlateDecode-compressed: reading it decodes the image's samples. Built once at module level, so the writer's first-use cost falls outside every test's timeout. +function imagePdf(): Uint8Array { + const png = encodePng({ + width: IMAGE_SIDE_PX, + height: IMAGE_SIDE_PX, + channels: RGB_CHANNELS, + data: new Uint8Array(IMAGE_SIDE_PX * IMAGE_SIDE_PX * RGB_CHANNELS), + }); + return writePdf({ + formatVersion: LAYOUT_FORMAT_VERSION, + metadata: {}, + images: { + img1: { + format: "png", + base64: bytesToBase64(png), + widthPx: IMAGE_SIDE_PX, + heightPx: IMAGE_SIDE_PX, + }, + }, + pages: [ + { + widthPt: 300, + heightPt: 300, + items: [ + { + kind: "text", + text: "Caption text", + xPt: 20, + yPt: 250, + font: DEFAULT_LAYOUT_FONT, + sizePt: 12, + color: { r: 0, g: 0, b: 0 }, + }, + { + kind: "image", + imageId: "img1", + xPt: 20, + yPt: 100, + widthPt: 100, + heightPt: 100, + }, + ], + }, + ], + }); +} + +const IMAGE_PDF = imagePdf(); + +function refusal(run: () => unknown): unknown { + try { + run(); + } catch (error) { + return error; + } + return undefined; +} + +describe("the pdf conversions with maxDecodedBytes", () => { + const conversions: readonly { + readonly name: string; + readonly run: (maxDecodedBytes?: number) => unknown; + }[] = [ + { + name: "pdfToMarkdown", + run: (maxDecodedBytes) => pdfToMarkdown(IMAGE_PDF, { maxDecodedBytes }), + }, + { + name: "pdfToDocx", + run: (maxDecodedBytes) => pdfToDocx(IMAGE_PDF, { maxDecodedBytes }), + }, + { + name: "convertDocument from pdf", + run: (maxDecodedBytes) => + convertDocument("pdf", "ods", IMAGE_PDF, { maxDecodedBytes }), + }, + { + name: "readNativeDocumentTree", + run: (maxDecodedBytes) => + readNativeDocumentTree("pdf", IMAGE_PDF, { maxDecodedBytes }), + }, + { + name: "readContentDocument", + run: (maxDecodedBytes) => + readContentDocument("pdf", IMAGE_PDF, { maxDecodedBytes }), + }, + ]; + + for (const { name, run } of conversions) { + it(`${name} refuses a pdf that would decode past the budget with PdfDecodeBudgetError, and converts one within it`, () => { + const error = refusal(() => run(1)); + expect(error).toBeInstanceOf(PdfDecodeBudgetError); + expect(error).toBeInstanceOf(PdfParseError); + expect(refusal(() => run(AMPLE_BUDGET))).toBeUndefined(); + }); + } + + it("converts a pdf within the budget exactly as without one", () => { + expect(pdfToMarkdown(IMAGE_PDF, { maxDecodedBytes: AMPLE_BUDGET })).toEqual( + pdfToMarkdown(IMAGE_PDF), + ); + }); + + it("budgets only the caller's own pdf: an intermediate pdf a route renders is not charged", () => { + const markdown = encodeMarkdownText("# Title\n\nBody.\n"); + expect( + refusal(() => + convertDocument("markdown", "ods", markdown, { maxDecodedBytes: 0 }), + ), + ).toBeUndefined(); + }); +}); diff --git a/packages/documents.js/src/convert/from-pdf.ts b/packages/documents.js/src/convert/from-pdf.ts index ddaf7e1359..ba29ae3114 100644 --- a/packages/documents.js/src/convert/from-pdf.ts +++ b/packages/documents.js/src/convert/from-pdf.ts @@ -34,6 +34,8 @@ const METADATA_SURFACES: Required = { export interface ReadDocumentMetadataOptions { readonly signal?: AbortSignal; + // The decode budget for a pdf source, as UnifiedConversionOptions.maxDecodedBytes describes it; consulted only for a pdf source. + readonly maxDecodedBytes?: number; } // Pulls a LayoutMetadata out of any DocumentFormat's bytes. Both dispatch halves already carry each format's own cancellation policy (a one-time throwIfAborted for the synchronous single-pass readers, straight signal-forwarding for pdf, matching odtToDocx's own reasoning in convert.ts for the identical shape of read). @@ -48,6 +50,7 @@ export function readDocumentMetadata( signal: options?.signal, pages: [], surfaces: METADATA_SURFACES, + maxDecodedBytes: options?.maxDecodedBytes, }).metadata; } return CONTENT_READERS[format](bytes, { signal: options?.signal }).metadata; @@ -67,6 +70,10 @@ export interface ReadNativeDocumentTreeOptions { * The document-level surfaces of a pdf source to read, exactly as UnifiedConversionOptions.surfaces describes them: one boolean per surface, each read unless set to `false`, independent of `pages`. Consulted only for a pdf source. A skipped surface is absent from the read, so the tree carries nothing built from it (see UnifiedConversionOptions.surfaces for exactly what: `outline-N` entries in `destinations`, destinations and internal link constructs, form `contentControl` constructs, the layers table, structure entries in `definitions`, residue, images), and reconstruction loses what it took from it: without `structure` a tagged PDF's headings, table cells and divisions are inferred from geometry, and without `layers` hidden-layer content is kept as if visible. */ readonly surfaces?: ReadPdfSurfaces; + /** + * The decode budget for a pdf source, exactly as UnifiedConversionOptions.maxDecodedBytes describes it: the first decode that would pass it throws `PdfDecodeBudgetError`. Consulted only for a pdf source. + */ + readonly maxDecodedBytes?: number; } // readContentDocument's own options are the identical shape — one options type for both, so a caller forwarding {signal, images, sink} through either entry point never needs to know which fields the OTHER one ignores. @@ -84,6 +91,7 @@ function readNativeContent( sink: options?.sink, pages: options?.pages, surfaces: options?.surfaces, + maxDecodedBytes: options?.maxDecodedBytes, }); const content = reconstructWordprocessing(layout, { signal: options?.signal, @@ -142,6 +150,10 @@ export interface PdfToDocumentOptions { * The document-level surfaces to read, exactly as UnifiedConversionOptions.surfaces describes them: one boolean per surface (`outline`, `destinations`, `form`, `layers`, `structure`, `residue`, `images`), each read unless set to `false`, independent of `pages`. A skipped surface is never parsed, and the conversion carries nothing built from it. Without `structure` a tagged PDF's headings, table cells and divisions are inferred from geometry as for an untagged file, and without `layers` content the default view hides is kept as if visible. */ readonly surfaces?: ReadPdfSurfaces; + /** + * The decode budget for the pdf source, exactly as UnifiedConversionOptions.maxDecodedBytes describes it: the first decode that would pass it throws `PdfDecodeBudgetError`. + */ + readonly maxDecodedBytes?: number; // Called exactly once, synchronously, with the DocumentTree (the readPdf-produced layout, and the reconstructed content built from it) this conversion built internally, before the function returns its bytes — see convert.ts's DocumentToPdfOptions.onDocument for the mirrored X-to-PDF side of this same side channel. readonly onDocument?: (pkg: DocumentTree) => void; } diff --git a/packages/documents.js/src/index.ts b/packages/documents.js/src/index.ts index f8fb39f89b..56a9d4f993 100644 --- a/packages/documents.js/src/index.ts +++ b/packages/documents.js/src/index.ts @@ -406,6 +406,7 @@ export type { } from "pdf-codec"; export { NOOP_DIAGNOSTIC_SINK, + PdfDecodeBudgetError, PdfEncryptedError, PdfPageSelectionError, PdfParseError,