Skip to content

Commit 34d46b9

Browse files
committed
fix: preserve request overrides and PHP request-order semantics
Reuse fetchGlobal for merged request data, preserve explicit request globals, and clear temporary data after filtering. Remove the added protected helper to avoid subclass signature collisions. Normalize request-order case and process each source once.
1 parent d0522b8 commit 34d46b9

6 files changed

Lines changed: 93 additions & 29 deletions

File tree

‎system/HTTP/IncomingRequest.php‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,21 @@ public function getVar($index = null, $filter = null, $flags = null)
388388
return $this->getJsonVar($index, false, $filter, $flags);
389389
}
390390

391-
// $_REQUEST is populated only once at the start of the request, so it
392-
// can become stale when $_GET is modified later (e.g. by SiteURIFactory).
393-
// Merge the current superglobals instead of reading the stale $_REQUEST.
394-
$data = service('superglobals')->getRequestData();
391+
// Preserve request data explicitly supplied through setGlobal().
392+
if (isset($this->globals['request'])) {
393+
return $this->fetchGlobal('request', $index, $filter, $flags);
394+
}
395+
396+
// $_REQUEST can become stale when SiteURIFactory updates $_GET.
397+
// Use the existing filtering path with a fresh merged view, without
398+
// caching it between calls or modifying the superglobals.
399+
$this->globals['request'] = service('superglobals')->getRequestData();
395400

396-
return $this->fetchFromArray($data, $index, $filter, $flags);
401+
try {
402+
return $this->fetchGlobal('request', $index, $filter, $flags);
403+
} finally {
404+
unset($this->globals['request']);
405+
}
397406
}
398407

