From e2e59fb3db537bc7b2c0baa00267edfdd415035c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20B=C3=BCrk?= <stefan@buerk.tech>
Date: Wed, 14 Jul 2021 20:15:37 +0200
Subject: [PATCH] [BUGFIX] Remove prefixed scriptName from urlPath in
 PageRouter

Remove resolved scriptName with leading slash from url in
PageRouter matchRequest method.

This prevent to change the url to a invalid url if
PageTypeSuffix Decorator with .php is used and a page slugs
ends in index.

Resolves: #94537
Releases: master, 10.4, 9.5
Change-Id: I5057bb6888c228a4ca5b53d363ecf1bc7a6af1c6
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/69854
Tested-by: core-ci <typo3@b13.com>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Benni Mack <benni@typo3.org>
---
 .../core/Classes/Routing/PageRouter.php       |  2 +-
 .../PageTypeDecoratorTest.php                 |  8 +++++++-
 .../Framework/Builder/Builder.php             | 19 +++++++++++++++++++
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/typo3/sysext/core/Classes/Routing/PageRouter.php b/typo3/sysext/core/Classes/Routing/PageRouter.php
index 4f70dfc2cf64..b89eb4be7654 100644
--- a/typo3/sysext/core/Classes/Routing/PageRouter.php
+++ b/typo3/sysext/core/Classes/Routing/PageRouter.php
@@ -147,7 +147,7 @@ class PageRouter implements RouterInterface
             if ($normalizedParams instanceof NormalizedParams) {
                 $scriptName = ltrim($normalizedParams->getScriptName(), '/');
                 if ($scriptName !== '' && strpos($urlPath, $scriptName) !== false) {
-                    $urlPath = str_replace($scriptName, '', $urlPath);
+                    $urlPath = str_replace('/' . $scriptName, '', $urlPath);
                 }
             }
         }
diff --git a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerSiteRequest/PageTypeDecoratorTest.php b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerSiteRequest/PageTypeDecoratorTest.php
index 3c2f1cd76d17..2c4b6fa883dd 100644
--- a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerSiteRequest/PageTypeDecoratorTest.php
+++ b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerSiteRequest/PageTypeDecoratorTest.php
@@ -94,11 +94,17 @@ class PageTypeDecoratorTest extends AbstractEnhancerSiteRequestTest
                         )
                     )
             ;
+
             $testSets = array_merge(
                 $testSets,
-                [$testSetForPageContainingIndexInSlug->describe() => [$testSetForPageContainingIndexInSlug]]
+                [
+                    $testSetForPageContainingIndexInSlug->describe() => [
+                        $testSetForPageContainingIndexInSlug,
+                    ],
+                ],
             );
         }
+
         return $testSets;
     }
 
diff --git a/typo3/sysext/frontend/Tests/Functional/SiteHandling/Framework/Builder/Builder.php b/typo3/sysext/frontend/Tests/Functional/SiteHandling/Framework/Builder/Builder.php
index 05daa95b18d3..8c3ad7b7d37d 100644
--- a/typo3/sysext/frontend/Tests/Functional/SiteHandling/Framework/Builder/Builder.php
+++ b/typo3/sysext/frontend/Tests/Functional/SiteHandling/Framework/Builder/Builder.php
@@ -126,6 +126,16 @@ class Builder
                 'menu.json' => 10,
             ],
         ];
+        $multipleTypesConfigurationDotPhp = [
+            'type' => 'PageType',
+            'default' => '.php',
+            'index' => 'index',
+            'map' => [
+                '.php' =>  0,
+                'menu.json' =>  10,
+                '.xml' => 20
+            ],
+        ];
 
         return [
             PageTypeDeclaration::create('null ".html"')
@@ -156,6 +166,15 @@ class Builder
                 ->withGenerateParameters(['&type=0'])
                 ->withResolveArguments(['pageType' => 0])
                 ->withVariables(Variables::create(['pathSuffix' => '/', 'index' => ''])),
+            PageTypeDeclaration::create('null ".php"')
+                ->withConfiguration($multipleTypesConfigurationDotPhp)
+                ->withResolveArguments(['pageType' => 0])
+                ->withVariables(Variables::create(['pathSuffix' => '.php', 'index' => 'index'])),
+            PageTypeDeclaration::create('0 ".php"')
+                ->withConfiguration($multipleTypesConfigurationDotPhp)
+                ->withGenerateParameters(['&type=0'])
+                ->withResolveArguments(['pageType' => 0])
+                ->withVariables(Variables::create(['pathSuffix' => '.php', 'index' => 'index'])),
         ];
     }
 
-- 
GitLab