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.
-
$skipCheckCallback protected
callable|nullCallback for deciding whether to skip the token check for particular request.
Method Summary
-
__construct() public
Constructor
-
_addTokenCookie() protected
Add a CSRF token to the response cookies.
-
_createCookie() protected
Create response cookie
-
_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.
-
createToken() public
Create a new token to be used for CSRF protection
-
isHexadecimalToken() protected
Test if the token predates salted tokens.
-
process() public
Checks and sets the CSRF token depending on the HTTP verb.
-
saltToken() public
Apply entropy to a CSRF token
-
skipCheckCallback() public
Set callback for allowing to skip token check for particular request.
-
unsaltToken() public
Remove the salt from a CSRF token.
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\CookieInterface_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\ServerRequestInterface_validateToken() ¶ 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
boolcreateToken() ¶ public
createToken(): string
Create a new token to be used for CSRF protection
Returns
stringisHexadecimalToken() ¶ protected
isHexadecimalToken(string $token): bool
Test if the token predates salted tokens.
These tokens are hexadecimal values and equal to the token with checksum length. While they are vulnerable to BREACH they should rotate over time and support will be dropped in 5.x.
Parameters
-
string$token The token to test.
Returns
boolprocess() ¶ public
process(ServerRequestInterface $request, RequestHandlerInterface $handler): Psr\Http\Message\ResponseInterface
Checks and sets the CSRF token depending on the HTTP verb.
Processes an incoming server request in order to produce a response. If unable to produce the response itself, it may delegate to the provided request handler to do so.
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(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
$thisunsaltToken() ¶ public
unsaltToken(string $token): string
Remove the salt from a CSRF token.
If the token is not TOKEN_VALUE_LENGTH * 2 it is an old unsalted value that is supported for backwards compatibility.
Parameters
-
string$token The token that could be salty.
Returns
stringAn unsalted token.
Property Detail
$_config ¶ protected
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 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
callable|null