Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,18 @@ public function isSecure(): bool
return false;
}

if ($this->hasHeader('X-Forwarded-Proto') && $this->header('X-Forwarded-Proto')->getValue() === 'https') {
return true;
if ($this->hasHeader('X-Forwarded-Proto')) {
$proto = $this->header('X-Forwarded-Proto')->getValue();

// A single value may be a string or a one-element array. Multiple
// values are ambiguous and cannot be trusted, so they are ignored.
if (is_array($proto)) {
$proto = count($proto) === 1 ? reset($proto) : null;
}

if (is_string($proto) && strtolower(trim($proto)) === 'https') {
return true;
}
}

if (! $this->hasHeader('Front-End-Https')) {
Expand Down
32 changes: 32 additions & 0 deletions tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,12 @@ public static function provideIsSecureWithForwardedHeaders(): iterable
'X-Forwarded-Proto trusted proxy IP' => [
'X-Forwarded-Proto', 'https', '10.0.1.200', ['10.0.1.200' => 'X-Forwarded-For'], true,
],
'X-Forwarded-Proto trusted proxy IP uppercase' => [
'X-Forwarded-Proto', 'HTTPS', '10.0.1.200', ['10.0.1.200' => 'X-Forwarded-For'], true,
],
'X-Forwarded-Proto trusted proxy IP with surrounding whitespace' => [
'X-Forwarded-Proto', ' https ', '10.0.1.200', ['10.0.1.200' => 'X-Forwarded-For'], true,
],
'X-Forwarded-Proto no trusted proxies' => [
'X-Forwarded-Proto', 'https', '10.0.1.200', [], false,
],
Expand Down Expand Up @@ -879,6 +885,32 @@ public static function provideIsSecureWithForwardedHeaders(): iterable
];
}

public function testIsSecureWithForwardedProtoSingleElementArray(): void
{
service('superglobals')->setServer('REMOTE_ADDR', '10.0.1.200');

$config = new App();
$config->proxyIPs = ['10.0.1.200' => 'X-Forwarded-For'];

$request = $this->createRequest($config);
$request->setHeader('X-Forwarded-Proto', ['https']);

$this->assertTrue($request->isSecure());
}

public function testIsSecureWithForwardedProtoMultipleValuesIsNotSecure(): void
{
service('superglobals')->setServer('REMOTE_ADDR', '10.0.1.200');

$config = new App();
$config->proxyIPs = ['10.0.1.200' => 'X-Forwarded-For'];

$request = $this->createRequest($config);
$request->setHeader('X-Forwarded-Proto', ['https', 'http']);

$this->assertFalse($request->isSecure());
}

public function testUserAgent(): void
{
service('superglobals')->setServer('HTTP_USER_AGENT', 'Mozilla');
Expand Down
4 changes: 4 additions & 0 deletions user_guide_src/source/changelogs/v4.7.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Bugs Fixed
- **Security:** Fixed a bug where a valid JSON request body that is not an object
(for example a JSON array) was corrupted after CSRF verification when the
token was sent via the ``X-CSRF-TOKEN`` header.
- **HTTP:** Fixed a bug where ``IncomingRequest::isSecure()`` did not
recognize ``https`` in the ``X-Forwarded-Proto`` header from a trusted
proxy when the value contained uppercase letters or surrounding
whitespace, or was represented as a single-element array.

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