Skip to content

Commit 44524ee

Browse files
authored
Merge pull request #6406 from kenjis/fix-csrf-redirect-default
config: change Security::$redirect to false
2 parents 94de4bd + 55bb2e5 commit 44524ee

File tree

7 files changed

+22
-13
lines changed

7 files changed

+22
-13
lines changed

app/Config/App.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ class App extends BaseConfig
369369
*
370370
* @deprecated Use `Config\Security` $redirect property instead of using this property.
371371
*/
372-
public bool $CSRFRedirect = true;
372+
public bool $CSRFRedirect = false;
373373

374374
/**
375375
* --------------------------------------------------------------------------

app/Config/Security.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Security extends BaseConfig
8080
*
8181
* Redirect to previous page with error on failure.
8282
*/
83-
public bool $redirect = true;
83+
public bool $redirect = false;
8484

8585
/**
8686
* --------------------------------------------------------------------------

env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
# security.cookieName = 'csrf_cookie_name'
125125
# security.expires = 7200
126126
# security.regenerate = true
127-
# security.redirect = true
127+
# security.redirect = false
128128
# security.samesite = 'Lax'
129129

130130
#--------------------------------------------------------------------

system/Security/Security.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class Security implements SecurityInterface
123123
*
124124
* @var bool
125125
*/
126-
protected $redirect = true;
126+
protected $redirect = false;
127127

128128
/**
129129
* CSRF SameSite

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ Changes
8585
- The ``CodeIgniter\CLI\CommandRunner`` class has been removed due to a change in Spark commands processing.
8686
- The system route configuration file ``system/Config/Routes.php`` has been removed.
8787
- The route configuration file ``app/Config/Routes.php`` has been changed. Removed include of system routes configuration file.
88-
- All atomic type properties in ``Config`` classes have been typed.
88+
- Config
89+
- All atomic type properties in ``Config`` classes have been typed.
90+
- Changed the default setting to not redirect when a CSRF check fails so that it is easy to see that it is a CSRF error.
8991

9092
Deprecations
9193
************

user_guide_src/source/libraries/security.rst

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,22 @@ may alter this behavior by editing the following config parameter value in
112112
Redirection on Failure
113113
----------------------
114114

115-
When a request fails the CSRF validation check, it will redirect to the previous page by default,
116-
setting an ``error`` flash message that you can display to the end user with the following code in your view::
115+
Since v4.3.0, when a request fails the CSRF validation check,
116+
it will throw a SecurityException by default,
117117

118-
<?= session()->getFlashdata('error') ?>
119-
120-
This provides a nicer experience
121-
than simply crashing. This can be turned off by editing the following config parameter value in
118+
If you want to make it redirect to the previous page,
119+
change the following config parameter value in
122120
**app/Config/Security.php**:
123121

124122
.. literalinclude:: security/005.php
125123

126-
Even when the redirect value is ``true``, AJAX calls will not redirect, but will throw an error.
124+
When redirected, an ``error`` flash message is set and can be displayed to the end user with the following code in your view::
125+
126+
<?= session()->getFlashdata('error') ?>
127+
128+
This provides a nicer experience than simply crashing.
129+
130+
Even when the redirect value is ``true``, AJAX calls will not redirect, but will throw a SecurityException.
127131

128132
Enable CSRF Protection
129133
======================

user_guide_src/source/libraries/security/005.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
class Security extends BaseConfig
88
{
9-
public $redirect = false;
9+
// ...
10+
11+
public bool $redirect = true;
12+
1013
// ...
1114
}

0 commit comments

Comments
 (0)