Repository navigation
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1814
readPdf,renderPdfPageandopenPdfDocumenttakemaxDecodedBytes, one budget charged by every decode the document reaches. The first decode that would pass it throwsPdfDecodeBudgetError(aPdfParseError, codepdf/decode-budget-exceeded, carryingmaxDecodedBytes,decodedBytesalready spent, the stream'sobjectNumberand itsfilters). documents.js forwards the option from every pdf conversion and read and re-exports the error.Decisions
PdfObjectResolvergainsdecodeStream(stream, sink)andchargeDecodedBytes(stream, n), and every reader decodes through it instead of importingfilters.ts'sdecodeStream. Onlydocument.tsandxref.ts(which charge the budget themselves) import it, and a test fails if any other module starts to, so a new decode site cannot bypass the budget./Contentsarrays, form XObjects, image XObjects and inline images,/SMask,/Indexedlookups,/Metadata, embedded files,/ToUnicode,/CIDToGIDMap(font read and raster), and font programs for encoding, PostScript name, digest and raster outlines.[/ASCII85Decode /FlateDecode]pays for both. A predictor is not charged again, since its output is never longer than the stage it rewrites. A stream decoded twice is charged twice (a program two fonts share costs two decodes); the existing per-stream name and digest caches still decode once.inflateTolerant's four recovery tiers. LZW, RunLength and ASCII85 throw at the first byte past the limit. ASCIIHex is refused from its exact size before allocating. CCITTFax is refused from/Rows(or, with none, the most rows its input bits can encode), JBIG2 from/Widthand/Height(refused without them), JPEG 2000 from its sample planes beforedecodeJpeg2000allocates them (newonSampleAllocationoption), and a JPEGrenderPdfPagehands to a rasteriser from its frame header.readPdfpasses JPEGs through undecoded, so they cost nothing there.readJpeg2000Imagerethrows the budget error instead of degrading to "skip this image", and once one decode is refused the budget is spent, so later decodes are refused even through a caller that caught the first error.fetch()before decoding, so the budget charges plaintext decoding. Cross-reference streams are never encrypted. An object stream found by recovery is decoded before the decryptor exists, as it already was, and is charged for whatever that produces.decodeStreamcalls the same unbounded decoders as before (inflateTolerant, not the bounded ladder), so output is unchanged.UnifiedConversionOptions,PdfToDocumentOptions,ReadNativeDocumentTreeOptionsandReadDocumentMetadataOptionstakemaxDecodedBytesand forward it likepagesandsurfaces: only to the hop that reads the caller's own pdf, never an intermediate pdf a route renders.Not bounded
Work that produces no decoded bytes: content interpretation, text layout and PNG encoding, which scale with page and content-stream sizes a caller can measure beforehand. JBIG2's intermediate bitmaps stay bounded by its existing per-bitmap ceiling rather than charged.
Tests
src/decode-budget.test.tsandsrc/decode-budget-sites.test.ts: each filter refuses a small stream expanding past the budget, and decodes what fits byte-for-byte as without one (seeded sizes, exact-fit and one-short budgets);inflateTolerantBoundedagainstinflateTolerantover 200 seeded valid, truncated, whitespace-prefixed and raw streams; one document per site whose only large stream is at that site, which reads without a budget and throws naming that object with one (reverting any site's charge makes its test fail); shared font programs charged twice; JPEG 2000 samples; encrypted object streams;renderPdfPage's content, font-program,CIDToGIDMapand JPEG charges; every existing document fixture reads identically with an unlimited budget and with none.src/convert/from-pdf-decode-budget.test.ts(documents.js): each pdf conversion and read refuses withPdfDecodeBudgetError, converts within the budget, and an intermediate pdf is not charged.test/workers/pdf-codec.test.ts: the refusal inside workerd.