From 028bf323c5bee6d7974457d09ed61e6c40587db9 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 12 Feb 2021 07:32:47 +0700 Subject: [PATCH] Fixes #4263 --- system/HTTP/IncomingRequest.php | 14 +++++++----- tests/system/HTTP/IncomingRequestTest.php | 28 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/system/HTTP/IncomingRequest.php b/system/HTTP/IncomingRequest.php index a05c00710a95..2d176f722c0f 100755 --- a/system/HTTP/IncomingRequest.php +++ b/system/HTTP/IncomingRequest.php @@ -718,8 +718,15 @@ protected function parseRequestURI(): string if (isset($_SERVER['SCRIPT_NAME'][0]) && pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_EXTENSION) === 'php') { + // if index.php is implied + if (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) + { + $uri = $uri === '/index.php' + ? '/' + : (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME']))); + } // strip the script name from the beginning of the URI - if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) + elseif (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) { $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME'])); } @@ -728,11 +735,6 @@ protected function parseRequestURI(): string { $uri = (string) substr($uri, strpos($uri, $_SERVER['SCRIPT_NAME']) + strlen($_SERVER['SCRIPT_NAME'])); } - // or if index.php is implied - elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) - { - $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME']))); - } } // This section ensures that even on servers that require the URI to contain the query string (Nginx) a correct diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index 67371cce52d4..53a14af1686f 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -545,4 +545,32 @@ public function testGetPostIndexNotExists() $this->assertNull($this->request->getPostGet('gc')); $this->assertNull($this->request->getGetPost('gc')); } + + public function providePathChecks() + { + return [ + 'not /index.php' => [ + '/test.php', + 'test.php', + ], + '/index.php' => [ + '/index.php', + '/', + ], + ]; + } + + /** + * @dataProvider providePathChecks + */ + public function testExtensionPHP($path, $detectPath) + { + $config = new App(); + $config->baseURL = 'http://example.com/'; + + $_SERVER['REQUEST_URI'] = $path; + $_SERVER['SCRIPT_NAME'] = $path; + $request = new IncomingRequest($config, new URI($path), null, new UserAgent()); + $this->assertEquals($detectPath, $request->detectPath()); + } }