KnpLabs / snappy

PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage

Home Page:https://knplabs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Uncaught crash with html header or footer caused by wrong length estimation

Adelysnet opened this issue · comments

Hi,

i don't know the exact reason, but some times, html headers|footers (strings) sent in option don't pass the isFile test, crashing with a 500 error code. i suppose, depending of the encoding (utf8 in my case), the value length is miscalculated and this test fails in the AbstractGenerator (line 695) :

protected function isFile($filename)
{
return \strlen($filename) <= \PHP_MAXPATHLEN && \is_file($filename);
}

causing the is_file function to crash. I tried to replace strlen by mb_strlen without success but add an error control operator @\is_file does the trick :

protected function isFile($filename) { return \strlen($filename) <= \PHP_MAXPATHLEN && @\is_file($filename); }

Hello @Adelysnet!

Could you provide a reproducible gist or any equivalent ? It is hard tell what could be wrong with the information you currently provided.

Did you manage to spot anything that could help ?

Hi Antoine,

here is the Gist : https://gist.github.com/Adelysnet/8c52f20f06b82192aec4ecfb8e6cfd7f

Just call the generatePDF method like so :
$html = '<html><head><title>Test</title></head><body><h1>Test</h1></body></html>';

$pdfManager->generatePDF( $html );

Env :
PHP Version 8.1.16
Symfony : 5.4

Romain

Hi @Adelysnet ,

You wrote that "some times" the generation fails. Do you know every how many generation it fails?
I cannot reproduce what you are experiencing with a "filename" with a length longer than PHP_MAXPATHLEN

Do you have the message that triggered the 500 error code?

Hello @Adelysnet,
We can't replicate the issue you are having using your gist.
Are you sure your issue is related to the isFile function?
In your example you don't generate a filename that's slightly below the PHP_MAXPATHLEN but an header that's slightly below that length.
Are you sure that you don't have any issue reading/writing that file instead and, therefore, it's correct that theisFile function returns false?

Hi @alexpozzi
the header-html option pass the optionsWithContentCheck test in the Pdf class, so the value (can be html string or filename) is sent to the isFile function, which in my environnement crash.

Oh! You are right, my bad, I missed that.
Can you give us more info on your environment? Like OS, Docker version (if using Docker) etc...?
I think that the assertion should be adjusted in order to better support this case.