CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 6.0 API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 6.0
      • 6.0
      • 5.4
      • 5.3
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • AttributeResolver
    • Cache
    • Collection
    • Command
    • Console
    • Container
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
      • Client
      • Cookie
      • Exception
      • Link
      • Middleware
      • RateLimit
      • Response
      • Session
      • TestSuite
    • I18n
    • Lock
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

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.

Namespace: Cake\Http\Middleware
See: https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#double-submit-cookie

Constants

  • int
    TOKEN_VALUE_LENGTH ¶
    16
  • int
    TOKEN_WITH_CHECKSUM_LENGTH ¶
    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.

Property Summary

  • $config protected
    array<string, mixed>

    Config for the CSRF handling.

    • cookieName The name of the cookie to send.
    • expiry A strtotime 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.
  • $skipCheckCallback protected
    Closure|null

    Callback 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 true if 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\CookieInterface

createToken() ¶ public

createToken(): string

Create a new token to be used for CSRF protection

Returns
string

process() ¶ 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\ResponseInterface

A 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
string

The 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
$this

unsaltToken() ¶ 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
string

An 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\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
void
Throws
Cake\Http\Exception\InvalidCsrfTokenException
When 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
bool

Property Detail

$config ¶ protected

Config for the CSRF handling.

  • cookieName The name of the cookie to send.
  • expiry A strtotime 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.
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
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs