From 63a5383b16058ef2d60aa15062508571f368e1c6 Mon Sep 17 00:00:00 2001
From: Anja Leichsenring <aleichsenring@ab-softlab.de>
Date: Sun, 20 Nov 2016 09:42:59 +0100
Subject: [PATCH] [BUGFIX] Trim input into xml2array
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Preciding whitespace in the xml input string causes xml2array()
to fail with an error message. This whitespace is often
introduced by formatting code via IDE, so blaming the developer
only helps so far. Inserting a trim() before the input is processed
mitigates the problem.

Resolves: #78752
Releases: master, 7.6
Change-Id: I3b9d3c81b64d502e7cefef80e72bef3a1bd9b3da
Reviewed-on: https://review.typo3.org/50716
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Łukasz Uznański <l.uznanski@macopedia.pl>
Tested-by: Łukasz Uznański <l.uznanski@macopedia.pl>
Reviewed-by: Ralf Zimmermann <ralf.zimmermann@tritum.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
---
 .../core/Classes/Utility/GeneralUtility.php   |  2 +-
 .../Tests/Unit/Utility/GeneralUtilityTest.php | 70 +++++++++++++++++++
 2 files changed, 71 insertions(+), 1 deletion(-)

diff --git a/typo3/sysext/core/Classes/Utility/GeneralUtility.php b/typo3/sysext/core/Classes/Utility/GeneralUtility.php
index 8ace260c8cf1..d1f0f1936edb 100644
--- a/typo3/sysext/core/Classes/Utility/GeneralUtility.php
+++ b/typo3/sysext/core/Classes/Utility/GeneralUtility.php
@@ -1795,7 +1795,7 @@ class GeneralUtility
         if (!empty($firstLevelCache[$identifier])) {
             $array = $firstLevelCache[$identifier];
         } else {
-            $array = self::xml2arrayProcess($string, $NSprefix, $reportDocTag);
+            $array = self::xml2arrayProcess(trim($string), $NSprefix, $reportDocTag);
             // Store content in first level cache
             $firstLevelCache[$identifier] = $array;
         }
diff --git a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
index f431d716f705..fc60703b6975 100644
--- a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
+++ b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
@@ -4631,6 +4631,76 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
 </phparray>', $output);
     }
 
+    /**
+     * @return array
+     */
+    public function providerForXml2Array(): array
+    {
+        return [
+            'inputWithoutWhitespaces' => [
+                '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
+                <T3FlexForms>
+                    <data>
+                        <field index="settings.persistenceIdentifier">
+                            <value index="vDEF">egon</value>
+                        </field>
+                    </data>
+                </T3FlexForms>'
+            ],
+            'inputWithPrecedingWhitespaces' => [
+                '
+                <?xml version="1.0" encoding="utf-8" standalone="yes" ?>
+                <T3FlexForms>
+                    <data>
+                        <field index="settings.persistenceIdentifier">
+                            <value index="vDEF">egon</value>
+                        </field>
+                    </data>
+                </T3FlexForms>'
+            ],
+            'inputWithTrailingWhitespaces' => [
+                '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
+                <T3FlexForms>
+                    <data>
+                        <field index="settings.persistenceIdentifier">
+                            <value index="vDEF">egon</value>
+                        </field>
+                    </data>
+                </T3FlexForms>
+                '
+            ],
+            'inputWithPrecedingAndTrailingWhitespaces' => [
+                '
+                <?xml version="1.0" encoding="utf-8" standalone="yes" ?>
+                <T3FlexForms>
+                    <data>
+                        <field index="settings.persistenceIdentifier">
+                            <value index="vDEF">egon</value>
+                        </field>
+                    </data>
+                </T3FlexForms>
+                '
+            ],
+        ];
+    }
+
+    /**
+     * @test
+     * @dataProvider providerForXml2Array
+     * @param string $input
+     */
+    public function xml2ArrayDealsProperlyWithWhitespace(string $input)
+    {
+        $expected = [
+            'data' => [
+                'settings.persistenceIdentifier' => [
+                    'vDEF' => 'egon',
+                ]
+            ],
+        ];
+        $this->assertSame($expected, GeneralUtility::xml2array($input));
+    }
+
     /**
      * @test
      * @dataProvider idnaEncodeDataProvider
-- 
GitLab