Skip to content

Commit 4632602

Browse files
authored
Merge pull request from GHSA-6gch-wjxj-hc2w
Improve CSRF protection (for Shield CSRF security fix)
2 parents 8275327 + 23d71d6 commit 4632602

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

system/Security/Security.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,9 @@ private function restoreHash(): void
528528
}
529529

530530
/**
531-
* Generates (Regenerate) the CSRF Hash.
531+
* Generates (Regenerates) the CSRF Hash.
532532
*/
533-
protected function generateHash(): string
533+
public function generateHash(): string
534534
{
535535
$this->hash = bin2hex(random_bytes(static::CSRF_HASH_BYTES));
536536

tests/system/Security/SecurityTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,32 @@ public function testRegenerateWithFalseSecurityRegenerateProperty()
243243
$this->assertSame($oldHash, $newHash);
244244
}
245245

246+
public function testRegenerateWithFalseSecurityRegeneratePropertyManually()
247+
{
248+
$_SERVER['REQUEST_METHOD'] = 'POST';
249+
$_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a';
250+
$_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a';
251+
252+
$config = new SecurityConfig();
253+
$config->regenerate = false;
254+
Factories::injectMock('config', 'Security', $config);
255+
256+
$security = new MockSecurity(new MockAppConfig());
257+
$request = new IncomingRequest(
258+
new MockAppConfig(),
259+
new URI('http://badurl.com'),
260+
null,
261+
new UserAgent()
262+
);
263+
264+
$oldHash = $security->getHash();
265+
$security->verify($request);
266+
$security->generateHash();
267+
$newHash = $security->getHash();
268+
269+
$this->assertNotSame($oldHash, $newHash);
270+
}
271+
246272
public function testRegenerateWithTrueSecurityRegenerateProperty()
247273
{
248274
$_SERVER['REQUEST_METHOD'] = 'POST';

user_guide_src/source/changelogs/v4.2.2.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ BREAKING
1818
- A bug that caused pages to be cached before after filters were executed when using page caching has been fixed. Adding response headers or changing the response body in after filters now caches them correctly.
1919
- Due to a bug fix, now :php:func:`random_string` with the first parameter ``'crypto'`` throws ``InvalidArgumentException`` if the second parameter ``$len`` is an odd number.
2020

21-
Enhancements
22-
************
23-
24-
none.
25-
2621
Changes
2722
*******
2823

user_guide_src/source/changelogs/v4.2.3.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ none.
1717
Enhancements
1818
************
1919

20-
none.
20+
- Now ``Security::generateHash()`` is public, and can be used to regenerate CSRF token manually when ``Config\Security::$regenerate`` is false.
2121

2222
Changes
2323
*******

user_guide_src/source/libraries/security.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ may alter this behavior by editing the following config parameter value in
106106

107107
.. literalinclude:: security/004.php
108108

109+
.. note:: Since v4.2.3, you can regenerate CSRF token manually with the
110+
``Security::generateHash()`` method.
111+
109112
Redirection on Failure
110113
----------------------
111114

0 commit comments

Comments
 (0)