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.
Properties summary
- $_config protected
arrayConfiguration
- $_defaultConfig protected
arrayDefault config for the CSRF handling.
- $whitelistCallback protected
callable|nullCallback for deciding whether or not to skip the token check for particular request.
Method Summary
- _validateAndUnsetTokenField() protected
Checks if the request is POST, PUT, DELETE or PATCH and validates the CSRF token
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, mixed $next)
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\ResponseA response
_addTokenCookie() protected
_addTokenCookie(mixed $token, \Cake\Http\ServerRequest $request, \Cake\Http\Response $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$response Modified response.
_addTokenToRequest() protected
_addTokenToRequest(mixed $token, \Cake\Http\ServerRequest $request)
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\ServerRequestModified request
_createToken() protected
_createToken()
Create a new token to be used for CSRF protection
Returns
string_validateAndUnsetTokenField() protected
_validateAndUnsetTokenField(\Cake\Http\ServerRequest $request)
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)
Validate the request data against the cookie token.
Parameters
-
\Cake\Http\ServerRequest$request The request to validate against.
Throws
Cake\Http\Exception\InvalidCsrfTokenExceptionWhen the CSRF token is invalid or missing.
whitelistCallback() public
whitelistCallback(callable $callback)
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
$thisProperty Detail
$_config protected
Configuration
Type
array$_defaultConfig protected
Default config for the CSRF handling.
-
cookieNameThe name of the cookie to send.-
expiryA strotime compatible value of how long the CSRF token should last. Defaults to browser session. -
secureWhether or not the cookie will be set with the Secure flag. Defaults to false. -
httpOnlyWhether or not the cookie will be set with the HttpOnly flag. Defaults to false.
-
-
samesiteValue for "SameSite" attribute. Default to null.-
fieldThe form field to check. Changing this will also require configuring FormHelper.
-
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
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.9/class-Cake.Http.Middleware.CsrfProtectionMiddleware.html