CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.7 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.7
      • 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

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • BodyParserMiddleware
  • CsrfProtectionMiddleware
  • EncryptedCookieMiddleware
  • SecurityHeadersMiddleware

Class CsrfProtectionMiddleware

Provides CSRF protection & validation.

This middleware adds a CSRF token to a cookie. The cookie value is compared to request data, or the X-CSRF-Token header on each PATCH, POST, PUT, or DELETE request.

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
Location: Http/Middleware/CsrfProtectionMiddleware.php

Properties summary

  • $_config protected
    array
    Configuration
  • $_defaultConfig protected
    array
    Default config for the CSRF handling.

Method Summary

  • __construct() public
    Constructor
  • __invoke() public
    Checks and sets the CSRF token depending on the HTTP verb.
  • _addTokenCookie() protected
    Add a CSRF token to the response cookies.
  • _addTokenToRequest() protected
    Add a CSRF token to the request parameters.
  • _createToken() protected
    Create a new token to be used for CSRF protection
  • _validateAndUnsetTokenField() protected
    Checks if the request is POST, PUT, DELETE or PATCH and validates the CSRF token
  • _validateToken() protected
    Validate the request data against the cookie token.

Method Detail

__construct() public ¶

__construct( array $config [] )

Constructor

Parameters
array $config optional []
Config options. See $_defaultConfig for valid keys.

__invoke() public ¶

__invoke( Cake\Http\ServerRequest $request , Cake\Http\Response $response , callable $next )

Checks and sets the CSRF token depending on the HTTP verb.

Parameters
Cake\Http\ServerRequest $request
The request.
Cake\Http\Response $response
The response.
callable $next
Callback to invoke the next middleware.
Returns
Cake\Http\Response
A response

_addTokenCookie() protected ¶

_addTokenCookie( string $token , Cake\Http\ServerRequest $request , Cake\Http\Response $response )

Add a CSRF token to the response cookies.

Parameters
string $token
The token to add.
Cake\Http\ServerRequest $request
The request to validate against.
Cake\Http\Response $response
The response.
Returns
Cake\Http\Response
$response Modified response.

_addTokenToRequest() protected ¶

_addTokenToRequest( string $token , Cake\Http\ServerRequest $request )

Add a CSRF token to the request parameters.

Parameters
string $token
The token to add.
Cake\Http\ServerRequest $request
The request to augment
Returns
Cake\Http\ServerRequest
Modified request

_createToken() protected ¶

_createToken( )

Create a new token to be used for CSRF protection

Returns
string

_validateAndUnsetTokenField() protected ¶

_validateAndUnsetTokenField( Cake\Http\ServerRequest $request )

Checks if the request is POST, PUT, DELETE or PATCH and validates the CSRF token

Parameters
Cake\Http\ServerRequest $request
The request object.
Returns
Cake\Http\ServerRequest

_validateToken() protected ¶

_validateToken( Cake\Http\ServerRequest $request )

Validate the request data against the cookie token.

Parameters
Cake\Http\ServerRequest $request
The request to validate against.
Throws
Cake\Http\Exception\InvalidCsrfTokenException
When the CSRF token is invalid or missing.

Properties detail

$_config ¶

protected array

Configuration

[]

$_defaultConfig ¶

protected array

Default config 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 or not the cookie will be set with the Secure flag. Defaults to false.
    • httpOnly Whether or not the cookie will be set with the HttpOnly flag. Defaults to false.
    • field The form field to check. Changing this will also require configuring FormHelper.
[
    'cookieName' => 'csrfToken',
    'expiry' => 0,
    'secure' => false,
    'httpOnly' => false,
    'field' => '_csrfToken',
]
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs