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 5.0 Chiffon API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 5.0
      • 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
    • Cache
    • Collection
    • Command
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
      • Client
      • Cookie
      • Exception
      • Middleware
      • Session
      • TestSuite
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

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.

Namespace: Cake\Http\Middleware
See: https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#synchronizer-token-pattern

Constants

  • int
    TOKEN_VALUE_LENGTH ¶
    32

Property Summary

  • $_config protected
    array<string, mixed>

    Config for the CSRF handling.

  • $skipCheckCallback protected
    callable|null

    Callback for deciding whether to skip the token check for particular request.

Method Summary

  • __construct() public

    Constructor

  • 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.

  • replaceToken() public static

    Replace the token in the provided request.

  • saltToken() public

    Apply entropy to a CSRF token

  • skipCheckCallback() public

    Set callback for allowing to skip token check for particular request.

  • unsaltToken() protected

    Remove the salt from a CSRF token.

  • unsetTokenField() protected

    Remove CSRF protection token from request data.

  • validateToken() protected

    Validate 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(): string

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

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

replaceToken() ¶ public static

replaceToken(Cake\Http\ServerRequest $request, string $key = 'csrfToken'): Cake\Http\ServerRequest

Replace 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\ServerRequest

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

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

unsaltToken() ¶ protected

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
string

unsetTokenField() ¶ protected

unsetTokenField(Psr\Http\Message\ServerRequestInterface $request): Psr\Http\Message\ServerRequestInterface

Remove 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\ServerRequestInterface

validateToken() ¶ protected

validateToken(Psr\Http\Message\ServerRequestInterface $request, Cake\Http\Session $session): void

Validate 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
void
Throws
Cake\Http\Exception\InvalidCsrfTokenException
When the CSRF token is invalid or missing.

Property Detail

$_config ¶ protected

Config for the CSRF handling.

  • key The session key to use. Defaults to csrfToken
    • 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
callable|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