Skip to content

Commit 863789d

Browse files
committed
prevent silent failure when accessing an invalid superglobal
1 parent 3303443 commit 863789d

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

system/Superglobals.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace CodeIgniter;
1515

16+
use CodeIgniter\Exceptions\InvalidArgumentException;
17+
1618
/**
1719
* Superglobals manipulation.
1820
*
@@ -348,6 +350,8 @@ public function setFilesArray(array $array): void
348350
* @param string $name The superglobal name (server, get, post, cookie, files, request)
349351
*
350352
* @return array<string, server_items>
353+
*
354+
* @throws InvalidArgumentException If the superglobal name is invalid
351355
*/
352356
public function getGlobalArray(string $name): array
353357
{
@@ -358,7 +362,9 @@ public function getGlobalArray(string $name): array
358362
'cookie' => $this->cookie,
359363
'files' => $this->files,
360364
'request' => $this->request,
361-
default => [],
365+
default => throw new InvalidArgumentException(
366+
"Invalid superglobal name '{$name}'. Must be one of: server, get, post, cookie, files, request.",
367+
),
362368
};
363369
}
364370

@@ -367,6 +373,8 @@ public function getGlobalArray(string $name): array
367373
*
368374
* @param string $name The superglobal name (server, get, post, cookie, files, request)
369375
* @param array<string, server_items> $array The array to set
376+
*
377+
* @throws InvalidArgumentException If the superglobal name is invalid
370378
*/
371379
public function setGlobalArray(string $name, array $array): void
372380
{
@@ -377,7 +385,9 @@ public function setGlobalArray(string $name, array $array): void
377385
'cookie' => $this->setCookieArray($array),
378386
'files' => $this->setFilesArray($array),
379387
'request' => $this->setRequestArray($array),
380-
default => null,
388+
default => throw new InvalidArgumentException(
389+
"Invalid superglobal name '{$name}'. Must be one of: server, get, post, cookie, files, request.",
390+
),
381391
};
382392
}
383393
}

tests/system/SuperglobalsTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter;
1515

16+
use CodeIgniter\Exceptions\InvalidArgumentException;
1617
use CodeIgniter\Test\CIUnitTestCase;
1718
use PHPUnit\Framework\Attributes\BackupGlobals;
1819
use PHPUnit\Framework\Attributes\Group;
@@ -385,9 +386,12 @@ public function testGetGlobalArrayForFiles(): void
385386
$this->assertSame($filesData, $this->superglobals->getGlobalArray('files'));
386387
}
387388

388-
public function testGetGlobalArrayReturnsEmptyForInvalid(): void
389+
public function testGetGlobalArrayThrowsExceptionForInvalidName(): void
389390
{
390-
$this->assertSame([], $this->superglobals->getGlobalArray('invalid'));
391+
$this->expectException(InvalidArgumentException::class);
392+
$this->expectExceptionMessage("Invalid superglobal name 'invalid'. Must be one of: server, get, post, cookie, files, request.");
393+
394+
$this->superglobals->getGlobalArray('invalid');
391395
}
392396

393397
public function testSetGlobalArray(): void
@@ -418,12 +422,12 @@ public function testSetGlobalArrayForFiles(): void
418422
$this->assertSame($filesData, $_FILES);
419423
}
420424

421-
public function testSetGlobalArrayWithInvalidNameDoesNothing(): void
425+
public function testSetGlobalArrayThrowsExceptionForInvalidName(): void
422426
{
423-
$this->superglobals->setGlobalArray('invalid', ['key' => 'value']);
427+
$this->expectException(InvalidArgumentException::class);
428+
$this->expectExceptionMessage("Invalid superglobal name 'invalid'. Must be one of: server, get, post, cookie, files, request.");
424429

425-
// Should not throw exception, just does nothing
426-
$this->assertTrue(true);
430+
$this->superglobals->setGlobalArray('invalid', ['key' => 'value']);
427431
}
428432

429433
// Constructor tests

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 2199 errors
1+
# total 2198 errors
22

33
includes:
44
- argument.type.neon

utils/phpstan-baseline/method.alreadyNarrowedType.neon

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 23 errors
1+
# total 22 errors
22

33
parameters:
44
ignoreErrors:
@@ -77,11 +77,6 @@ parameters:
7777
count: 3
7878
path: ../../tests/system/Security/SecurityTest.php
7979

80-
-
81-
message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertTrue\(\) with true will always evaluate to true\.$#'
82-
count: 1
83-
path: ../../tests/system/SuperglobalsTest.php
84-
8580
-
8681
message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertIsNumeric\(\) with int will always evaluate to true\.$#'
8782
count: 1

0 commit comments

Comments
 (0)