Class SessionCsrfProtectionMiddleware
Provides CSRF protection via session based tokens.
This middleware adds a CSRF token to the session. Each request must contain a token in request data, or the X-CSRF-Token header on each PATCH, POST, PUT, or DELETE request. This follows a 'synchronizer token' pattern.
If the request data is missing or does not match the session 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.
If you use this middleware do not also use CsrfProtectionMiddleware.
See: https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#synchronizer-token-pattern
Constants
- 
          
          intTOKEN_VALUE_LENGTH ¶32
Property Summary
- 
        $_config protectedarray<string, mixed>Config for the CSRF handling. 
- 
        $skipCheckCallback protectedcallable|nullCallback for deciding whether to skip the token check for particular request. 
Method Summary
- 
          __construct() publicConstructor 
- 
          createToken() publicCreate a new token to be used for CSRF protection 
- 
          process() publicChecks and sets the CSRF token depending on the HTTP verb. 
- 
          replaceToken() public staticReplace the token in the provided request. 
- 
          saltToken() publicApply entropy to a CSRF token 
- 
          skipCheckCallback() publicSet callback for allowing to skip token check for particular request. 
- 
          unsaltToken() protectedRemove the salt from a CSRF token. 
- 
          unsetTokenField() protectedRemove CSRF protection token from request data. 
- 
          validateToken() protectedValidate the request data against the cookie token. 
Method Detail
__construct() ¶ public
__construct(array<string, mixed> $config = [])Constructor
Parameters
- 
                array<string, mixed>$config optional
- Config options. See $_config for valid keys. 
createToken() ¶ public
createToken(): stringCreate a new token to be used for CSRF protection
This token is a simple unique random value as the compare value is stored in the session where it cannot be tampered with.
Returns
stringprocess() ¶ public
process(ServerRequestInterface $request, RequestHandlerInterface $handler): Psr\Http\Message\ResponseInterfaceChecks 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.
replaceToken() ¶ public static
replaceToken(Cake\Http\ServerRequest $request, string $key = 'csrfToken'): Cake\Http\ServerRequestReplace the token in the provided request.
Replace the token in the session and request attribute. Replacing tokens is a good idea during privilege escalation or privilege reduction.
Parameters
- 
                Cake\Http\ServerRequest$request
- The request to update 
- 
                string$key optional
- The session key/attribute to set. 
Returns
Cake\Http\ServerRequestAn updated request.
saltToken() ¶ public
saltToken(string $token): stringApply 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): $thisSet 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() ¶ protected
unsaltToken(string $token): stringRemove 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.
unsetTokenField() ¶ protected
unsetTokenField(Psr\Http\Message\ServerRequestInterface $request): Psr\Http\Message\ServerRequestInterfaceRemove CSRF protection token from request data.
This ensures that the token does not cause failures during form tampering protection.
Parameters
- 
                Psr\Http\Message\ServerRequestInterface$request
- The request object. 
Returns
Psr\Http\Message\ServerRequestInterfacevalidateToken() ¶ protected
validateToken(Psr\Http\Message\ServerRequestInterface $request, Cake\Http\Session $session): voidValidate the request data against the cookie token.
Parameters
- 
                Psr\Http\Message\ServerRequestInterface$request
- The request to validate against. 
- 
                Cake\Http\Session$session
- The session instance. 
Returns
voidThrows
Cake\Http\Exception\InvalidCsrfTokenExceptionWhen the CSRF token is invalid or missing.
Property Detail
$_config ¶ protected
Config for the CSRF handling.
- keyThe session key to use. Defaults to- csrfToken- 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