[BUGFIX] Remove Byte Order Mark from TypoScript includes 15/57315/5
authorAndreas Fernandez <a.fernandez@scripting-base.de>
Fri, 22 Jun 2018 20:52:13 +0000 (22:52 +0200)
committerMathias Brodala <mbrodala@pagemachine.de>
Fri, 22 Jun 2018 22:22:15 +0000 (00:22 +0200)
If TypoScript files have a Byte Order Mark (BOM) set, the TypoScript
parser may fail as the invisible characters disturb the parsing process.

The BOM gets removed from the TypoScript strings with this patch.

Resolves: #56483
Releases: master, 8.7
Change-Id: I59318f9d2d92efb224d62906fb404fcb41459f8f
Reviewed-on: https://review.typo3.org/57315
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
typo3/sysext/core/Classes/TypoScript/Parser/TypoScriptParser.php
typo3/sysext/core/Classes/Utility/StringUtility.php
typo3/sysext/core/Tests/Unit/Utility/StringUtilityTest.php

index 84a9f70..3efe7f8 100644 (file)
@@ -25,6 +25,7 @@ use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
 use TYPO3\CMS\Core\Utility\PathUtility;
+use TYPO3\CMS\Core\Utility\StringUtility;
 use TYPO3\CMS\Frontend\Configuration\TypoScript\ConditionMatching\ConditionMatcher as FrontendConditionMatcher;
 
 /**
@@ -811,6 +812,10 @@ class TypoScriptParser
 ';
         }
 
+        if ($string !== null) {
+            $string = StringUtility::removeByteOrderMark($string);
+        }
+
         // Checking for @import syntax imported files
         $string = self::addImportsFromExternalFiles($string, $cycle_counter, $returnFiles, $includedFiles, $parentFilenameOrPath);
 
index 54efb7d..0e3575a 100644 (file)
@@ -107,4 +107,19 @@ class StringUtility
     {
         return preg_replace('/([#:.\\[\\],=@])/', '\\\\$1', $selector);
     }
+
+    /**
+     * Removes the Byte Order Mark (BOM) from the input string. This method supports UTF-8 encoded strings only!
+     *
+     * @param string $input
+     * @return string
+     */
+    public static function removeByteOrderMark(string $input): string
+    {
+        if (strpos($input, "\xef\xbb\xbf") === 0) {
+            $input = substr($input, 3);
+        }
+
+        return $input;
+    }
 }
index 250a098..8e1dc86 100644 (file)
@@ -243,4 +243,33 @@ class StringUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
             ['input[name=foo]', 'input\\[name\\=foo\\]'],
         ];
     }
+
+    /**
+     * @param string $input
+     * @param string $expectedValue
+     * @test
+     * @dataProvider removeByteOrderMarkDataProvider
+     */
+    public function removeByteOrderMark(string $input, string $expectedValue): void
+    {
+        // assertContains is necessary as one test contains non-string characters
+        $this->assertSame($expectedValue, StringUtility::removeByteOrderMark(hex2bin($input)));
+    }
+
+    /**
+     * @return array
+     */
+    public function removeByteOrderMarkDataProvider(): array
+    {
+        return [
+            'BOM gets removed' => [
+                'efbbbf424f4d2061742074686520626567696e6e696e6720676574732072656d6f766564',
+                'BOM at the beginning gets removed'
+            ],
+            'No BOM available' => [
+                '4e6f20424f4d20617661696c61626c65',
+                'No BOM available',
+            ],
+        ];
+    }
 }