Class CsrfProtectionMiddleware
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.
See: https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#double-submit-cookie
Constants
Property Summary
-
$config protected
array<string, mixed>Config for the CSRF handling.
cookieNameThe name of the cookie to send.expiryA strtotime compatible value of how long the CSRF token should last. Defaults to browser session.secureWhether the cookie will be set with the Secure flag. Defaults to false.httponlyWhether the cookie will be set with the HttpOnly flag. Defaults to false.samesite"SameSite" attribute for cookies. Defaults tonull. Valid values:CookieInterface::SAMESITE_LAX,CookieInterface::SAMESITE_STRICT,CookieInterface::SAMESITE_NONEornull.fieldThe form field to check. Changing this will also require configuring FormHelper.
-
$skipCheckCallback protected
Closure|nullCallback for deciding whether to skip the token check for particular request.
CSRF protection token check will be skipped if the callback returns
true.
Method Summary
-
__construct() public
Constructor
-
addTokenCookie() protected
Add a CSRF token to the response cookies.
-
createCookie() protected
Create response cookie
-
createToken() public
Create a new token to be used for CSRF protection
-
process() public
Checks and sets the CSRF token depending on the HTTP verb.
-
saltToken() public
Apply entropy to a CSRF token
To avoid BREACH apply a random salt value to a token When the token is compared to the session the token needs to be unsalted.
-
skipCheckCallback() public
Set callback for allowing to skip token check for particular request.
The callback will receive request instance as argument and must return
trueif you want to skip token check for the current request. -
unsaltToken() public
Remove the salt from a CSRF token.
Returns the token unchanged if it isn't a validly-salted value (e.g. malformed or tampered-with input).
-
unsetTokenField() protected
Remove CSRF protection token from request data.
-
validateToken() protected
Validate the request data against the cookie token.
-
verifyToken() protected
Verify that CSRF token was originally generated by the receiving application.
Method Detail
__construct() ¶ public
__construct(array<string, mixed> $config = [])
Constructor
Parameters
-
array<string, mixed>$config optional Config options. See $config for valid keys.
addTokenCookie() ¶ protected
addTokenCookie(string $token, Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response): Psr\Http\Message\ResponseInterface
Add a CSRF token to the response cookies.
Parameters
-
string$token The token to add.
-
Psr\Http\Message\ServerRequestInterface$request The request to validate against.
-
Psr\Http\Message\ResponseInterface$response The response.
Returns
Psr\Http\Message\ResponseInterface$response Modified response.
createCookie() ¶ protected
createCookie(string $value, Psr\Http\Message\ServerRequestInterface $request): Cake\Http\Cookie\CookieInterface
Create response cookie
Parameters
-
string$value Cookie value
-
Psr\Http\Message\ServerRequestInterface$request The request object.
Returns
Cake\Http\Cookie\CookieInterfacecreateToken() ¶ public
createToken(): string
Create a new token to be used for CSRF protection
Returns
stringprocess() ¶ public
process(ServerRequestInterface $request, RequestHandlerInterface $handler): Psr\Http\Message\ResponseInterface
Checks and sets the CSRF token depending on the HTTP verb.
Parameters
-
ServerRequestInterface$request The request.
-
RequestHandlerInterface$handler The request handler.
Returns
Psr\Http\Message\ResponseInterfaceA response.
saltToken() ¶ public
saltToken(string $token): string
Apply entropy to a CSRF token
To avoid BREACH apply a random salt value to a token When the token is compared to the session the token needs to be unsalted.
Parameters
-
string$token The token to salt.
Returns
stringThe salted token with the salt appended.
skipCheckCallback() ¶ public
skipCheckCallback(Closure(Psr\Http\Message\ServerRequestInterface): bool $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
-
Closure(Psr\Http\Message\ServerRequestInterface): bool$callback A closure.
Returns
$thisunsaltToken() ¶ public
unsaltToken(string $token): string
Remove the salt from a CSRF token.
Returns the token unchanged if it isn't a validly-salted value (e.g. malformed or tampered-with input).
Parameters
-
string$token The token that could be salty.
Returns
stringAn unsalted token.
unsetTokenField() ¶ protected
unsetTokenField(Psr\Http\Message\ServerRequestInterface $request): Psr\Http\Message\ServerRequestInterface
Remove CSRF protection token from request data.
Parameters
-
Psr\Http\Message\ServerRequestInterface$request The request object.
Returns
Psr\Http\Message\ServerRequestInterfacevalidateToken() ¶ protected
validateToken(Psr\Http\Message\ServerRequestInterface $request): void
Validate the request data against the cookie token.
Parameters
-
Psr\Http\Message\ServerRequestInterface$request The request to validate against.
Returns
voidThrows
Cake\Http\Exception\InvalidCsrfTokenExceptionWhen the CSRF token is invalid or missing.
verifyToken() ¶ protected
verifyToken(string $token): bool
Verify that CSRF token was originally generated by the receiving application.
Parameters
-
string$token The CSRF token.
Returns
boolProperty Detail
$config ¶ protected
Config for the CSRF handling.
cookieNameThe name of the cookie to send.expiryA strtotime compatible value of how long the CSRF token should last. Defaults to browser session.secureWhether the cookie will be set with the Secure flag. Defaults to false.httponlyWhether the cookie will be set with the HttpOnly flag. Defaults to false.samesite"SameSite" attribute for cookies. Defaults tonull. Valid values:CookieInterface::SAMESITE_LAX,CookieInterface::SAMESITE_STRICT,CookieInterface::SAMESITE_NONEornull.fieldThe form field to check. Changing this will also require configuring FormHelper.
Type
array<string, mixed>$skipCheckCallback ¶ protected
Callback for deciding whether to skip the token check for particular request.
CSRF protection token check will be skipped if the callback returns true.
Type
Closure|null