From 9835fda726bd255d5fbcebd5934467cd09246d00 Mon Sep 17 00:00:00 2001
From: Oliver Bartsch <bo@cedev.de>
Date: Tue, 8 Nov 2022 17:58:17 +0100
Subject: [PATCH] [BUGFIX] Quote entity name for regex in
 ExtensionManagementUtility

Due to changes in #98960 the resolved entity name
might contain slashes, which need to be quoted to
prevent PHP warnings.

Resolves: #99023
Related: #98960
Releases: main, 11.5
Change-Id: I75eecbdbbddc82b5b1189388413fabd7370d4229
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/76488
Tested-by: core-ci <typo3@b13.com>
Tested-by: Dmitry Dulepov <9csxfqr4jy@liamekaens.com>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Dmitry Dulepov <9csxfqr4jy@liamekaens.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
---
 .../Utility/ExtensionManagementUtility.php        |  4 ++--
 .../Utility/ExtensionManagementUtilityTest.php    | 15 +++++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/typo3/sysext/core/Classes/Utility/ExtensionManagementUtility.php b/typo3/sysext/core/Classes/Utility/ExtensionManagementUtility.php
index 18e1da3e3b41..0ac8b458db24 100644
--- a/typo3/sysext/core/Classes/Utility/ExtensionManagementUtility.php
+++ b/typo3/sysext/core/Classes/Utility/ExtensionManagementUtility.php
@@ -252,7 +252,7 @@ class ExtensionManagementUtility
                             switch ($positionIdentifier) {
                                 case 'after':
                                 case 'before':
-                                    if (preg_match('/\\b' . $entityName . '\\b/', $palette['showitem']) > 0 || $entityName === 'palette:' . $paletteName) {
+                                    if (preg_match('/\\b' . preg_quote($entityName, '/') . '\\b/', $palette['showitem']) > 0 || $entityName === 'palette:' . $paletteName) {
                                         $newPosition = $positionIdentifier . ':--palette--;;' . $paletteName;
                                     }
                                     break;
@@ -262,7 +262,7 @@ class ExtensionManagementUtility
                                         $fieldExists = true;
                                         continue 2;
                                     }
-                                    if (preg_match('/\\b' . $entityName . '\\b/', $palette['showitem']) > 0) {
+                                    if (preg_match('/\\b' . preg_quote($entityName, '/') . '\\b/', $palette['showitem']) > 0) {
                                         self::addFieldsToPalette($table, $paletteName, $newFieldsString, $position);
                                         // Memorize that we already changed this palette, in case other types also use it
                                         $palettesChanged[$paletteName] = true;
diff --git a/typo3/sysext/core/Tests/Unit/Utility/ExtensionManagementUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/ExtensionManagementUtilityTest.php
index ccfe843ebf50..73e7e94241b2 100644
--- a/typo3/sysext/core/Tests/Unit/Utility/ExtensionManagementUtilityTest.php
+++ b/typo3/sysext/core/Tests/Unit/Utility/ExtensionManagementUtilityTest.php
@@ -385,6 +385,21 @@ class ExtensionManagementUtilityTest extends UnitTestCase
         self::assertEquals('fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldX, --palette--;;newpalette, fieldC1, fieldD, fieldD1', $GLOBALS['TCA'][$table]['types']['typeB']['showitem']);
     }
 
+    /**
+     * @test
+     */
+    public function addToAllTCAtypesAddsBeforeDiv(): void
+    {
+        $showitemDiv = '--div--;LLL:EXT:my_ext/Resources/Private/Language/locallang.xlf:foobar';
+        $table = StringUtility::getUniqueId('tx_coretest_table');
+        $GLOBALS['TCA'] = $this->generateTCAForTable($table);
+        $GLOBALS['TCA'][$table]['types']['typeD']['showitem'] = $showitemDiv . ', ' . $GLOBALS['TCA'][$table]['types']['typeA']['showitem'];
+
+        ExtensionManagementUtility::addToAllTCAtypes($table, 'fieldX', '', 'before:' . $showitemDiv);
+        self::assertEquals('fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldC1, fieldD, fieldD1, fieldX', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
+        self::assertEquals('fieldX, ' . $showitemDiv . ', fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldC1, fieldD, fieldD1', $GLOBALS['TCA'][$table]['types']['typeD']['showitem']);
+    }
+
     /**
      * Tests whether fields can be added to a palette before existing elements.
      *
-- 
GitLab