Skip to content

Commit c70b617

Browse files
committed
fix: sync \ after \ is updated in SiteURIFactory
SiteURIFactory updates \ (and \['QUERY_STRING']) when it detects the route path, but \ was left stale. Since PHP populates \ only once at the start of the request, getVar() (which reads \) returned outdated values, breaking \->withRequest() for GET parameters. Add Superglobals::syncRequest() to rebuild \ from \, \, and \ according to request_order/variables_order, and call it from SiteURIFactory after setGetArray(). Fixes #9872
1 parent fba4362 commit c70b617

4 files changed

Lines changed: 59 additions & 2 deletions

File tree

‎system/HTTP/SiteURIFactory.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private function parseRequestURI(): string
171171

172172
// Update our global GET for values likely to have been changed
173173
parse_str($this->superglobals->server('QUERY_STRING'), $get);
174-
$this->superglobals->setGetArray($get);
174+
$this->superglobals->setGetArray($get)->syncRequest();
175175

176176
return URI::removeDotSegments($path);
177177
}
@@ -203,7 +203,7 @@ private function parseQueryString(): string
203203

204204
// Update our global GET for values likely to have been changed
205205
parse_str($this->superglobals->server('QUERY_STRING'), $get);
206-
$this->superglobals->setGetArray($get);
206+
$this->superglobals->setGetArray($get)->syncRequest();
207207

208208
return URI::removeDotSegments($path);
209209
}

‎system/Superglobals.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,34 @@ public function setRequestArray(array $array): self
387387
return $this;
388388
}
389389

390+
/**
391+
* Rebuilds $_REQUEST from $_GET, $_POST, and $_COOKIE according to the
392+
* `request_order` (or `variables_order`) ini setting.
393+
*
394+
* PHP populates $_REQUEST only once at the start of the request. When
395+
* $_GET is modified later (e.g. by SiteURIFactory), $_REQUEST becomes
396+
* stale. This method re-synchronizes $_REQUEST with the current values.
397+
*
398+
* @return self
399+
*/
400+
public function syncRequest(): self
401+
{
402+
$requestOrder = ini_get('request_order') ?: ini_get('variables_order') ?: 'GP';
403+
404+
$request = [];
405+
406+
foreach (str_split($requestOrder) as $type) {
407+
match ($type) {
408+
'G' => $request = array_merge($request, $this->get),
409+
'P' => $request = array_merge($request, $this->post),
410+
'C' => $request = array_merge($request, $this->cookie),
411+
default => null,
412+
};
413+
}
414+
415+
return $this->setRequestArray($request);
416+
}
417+
390418
/**
391419
* Get all $_FILES values.
392420
*

‎tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ public function testQueryStringWithQueryString(): void
256256
$this->assertSame($expected, $factory->detectRoutePath('QUERY_STRING'));
257257
$this->assertSame('code=good', $_SERVER['QUERY_STRING']); // @phpstan-ignore codeigniter.superglobalsOffsetAccess (checks the live superglobal, not the snapshot service)
258258
$this->assertSame(['code' => 'good'], $_GET);
259+
$this->assertSame(['code' => 'good'], $_REQUEST); // @phpstan-ignore codeigniter.superglobalsOffsetAccess (checks the live superglobal, not the snapshot service)
259260
}
260261

261262
public function testQueryStringEmpty(): void

‎tests/system/SuperglobalsTest.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,34 @@ public function testRequestSetArray(): void
302302
$this->assertSame($data, $_REQUEST);
303303
}
304304

305+
public function testSyncRequestRebuildsRequestFromGetPostCookie(): void
306+
{
307+
$this->superglobals->setGetArray(['get_key' => 'get_value']);
308+
$this->superglobals->setPostArray(['post_key' => 'post_value']);
309+
$this->superglobals->setCookieArray(['cookie_key' => 'cookie_value']);
310+
311+
$this->superglobals->syncRequest();
312+
313+
$this->assertSame('get_value', $this->superglobals->request('get_key'));
314+
$this->assertSame('post_value', $this->superglobals->request('post_key'));
315+
$this->assertSame('cookie_value', $this->superglobals->request('cookie_key'));
316+
$this->assertSame('get_value', $_REQUEST['get_key']); // @phpstan-ignore codeigniter.superglobalsOffsetAccess (checks the live superglobal, not the snapshot service)
317+
}
318+
319+
public function testSyncRequestReflectsGetChanges(): void
320+
{
321+
$this->superglobals->setGetArray(['key' => 'old']);
322+
$this->superglobals->syncRequest();
323+
324+
$this->assertSame('old', $this->superglobals->request('key'));
325+
326+
// Simulate SiteURIFactory updating $_GET after the request started.
327+
$this->superglobals->setGetArray(['key' => 'new']);
328+
$this->superglobals->syncRequest();
329+
330+
$this->assertSame('new', $this->superglobals->request('key'));
331+
}
332+
305333
// $_FILES tests
306334
public function testFilesGetArray(): void
307335
{

0 commit comments

Comments
 (0)