CVE-2023-28115: Deserialization of Untrusted Data (CWE-502)
value: Snappy is a PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. Prior to version 1.4.2, Snappy is vulnerable to PHAR deserialization due to a lack of checking on the protocol before passing it into the `file_exists()` function. If an attacker can upload files of any type to the server he can pass in the phar:// protocol to unserialize the uploaded file and instantiate arbitrary PHP objects. This can lead to remote code execution especially when snappy is used with frameworks with documented POP chains like Laravel/Symfony vulnerable developer code. If a user can control the output file from the `generateFromHtml()` function, it will invoke deserialization. This vulnerability is capable of remote code execution if Snappy is used with frameworks or developer code with vulnerable POP chains. It has been fixed in version 1.4.2.
CWE description: The product deserializes untrusted data without sufficiently verifying that the resulting data will be valid.
CVSS v3 base score: 9.8
Published: 2023-03-17T22:15Z
Repository: https://github.com/KnpLabs/snappy
Language: PHP
Files changed: src/Knp/Snappy/AbstractGenerator.php, tests/Knp/Snappy/AbstractGeneratorTest.php
Commit date: 2023-03-17 15:46:37 +0100
Commit message: fix: security issue GHSA-gq6w-q6wh-jggc

Diff: diff --git a/src/Knp/Snappy/AbstractGenerator.php b/src/Knp/Snappy/AbstractGenerator.php
index dba9873..643376f 100644
--- a/src/Knp/Snappy/AbstractGenerator.php
+++ b/src/Knp/Snappy/AbstractGenerator.php
@@ -623,10 +623,14 @@ abstract class AbstractGenerator implements GeneratorInterface, LoggerAwareInter
      *
      * @return void
      */
     protected function prepareOutput($filename, $overwrite)
     {
+        if (\strpos($filename, 'phar://') === 0) {
+            throw new InvalidArgumentException('The output file cannot be a phar archive.');
+        }
+
         $directory = \dirname($filename);
 
         if ($this->fileExists($filename)) {
             if (!$this->isFile($filename)) {
                 throw new InvalidArgumentException(\sprintf('The output file \'%s\' already exists and it is a %s.', $filename, $this->isDir($filename) ? 'directory' : 'link'));
diff --git a/tests/Knp/Snappy/AbstractGeneratorTest.php b/tests/Knp/Snappy/AbstractGeneratorTest.php
index 803ccd8..55d5fe4 100644
--- a/tests/Knp/Snappy/AbstractGeneratorTest.php
+++ b/tests/Knp/Snappy/AbstractGeneratorTest.php
@@ -936,10 +936,34 @@ class AbstractGeneratorTest extends TestCase
             ],
             $media->getOptions()
         );
     }
 
+    public function testFailingGenerateWithOutputContainingPharPrefix(): void
+    {
+        $media = $this->getMockBuilder(AbstractGenerator::class)
+            ->setMethods([
+                'configure',
+                'prepareOutput',
+            ])
+            ->setConstructorArgs(['the_binary', [], ['PATH' => '/usr/bin']])
+            ->getMock()
+        ;
+
+        $media->setTimeout(2000);
+
+        $media
+            ->expects($this->once())
+            ->method('prepareOutput')
+            ->with($this->equalTo('phar://the_output_file'))
+        ;
+
+        $this->expectException(InvalidArgumentException::class);
+
+        $media->generate('the_input_file', 'phar://the_output_file', ['foo' => 'bar']);
+    }
+
     /**
      * @return null|string
      */
     private function getPHPExecutableFromPath(): ?string
     {

Fixed code:         if (\strpos($filename, 'phar://') === 0) {
            throw new InvalidArgumentException('The output file cannot be a phar archive.');
        }

    public function testFailingGenerateWithOutputContainingPharPrefix(): void
    {
        $media = $this->getMockBuilder(AbstractGenerator::class)
            ->setMethods([
                'configure',
                'prepareOutput',
            ])
            ->setConstructorArgs(['the_binary', [], ['PATH' => '/usr/bin']])
            ->getMock()
        ;

        $media->setTimeout(2000);

        $media
            ->expects($this->once())
            ->method('prepareOutput')
            ->with($this->equalTo('phar://the_output_file'))
        ;

        $this->expectException(InvalidArgumentException::class);

        $media->generate('the_input_file', 'phar://the_output_file', ['foo' => 'bar']);
    }