Skip to content
GHSA 4w9h 57gf H3px

GHSA 4w9h 57gf H3px

github.com • November 28, 2026

smalot/pdfparser before 2.14.0 is vulnerable to a denial of service: a small crafted PDF with deeply nested or unbalanced parentheses in a content-stream string makes getText() and getTextArray() run in O(n²) time, tying up a PHP worker for seconds to minutes. Fixed in 2.14.0.

PDFObject::formatContent() isolates strings, inline images and marked-content dictionaries before splitting a page content stream into commands ( src/Smalot/PdfParser/PDFObject.php , lines 319 to 351 in v2.13.1 for the string isolation loop).

Each string operand (...) was found with a regular expression matching from ( to the ) . When the match had unbalanced parentheses, the loop stored it in $attempt , rebuilt the pattern with preg_quote($attempt) and searched again from the same offset. Each pass extended the match by a single ) while rescanning the growing prefix: a string of n nested ( followed by n ) took n passes of O(n) work each, i.e. O(n²) CPU time.

The same construct, a regular expression rebuilt from the content, was used for two other elements:

inline images ( BI ... ID ... EI ): each image was replaced with a preg_replace() over the whole stream and restored with one str_replace() per image;

marked-content dictionaries ( BDC , BMC , DP , MP ): each dictionary was searched from the start of the stream and replaced with a preg_replace() over the whole stream.

The content stream is fully controlled by whoever supplies the PDF, and the code is reached from the public text extraction API:

Parser::parseFile() / parseContent() → PDFObject::getText() → getTextArray() → getSectionsText() → formatContent()

Measurements (v2.13.1, PHP 8.3)

Doubling the input roughly quadruples the processing time. A PDF of 64 KB costs over a minute of CPU. With FlateDecode compression, a 520-byte PDF takes 6 s.

Related issue: silent text loss

The same construct failed for elements larger than 64 KB: the rebuilt regular expression did not compile, and the text of the whole content stream was silently dropped. A crafted PDF could therefore hide its text from tools extracting it with this library.

Version 2.14.0 removes every regular expression built from the content:

each string is scanned once, left to right, tracking parenthesis depth and backslash escapes;

inline images and dictionaries are searched only after the match, and the output is assembled piece by piece; inline images are restored with a single strtr() .

formatContent() now runs in linear time. A differential run over 320,000 generated content streams produced output identical to the implementation, except for inputs that previously threw a TypeError .

The release also hardens text extraction: an unmatched ] or >> in an inline image dictionary or a BDC property list, and a Q operator without a preceding q , no longer make getText() throw a TypeError .

Build a minimal one-page PDF whose content stream holds a single Tj string made of N ( followed by N ) , then extract its text:

Output on v2.13.1 with PHP 8.3:

Doubling N roughly quadruples the processing time, whereas text extraction from input of this size is expected to be close to linear. A PDF of 64 KB costs over a minute of CPU. With FlateDecode compression, the same effect is reached with a 520-byte file.

On 2.14.0, each of these inputs is processed in a few milliseconds.

PDFObject::formatContent() isolates strings, inline images and marked-content dictionaries (BDC, BMC, DP, MP) before splitting a content stream into commands. All three steps rebuilt a regular expression from the matched text and searched again, rescanning the content on each pass. A small crafted PDF kept getText() and getTextArray() busy for seconds to minutes (O(n²)):

a string of n nested or sibling pairs of unescaped parentheses: 6 s for a 520-byte FlateDecode PDF;

inline images, each replaced with a preg_replace() over the whole stream;

marked-content dictionaries, each searched from the start of the stream.

Any application extracting text from untrusted PDFs is affected.

The same construct failed for elements larger than 64 KB: the rebuilt expression did not compile and the text of the whole content stream was silently lost, so a crafted PDF could hide its text from tools relying on this library.

Extracted Entities