Skip to content

Commit 9d7d143

Browse files
committed
fix: distinguish request overrides from cached globals
1 parent 34d46b9 commit 9d7d143

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎system/HTTP/IncomingRequest.php‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
*/
5050
class IncomingRequest extends Request
5151
{
52+
/** Distinguishes an explicit request override from fetchGlobal()'s cache. */
53+
private bool $requestGlobalWasExplicitlySet = false;
54+
5255
/**
5356
* The URI for this request.
5457
*
@@ -389,20 +392,36 @@ public function getVar($index = null, $filter = null, $flags = null)
389392
}
390393

391394
// Preserve request data explicitly supplied through setGlobal().
392-
if (isset($this->globals['request'])) {
395+
if ($this->requestGlobalWasExplicitlySet) {
393396
return $this->fetchGlobal('request', $index, $filter, $flags);
394397
}
395398

396399
// $_REQUEST can become stale when SiteURIFactory updates $_GET.
397400
// Use the existing filtering path with a fresh merged view, without
398401
// caching it between calls or modifying the superglobals.
402+
$cachedRequest = $this->globals['request'] ?? null;
399403
$this->globals['request'] = service('superglobals')->getRequestData();
400404

401405
try {
402406
return $this->fetchGlobal('request', $index, $filter, $flags);
403407
} finally {
404-
unset($this->globals['request']);
408+
if ($cachedRequest === null) {
409+
unset($this->globals['request']);
410+
} else {
411+
$this->globals['request'] = $cachedRequest;
412+
}
413+
}
414+
}
415+
416+
public function setGlobal(string $name, $value)
417+
{
418+
parent::setGlobal($name, $value);
419+
420+
if ($name === 'request') {
421+
$this->requestGlobalWasExplicitlySet = true;
405422
}
423+
424+
return $this;
406425
}
407426

408427
/**

‎tests/system/HTTP/IncomingRequestTest.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ public function testGetVarRefreshesMergedDataAfterFirstRead(): void
112112
$this->assertSame('new', $this->request->getVar('foo'));
113113
}
114114

115+
public function testGetVarDoesNotTreatFetchGlobalCacheAsRequestOverride(): void
116+
{
117+
service('superglobals')
118+
->setGetArray(['foo' => 'fresh'])
119+
->setRequestArray(['foo' => 'stale']);
120+
121+
$this->assertSame('stale', $this->request->fetchGlobal('request', 'foo'));
122+
$this->assertSame('fresh', $this->request->getVar('foo'));
123+
$this->assertSame('stale', $this->request->fetchGlobal('request', 'foo'));
124+
125+
$this->request->setGlobal('request', ['foo' => 'explicit']);
126+
127+
$this->assertSame('explicit', $this->request->getVar('foo'));
128+
}
129+
115130
public function testGetVarClearsMergedDataAfterFilterThrows(): void
116131
{
117132
service('superglobals')->setGetArray(['foo' => 'old']);

0 commit comments

Comments
 (0)