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
int 56
Tokens have an hmac generated so we can ensure that tokens were generated by our application.
Should be TOKEN_VALUE_LENGTH + strlen(hmac)
We are currently using sha1 for the hmac which creates 40 bytes.
array<string, mixed>Config for the CSRF handling.
callable|nullCallback for deciding whether to skip the token check for particular request.
Constructor
Add a CSRF token to the response cookies.
Create response cookie
Create a new token to be used for CSRF protection
Remove CSRF protection token from request data.
Validate the request data against the cookie token.
Verify that CSRF token was originally generated by the receiving application.
Create a new token to be used for CSRF protection
Test if the token predates salted tokens.
Checks and sets the CSRF token depending on the HTTP verb.
Apply entropy to a CSRF token
Set callback for allowing to skip token check for particular request.
Remove the salt from a CSRF token.
Set callback for allowing to skip token check for particular request.
__construct(array<string, mixed> $config = [])
Constructor
array<string, mixed> $config optional Config options. See $_config for valid keys.
_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.
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_createCookie(string $value, Psr\Http\Message\ServerRequestInterface $request): Cake\Http\Cookie\CookieInterface
Create response cookie
string $value Cookie value
Psr\Http\Message\ServerRequestInterface $request The request object.
Cake\Http\Cookie\CookieInterface_createToken(): string
Create a new token to be used for CSRF protection
string_unsetTokenField(Psr\Http\Message\ServerRequestInterface $request): Psr\Http\Message\ServerRequestInterface
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): void
Validate the request data against the cookie token.
Psr\Http\Message\ServerRequestInterface $request The request to validate against.
voidCake\Http\Exception\InvalidCsrfTokenException_verifyToken(string $token): bool
Verify that CSRF token was originally generated by the receiving application.
string $token The CSRF token.
boolcreateToken(): string
Create a new token to be used for CSRF protection
stringisHexadecimalToken(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.
string $token The token to test.
boolprocess(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.
ServerRequestInterface $request The request.
RequestHandlerInterface $handler The request handler.
Psr\Http\Message\ResponseInterfacesaltToken(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.
string $token The token to salt.
stringskipCheckCallback(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.
callable $callback A callable.
$thisunsaltToken(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.
string $token The token that could be salty.
stringwhitelistCallback(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.
callable $callback A callable.
$thisConfig 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 the cookie will be set with the Secure flag. Defaults to false.httponly Whether 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<string, mixed>Callback for deciding whether 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.4/class-Cake.Http.Middleware.CsrfProtectionMiddleware.html