Skip to content

Commit 4434177

Browse files
committed
fix: do not allow Config\Security::$csrfProtection = 'cookie'
Same-site attakcers can set CSRF token cookie. So CSRF protection is bypassed.
1 parent 3a33192 commit 4434177

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

src/Authentication/Authenticators/Session.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
use CodeIgniter\Shield\Entities\UserIdentity;
1616
use CodeIgniter\Shield\Exceptions\InvalidArgumentException;
1717
use CodeIgniter\Shield\Exceptions\LogicException;
18+
use CodeIgniter\Shield\Exceptions\SecurityException;
1819
use CodeIgniter\Shield\Models\LoginModel;
1920
use CodeIgniter\Shield\Models\RememberModel;
2021
use CodeIgniter\Shield\Models\UserIdentityModel;
2122
use CodeIgniter\Shield\Models\UserModel;
2223
use CodeIgniter\Shield\Result;
24+
use Config\Security;
2325
use Config\Services;
2426
use stdClass;
2527

@@ -73,6 +75,25 @@ public function __construct(UserModel $provider)
7375
$this->loginModel = model(LoginModel::class);
7476
$this->rememberModel = model(RememberModel::class);
7577
$this->userIdentityModel = model(UserIdentityModel::class);
78+
79+
$this->checkSecurityConfig();
80+
}
81+
82+
/**
83+
* Checks less secure Configuration.
84+
*/
85+
private function checkSecurityConfig(): void
86+
{
87+
/** @var Security $securityConfig */
88+
$securityConfig = config('Security');
89+
90+
if ($securityConfig->csrfProtection === 'cookie') {
91+
throw new SecurityException(
92+
'Config\Security::$csrfProtection is set to \'cookie\'.'
93+
. ' Same-site attackers may bypass the CSRF protection.'
94+
. ' Please set it to \'session\'.'
95+
);
96+
}
7697
}
7798

7899
/**
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace CodeIgniter\Shield\Exceptions;
4+
5+
use RuntimeException;
6+
7+
class SecurityException extends RuntimeException
8+
{
9+
}

tests/Controllers/RegisterTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ protected function setUp(): void
3030
parent::setUp();
3131

3232
helper('auth');
33-
Factories::reset();
3433

3534
// Add auth routes
3635
$routes = service('routes');

tests/_support/TestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@ protected function setUp(): void
3333
$config = config('Auth');
3434
$config->actions = ['login' => null, 'register' => null];
3535
Factories::injectMock('config', 'Auth', $config);
36+
37+
// Set Config\Security::$csrfProtection to 'session'
38+
$config = config('Security');
39+
$config->csrfProtection = 'session';
40+
Factories::injectMock('config', 'Security', $config);
3641
}
3742
}

0 commit comments

Comments
 (0)