Skip to content

Commit da4e508

Browse files
authored
fix: detect X-Forwarded-Proto case-insensitively in isSecure() (#10627)
1 parent 1c0920d commit da4e508

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎system/HTTP/IncomingRequest.php‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,18 @@ public function isSecure(): bool
274274
return false;
275275
}
276276

277-
if ($this->hasHeader('X-Forwarded-Proto') && $this->header('X-Forwarded-Proto')->getValue() === 'https') {
278-
return true;
277+
if ($this->hasHeader('X-Forwarded-Proto')) {
278+
$proto = $this->header('X-Forwarded-Proto')->getValue();
279+
280+
// A single value may be a string or a one-element array. Multiple
281+
// values are ambiguous and cannot be trusted, so they are ignored.
282+
if (is_array($proto)) {
283+
$proto = count($proto) === 1 ? reset($proto) : null;
284+
}
285+
286+
if (is_string($proto) && strtolower(trim($proto)) === 'https') {
287+
return true;
288+
}
279289
}
280290

281291
if (! $this->hasHeader('Front-End-Https')) {

‎tests/system/HTTP/IncomingRequestTest.php‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,12 @@ public static function provideIsSecureWithForwardedHeaders(): iterable
828828
'X-Forwarded-Proto trusted proxy IP' => [
829829
'X-Forwarded-Proto', 'https', '10.0.1.200', ['10.0.1.200' => 'X-Forwarded-For'], true,
830830
],
831+
'X-Forwarded-Proto trusted proxy IP uppercase' => [
832+
'X-Forwarded-Proto', 'HTTPS', '10.0.1.200', ['10.0.1.200' => 'X-Forwarded-For'], true,
833+
],
834+
'X-Forwarded-Proto trusted proxy IP with surrounding whitespace' => [
835+
'X-Forwarded-Proto', ' https ', '10.0.1.200', ['10.0.1.200' => 'X-Forwarded-For'], true,
836+
],
831837
'X-Forwarded-Proto no trusted proxies' => [
832838
'X-Forwarded-Proto', 'https', '10.0.1.200', [], false,
833839
],
@@ -879,6 +885,32 @@ public static function provideIsSecureWithForwardedHeaders(): iterable
879885
];
880886
}
881887

888+
public function testIsSecureWithForwardedProtoSingleElementArray(): void
889+
{
890+
service('superglobals')->setServer('REMOTE_ADDR', '10.0.1.200');
891+
892+
$config = new App();
893+
$config->proxyIPs = ['10.0.1.200' => 'X-Forwarded-For'];
894+
895+
$request = $this->createRequest($config);
896+
$request->setHeader('X-Forwarded-Proto', ['https']);
897+
898+
$this->assertTrue($request->isSecure());
899+
}
900+
901+
public function testIsSecureWithForwardedProtoMultipleValuesIsNotSecure(): void
902+
{
903+
service('superglobals')->setServer('REMOTE_ADDR', '10.0.1.200');
904+
905+
$config = new App();
906+
$config->proxyIPs = ['10.0.1.200' => 'X-Forwarded-For'];
907+
908+
$request = $this->createRequest($config);
909+
$request->setHeader('X-Forwarded-Proto', ['https', 'http']);
910+
911+
$this->assertFalse($request->isSecure());
912+
}
913+
882914
public function testUserAgent(): void
883915
{
884916
service('superglobals')->setServer('HTTP_USER_AGENT', 'Mozilla');

‎user_guide_src/source/changelogs/v4.7.6.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Bugs Fixed
4141
- **Security:** Fixed a bug where a valid JSON request body that is not an object
4242
(for example a JSON array) was corrupted after CSRF verification when the
4343
token was sent via the ``X-CSRF-TOKEN`` header.
44+
- **HTTP:** Fixed a bug where ``IncomingRequest::isSecure()`` did not
45+
recognize ``https`` in the ``X-Forwarded-Proto`` header from a trusted
46+
proxy when the value contained uppercase letters or surrounding
47+
whitespace, or was represented as a single-element array.
4448

4549
See the repo's
4650
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

0 commit comments

Comments
 (0)