Skip to content
Merged
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
2 changes: 2 additions & 0 deletions docs/changelogs/v4.7.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ Release Date: Unreleased

## Bugs Fixed

- **Security:** Fixed a bug where `Security::__construct()` threw a `TypeError` when the CSRF cookie was an array, causing an HTTP 500 response for affected requests.

See the repo's [CHANGELOG.md](https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md) for a complete list of bugs fixed.
3 changes: 2 additions & 1 deletion system/Security/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ public function __construct(SecurityConfig $config)
}

$this->request = service('request');
$this->hashInCookie = $this->request->getCookie($this->cookieName);
$hashInCookie = $this->request->getCookie($this->cookieName);
$this->hashInCookie = is_string($hashInCookie) ? $hashInCookie : null;

$this->restoreHash();
if ($this->hash === null) {
Expand Down
13 changes: 13 additions & 0 deletions tests/system/Security/SecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,4 +464,17 @@ public static function provideGetPostedTokenReturnsNullForInvalidInputs(): itera

yield 'invalid_form_data' => [self::createIncomingRequest()->setBody('csrf_test_name[]=invalid')];
}

public function testConstructorHandlesArrayCookieWithoutTypeError(): void
{
service('superglobals')
->setServer('REQUEST_METHOD', 'GET')
->setCookie('csrf_cookie_name', ['malicious' => 'array']);

$config = new SecurityConfig();
$security = new Security($config);

$this->assertIsString($security->getHash());
$this->assertSame(32, strlen($security->getHash()));
}
}
Loading