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
protectedConfigurationarray
-
$_defaultConfig
protectedDefault config for the CSRF handling.array
Method Summary
- __construct() publicConstructor
- __invoke() publicChecks and sets the CSRF token depending on the HTTP verb.
- _addTokenCookie() protectedAdd a CSRF token to the response cookies.
- _addTokenToRequest() protectedAdd a CSRF token to the request parameters.
- _createToken() protectedCreate a new token to be used for CSRF protection
- _validateAndUnsetTokenField() protectedChecks if the request is POST, PUT, DELETE or PATCH and validates the CSRF token
- _validateToken() protectedValidate the request data against the cookie token.
Method Detail
__construct()source public
__construct( array $config [] )
Constructor
Parameters
- array
$config
optional [] - Config options. See $_defaultConfig for valid keys.
__invoke()source public
__invoke( Cake\Http\ServerRequest $request , Cake\Http\Response $response , callable $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\Response
A response
_addTokenCookie()source protected
_addTokenCookie( string $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
@param\Cake\Http\Response $response Modified response.
_addTokenToRequest()source protected
_addTokenToRequest( string $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\ServerRequest
Modified request
_createToken()source protected
_createToken( )
Create a new token to be used for CSRF protection
Returns
string_validateAndUnsetTokenField()source 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()source 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\Network\Exception\InvalidCsrfTokenException
When the CSRF token is invalid or missing.
Properties detail
$_defaultConfigsource
protected array
Default config for the CSRF handling.
-
cookieName
= The name of the cookie to send.-
expiry
= 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. -
field
= The form field to check. Changing this will also require configuring FormHelper.
-
[ 'cookieName' => 'csrfToken', 'expiry' => 0, 'secure' => false, 'httpOnly' => false, 'field' => '_csrfToken', ]
© 2005–2017 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.4/class-Cake.Http.Middleware.CsrfProtectionMiddleware.html