smalot / pdfparser

PdfParser, a standalone PHP library, provides various tools to extract data from a PDF file.
GNU Lesser General Public License v3.0
2.3k stars 534 forks source link

Check for binary content in formatContent() before a problematic regexp #676

Closed GreyWyvern closed 4 months ago

GreyWyvern commented 4 months ago

Type of pull request

About

Some PDF files manage to serve binary content (a font, or an image, etc.) to the formatContent() method which only accepts text document streams.

Since (strings) in PDF document streams can contain binary content (that gets decoded by a font library, for instance) we must test the document stream for text-only content EXCLUDING (string) content.

Previously the check for text-only content was done after a regexp removing the (strings) from the stream, recursively extending the regexp to check for balanced parentheses. However for a sufficiently long binary stream, this might create a regexp long enough to cause a PHP error.

The solution is to move the check for binary content before this (string)-removing regexp. Simplify the binary check by truncating the document stream at the first open parenthesis, which indicates the start of a (string), then testing what remains for valid UTF-8.

This makes the later check for binary content unnecessary and it has been removed. Resolves issue #668.

Future work on this issue should be done to determine why PdfParser is creating PDFObject objects from binary data in the first place.

Checklist for code / configuration changes

k00ni commented 4 months ago

I fixed coding style issues in #677, which got merged into master branch.

k00ni commented 4 months ago

@GreyWyvern Thanks again for your contribution.