From d88a9041c70a606338c643bce516292b8b4fce33 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Thu, 12 Nov 2026 02:56:56 +0100 Subject: [PATCH] fix(HTTP): fix query string truncation in SiteURIFactory::parseRequestURI() --- docs/changelogs/v4.7.7.md | 1 + system/HTTP/SiteURIFactory.php | 2 +- .../SiteURIFactoryDetectRoutePathTest.php | 67 +++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index c52264820154..bf14b3617c87 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -20,6 +20,7 @@ Release Date: Unreleased - **Database:** Fixed a bug where `SQLSRV\Builder::_upsertBatch()` concatenated `$table` unescaped into `Object_ID()`, causing syntax errors for table names with quotes or special characters. - **Database:** Fixed a bug where `SQLSRV\Forge::_alterTable()` interpolated table and column names without escaping in default constraint lookups. - **Database:** Fixed a bug where `SQLite3\Table::dropIndexes()` used single-quoted string literals instead of escaped identifiers in `DROP INDEX IF EXISTS`, causing syntax errors on index names containing quotes or special characters. +- **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access. - **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names. - **Images:** Fixed a bug in `ImageMagickHandler::save()` where saving without an extension or with an unsupported extension threw an `ImagickException` instead of falling back to the image's format. - **Security:** Fixed a bug where `Security::__construct()` threw a `TypeError` when the CSRF cookie was an array, causing an HTTP 500 response for affected requests. diff --git a/system/HTTP/SiteURIFactory.php b/system/HTTP/SiteURIFactory.php index 3533b8383c6a..56fa6dacea6f 100644 --- a/system/HTTP/SiteURIFactory.php +++ b/system/HTTP/SiteURIFactory.php @@ -160,7 +160,7 @@ private function parseRequestURI(): string if (trim($path, '/') === '' && str_starts_with($query, '/')) { $parts = explode('?', $query, 2); $path = $parts[0]; - $newQuery = $query[1] ?? ''; + $newQuery = $parts[1] ?? ''; $this->superglobals->setServer('QUERY_STRING', $newQuery); } else { diff --git a/tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php b/tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php index a61f19a2fefe..43a80e58d1e1 100644 --- a/tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php +++ b/tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php @@ -258,6 +258,73 @@ public function testQueryStringWithQueryString(): void $this->assertSame(['code' => 'good'], $_GET); } + public function testRequestURIWithQueryBasedRoutingAndQueryParameters(): void + { + // /index.php?/ci/woot?code=good#pos + service('superglobals') + ->setServer('REQUEST_URI', '/index.php?/ci/woot?code=good') + ->setServer('SCRIPT_NAME', '/index.php') + ->setGet('/ci/woot?code', 'good'); + + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); + + $expected = 'ci/woot'; + $this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI')); + $this->assertSame('code=good', $_SERVER['QUERY_STRING']); // @phpstan-ignore codeigniter.superglobalsOffsetAccess (checks the live superglobal, not the snapshot service) + $this->assertSame(['code' => 'good'], $_GET); + } + + public function testRequestURIWithQueryBasedRoutingWithoutAdditionalQuery(): void + { + // /index.php?/ci/woot + service('superglobals') + ->setServer('REQUEST_URI', '/index.php?/ci/woot') + ->setServer('SCRIPT_NAME', '/index.php') + ->setGet('/ci/woot', ''); + + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); + + $expected = 'ci/woot'; + $this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI')); + $this->assertSame('', $_SERVER['QUERY_STRING']); // @phpstan-ignore codeigniter.superglobalsOffsetAccess (checks the live superglobal, not the snapshot service) + $this->assertSame([], $_GET); + } + + public function testRequestURIWithQueryBasedRoutingPreservesSubsequentQuestionMarks(): void + { + // /index.php?/ci/woot?redirect=/a?b=1 + service('superglobals') + ->setServer('REQUEST_URI', '/index.php?/ci/woot?redirect=/a?b=1') + ->setServer('SCRIPT_NAME', '/index.php') + ->setGet('/ci/woot?redirect', '/a?b=1'); + + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); + + $expected = 'ci/woot'; + $this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI')); + // Only the first '?' separates the path from the query string, so the + // remaining question marks must be kept inside the query string. + $this->assertSame('redirect=/a?b=1', $_SERVER['QUERY_STRING']); // @phpstan-ignore codeigniter.superglobalsOffsetAccess (checks the live superglobal, not the snapshot service) + $this->assertSame(['redirect' => '/a?b=1'], $_GET); + } + + public function testRequestURIWithQueryBasedRoutingWithTrailingQuestionMark(): void + { + // /index.php?/ci/woot? + service('superglobals') + ->setServer('REQUEST_URI', '/index.php?/ci/woot?') + ->setServer('SCRIPT_NAME', '/index.php') + ->setGet('/ci/woot?', ''); + + $factory = $this->createSiteURIFactory(service('superglobals')->getServerArray()); + + $expected = 'ci/woot'; + $this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI')); + // A trailing '?' keeps an empty query string, not the '?' itself. + $this->assertSame('', $_SERVER['QUERY_STRING']); // @phpstan-ignore codeigniter.superglobalsOffsetAccess (checks the live superglobal, not the snapshot service) + $this->assertSame([], $_GET); + } + public function testQueryStringRefreshesRequestWithoutOverwritingCustomValues(): void { if (! $this->effectiveRequestOrderIncludesGet()) {