Skip to content

Fix integer overflow in XRef stream byte width parsing#1781

Open
eddieran wants to merge 1 commit intoHopding:masterfrom
eddieran:fix/xref-stream-byte-width-validation
Open

Fix integer overflow in XRef stream byte width parsing#1781
eddieran wants to merge 1 commit intoHopding:masterfrom
eddieran:fix/xref-stream-byte-width-validation

Conversation

@eddieran
Copy link
Copy Markdown

Summary

Fixes #1776 -- integer overflow in XRef stream byte width parsing.

The /W array values from XRef streams are read without validation. These widths control loop bounds in parseEntries(). A crafted PDF with /W [100, 100, 100] causes excessive byte reads, integer overflow via bit shifting, and NaN propagation.

Changes

  • Validate /W byte widths after reading from the array. Each width must be 0--8 per the PDF spec (ISO 32000-1:2008, Table 17). Throws InvalidXRefStreamByteWidthError for out-of-range values.
  • Bounds-check bytes.next() in parseEntries() to gracefully handle premature end-of-stream instead of propagating undefined through bitwise operations.
  • Added tests for both the rejection of invalid widths and acceptance of valid widths.

Files changed

  • src/core/errors.ts -- new InvalidXRefStreamByteWidthError class
  • src/core/parser/PDFXRefStreamParser.ts -- validation + bounds checking
  • tests/core/parser/PDFXRefStreamParser.spec.ts -- 2 new test cases

Test plan

  • All existing XRef stream parser tests pass (7/7)
  • New test: /W [100, 100, 100] throws descriptive error
  • New test: /W [1, 4, 0] (valid widths) does not throw

The /W array values from XRef streams were read without validation.
A crafted PDF with large byte widths (e.g. /W [100, 100, 100]) causes
excessive byte reads, integer overflow via bit shifting in parseEntries(),
and NaN propagation.

This adds:
- Validation that each /W byte width is 0-8 per ISO 32000-1:2008, Table 17
- Bounds checking in parseEntries() for premature end of stream

Fixes Hopding#1776
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integer overflow in XRef stream byte width parsing leads to memory corruption

1 participant