399408
/**

‎system/HTTP/RequestTrait.php‎

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -291,22 +291,6 @@ public function fetchGlobal(string $name, $index = null, ?int $filter = null, $f
291291
$this->populateGlobals($name);
292292
}
293293

294-
return $this->fetchFromArray($this->globals[$name], $index, $filter, $flags);
295-
}
296-
297-
/**
298-
* Fetches one or more items from an array, applying the same filtering
299-
* and index resolution as fetchGlobal().
300-
*
301-
* @param array<string, mixed> $data
302-
* @param int|list<string>|string|null $index
303-
* @param int|null $filter Filter constant
304-
* @param array<string, mixed>|int|null $flags Options
305-
*
306-
* @return mixed
307-
*/
308-
protected function fetchFromArray(array $data, $index = null, ?int $filter = null, $flags = null)
309-
{
310294
// Null filters cause null values to return.
311295
$filter ??= FILTER_UNSAFE_RAW;
312296
$flags = is_array($flags) ? $flags : (is_numeric($flags) ? (int) $flags : 0);
@@ -315,9 +299,9 @@ protected function fetchFromArray(array $data, $index = null, ?int $filter = nul
315299
if ($index === null) {
316300
$values = [];
317301

318-
foreach ($data as $key => $value) {
302+
foreach ($this->globals[$name] as $key => $value) {
319303
$values[$key] = is_array($value)
320-
? $this->fetchFromArray($data, $key, $filter, $flags)
304+
? $this->fetchGlobal($name, $key, $filter, $flags)
321305
: filter_var($value, $filter, $flags);
322306
}
323307

@@ -329,15 +313,15 @@ protected function fetchFromArray(array $data, $index = null, ?int $filter = nul
329313
$output = [];
330314

331315
foreach ($index as $key) {
332-
$output[$key] = $this->fetchFromArray($data, $key, $filter, $flags);
316+
$output[$key] = $this->fetchGlobal($name, $key, $filter, $flags);
333317
}
334318

335319
return $output;
336320
}
337321

338322
// Does the index contain array notation?
339323
if (is_string($index) && ($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) {
340-
$value = $data;
324+
$value = $this->globals[$name];
341325

342326
for ($i = 0; $i < $count; $i++) {
343327
$key = trim($matches[0][$i], '[]');
@@ -354,7 +338,7 @@ protected function fetchFromArray(array $data, $index = null, ?int $filter = nul
354338
}
355339
}
356340

357-
$value ??= $data[$index] ?? null;
341+
$value ??= $this->globals[$name][$index] ?? null;
358342

359343
if (is_array($value)
360344
&& (

‎system/Superglobals.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ public function getRequestData(?string $requestOrder = null): array
417417

418418
$request = [];
419419

420-
foreach (str_split($requestOrder) as $type) {
420+
foreach (array_unique(str_split(strtoupper($requestOrder))) as $type) {
421421
match ($type) {
422422
// array_replace_recursive() matches PHP's own $_REQUEST merge
423423
// (php_autoglobal_merge): numeric keys are preserved and

‎tests/system/HTTP/IncomingRequestTest.php‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use PHPUnit\Framework\Attributes\PreserveGlobalState;
3030
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
3131
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
32+
use RuntimeException;
3233

3334
/**
3435
* @internal
@@ -87,6 +88,65 @@ public function testGetVarReflectsGetChangesWhenRequestIsStale(): void
8788
$this->assertSame('good', $this->request->getVar('code'));
8889
}
8990

91+
public function testGetVarPreservesExplicitRequestOverride(): void
92+
{
93+
service('superglobals')->setGetArray(['foo' => 'get', 'extra' => 'value']);
94+
$this->request->setGlobal('request', ['foo' => 'bar']);
95+
96+
$this->assertSame('bar', $this->request->getVar('foo'));
97+
$this->assertNull($this->request->getVar('extra'));
98+
$this->assertSame(['foo' => 'bar'], $this->request->getVar());
99+
100+
$this->request->setGlobal('request', []);
101+
102+
$this->assertNull($this->request->getVar('foo'));
103+
}
104+
105+
public function testGetVarRefreshesMergedDataAfterFirstRead(): void
106+
{
107+
service('superglobals')->setGetArray(['foo' => 'old']);
108+
$this->assertSame('old', $this->request->getVar('foo'));
109+
110+
service('superglobals')->setGet('foo', 'new');
111+
112+
$this->assertSame('new', $this->request->getVar('foo'));
113+
}
114+
115+
public function testGetVarClearsMergedDataAfterFilterThrows(): void
116+
{
117+
service('superglobals')->setGetArray(['foo' => 'old']);
118+
119+
try {
120+
$this->request->getVar('foo', FILTER_CALLBACK, [
121+
'options' => static function (): never {
122+
throw new RuntimeException('Filter failed');
123+
},
124+
]);
125+
$this->fail('The filter should throw an exception.');
126+
} catch (RuntimeException $e) {
127+
$this->assertSame('Filter failed', $e->getMessage());
128+
}
129+
130+
service('superglobals')->setGet('foo', 'new');
131+
132+
$this->assertSame('new', $this->request->getVar('foo'));
133+
}
134+
135+
public function testGetVarAllowsSubclassWithFetchFromArrayMethod(): void
136+
{
137+
$config = new App();
138+
$request = new class ($config, new SiteURI($config), null, new UserAgent()) extends IncomingRequest {
139+
protected function fetchFromArray(): string
140+
{
141+
return 'application helper';
142+
}
143+
};
144+
145+
service('superglobals')->setGetArray(['foo' => 'bar']);
146+
147+
$this->assertSame('bar', $request->getVar('foo'));
148+
}
149+
90150
public function testCanGrabGetVars(): void
91151
{
92152
service('superglobals')->setGet('TEST', '5');

‎tests/system/SuperglobalsTest.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,17 @@ public function testGetRequestDataRespectsOrder(): void
350350
$this->assertSame('get', $this->superglobals->getRequestData('PG')['shared']);
351351
}
352352

353+
public function testGetRequestDataNormalizesOrderAndIgnoresDuplicates(): void
354+
{
355+
$this->superglobals->setGetArray(['shared' => 'get']);
356+
$this->superglobals->setPostArray(['shared' => 'post']);
357+
$this->superglobals->setCookieArray(['shared' => 'cookie']);
358+
359+
$this->assertSame('post', $this->superglobals->getRequestData('gp')['shared']);
360+
$this->assertSame('post', $this->superglobals->getRequestData('GPG')['shared']);
361+
$this->assertSame('cookie', $this->superglobals->getRequestData('gPcGpC')['shared']);
362+
}
363+
353364
public function testGetRequestDataIgnoresUnknownOrderTypes(): void
354365
{
355366
$this->superglobals->setGetArray(['get_key' => 'get_value']);

‎user_guide_src/source/incoming/incomingrequest.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,15 @@ getVar()
162162
in new projects. Even if you are already using it, we recommend that you use
163163
another, more appropriate method.
164164

165-
The ``getVar()`` method returns a merged view of ``$_GET``, ``$_POST``, and ``$_COOKIE`` (depending on php.ini `request-order <https://www.php.net/manual/en/ini.core.php#ini.request-order>`_). It does not read or modify ``$_REQUEST``.
165+
The ``getVar()`` method returns a merged view of ``$_GET``, ``$_POST``, and ``$_COOKIE`` (depending on php.ini `request-order <https://www.php.net/manual/en/ini.core.php#ini.request-order>`_). It does not read or modify ``$_REQUEST``. Data explicitly supplied through ``setGlobal('request', ...)`` takes precedence over the merged view.
166166

167167
.. warning:: If you want to validate POST data only, don't use ``getVar()``.
168168
Newer values override older values. POST values may be overridden by the
169169
cookies if they have the same name, and you set "C" after "P" in
170170
`request-order <https://www.php.net/manual/en/ini.core.php#ini.request-order>`_.
171171

172172
.. note:: If the incoming request has a ``Content-Type`` header set to ``application/json``,
173-
the ``getVar()`` method returns the JSON data instead of ``$_REQUEST`` data.
173+
the ``getVar()`` method returns the JSON data instead of the merged superglobal data.
174174

175175
.. _incomingrequest-getting-json-data:
176176

0 commit comments

Comments
 (0)