2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-05-29 15:20:28 +00:00
|
|
|
* Security Component
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.8.2156
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-28 06:32:33 +00:00
|
|
|
|
|
|
|
App::uses('Component', 'Controller');
|
2015-01-05 00:00:57 +00:00
|
|
|
App::uses('CakeText', 'Utility');
|
2012-03-11 01:57:18 +00:00
|
|
|
App::uses('Hash', 'Utility');
|
2010-12-15 05:50:02 +00:00
|
|
|
App::uses('Security', 'Utility');
|
2010-05-29 15:20:28 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-07-18 01:55:29 +00:00
|
|
|
* The Security Component creates an easy way to integrate tighter security in
|
2011-12-08 15:35:02 +00:00
|
|
|
* your application. It provides methods for various tasks like:
|
|
|
|
*
|
|
|
|
* - Restricting which HTTP methods your application accepts.
|
|
|
|
* - CSRF protection.
|
|
|
|
* - Form tampering protection
|
|
|
|
* - Requiring that SSL be used.
|
|
|
|
* - Limiting cross controller communication.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-libraries/components/security-component.html
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-04 19:12:42 +00:00
|
|
|
class SecurityComponent extends Component {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2016-10-16 04:43:36 +00:00
|
|
|
/**
|
|
|
|
* Default message used for exceptions thrown
|
|
|
|
*/
|
|
|
|
const DEFAULT_EXCEPTION_MESSAGE = 'The request has been black-holed';
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The controller method that will be called if this request is black-hole'd
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $blackHoleCallback = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* List of controller actions for which a POST request is required
|
|
|
|
*
|
|
|
|
* @var array
|
2015-07-16 10:37:41 +00:00
|
|
|
* @deprecated 3.0.0 Use CakeRequest::allowMethod() instead.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see SecurityComponent::requirePost()
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $requirePost = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* List of controller actions for which a GET request is required
|
|
|
|
*
|
|
|
|
* @var array
|
2015-07-16 10:37:41 +00:00
|
|
|
* @deprecated 3.0.0 Use CakeRequest::allowMethod() instead.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see SecurityComponent::requireGet()
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $requireGet = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* List of controller actions for which a PUT request is required
|
|
|
|
*
|
|
|
|
* @var array
|
2015-07-16 10:37:41 +00:00
|
|
|
* @deprecated 3.0.0 Use CakeRequest::allowMethod() instead.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see SecurityComponent::requirePut()
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $requirePut = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* List of controller actions for which a DELETE request is required
|
|
|
|
*
|
|
|
|
* @var array
|
2015-07-16 10:37:41 +00:00
|
|
|
* @deprecated 3.0.0 Use CakeRequest::allowMethod() instead.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see SecurityComponent::requireDelete()
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $requireDelete = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* List of actions that require an SSL-secured connection
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see SecurityComponent::requireSecure()
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $requireSecure = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* List of actions that require a valid authentication key
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see SecurityComponent::requireAuth()
|
2016-02-10 12:37:10 +00:00
|
|
|
* @deprecated 2.8.1 This feature is confusing and not useful.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $requireAuth = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Controllers from which actions of the current controller are allowed to receive
|
|
|
|
* requests.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see SecurityComponent::requireAuth()
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $allowedControllers = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Actions from which actions of the current controller are allowed to receive
|
|
|
|
* requests.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see SecurityComponent::requireAuth()
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $allowedActions = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-06-15 02:01:59 +00:00
|
|
|
* Deprecated property, superseded by unlockedFields.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Superseded by unlockedFields.
|
2011-06-15 02:01:59 +00:00
|
|
|
* @see SecurityComponent::$unlockedFields
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $disabledFields = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-06-15 02:01:59 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $unlockedFields = array();
|
|
|
|
|
2012-08-03 18:01:19 +00:00
|
|
|
/**
|
2013-02-09 18:57:55 +00:00
|
|
|
* Actions to exclude from CSRF and POST validation checks.
|
|
|
|
* Other checks like requireAuth(), requireSecure(),
|
|
|
|
* requirePost(), requireGet() etc. will still be applied.
|
2012-08-03 18:01:19 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $unlockedActions = array();
|
|
|
|
|
2008-09-17 15:27:41 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Whether to validate POST data. Set to false to disable for data coming from 3rd party
|
2008-09-17 15:27:41 +00:00
|
|
|
* services, etc.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2008-09-17 15:27:41 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $validatePost = true;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-30 04:18:25 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Whether to use CSRF protected forms. Set to false to disable CSRF protection on forms.
|
2010-09-30 04:18:25 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2010-09-30 04:18:25 +00:00
|
|
|
* @see http://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)
|
|
|
|
* @see SecurityComponent::$csrfExpires
|
|
|
|
*/
|
|
|
|
public $csrfCheck = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The duration from when a CSRF token is created that it will expire on.
|
2011-08-16 03:55:08 +00:00
|
|
|
* Each form/page request will generate a new token that can only be submitted once unless
|
2012-12-22 22:48:15 +00:00
|
|
|
* it expires. Can be any value compatible with strtotime()
|
2010-09-30 04:18:25 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $csrfExpires = '+30 minutes';
|
|
|
|
|
2010-10-25 00:26:31 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Controls whether or not CSRF tokens are use and burn. Set to false to not generate
|
|
|
|
* new tokens on each request. One token will be reused until it expires. This reduces
|
2010-10-25 00:26:31 +00:00
|
|
|
* the chances of users getting invalid requests because of token consumption.
|
|
|
|
* It has the side effect of making CSRF less secure, as tokens are reusable.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2010-10-25 00:26:31 +00:00
|
|
|
*/
|
|
|
|
public $csrfUseOnce = true;
|
|
|
|
|
2011-12-04 01:12:40 +00:00
|
|
|
/**
|
|
|
|
* Control the number of tokens a user can keep open.
|
2012-12-22 22:48:15 +00:00
|
|
|
* This is most useful with one-time use tokens. Since new tokens
|
2011-12-04 01:12:40 +00:00
|
|
|
* are created on each request, having a hard limit on the number of open tokens
|
|
|
|
* can be useful in controlling the size of the session file.
|
|
|
|
*
|
|
|
|
* When tokens are evicted, the oldest ones will be removed, as they are the most likely
|
|
|
|
* to be dead/expired.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2011-12-04 01:12:40 +00:00
|
|
|
*/
|
|
|
|
public $csrfLimit = 100;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Other components used by the Security component
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-05-29 04:01:11 +00:00
|
|
|
public $components = array('Session');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds the current action of the controller
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_action = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
/**
|
|
|
|
* Request object
|
|
|
|
*
|
|
|
|
* @var CakeRequest
|
|
|
|
*/
|
|
|
|
public $request;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Component startup. All security checking happens here.
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Controller $controller Instantiating controller
|
2016-10-16 04:43:36 +00:00
|
|
|
* @throws AuthSecurityException
|
2009-12-17 03:39:01 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function startup(Controller $controller) {
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->request = $controller->request;
|
2016-10-16 11:20:11 +00:00
|
|
|
$this->_action = $controller->request->params['action'];
|
2017-05-06 12:43:51 +00:00
|
|
|
$hasData = ($controller->request->data || $controller->request->is(array('put', 'post', 'delete', 'patch')));
|
2016-10-16 04:43:36 +00:00
|
|
|
try {
|
|
|
|
$this->_methodsRequired($controller);
|
|
|
|
$this->_secureRequired($controller);
|
|
|
|
$this->_authRequired($controller);
|
|
|
|
|
|
|
|
$isNotRequestAction = (
|
|
|
|
!isset($controller->request->params['requested']) ||
|
|
|
|
$controller->request->params['requested'] != 1
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($this->_action === $this->blackHoleCallback) {
|
|
|
|
throw new AuthSecurityException(sprintf('Action %s is defined as the blackhole callback.', $this->_action));
|
2010-10-01 04:13:34 +00:00
|
|
|
}
|
2016-10-16 04:43:36 +00:00
|
|
|
|
|
|
|
if (!in_array($this->_action, (array)$this->unlockedActions) && $hasData && $isNotRequestAction) {
|
|
|
|
if ($this->validatePost) {
|
|
|
|
$this->_validatePost($controller);
|
|
|
|
}
|
|
|
|
if ($this->csrfCheck) {
|
|
|
|
$this->_validateCsrf($controller);
|
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
}
|
2016-10-16 04:43:36 +00:00
|
|
|
|
|
|
|
} catch (SecurityException $se) {
|
|
|
|
return $this->blackHole($controller, $se->getType(), $se);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2016-10-16 04:43:36 +00:00
|
|
|
|
2011-12-04 00:37:23 +00:00
|
|
|
$this->generateToken($controller->request);
|
2016-01-21 02:17:27 +00:00
|
|
|
if ($hasData && is_array($controller->request->data)) {
|
2011-11-15 23:40:48 +00:00
|
|
|
unset($controller->request->data['_Token']);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the actions that require a POST request, or empty for all actions
|
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Use CakeRequest::onlyAllow() instead.
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-libraries/components/security-component.html#SecurityComponent::requirePost
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function requirePost() {
|
2012-09-14 14:29:48 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$this->_requireMethod('Post', $args);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the actions that require a GET request, or empty for all actions
|
|
|
|
*
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Use CakeRequest::onlyAllow() instead.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function requireGet() {
|
2012-09-14 14:29:48 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$this->_requireMethod('Get', $args);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the actions that require a PUT request, or empty for all actions
|
|
|
|
*
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Use CakeRequest::onlyAllow() instead.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function requirePut() {
|
2012-09-14 14:29:48 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$this->_requireMethod('Put', $args);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the actions that require a DELETE request, or empty for all actions
|
|
|
|
*
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Use CakeRequest::onlyAllow() instead.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function requireDelete() {
|
2012-09-14 14:29:48 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$this->_requireMethod('Delete', $args);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the actions that require a request that is SSL-secured, or empty for all actions
|
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-libraries/components/security-component.html#SecurityComponent::requireSecure
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function requireSecure() {
|
2012-09-14 14:29:48 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$this->_requireMethod('Secure', $args);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-02-09 19:06:24 +00:00
|
|
|
* Sets the actions that require whitelisted form submissions.
|
|
|
|
*
|
|
|
|
* Adding actions with this method will enforce the restrictions
|
|
|
|
* set in SecurityComponent::$allowedControllers and
|
|
|
|
* SecurityComponent::$allowedActions.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-libraries/components/security-component.html#SecurityComponent::requireAuth
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function requireAuth() {
|
2012-09-14 14:29:48 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$this->_requireMethod('Auth', $args);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-10-18 21:02:38 +00:00
|
|
|
* Black-hole an invalid request with a 400 error or custom callback. If SecurityComponent::$blackHoleCallback
|
2008-05-30 11:40:08 +00:00
|
|
|
* is specified, it will use this callback by executing the method indicated in $error
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Controller $controller Instantiating controller
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $error Error method
|
2016-10-16 04:43:36 +00:00
|
|
|
* @param SecurityException|null $exception Additional debug info describing the cause
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed If specified, controller blackHoleCallback's response, or no return otherwise
|
|
|
|
* @see SecurityComponent::$blackHoleCallback
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-libraries/components/security-component.html#handling-blackhole-callbacks
|
2011-10-18 21:02:38 +00:00
|
|
|
* @throws BadRequestException
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2016-10-16 04:43:36 +00:00
|
|
|
public function blackHole(Controller $controller, $error = '', SecurityException $exception = null) {
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$this->blackHoleCallback) {
|
2016-10-16 04:43:36 +00:00
|
|
|
$this->_throwException($exception);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-09-13 21:06:40 +00:00
|
|
|
return $this->_callback($controller, $this->blackHoleCallback, array($error));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2016-10-16 04:43:36 +00:00
|
|
|
/**
|
|
|
|
* Check debug status and throw an Exception based on the existing one
|
|
|
|
*
|
|
|
|
* @param SecurityException|null $exception Additional debug info describing the cause
|
|
|
|
* @throws BadRequestException
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _throwException($exception = null) {
|
|
|
|
if ($exception !== null) {
|
|
|
|
if (!Configure::read('debug') && $exception instanceof SecurityException) {
|
|
|
|
$exception->setReason($exception->getMessage());
|
|
|
|
$exception->setMessage(self::DEFAULT_EXCEPTION_MESSAGE);
|
|
|
|
}
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
throw new BadRequestException(self::DEFAULT_EXCEPTION_MESSAGE);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the actions that require a $method HTTP request, or empty for all actions
|
|
|
|
*
|
|
|
|
* @param string $method The HTTP method to assign controller actions to
|
|
|
|
* @param array $actions Controller actions to set the required HTTP method to.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _requireMethod($method, $actions = array()) {
|
2009-12-02 22:56:52 +00:00
|
|
|
if (isset($actions[0]) && is_array($actions[0])) {
|
|
|
|
$actions = $actions[0];
|
|
|
|
}
|
2013-10-13 16:18:24 +00:00
|
|
|
$this->{'require' . $method} = (empty($actions)) ? array('*') : $actions;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Check if HTTP methods are required
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Controller $controller Instantiating controller
|
2016-10-16 04:43:36 +00:00
|
|
|
* @throws SecurityException
|
2014-11-08 19:07:47 +00:00
|
|
|
* @return bool True if $method is required
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
protected function _methodsRequired(Controller $controller) {
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach (array('Post', 'Get', 'Put', 'Delete') as $method) {
|
|
|
|
$property = 'require' . $method;
|
|
|
|
if (is_array($this->$property) && !empty($this->$property)) {
|
2011-03-12 16:10:31 +00:00
|
|
|
$require = $this->$property;
|
2014-04-29 10:05:47 +00:00
|
|
|
if (in_array($this->_action, $require) || $this->$property === array('*')) {
|
2016-10-16 11:20:11 +00:00
|
|
|
if (!$controller->request->is($method)) {
|
2016-10-16 04:43:36 +00:00
|
|
|
throw new SecurityException(
|
|
|
|
sprintf('The request method must be %s', strtoupper($method))
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Check if access requires secure connection
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Controller $controller Instantiating controller
|
2016-10-16 04:43:36 +00:00
|
|
|
* @throws SecurityException
|
2014-11-08 19:07:47 +00:00
|
|
|
* @return bool True if secure connection required
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
protected function _secureRequired(Controller $controller) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($this->requireSecure) && !empty($this->requireSecure)) {
|
2011-03-12 16:10:31 +00:00
|
|
|
$requireSecure = $this->requireSecure;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2014-04-29 10:05:47 +00:00
|
|
|
if (in_array($this->_action, $requireSecure) || $this->requireSecure === array('*')) {
|
2016-10-16 11:20:11 +00:00
|
|
|
if (!$controller->request->is('ssl')) {
|
2016-10-16 04:43:36 +00:00
|
|
|
throw new SecurityException(
|
|
|
|
'Request is not SSL and the action is required to be secure'
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Check if authentication is required
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Controller $controller Instantiating controller
|
2014-11-05 12:03:27 +00:00
|
|
|
* @return bool|null True if authentication required
|
2016-10-16 04:43:36 +00:00
|
|
|
* @throws AuthSecurityException
|
2016-02-10 12:37:10 +00:00
|
|
|
* @deprecated 2.8.1 This feature is confusing and not useful.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
protected function _authRequired(Controller $controller) {
|
2016-10-16 11:20:11 +00:00
|
|
|
if (is_array($this->requireAuth) && !empty($this->requireAuth) && !empty($controller->request->data)) {
|
2011-03-12 16:10:31 +00:00
|
|
|
$requireAuth = $this->requireAuth;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2016-10-16 11:20:11 +00:00
|
|
|
if (in_array($controller->request->params['action'], $requireAuth) || $this->requireAuth === array('*')) {
|
2012-09-19 23:50:15 +00:00
|
|
|
if (!isset($controller->request->data['_Token'])) {
|
2016-10-16 04:43:36 +00:00
|
|
|
throw new AuthSecurityException('\'_Token\' was not found in request data.');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->Session->check('_Token')) {
|
2010-09-30 04:26:44 +00:00
|
|
|
$tData = $this->Session->read('_Token');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2014-12-09 02:17:35 +00:00
|
|
|
if (!empty($tData['allowedControllers']) &&
|
2016-10-16 11:20:11 +00:00
|
|
|
!in_array($controller->request->params['controller'], $tData['allowedControllers'])) {
|
2016-10-16 04:43:36 +00:00
|
|
|
throw new AuthSecurityException(
|
|
|
|
sprintf(
|
|
|
|
'Controller \'%s\' was not found in allowed controllers: \'%s\'.',
|
2016-10-16 11:20:11 +00:00
|
|
|
$controller->request->params['controller'],
|
2016-10-16 04:43:36 +00:00
|
|
|
implode(', ', (array)$tData['allowedControllers'])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!empty($tData['allowedActions']) &&
|
2016-10-16 11:20:11 +00:00
|
|
|
!in_array($controller->request->params['action'], $tData['allowedActions'])
|
2012-01-20 02:19:42 +00:00
|
|
|
) {
|
2016-10-16 04:43:36 +00:00
|
|
|
throw new AuthSecurityException(
|
|
|
|
sprintf(
|
|
|
|
'Action \'%s::%s\' was not found in allowed actions: \'%s\'.',
|
2016-10-16 11:20:11 +00:00
|
|
|
$controller->request->params['controller'],
|
|
|
|
$controller->request->params['action'],
|
2016-10-16 04:43:36 +00:00
|
|
|
implode(', ', (array)$tData['allowedActions'])
|
|
|
|
)
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-10-16 04:43:36 +00:00
|
|
|
throw new AuthSecurityException('\'_Token\' was not found in session.');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Validate submitted form
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Controller $controller Instantiating controller
|
2016-10-16 04:43:36 +00:00
|
|
|
* @throws AuthSecurityException
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true if submitted form is valid
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
protected function _validatePost(Controller $controller) {
|
2016-10-16 04:43:36 +00:00
|
|
|
$token = $this->_validToken($controller);
|
|
|
|
$hashParts = $this->_hashParts($controller);
|
|
|
|
$check = Security::hash(implode('', $hashParts), 'sha1');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2016-10-16 04:43:36 +00:00
|
|
|
if ($token === $check) {
|
|
|
|
return true;
|
2008-09-24 23:02:14 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2016-10-16 04:43:36 +00:00
|
|
|
$msg = self::DEFAULT_EXCEPTION_MESSAGE;
|
|
|
|
if (Configure::read('debug')) {
|
|
|
|
$msg = $this->_debugPostTokenNotMatching($controller, $hashParts);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new AuthSecurityException($msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if token is valid
|
|
|
|
*
|
|
|
|
* @param Controller $controller Instantiating controller
|
|
|
|
* @throws AuthSecurityException
|
|
|
|
* @throws SecurityException
|
|
|
|
* @return string fields token
|
|
|
|
*/
|
|
|
|
protected function _validToken(Controller $controller) {
|
2010-05-29 03:57:43 +00:00
|
|
|
$check = $controller->request->data;
|
2016-10-16 04:43:36 +00:00
|
|
|
|
|
|
|
$message = '\'%s\' was not found in request data.';
|
|
|
|
if (!isset($check['_Token'])) {
|
|
|
|
throw new AuthSecurityException(sprintf($message, '_Token'));
|
|
|
|
}
|
|
|
|
if (!isset($check['_Token']['fields'])) {
|
|
|
|
throw new AuthSecurityException(sprintf($message, '_Token.fields'));
|
|
|
|
}
|
|
|
|
if (!isset($check['_Token']['unlocked'])) {
|
|
|
|
throw new AuthSecurityException(sprintf($message, '_Token.unlocked'));
|
|
|
|
}
|
|
|
|
if (Configure::read('debug') && !isset($check['_Token']['debug'])) {
|
|
|
|
throw new SecurityException(sprintf($message, '_Token.debug'));
|
|
|
|
}
|
|
|
|
if (!Configure::read('debug') && isset($check['_Token']['debug'])) {
|
|
|
|
throw new SecurityException('Unexpected \'_Token.debug\' found in request data');
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = urldecode($check['_Token']['fields']);
|
|
|
|
if (strpos($token, ':')) {
|
|
|
|
list($token, ) = explode(':', $token, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return hash parts for the Token generation
|
|
|
|
*
|
|
|
|
* @param Controller $controller Instantiating controller
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _hashParts(Controller $controller) {
|
|
|
|
$fieldList = $this->_fieldsList($controller->request->data);
|
|
|
|
$unlocked = $this->_sortedUnlocked($controller->request->data);
|
|
|
|
|
|
|
|
return array(
|
|
|
|
$controller->request->here(),
|
|
|
|
serialize($fieldList),
|
|
|
|
$unlocked,
|
|
|
|
Configure::read('Security.salt')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the fields list for the hash calculation
|
|
|
|
*
|
|
|
|
* @param array $check Data array
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _fieldsList(array $check) {
|
|
|
|
$locked = '';
|
2008-09-24 23:02:14 +00:00
|
|
|
$token = urldecode($check['_Token']['fields']);
|
2016-10-16 04:43:36 +00:00
|
|
|
$unlocked = $this->_unlocked($check);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
if (strpos($token, ':')) {
|
|
|
|
list($token, $locked) = explode(':', $token, 2);
|
|
|
|
}
|
2016-10-16 04:43:36 +00:00
|
|
|
unset($check['_Token'], $check['_csrfToken']);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-11-21 04:42:54 +00:00
|
|
|
$locked = explode('|', $locked);
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = explode('|', $unlocked);
|
2010-11-08 01:53:04 +00:00
|
|
|
|
2012-03-11 01:57:18 +00:00
|
|
|
$fields = Hash::flatten($check);
|
2008-09-24 23:02:14 +00:00
|
|
|
$fieldList = array_keys($fields);
|
2016-10-16 04:43:36 +00:00
|
|
|
$multi = $lockedFields = array();
|
|
|
|
$isUnlocked = false;
|
2008-10-23 03:18:08 +00:00
|
|
|
|
|
|
|
foreach ($fieldList as $i => $key) {
|
2014-07-06 13:42:20 +00:00
|
|
|
if (preg_match('/(\.\d+){1,10}$/', $key)) {
|
|
|
|
$multi[$i] = preg_replace('/(\.\d+){1,10}$/', '', $key);
|
2008-10-23 03:18:08 +00:00
|
|
|
unset($fieldList[$i]);
|
2016-10-16 04:43:36 +00:00
|
|
|
} else {
|
|
|
|
$fieldList[$i] = (string)$key;
|
2008-10-23 03:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($multi)) {
|
|
|
|
$fieldList += array_unique($multi);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlockedFields = array_unique(
|
|
|
|
array_merge((array)$this->disabledFields, (array)$this->unlockedFields, $unlocked)
|
|
|
|
);
|
2011-06-09 02:47:49 +00:00
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
foreach ($fieldList as $i => $key) {
|
|
|
|
$isLocked = (is_array($locked) && in_array($key, $locked));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-06-15 02:01:59 +00:00
|
|
|
if (!empty($unlockedFields)) {
|
|
|
|
foreach ($unlockedFields as $off) {
|
2011-06-09 02:47:49 +00:00
|
|
|
$off = explode('.', $off);
|
|
|
|
$field = array_values(array_intersect(explode('.', $key), $off));
|
2011-06-15 02:01:59 +00:00
|
|
|
$isUnlocked = ($field === $off);
|
|
|
|
if ($isUnlocked) {
|
2008-09-24 23:02:14 +00:00
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-15 02:01:59 +00:00
|
|
|
if ($isUnlocked || $isLocked) {
|
2008-09-24 23:02:14 +00:00
|
|
|
unset($fieldList[$i]);
|
|
|
|
if ($isLocked) {
|
|
|
|
$lockedFields[$key] = $fields[$key];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
sort($fieldList, SORT_STRING);
|
|
|
|
ksort($lockedFields, SORT_STRING);
|
|
|
|
$fieldList += $lockedFields;
|
2016-10-16 04:43:36 +00:00
|
|
|
|
|
|
|
return $fieldList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the unlocked string
|
|
|
|
*
|
|
|
|
* @param array $data Data array
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function _unlocked(array $data) {
|
|
|
|
return urldecode($data['_Token']['unlocked']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the sorted unlocked string
|
|
|
|
*
|
|
|
|
* @param array $data Data array
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function _sortedUnlocked($data) {
|
|
|
|
$unlocked = $this->_unlocked($data);
|
|
|
|
$unlocked = explode('|', $unlocked);
|
|
|
|
sort($unlocked, SORT_STRING);
|
|
|
|
|
|
|
|
return implode('|', $unlocked);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a message for humans to understand why Security token is not matching
|
|
|
|
*
|
|
|
|
* @param Controller $controller Instantiating controller
|
|
|
|
* @param array $hashParts Elements used to generate the Token hash
|
|
|
|
* @return string Message explaining why the tokens are not matching
|
|
|
|
*/
|
|
|
|
protected function _debugPostTokenNotMatching(Controller $controller, $hashParts) {
|
|
|
|
$messages = array();
|
|
|
|
$expectedParts = json_decode(urldecode($controller->request->data['_Token']['debug']), true);
|
|
|
|
if (!is_array($expectedParts) || count($expectedParts) !== 3) {
|
|
|
|
return 'Invalid security debug token.';
|
|
|
|
}
|
|
|
|
$expectedUrl = Hash::get($expectedParts, 0);
|
|
|
|
$url = Hash::get($hashParts, 0);
|
|
|
|
if ($expectedUrl !== $url) {
|
|
|
|
$messages[] = sprintf('URL mismatch in POST data (expected \'%s\' but found \'%s\')', $expectedUrl, $url);
|
|
|
|
}
|
|
|
|
$expectedFields = Hash::get($expectedParts, 1);
|
|
|
|
$dataFields = Hash::get($hashParts, 1);
|
|
|
|
if ($dataFields) {
|
|
|
|
$dataFields = unserialize($dataFields);
|
|
|
|
}
|
|
|
|
$fieldsMessages = $this->_debugCheckFields(
|
|
|
|
$dataFields,
|
|
|
|
$expectedFields,
|
|
|
|
'Unexpected field \'%s\' in POST data',
|
|
|
|
'Tampered field \'%s\' in POST data (expected value \'%s\' but found \'%s\')',
|
|
|
|
'Missing field \'%s\' in POST data'
|
2014-04-26 02:05:58 +00:00
|
|
|
);
|
2016-10-16 04:43:36 +00:00
|
|
|
$expectedUnlockedFields = Hash::get($expectedParts, 2);
|
|
|
|
$dataUnlockedFields = Hash::get($hashParts, 2) ?: array();
|
|
|
|
if ($dataUnlockedFields) {
|
|
|
|
$dataUnlockedFields = explode('|', $dataUnlockedFields);
|
|
|
|
}
|
|
|
|
$unlockFieldsMessages = $this->_debugCheckFields(
|
|
|
|
$dataUnlockedFields,
|
|
|
|
$expectedUnlockedFields,
|
|
|
|
'Unexpected unlocked field \'%s\' in POST data',
|
|
|
|
null,
|
|
|
|
'Missing unlocked field: \'%s\''
|
|
|
|
);
|
|
|
|
|
|
|
|
$messages = array_merge($messages, $fieldsMessages, $unlockFieldsMessages);
|
|
|
|
|
|
|
|
return implode(', ', $messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterates data array to check against expected
|
|
|
|
*
|
|
|
|
* @param array $dataFields Fields array, containing the POST data fields
|
|
|
|
* @param array $expectedFields Fields array, containing the expected fields we should have in POST
|
|
|
|
* @param string $intKeyMessage Message string if unexpected found in data fields indexed by int (not protected)
|
|
|
|
* @param string $stringKeyMessage Message string if tampered found in data fields indexed by string (protected)
|
|
|
|
* @param string $missingMessage Message string if missing field
|
|
|
|
* @return array Messages
|
|
|
|
*/
|
|
|
|
protected function _debugCheckFields($dataFields, $expectedFields = array(), $intKeyMessage = '', $stringKeyMessage = '', $missingMessage = '') {
|
|
|
|
$messages = $this->_matchExistingFields($dataFields, $expectedFields, $intKeyMessage, $stringKeyMessage);
|
|
|
|
$expectedFieldsMessage = $this->_debugExpectedFields($expectedFields, $missingMessage);
|
|
|
|
if ($expectedFieldsMessage !== null) {
|
|
|
|
$messages[] = $expectedFieldsMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $messages;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-12-04 00:37:23 +00:00
|
|
|
* Manually add CSRF token information into the provided request object.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-12-04 00:37:23 +00:00
|
|
|
* @param CakeRequest $request The request object to add into.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-12-04 00:37:23 +00:00
|
|
|
public function generateToken(CakeRequest $request) {
|
|
|
|
if (isset($request->params['requested']) && $request->params['requested'] === 1) {
|
2009-12-19 00:05:33 +00:00
|
|
|
if ($this->Session->check('_Token')) {
|
2011-12-04 00:37:23 +00:00
|
|
|
$request->params['_Token'] = $this->Session->read('_Token');
|
2009-12-19 00:05:33 +00:00
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-22 05:14:44 +00:00
|
|
|
$authKey = hash('sha512', Security::randomBytes(16), false);
|
2008-09-24 23:02:14 +00:00
|
|
|
$token = array(
|
|
|
|
'key' => $authKey,
|
|
|
|
'allowedControllers' => $this->allowedControllers,
|
|
|
|
'allowedActions' => $this->allowedActions,
|
2011-06-15 02:18:05 +00:00
|
|
|
'unlockedFields' => array_merge($this->disabledFields, $this->unlockedFields),
|
2010-09-30 04:26:44 +00:00
|
|
|
'csrfTokens' => array()
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-10-25 00:26:31 +00:00
|
|
|
$tokenData = array();
|
2008-09-24 23:02:14 +00:00
|
|
|
if ($this->Session->check('_Token')) {
|
2010-09-30 04:06:38 +00:00
|
|
|
$tokenData = $this->Session->read('_Token');
|
2011-02-12 03:30:51 +00:00
|
|
|
if (!empty($tokenData['csrfTokens']) && is_array($tokenData['csrfTokens'])) {
|
2010-10-25 00:26:31 +00:00
|
|
|
$token['csrfTokens'] = $this->_expireTokens($tokenData['csrfTokens']);
|
2010-09-30 04:26:44 +00:00
|
|
|
}
|
2011-08-16 03:55:08 +00:00
|
|
|
}
|
2011-12-04 00:37:23 +00:00
|
|
|
if ($this->csrfUseOnce || empty($token['csrfTokens'])) {
|
2010-10-25 00:26:31 +00:00
|
|
|
$token['csrfTokens'][$authKey] = strtotime($this->csrfExpires);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-12-04 00:37:23 +00:00
|
|
|
if (!$this->csrfUseOnce) {
|
2011-05-31 01:48:26 +00:00
|
|
|
$csrfTokens = array_keys($token['csrfTokens']);
|
2014-07-06 08:39:00 +00:00
|
|
|
$authKey = $csrfTokens[0];
|
|
|
|
$token['key'] = $authKey;
|
|
|
|
$token['csrfTokens'][$authKey] = strtotime($this->csrfExpires);
|
2011-05-31 01:48:26 +00:00
|
|
|
}
|
2010-09-30 04:06:38 +00:00
|
|
|
$this->Session->write('_Token', $token);
|
2011-12-04 00:37:23 +00:00
|
|
|
$request->params['_Token'] = array(
|
2011-05-17 00:35:27 +00:00
|
|
|
'key' => $token['key'],
|
2011-06-15 02:18:05 +00:00
|
|
|
'unlockedFields' => $token['unlockedFields']
|
2011-05-17 00:35:27 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-01 04:13:34 +00:00
|
|
|
/**
|
|
|
|
* Validate that the controller has a CSRF token in the POST data
|
2012-12-22 22:48:15 +00:00
|
|
|
* and that the token is legit/not expired. If the token is valid
|
2010-10-02 04:22:44 +00:00
|
|
|
* it will be removed from the list of valid tokens.
|
2010-10-01 04:13:34 +00:00
|
|
|
*
|
|
|
|
* @param Controller $controller A controller to check
|
2016-10-16 04:43:36 +00:00
|
|
|
* @throws SecurityException
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Valid csrf token.
|
2010-10-01 04:13:34 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
protected function _validateCsrf(Controller $controller) {
|
2010-10-01 04:13:34 +00:00
|
|
|
$token = $this->Session->read('_Token');
|
2010-10-02 21:16:40 +00:00
|
|
|
$requestToken = $controller->request->data('_Token.key');
|
2016-10-16 04:43:36 +00:00
|
|
|
|
|
|
|
if (!$requestToken) {
|
|
|
|
throw new SecurityException('Missing CSRF token');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($token['csrfTokens'][$requestToken])) {
|
|
|
|
throw new SecurityException('CSRF token mismatch');
|
2010-10-01 04:13:34 +00:00
|
|
|
}
|
2016-10-16 04:43:36 +00:00
|
|
|
|
|
|
|
if ($token['csrfTokens'][$requestToken] < time()) {
|
|
|
|
throw new SecurityException('CSRF token expired');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->csrfUseOnce) {
|
|
|
|
$this->Session->delete('_Token.csrfTokens.' . $requestToken);
|
|
|
|
}
|
|
|
|
return true;
|
2010-10-01 04:13:34 +00:00
|
|
|
}
|
|
|
|
|
2010-10-02 04:20:58 +00:00
|
|
|
/**
|
|
|
|
* Expire CSRF nonces and remove them from the valid tokens.
|
|
|
|
* Uses a simple timeout to expire the tokens.
|
|
|
|
*
|
|
|
|
* @param array $tokens An array of nonce => expires.
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return array An array of nonce => expires.
|
2010-10-02 04:20:58 +00:00
|
|
|
*/
|
|
|
|
protected function _expireTokens($tokens) {
|
2011-12-06 17:35:18 +00:00
|
|
|
$now = time();
|
2010-10-02 04:20:58 +00:00
|
|
|
foreach ($tokens as $nonce => $expires) {
|
2010-10-02 22:27:39 +00:00
|
|
|
if ($expires < $now) {
|
2010-10-02 04:20:58 +00:00
|
|
|
unset($tokens[$nonce]);
|
|
|
|
}
|
|
|
|
}
|
2011-12-06 17:23:15 +00:00
|
|
|
$overflow = count($tokens) - $this->csrfLimit;
|
|
|
|
if ($overflow > 0) {
|
|
|
|
$tokens = array_slice($tokens, $overflow + 1, null, true);
|
|
|
|
}
|
2010-10-02 04:20:58 +00:00
|
|
|
return $tokens;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Calls a controller callback method
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param Controller $controller Controller to run callback on
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $method Method to execute
|
|
|
|
* @param array $params Parameters to send to method
|
|
|
|
* @return mixed Controller callback method's response
|
2012-07-06 02:42:57 +00:00
|
|
|
* @throws BadRequestException When a the blackholeCallback is not callable.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
protected function _callback(Controller $controller, $method, $params = array()) {
|
2012-09-13 21:06:40 +00:00
|
|
|
if (!is_callable(array($controller, $method))) {
|
2012-07-03 12:23:21 +00:00
|
|
|
throw new BadRequestException(__d('cake_dev', 'The request has been black-holed'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-09-13 21:06:40 +00:00
|
|
|
return call_user_func_array(array(&$controller, $method), empty($params) ? null : $params);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-03-04 00:27:46 +00:00
|
|
|
|
2016-10-16 04:43:36 +00:00
|
|
|
/**
|
|
|
|
* Generate array of messages for the existing fields in POST data, matching dataFields in $expectedFields
|
|
|
|
* will be unset
|
|
|
|
*
|
|
|
|
* @param array $dataFields Fields array, containing the POST data fields
|
2016-10-16 11:20:11 +00:00
|
|
|
* @param array &$expectedFields Fields array, containing the expected fields we should have in POST
|
2016-10-16 04:43:36 +00:00
|
|
|
* @param string $intKeyMessage Message string if unexpected found in data fields indexed by int (not protected)
|
|
|
|
* @param string $stringKeyMessage Message string if tampered found in data fields indexed by string (protected)
|
|
|
|
* @return array Error messages
|
|
|
|
*/
|
|
|
|
protected function _matchExistingFields($dataFields, &$expectedFields, $intKeyMessage, $stringKeyMessage) {
|
|
|
|
$messages = array();
|
|
|
|
foreach ((array)$dataFields as $key => $value) {
|
|
|
|
if (is_int($key)) {
|
|
|
|
$foundKey = array_search($value, (array)$expectedFields);
|
|
|
|
if ($foundKey === false) {
|
|
|
|
$messages[] = sprintf($intKeyMessage, $value);
|
|
|
|
} else {
|
|
|
|
unset($expectedFields[$foundKey]);
|
|
|
|
}
|
|
|
|
} elseif (is_string($key)) {
|
|
|
|
if (isset($expectedFields[$key]) && $value !== $expectedFields[$key]) {
|
|
|
|
$messages[] = sprintf($stringKeyMessage, $key, $expectedFields[$key], $value);
|
|
|
|
}
|
|
|
|
unset($expectedFields[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate debug message for the expected fields
|
|
|
|
*
|
|
|
|
* @param array $expectedFields Expected fields
|
|
|
|
* @param string $missingMessage Message template
|
|
|
|
* @return string Error message about expected fields
|
|
|
|
*/
|
|
|
|
protected function _debugExpectedFields($expectedFields = array(), $missingMessage = '') {
|
|
|
|
if (count($expectedFields) === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$expectedFieldNames = array();
|
|
|
|
foreach ((array)$expectedFields as $key => $expectedField) {
|
|
|
|
if (is_int($key)) {
|
|
|
|
$expectedFieldNames[] = $expectedField;
|
|
|
|
} else {
|
|
|
|
$expectedFieldNames[] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf($missingMessage, implode(', ', $expectedFieldNames));
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|