Skip to content

Commit 054ce95

Browse files
authored
Merge pull request #5327 from kenjis/fix-docs-security.rst
docs: improve security.rst
2 parents c4a2339 + 12728c0 commit 054ce95

File tree

1 file changed

+48
-41
lines changed

1 file changed

+48
-41
lines changed

user_guide_src/source/libraries/security.rst

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The Security Class contains methods that help protect your site against Cross-Si
66

77
.. contents::
88
:local:
9-
:depth: 2
9+
:depth: 3
1010

1111
*******************
1212
Loading the Library
@@ -26,8 +26,11 @@ Cross-site request forgery (CSRF)
2626
.. warning:: The CSRF Protection is only available for **POST/PUT/PATCH/DELETE** requests.
2727
Requests for other methods are not protected.
2828

29+
Config for CSRF
30+
===============
31+
2932
CSRF Protection Methods
30-
=======================
33+
-----------------------
3134

3235
By default, the Cookie based CSRF Protection is used. It is
3336
`Double Submit Cookie <https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#double-submit-cookie>`_
@@ -41,6 +44,49 @@ You can set to use the Session based CSRF protection by editing the following co
4144

4245
public $csrfProtection = 'session';
4346

47+
Token Randomization
48+
-------------------
49+
50+
To mitigate compression side-channel attacks like `BREACH`_, and prevent an attacker from guessing the CSRF tokens, you can configure token randomization (off by default).
51+
52+
If you enable it, a random mask is added to the token and used to scramble it.
53+
54+
.. _`BREACH`: https://en.wikipedia.org/wiki/BREACH
55+
56+
You can enable it by editing the following config parameter value in
57+
**app/Config/Security.php**::
58+
59+
public $tokenRandomize = true;
60+
61+
Token Regeneration
62+
------------------
63+
64+
Tokens may be either regenerated on every submission (default) or
65+
kept the same throughout the life of the CSRF cookie. The default
66+
regeneration of tokens provides stricter security, but may result
67+
in usability concerns as other tokens become invalid (back/forward
68+
navigation, multiple tabs/windows, asynchronous actions, etc). You
69+
may alter this behavior by editing the following config parameter value in
70+
**app/Config/Security.php**::
71+
72+
public $regenerate = true;
73+
74+
Redirection on Failure
75+
----------------------
76+
77+
When a request fails the CSRF validation check, it will redirect to the previous page by default,
78+
setting an ``error`` flash message that you can display to the end user with the following code in your view::
79+
80+
<?= session()->getFlashdata('error') ?>
81+
82+
This provides a nicer experience
83+
than simply crashing. This can be turned off by editing the following config parameter value in
84+
**app/Config/Security.php**::
85+
86+
public $redirect = false;
87+
88+
Even when the redirect value is ``true``, AJAX calls will not redirect, but will throw an error.
89+
4490
Enable CSRF Protection
4591
======================
4692

@@ -79,20 +125,6 @@ It is also possible to enable the CSRF filter only for specific methods::
79125
'post' => ['csrf'],
80126
];
81127

82-
Token Randomization
83-
===================
84-
85-
To mitigate compression side-channel attacks like `BREACH`_, and prevent an attacker from guessing the CSRF tokens, you can configure token randomization (default off).
86-
87-
If you enable it, a random mask is added to the token and used to scramble it.
88-
89-
.. _`BREACH`: https://en.wikipedia.org/wiki/BREACH
90-
91-
You can enable it by editing the following config parameter value in
92-
**app/Config/Security.php**::
93-
94-
public $tokenRandomize = true;
95-
96128
HTML Forms
97129
==========
98130

@@ -134,31 +166,6 @@ The order of checking the availability of the CSRF token is as follows:
134166
2. HTTP header
135167
3. ``php://input`` (JSON request) - bear in mind that this approach is the slowest one since we have to decode JSON and then re-encode it
136168

137-
Token Regeneration
138-
===================
139-
140-
Tokens may be either regenerated on every submission (default) or
141-
kept the same throughout the life of the CSRF cookie. The default
142-
regeneration of tokens provides stricter security, but may result
143-
in usability concerns as other tokens become invalid (back/forward
144-
navigation, multiple tabs/windows, asynchronous actions, etc). You
145-
may alter this behavior by editing the following config parameter value in
146-
**app/Config/Security.php**::
147-
148-
public $regenerate = true;
149-
150-
Redirection on Failure
151-
======================
152-
153-
When a request fails the CSRF validation check, it will redirect to the previous page by default,
154-
setting an ``error`` flash message that you can display to the end user. This provides a nicer experience
155-
than simply crashing. This can be turned off by editing the following config parameter value in
156-
**app/Config/Security.php**::
157-
158-
public $redirect = false;
159-
160-
Even when the redirect value is ``true``, AJAX calls will not redirect, but will throw an error.
161-
162169
*********************
163170
Other Helpful Methods
164171
*********************

0 commit comments

Comments
 (0)