Maruan Sahyoun created PDFBOX-6356:
--------------------------------------
Summary: Bound the total subroutine work per glyph in
Type1CharStringParser and Type2CharStringParser
Key: PDFBOX-6356
URL: https://issues.apache.org/jira/browse/PDFBOX-6356
Project: PDFBox
Issue Type: Bug
Reporter: Maruan Sahyoun
The Type 1 and Type 2 charstring parsers expand subroutine calls (`callsubr`,
and `callgsubr` for Type 2) recursively while parsing a glyph:
`Type1CharStringParser.processCallSubr()` and
`Type2CharStringParser.processSubr()`. Since PDFBOX-6308 the nesting depth is
limited (`MAX_SUBR_NESTING`: 106 for Type 1, 50 for Type 2), which stops
endless recursion.
The total number of calls is not limited. If each subroutine calls the next one
twice, a chain of 64 subroutines needs 2^64 calls while staying below the
nesting limit, so parsing the glyph never finishes. This affects rendering, and
text extraction when the PDF doesn't provide glyph widths.
Proposed change
- Count the subroutine bytes parsed for each glyph, and throw an `IOException`
when they exceed a budget (for example 1 MB). Every call costs at least two
bytes in the calling code, so this also limits the number of calls and the size
of the resulting sequence.
- Track the budget per call to `parse()`, like the nesting level, not in
fields: one parser instance is shared by all glyphs of a font and may be used
by several threads.
- Keep the existing nesting limits; lowering them would bring back the
regressions fixed in PDFBOX-6308.
- Tests for both parsers: a subroutine chain with exponential calls must fail
within a timeout, and a chain nested deeper than the specification's 10 levels
must still parse.
Claude on behalf of Maruan Sahyoun (msahyoun)
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]