Class CsrfProtectionMiddleware
Provides CSRF protection & validation.
This middleware adds a CSRF token to a cookie. The cookie value is compared to request data, or the X-CSRF-Token header on each PATCH, POST, PUT, or DELETE request.
If the request data is missing or does not match the cookie data, an InvalidCsrfTokenException will be raised.
This middleware integrates with the FormHelper automatically and when
used together your forms will have CSRF tokens automatically added
when $this->Form->create(...)
is used in a view.
Constants
-
int
TOKEN_VALUE_LENGTH ¶16
Property Summary
-
$_config protected
array
Configuration
-
$_defaultConfig protected
array
Default config for the CSRF handling.
-
$whitelistCallback protected
callable|null
Callback for deciding whether or not to skip the token check for particular request.
Method Summary
-
__construct() public
Constructor
-
__invoke() public
Checks and sets the CSRF token depending on the HTTP verb.
-
_addTokenCookie() protected
Add a CSRF token to the response cookies.
-
_addTokenToRequest() protected
Add a CSRF token to the request parameters.
-
_compareToken() protected
Ensure that the request token matches the cookie value and that both were generated by us.
-
_createToken() protected
Create a new token to be used for CSRF protection
-
_validateAndUnsetTokenField() protected
Checks if the request is POST, PUT, DELETE or PATCH and validates the CSRF token
-
_validateToken() protected
Validate the request data against the cookie token.
-
createToken() public
Create a new token to be used for CSRF protection.
-
whitelistCallback() public
Set callback for allowing to skip token check for particular request.
Method Detail
__construct() ¶ public
__construct(array $config = [])
Constructor
Parameters
-
array
$config optional Config options. See $_defaultConfig for valid keys.
__invoke() ¶ public
__invoke(Cake\Http\ServerRequest $request, Cake\Http\Response $response, callable $next): Cake\Http\Response
Checks and sets the CSRF token depending on the HTTP verb.
Parameters
-
Cake\Http\ServerRequest
$request The request.
-
Cake\Http\Response
$response The response.
-
callable
$next Callback to invoke the next middleware.
Returns
Cake\Http\Response
_addTokenCookie() ¶ protected
_addTokenCookie(string $token, Cake\Http\ServerRequest $request, Cake\Http\Response $response): Cake\Http\Response
Add a CSRF token to the response cookies.
Parameters
-
string
$token The token to add.
-
Cake\Http\ServerRequest
$request The request to validate against.
-
Cake\Http\Response
$response The response.
Returns
Cake\Http\Response
_addTokenToRequest() ¶ protected
_addTokenToRequest(string $token, Cake\Http\ServerRequest $request): Cake\Http\ServerRequest
Add a CSRF token to the request parameters.
Parameters
-
string
$token The token to add.
-
Cake\Http\ServerRequest
$request The request to augment
Returns
Cake\Http\ServerRequest
_compareToken() ¶ protected
_compareToken(mixed $post, mixed $cookie): bool
Ensure that the request token matches the cookie value and that both were generated by us.
Parameters
-
mixed
$post The request token.
-
mixed
$cookie The cookie token.
Returns
bool
_createToken() ¶ protected
_createToken(): string
Create a new token to be used for CSRF protection
Returns
string
_validateAndUnsetTokenField() ¶ protected
_validateAndUnsetTokenField(Cake\Http\ServerRequest $request): Cake\Http\ServerRequest
Checks if the request is POST, PUT, DELETE or PATCH and validates the CSRF token
Parameters
-
Cake\Http\ServerRequest
$request The request object.
Returns
Cake\Http\ServerRequest
_validateToken() ¶ protected
_validateToken(Cake\Http\ServerRequest $request): void
Validate the request data against the cookie token.
Parameters
-
Cake\Http\ServerRequest
$request The request to validate against.
Returns
void
Throws
Cake\Http\Exception\InvalidCsrfTokenException
When the CSRF token is invalid or missing.
createToken() ¶ public
createToken(): string
Create a new token to be used for CSRF protection.
Returns
string
whitelistCallback() ¶ public
whitelistCallback(callable $callback): $this
Set callback for allowing to skip token check for particular request.
The callback will receive request instance as argument and must return
true
if you want to skip token check for the current request.
Parameters
-
callable
$callback A callable.
Returns
$this
Property Detail
$_defaultConfig ¶ protected
Default config for the CSRF handling.
cookieName
The name of the cookie to send.expiry
A strotime compatible value of how long the CSRF token should last. Defaults to browser session.secure
Whether or not the cookie will be set with the Secure flag. Defaults to false.httpOnly
Whether or not the cookie will be set with the HttpOnly flag. Defaults to false.samesite
Value for "SameSite" attribute. Default to null.field
The form field to check. Changing this will also require configuring FormHelper.verifyTokenSource
Generate and verify tokens that include the application salt value. This prevents tokens from being manipulated by an attacker via XSS or physical access. This behavior is disabled by default as it is not cross compatible with tokens created in earlier versions of CakePHP. It is recommended that you enable this setting if possible as it is the default in 4.x.
Type
array
$whitelistCallback ¶ protected
Callback for deciding whether or not to skip the token check for particular request.
CSRF protection token check will be skipped if the callback returns true
.
Type
callable|null