Provides CSRF protection & validation.
This middleware adds a CSRF token to a cookie. The cookie value is compared to token in request data, or the X-CSRF-Token header on each PATCH, POST, PUT, or DELETE request. This is known as "double submit cookie" technique.
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.
int
16
array
Config for the CSRF handling.
callable|null
Callback for deciding whether or not to skip the token check for particular request.
Verify that CSRF token was originally generated by the receiving application.
__construct(array $config)
Constructor
array
$config optional Config options. See $_config for valid keys.
_addTokenCookie(string $token, \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response)
Add a CSRF token to the response cookies.
string
$token The token to add.
\Psr\Http\Message\ServerRequestInterface
$request The request to validate against.
\Psr\Http\Message\ResponseInterface
$response The response.
\Psr\Http\Message\ResponseInterface
$response Modified response.
_createCookie(string $value, \Psr\Http\Message\ServerRequestInterface $request)
Create response cookie
string
$value Cookie value
\Psr\Http\Message\ServerRequestInterface
$request The request object.
\Cake\Http\Cookie\CookieInterface
_createToken()
Create a new token to be used for CSRF protection
string
_unsetTokenField(\Psr\Http\Message\ServerRequestInterface $request)
Remove CSRF protection token from request data.
\Psr\Http\Message\ServerRequestInterface
$request The request object.
\Psr\Http\Message\ServerRequestInterface
_validateToken(\Psr\Http\Message\ServerRequestInterface $request)
Validate the request data against the cookie token.
\Psr\Http\Message\ServerRequestInterface
$request The request to validate against.
Cake\Http\Exception\InvalidCsrfTokenException
_verifyToken(string $token)
Verify that CSRF token was originally generated by the receiving application.
string
$token The CSRF token.
bool
createToken()
Create a new token to be used for CSRF protection
string
process(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $handler)
Checks and sets the CSRF token depending on the HTTP verb.
\Psr\Http\Message\ServerRequestInterface
$request The request.
\Psr\Http\Server\RequestHandlerInterface
$handler The request handler.
\Psr\Http\Message\ResponseInterface
A response.
skipCheckCallback(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.
callable
$callback A callable.
$this
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.
callable
$callback A callable.
$this
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
"SameSite" attribute for cookies. Defaults to null
. Valid values: CookieInterface::SAMESITE_LAX
, CookieInterface::SAMESITE_STRICT
, CookieInterface::SAMESITE_NONE
or null
.field
The form field to check. Changing this will also require configuring FormHelper.array
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
.
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/4.1/class-Cake.Http.Middleware.CsrfProtectionMiddleware.html