[BUGFIX] Prevent throwing warnings when extract svg image sizes 02/59802/4
authorRemo Häusler <r3h6@outlook.com>
Mon, 25 Feb 2019 16:02:30 +0000 (17:02 +0100)
committerSusanne Moog <look@susi.dev>
Sun, 14 Apr 2019 15:17:07 +0000 (17:17 +0200)
TYPO3 should silently ignore errors and warnings in svg images.

Resolves: #86428
Releases: master, 9.5, 8.7
Change-Id: I54bb19ac0a6c949a8b150808f52562b73653e739
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/59802
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Susanne Moog <look@susi.dev>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Jörg Bösche <typo3@joergboesche.de>
Reviewed-by: Susanne Moog <look@susi.dev>
typo3/sysext/core/Classes/Type/File/ImageInfo.php
typo3/sysext/core/Tests/Unit/Type/File/ImageInfoTest.php

index 45aeda4..efcc097 100644 (file)
@@ -120,7 +120,7 @@ class ImageInfo extends FileInfo implements LoggerAwareInterface
         $fileContent = file_get_contents($this->getPathname());
         // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
         $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
-        $xml = simplexml_load_string($fileContent, 'SimpleXMLElement', LIBXML_NOERROR);
+        $xml = simplexml_load_string($fileContent, 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING);
 
         // If something went wrong with simpleXml don't try to read information
         if ($xml === false) {
index ca49ca4..71b2208 100644 (file)
@@ -39,13 +39,50 @@ class ImageInfoTest extends UnitTestCase
     }
 
     /**
+     * @return array
+     */
+    public function doesNotBreakOnImageInfoWithInvalidSvgDataProvider(): array
+    {
+        return [
+            ['Invalid XML.', 0, 0],
+            [
+                '<?xml version="1.0" encoding="utf-8"?>
+                <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" [
+                    <!ENTITY ns_a "http://ns.typo3.com/test/a/1.0/">
+                    <!ENTITY ns_b "http://ns.typo3.com/test/b/1.0/">
+                ]>
+                <svg version="1.0"
+                    xmlns:x="&ns_a;"
+                    xmlns="http://www.w3.org/2000/svg"
+                    xmlns:xlink="http://www.w3.org/1999/xlink"
+                    xml:space="preserve"
+                    x="0px" y="0px" viewBox="0 0 436 177">
+                    <metadata>
+                        <sfw xmlns="&ns_b;">
+                            <slices></slices>
+                        </sfw>
+                    </metadata>
+                </svg>',
+                436,
+                177
+            ],
+        ];
+    }
+
+    /**
      * @test
+     * @dataProvider doesNotBreakOnImageInfoWithInvalidSvgDataProvider
+     * @param string $svg
+     * @param int $width
+     * @param int $height
      */
-    public function doesNotBreakOnImageInfoWithInvalidSvg()
+    public function doesNotBreakOnImageInfoWithInvalidSvg($svg, $width, $height)
     {
+        $this->resetSingletonInstances = true;
+
         $root = vfsStream::setup('root');
         $testFile = 'test.svg';
-        vfsStream::newFile($testFile)->at($root)->setContent('Invalid XML.');
+        vfsStream::newFile($testFile)->at($root)->setContent($svg);
 
         $GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType'] = [
             'svg' => 'image/svg+xml',
@@ -62,8 +99,10 @@ class ImageInfoTest extends UnitTestCase
         $imageInfo = new ImageInfo($root->url() . '/' . $testFile);
         $imageInfo->setLogger($loggerProphecy->reveal());
 
-        $this->assertEquals(0, $imageInfo->getWidth());
-        $this->assertEquals(0, $imageInfo->getHeight());
+        $this->assertSame($width, $imageInfo->getWidth());
+        $this->assertSame($height, $imageInfo->getHeight());
+
+        GeneralUtility::makeInstance(GraphicalFunctions::class);
     }
 
     /**
@@ -81,6 +120,9 @@ class ImageInfoTest extends UnitTestCase
     /**
      * @test
      * @dataProvider canDetectImageSizesDataProvider
+     * @param string $file
+     * @param int $width
+     * @param int $height
      */
     public function canDetectImageSizes($file, $width, $height)
     {
@@ -88,7 +130,7 @@ class ImageInfoTest extends UnitTestCase
         $imageInfo = new ImageInfo(__DIR__ . '/../Fixture/' . $file);
         $imageInfo->setLogger($logger);
 
-        $this->assertEquals($width, $imageInfo->getWidth());
-        $this->assertEquals($height, $imageInfo->getHeight());
+        $this->assertSame($width, $imageInfo->getWidth());
+        $this->assertSame($height, $imageInfo->getHeight());
     }
 }