The Security Component creates an easy way to integrate tighter security in your application. It provides methods for various tasks like:
string
DEFAULT_EXCEPTION_MESSAGE 'The request has been black-holed' $_action protected string$_defaultConfig protected array$session public Generate array of messages for the existing fields in POST data, matching dataFields in $expectedFields will be unset
Black-hole an invalid request with a 400 error or custom callback. If SecurityComponent::$blackHoleCallback is specified, it will use this callback by executing the method indicated in $error
Manually add form tampering prevention token information into the provided request object.
_authRequired( Cake\Controller\Controller $controller )
Check if authentication is required
Cake\Controller\Controller $controller _callback( Cake\Controller\Controller $controller , string $method , array $params = [] )
Calls a controller callback method
Cake\Controller\Controller $controller $method $params optional [] Cake\Http\Exception\BadRequestException_debugCheckFields( array $dataFields , array $expectedFields = [] , string $intKeyMessage = '' , string $stringKeyMessage = '' , string $missingMessage = '' )
Iterates data array to check against expected
$dataFields $expectedFields optional [] $intKeyMessage optional '' $stringKeyMessage optional '' $missingMessage optional '' _debugExpectedFields( array $expectedFields = [] , string $missingMessage = '' )
Generate debug message for the expected fields
$expectedFields optional [] $missingMessage optional '' _debugPostTokenNotMatching( Cake\Controller\Controller $controller , array $hashParts )
Create a message for humans to understand why Security token is not matching
Cake\Controller\Controller $controller $hashParts _fieldsList( array $check )
Return the fields list for the hash calculation
$check _hashParts( Cake\Controller\Controller $controller )
Return hash parts for the Token generation
Cake\Controller\Controller $controller _matchExistingFields( array $dataFields , array $expectedFields , string $intKeyMessage , string $stringKeyMessage )
Generate array of messages for the existing fields in POST data, matching dataFields in $expectedFields will be unset
$dataFields $expectedFields $intKeyMessage $stringKeyMessage _requireMethod( string $method , array $actions = [] )
Sets the actions that require a $method HTTP request, or empty for all actions
$method $actions optional [] _secureRequired( Cake\Controller\Controller $controller )
Check if access requires secure connection
Cake\Controller\Controller $controller _sortedUnlocked( array $data )
Get the sorted unlocked string
$data _throwException( Cake\Controller\Exception\SecurityException|null $exception = null )
Check debug status and throw an Exception based on the existing one
Cake\Controller\Exception\SecurityException|null $exception optional null Cake\Http\Exception\BadRequestException_unlocked( array $data )
Get the unlocked string
$data _validToken( Cake\Controller\Controller $controller )
Check if token is valid
Cake\Controller\Controller $controller Cake\Controller\Exception\SecurityException_validatePost( Cake\Controller\Controller $controller )
Validate submitted form
Cake\Controller\Controller $controller Cake\Controller\Exception\AuthSecurityExceptionblackHole( Cake\Controller\Controller $controller , string $error = '' , Cake\Controller\Exception\SecurityException $exception = null )
Black-hole an invalid request with a 400 error or custom callback. If SecurityComponent::$blackHoleCallback is specified, it will use this callback by executing the method indicated in $error
Cake\Controller\Controller $controller $error optional '' Cake\Controller\Exception\SecurityException $exception optional null Cake\Http\Exception\BadRequestExceptiongenerateToken( Cake\Http\ServerRequest $request )
Manually add form tampering prevention token information into the provided request object.
Cake\Http\ServerRequest $request Cake\Http\ServerRequestimplementedEvents( )
Events supported by this component.
Cake\Controller\Component::implementedEvents() requireAuth( string|array $actions )
Sets the actions that require whitelisted form submissions.
Adding actions with this method will enforce the restrictions set in SecurityComponent::$allowedControllers and SecurityComponent::$allowedActions.
$actions requireSecure( string|array|null $actions = null )
Sets the actions that require a request that is SSL-secured, or empty for all actions
$actions optional null startup( Cake\Event\Event $event )
Component startup. All security checking happens here.
Cake\Event\Event $event __construct( Cake\Controller\ComponentRegistry $registry , array $config = [] )
Constructor
Cake\Controller\ComponentRegistry $registry $config optional [] __debugInfo( )
Returns an array that can be used to describe the internal state of this object.
__get( string $name )
Magic method for lazy loading $components.
$name getController( )
Get the controller this component is bound to.
Cake\Controller\Controllerinitialize( array $config )
Constructor hook method.
Implement this method to avoid having to overwrite the constructor and call parent.
$config _configDelete( string $key )
Deletes a single config key.
$key Cake\Core\Exception\Exception_configRead( string|null $key )
Reads a config key.
$key _configWrite( string|array $key , mixed $value , boolean|string $merge = false )
Writes a config key.
$key $value $merge optional false True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Cake\Core\Exception\Exceptionconfig( string|array|null $key = null , mixed|null $value = null , boolean $merge = true )
Gets/Sets the config.
Reading the whole config:
$this->config();
Reading a specific value:
$this->config('key'); Reading a nested value:
$this->config('some.nested.key'); Setting a specific value:
$this->config('key', $value); Setting a nested value:
$this->config('some.nested.key', $value); Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
$key optional null $value optional null $merge optional true Cake\Core\Exception\ExceptionconfigShallow( string|array $key , mixed|null $value = null )
Merge provided config with existing config. Unlike config() which does a recursive merge for nested keys, this method does a simple merge.
Setting a specific value:
$this->configShallow('key', $value); Setting a nested value:
$this->configShallow('some.nested.key', $value); Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
$key $value optional null getConfig( string|null $key = null , mixed $default = null )
Returns the config.
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key'); Reading a nested value:
$this->getConfig('some.nested.key'); Reading with default value:
$this->getConfig('some-key', 'default-value'); $key optional null $default optional null setConfig( string|array $key , mixed|null $value = null , boolean $merge = true )
Sets the config.
Setting a specific value:
$this->setConfig('key', $value); Setting a nested value:
$this->setConfig('some.nested.key', $value); Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
$key $value optional null $merge optional true Cake\Core\Exception\Exceptionlog( mixed $msg , integer|string $level = LogLevel::ERROR , string|array $context = [] )
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
$msg $level optional LogLevel::ERROR $context optional [] protected array
Default config
blackHoleCallback - The controller method that will be called if this request is black-hole'd.requireSecure - List of actions that require an SSL-secured connection.requireAuth - List of actions that require a valid authentication key. Deprecated as of 3.2.2allowedControllers - Controllers from which actions of the current controller are allowed to receive requests.allowedActions - Actions from which actions of the current controller are allowed to receive requests.unlockedFields - Form fields to exclude from POST validation. Fields can be unlocked either in the Component, or with FormHelper::unlockField(). Fields that have been unlocked are not required to be part of the POST and hidden unlocked fields do not have their values checked.unlockedActions - Actions to exclude from POST validation checks. Other checks like requireAuth(), requireSecure() etc. will still be applied.validatePost - Whether to validate POST data. Set to false to disable for data coming from 3rd party services, etc.[
'blackHoleCallback' => null,
'requireSecure' => [],
'requireAuth' => [],
'allowedControllers' => [],
'allowedActions' => [],
'unlockedFields' => [],
'unlockedActions' => [],
'validatePost' => true
]
© 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/3.8/class-Cake.Controller.Component.SecurityComponent.html