|
15 | 15 | use CodeIgniter\Shield\Entities\UserIdentity; |
16 | 16 | use CodeIgniter\Shield\Exceptions\InvalidArgumentException; |
17 | 17 | use CodeIgniter\Shield\Exceptions\LogicException; |
| 18 | +use CodeIgniter\Shield\Exceptions\SecurityException; |
18 | 19 | use CodeIgniter\Shield\Models\LoginModel; |
19 | 20 | use CodeIgniter\Shield\Models\RememberModel; |
20 | 21 | use CodeIgniter\Shield\Models\UserIdentityModel; |
21 | 22 | use CodeIgniter\Shield\Models\UserModel; |
22 | 23 | use CodeIgniter\Shield\Result; |
| 24 | +use Config\Security; |
| 25 | +use Config\Services; |
23 | 26 | use stdClass; |
24 | 27 |
|
25 | 28 | class Session implements AuthenticatorInterface |
@@ -72,6 +75,25 @@ public function __construct(UserModel $provider) |
72 | 75 | $this->loginModel = model(LoginModel::class); |
73 | 76 | $this->rememberModel = model(RememberModel::class); |
74 | 77 | $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 | + } |
75 | 97 | } |
76 | 98 |
|
77 | 99 | /** |
@@ -567,7 +589,10 @@ public function startLogin(User $user): void |
567 | 589 |
|
568 | 590 | // Regenerate the session ID to help protect against session fixation |
569 | 591 | if (ENVIRONMENT !== 'testing') { |
570 | | - session()->regenerate(); |
| 592 | + session()->regenerate(true); |
| 593 | + |
| 594 | + // Regenerate CSRF token even if `security.regenerate = false`. |
| 595 | + Services::security()->generateHash(); |
571 | 596 | } |
572 | 597 |
|
573 | 598 | // Let the session know we're logged in |
|
0 commit comments