Skip to content

Commit d88a904

Browse files
committed
fix(HTTP): fix query string truncation in SiteURIFactory::parseRequestURI()
1 parent 947a7fe commit d88a904

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Release Date: Unreleased
2020
- **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.
2121
- **Database:** Fixed a bug where `SQLSRV\Forge::_alterTable()` interpolated table and column names without escaping in default constraint lookups.
2222
- **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.
23+
- **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access.
2324
- **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.
2425
- **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.
2526
- **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.

‎system/HTTP/SiteURIFactory.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private function parseRequestURI(): string
160160
if (trim($path, '/') === '' && str_starts_with($query, '/')) {
161161
$parts = explode('?', $query, 2);
162162
$path = $parts[0];
163-
$newQuery = $query[1] ?? '';
163+
$newQuery = $parts[1] ?? '';
164164

165165
$this->superglobals->setServer('QUERY_STRING', $newQuery);
166166
} else {

‎tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,73 @@ public function testQueryStringWithQueryString(): void
258258
$this->assertSame(['code' => 'good'], $_GET);
259259
}
260260

261+
public function testRequestURIWithQueryBasedRoutingAndQueryParameters(): void
262+
{
263+
// /index.php?/ci/woot?code=good#pos
264+
service('superglobals')
265+
->setServer('REQUEST_URI', '/index.php?/ci/woot?code=good')
266+
->setServer('SCRIPT_NAME', '/index.php')
267+
->setGet('/ci/woot?code', 'good');
268+
269+
$factory = $this->createSiteURIFactory(service('superglobals')->getServerArray());
270+
271+
$expected = 'ci/woot';
272+
$this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI'));
273+
$this->assertSame('code=good', $_SERVER['QUERY_STRING']); // @phpstan-ignore codeigniter.superglobalsOffsetAccess (checks the live superglobal, not the snapshot service)
274+
$this->assertSame(['code' => 'good'], $_GET);
275+
}
276+
277+
public function testRequestURIWithQueryBasedRoutingWithoutAdditionalQuery(): void
278+
{
279+
// /index.php?/ci/woot
280+
service('superglobals')
281+
->setServer('REQUEST_URI', '/index.php?/ci/woot')
282+
->setServer('SCRIPT_NAME', '/index.php')
283+
->setGet('/ci/woot', '');
284+
285+
$factory = $this->createSiteURIFactory(service('superglobals')->getServerArray());
286+
287+
$expected = 'ci/woot';
288+
$this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI'));
289+
$this->assertSame('', $_SERVER['QUERY_STRING']); // @phpstan-ignore codeigniter.superglobalsOffsetAccess (checks the live superglobal, not the snapshot service)
290+
$this->assertSame([], $_GET);
291+
}
292+
293+
public function testRequestURIWithQueryBasedRoutingPreservesSubsequentQuestionMarks(): void
294+
{
295+
// /index.php?/ci/woot?redirect=/a?b=1
296+
service('superglobals')
297+
->setServer('REQUEST_URI', '/index.php?/ci/woot?redirect=/a?b=1')
298+
->setServer('SCRIPT_NAME', '/index.php')
299+
->setGet('/ci/woot?redirect', '/a?b=1');
300+
301+
$factory = $this->createSiteURIFactory(service('superglobals')->getServerArray());
302+
303+
$expected = 'ci/woot';
304+
$this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI'));
305+
// Only the first '?' separates the path from the query string, so the
306+
// remaining question marks must be kept inside the query string.
307+
$this->assertSame('redirect=/a?b=1', $_SERVER['QUERY_STRING']); // @phpstan-ignore codeigniter.superglobalsOffsetAccess (checks the live superglobal, not the snapshot service)
308+
$this->assertSame(['redirect' => '/a?b=1'], $_GET);
309+
}
310+
311+
public function testRequestURIWithQueryBasedRoutingWithTrailingQuestionMark(): void
312+
{
313+
// /index.php?/ci/woot?
314+
service('superglobals')
315+
->setServer('REQUEST_URI', '/index.php?/ci/woot?')
316+
->setServer('SCRIPT_NAME', '/index.php')
317+
->setGet('/ci/woot?', '');
318+
319+
$factory = $this->createSiteURIFactory(service('superglobals')->getServerArray());
320+
321+
$expected = 'ci/woot';
322+
$this->assertSame($expected, $factory->detectRoutePath('REQUEST_URI'));
323+
// A trailing '?' keeps an empty query string, not the '?' itself.
324+
$this->assertSame('', $_SERVER['QUERY_STRING']); // @phpstan-ignore codeigniter.superglobalsOffsetAccess (checks the live superglobal, not the snapshot service)
325+
$this->assertSame([], $_GET);
326+
}
327+
261328
public function testQueryStringRefreshesRequestWithoutOverwritingCustomValues(): void
262329
{
263330
if (! $this->effectiveRequestOrderIncludesGet()) {

0 commit comments

Comments
 (0)