Skip to content

Commit 72451b2

Browse files
authored
fix(Security): prevent TypeError when CSRF cookie is an array (#10724)
1 parent fbc5fcf commit 72451b2

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ Release Date: Unreleased
1414

1515
## Bugs Fixed
1616

17+
- **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.
18+
1719
See the repo's [CHANGELOG.md](https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md) for a complete list of bugs fixed.

‎system/Security/Security.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ public function __construct(SecurityConfig $config)
206206
}
207207

208208
$this->request = service('request');
209-
$this->hashInCookie = $this->request->getCookie($this->cookieName);
209+
$hashInCookie = $this->request->getCookie($this->cookieName);
210+
$this->hashInCookie = is_string($hashInCookie) ? $hashInCookie : null;
210211

211212
$this->restoreHash();
212213
if ($this->hash === null) {

‎tests/system/Security/SecurityTest.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,17 @@ public static function provideGetPostedTokenReturnsNullForInvalidInputs(): itera
464464

465465
yield 'invalid_form_data' => [self::createIncomingRequest()->setBody('csrf_test_name[]=invalid')];
466466
}
467+
468+
public function testConstructorHandlesArrayCookieWithoutTypeError(): void
469+
{
470+
service('superglobals')
471+
->setServer('REQUEST_METHOD', 'GET')
472+
->setCookie('csrf_cookie_name', ['malicious' => 'array']);
473+
474+
$config = new SecurityConfig();
475+
$security = new Security($config);
476+
477+
$this->assertIsString($security->getHash());
478+
$this->assertSame(32, strlen($security->getHash()));
479+
}
467480
}

0 commit comments

Comments
 (0)