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
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs.controller.components
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.8.2156
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-01-28 06:32:33 +00:00
|
|
|
|
|
|
|
App::uses('Component', 'Controller');
|
2010-12-15 05:50:02 +00:00
|
|
|
App::uses('String', 'Utility');
|
|
|
|
App::uses('Security', 'Utility');
|
2010-05-29 15:20:28 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-05-29 15:20:28 +00:00
|
|
|
* SecurityComponent
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs.controller.components
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1296/Security-Component
|
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
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The controller method that will be called if this request is black-hole'd
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
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
|
|
|
|
* @access public
|
|
|
|
* @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
|
|
|
|
* @access public
|
|
|
|
* @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
|
|
|
|
* @access public
|
|
|
|
* @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
|
|
|
|
* @access public
|
|
|
|
* @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
|
|
|
|
* @access public
|
|
|
|
* @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
|
|
|
|
* @access public
|
|
|
|
* @see SecurityComponent::requireAuth()
|
|
|
|
*/
|
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
|
|
|
|
* @access public
|
|
|
|
* @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
|
|
|
|
* @access public
|
|
|
|
* @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-09 02:47:49 +00:00
|
|
|
* Form fields to exclude from POST validation. Fields can be disabled
|
|
|
|
* either in the Component, or with FormHelper::disableField()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $disabledFields = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-17 15:27:41 +00:00
|
|
|
/**
|
|
|
|
* Whether to validate POST data. Set to false to disable for data coming from 3rd party
|
|
|
|
* services, etc.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Whether to use CSRF protected forms. Set to false to disable CSRF protection on forms.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @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.
|
|
|
|
* Each form/page request will generate a new token that can only be submitted once unless
|
|
|
|
* it expires. Can be any value compatible with strtotime()
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $csrfExpires = '+30 minutes';
|
|
|
|
|
2010-10-25 00:26:31 +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
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public $csrfUseOnce = true;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Other components used by the Security component
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
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.
|
|
|
|
*
|
|
|
|
* @param object $controller Instantiating controller
|
2009-12-17 03:39:01 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-18 05:03:03 +00:00
|
|
|
public function startup($controller) {
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->request = $controller->request;
|
2011-03-12 16:10:31 +00:00
|
|
|
$this->_action = $this->request->params['action'];
|
2008-09-16 01:39:20 +00:00
|
|
|
$this->_methodsRequired($controller);
|
|
|
|
$this->_secureRequired($controller);
|
|
|
|
$this->_authRequired($controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$isPost = ($this->request->is('post') || $this->request->is('put'));
|
2008-09-17 15:27:41 +00:00
|
|
|
$isRequestAction = (
|
2010-05-29 03:57:43 +00:00
|
|
|
!isset($controller->request->params['requested']) ||
|
|
|
|
$controller->request->params['requested'] != 1
|
2008-09-17 15:27:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($isPost && $isRequestAction && $this->validatePost) {
|
2008-09-24 23:02:14 +00:00
|
|
|
if ($this->_validatePost($controller) === false) {
|
2010-10-01 04:13:34 +00:00
|
|
|
return $this->blackHole($controller, 'auth');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($isPost && $this->csrfCheck) {
|
|
|
|
if ($this->_validateCsrf($controller) === false) {
|
|
|
|
return $this->blackHole($controller, 'csrf');
|
2008-09-24 23:02:14 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-16 01:39:20 +00:00
|
|
|
$this->_generateToken($controller);
|
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
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1299/requirePost
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function requirePost() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$args = func_get_args();
|
2008-09-16 01:39:20 +00:00
|
|
|
$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
|
|
|
|
*
|
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() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$args = func_get_args();
|
2008-09-16 01:39:20 +00:00
|
|
|
$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
|
|
|
|
*
|
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() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$args = func_get_args();
|
2008-09-16 01:39:20 +00:00
|
|
|
$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
|
|
|
|
*
|
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() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$args = func_get_args();
|
2008-09-16 01:39:20 +00:00
|
|
|
$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
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1300/requireSecure
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function requireSecure() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$args = func_get_args();
|
2008-09-16 01:39:20 +00:00
|
|
|
$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
|
|
|
/**
|
|
|
|
* Sets the actions that require an authenticated request, or empty for all actions
|
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1301/requireAuth
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function requireAuth() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$args = func_get_args();
|
2008-09-16 01:39:20 +00:00
|
|
|
$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
|
|
|
/**
|
|
|
|
* Black-hole an invalid request with a 404 error or custom callback. If SecurityComponent::$blackHoleCallback
|
|
|
|
* is specified, it will use this callback by executing the method indicated in $error
|
|
|
|
*
|
|
|
|
* @param object $controller Instantiating controller
|
|
|
|
* @param string $error Error method
|
|
|
|
* @return mixed If specified, controller blackHoleCallback's response, or no return otherwise
|
|
|
|
* @access public
|
|
|
|
* @see SecurityComponent::$blackHoleCallback
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1307/blackHole-object-controller-string-error
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function blackHole($controller, $error = '') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->blackHoleCallback == null) {
|
|
|
|
$code = 404;
|
|
|
|
if ($error == 'login') {
|
|
|
|
$code = 401;
|
2008-11-10 17:18:00 +00:00
|
|
|
$controller->header($this->loginRequest());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-10-01 04:13:34 +00:00
|
|
|
return $controller->redirect(null, $code, true);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2008-09-16 01:39:20 +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
|
|
|
|
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];
|
|
|
|
}
|
2008-07-30 20:34:01 +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
|
|
|
|
*
|
|
|
|
* @param object $controller Instantiating controller
|
|
|
|
* @return bool true if $method is required
|
|
|
|
*/
|
2010-12-18 05:03:03 +00:00
|
|
|
protected function _methodsRequired($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;
|
2008-09-16 01:39:20 +00:00
|
|
|
if (in_array($this->_action, $require) || $this->$property == array('*')) {
|
2011-03-12 16:10:31 +00:00
|
|
|
if (!$this->request->is($method)) {
|
|
|
|
if (!$this->blackHole($controller, $method)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Check if access requires secure connection
|
|
|
|
*
|
|
|
|
* @param object $controller Instantiating controller
|
|
|
|
* @return bool true if secure connection required
|
|
|
|
*/
|
2010-12-18 05:03:03 +00:00
|
|
|
protected function _secureRequired($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
|
|
|
|
2008-09-16 01:39:20 +00:00
|
|
|
if (in_array($this->_action, $requireSecure) || $this->requireSecure == array('*')) {
|
2010-05-29 03:57:43 +00:00
|
|
|
if (!$this->request->is('ssl')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$this->blackHole($controller, 'secure')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Check if authentication is required
|
|
|
|
*
|
|
|
|
* @param object $controller Instantiating controller
|
|
|
|
* @return bool true if authentication required
|
|
|
|
*/
|
2010-12-18 05:03:03 +00:00
|
|
|
protected function _authRequired($controller) {
|
2010-05-29 03:57:43 +00:00
|
|
|
if (is_array($this->requireAuth) && !empty($this->requireAuth) && !empty($this->request->data)) {
|
2011-03-12 16:10:31 +00:00
|
|
|
$requireAuth = $this->requireAuth;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
if (in_array($this->request->params['action'], $requireAuth) || $this->requireAuth == array('*')) {
|
|
|
|
if (!isset($controller->request->data['_Token'] )) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$this->blackHole($controller, 'auth')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
if (!empty($tData['allowedControllers']) && !in_array($this->request->params['controller'], $tData['allowedControllers']) || !empty($tData['allowedActions']) && !in_array($this->request->params['action'], $tData['allowedActions'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$this->blackHole($controller, 'auth')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!$this->blackHole($controller, 'auth')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Validate submitted form
|
|
|
|
*
|
|
|
|
* @param object $controller Instantiating controller
|
|
|
|
* @return bool true if submitted form is valid
|
|
|
|
*/
|
2010-12-18 05:03:03 +00:00
|
|
|
protected function _validatePost($controller) {
|
2010-05-29 03:57:43 +00:00
|
|
|
if (empty($controller->request->data)) {
|
2008-09-24 23:02:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-05-29 03:57:43 +00:00
|
|
|
$data = $controller->request->data;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-06-09 02:47:49 +00:00
|
|
|
if (!isset($data['_Token']) || !isset($data['_Token']['fields']) || !isset($data['_Token']['disabled'])) {
|
2008-09-24 23:02:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-06-09 02:47:49 +00:00
|
|
|
$locked = '';
|
2010-05-29 03:57:43 +00:00
|
|
|
$check = $controller->request->data;
|
2008-09-24 23:02:14 +00:00
|
|
|
$token = urldecode($check['_Token']['fields']);
|
2011-06-09 02:47:49 +00:00
|
|
|
$disabled = urldecode($check['_Token']['disabled']);
|
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);
|
|
|
|
}
|
|
|
|
unset($check['_Token']);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-11-21 04:42:54 +00:00
|
|
|
$locked = explode('|', $locked);
|
2011-06-09 02:47:49 +00:00
|
|
|
$disabled = explode('|', $disabled);
|
2010-11-08 01:53:04 +00:00
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
$lockedFields = array();
|
|
|
|
$fields = Set::flatten($check);
|
|
|
|
$fieldList = array_keys($fields);
|
2008-10-23 03:18:08 +00:00
|
|
|
$multi = array();
|
|
|
|
|
|
|
|
foreach ($fieldList as $i => $key) {
|
|
|
|
if (preg_match('/\.\d+$/', $key)) {
|
2008-10-29 06:55:42 +00:00
|
|
|
$multi[$i] = preg_replace('/\.\d+$/', '', $key);
|
2008-10-23 03:18:08 +00:00
|
|
|
unset($fieldList[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($multi)) {
|
|
|
|
$fieldList += array_unique($multi);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-06-09 02:47:49 +00:00
|
|
|
$disabledFields = array_unique(array_merge((array)$this->disabledFields, $disabled));
|
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
foreach ($fieldList as $i => $key) {
|
|
|
|
$isDisabled = false;
|
|
|
|
$isLocked = (is_array($locked) && in_array($key, $locked));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-06-09 02:47:49 +00:00
|
|
|
if (!empty($disabledFields)) {
|
|
|
|
foreach ($disabledFields as $off) {
|
|
|
|
$off = explode('.', $off);
|
|
|
|
$field = array_values(array_intersect(explode('.', $key), $off));
|
|
|
|
$isDisabled = ($field === $off);
|
2008-09-24 23:02:14 +00:00
|
|
|
if ($isDisabled) {
|
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
if ($isDisabled || $isLocked) {
|
|
|
|
unset($fieldList[$i]);
|
|
|
|
if ($isLocked) {
|
|
|
|
$lockedFields[$key] = $fields[$key];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-09 02:47:49 +00:00
|
|
|
sort($disabled, SORT_STRING);
|
2008-09-24 23:02:14 +00:00
|
|
|
sort($fieldList, SORT_STRING);
|
|
|
|
ksort($lockedFields, SORT_STRING);
|
|
|
|
|
|
|
|
$fieldList += $lockedFields;
|
2011-06-09 02:47:49 +00:00
|
|
|
$disabled = implode('|', $disabled);
|
|
|
|
$check = Security::hash(serialize($fieldList) . $disabled . Configure::read('Security.salt'));
|
2008-09-24 23:02:14 +00:00
|
|
|
return ($token === $check);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Add authentication key for new form posts
|
|
|
|
*
|
|
|
|
* @param object $controller Instantiating controller
|
|
|
|
* @return bool Success
|
|
|
|
*/
|
2010-12-18 05:03:03 +00:00
|
|
|
protected function _generateToken($controller) {
|
2010-09-30 04:18:25 +00:00
|
|
|
if (isset($controller->request->params['requested']) && $controller->request->params['requested'] === 1) {
|
2009-12-19 00:05:33 +00:00
|
|
|
if ($this->Session->check('_Token')) {
|
2010-09-30 04:06:38 +00:00
|
|
|
$tokenData = $this->Session->read('_Token');
|
2010-09-30 04:18:25 +00:00
|
|
|
$controller->request->params['_Token'] = $tokenData;
|
2009-12-19 00:05:33 +00:00
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$authKey = Security::generateAuthKey();
|
|
|
|
$token = array(
|
|
|
|
'key' => $authKey,
|
|
|
|
'allowedControllers' => $this->allowedControllers,
|
|
|
|
'allowedActions' => $this->allowedActions,
|
2010-09-30 04:26:44 +00:00
|
|
|
'disabledFields' => $this->disabledFields,
|
|
|
|
'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
|
|
|
}
|
2010-10-25 00:26:31 +00:00
|
|
|
}
|
|
|
|
if ($this->csrfCheck && ($this->csrfUseOnce || empty($tokenData['csrfTokens'])) ) {
|
|
|
|
$token['csrfTokens'][$authKey] = strtotime($this->csrfExpires);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-05-31 01:48:26 +00:00
|
|
|
if ($this->csrfCheck && $this->csrfUseOnce == false) {
|
|
|
|
$csrfTokens = array_keys($token['csrfTokens']);
|
|
|
|
$token['key'] = $csrfTokens[0];
|
|
|
|
}
|
2010-09-30 04:06:38 +00:00
|
|
|
$this->Session->write('_Token', $token);
|
2011-05-17 00:35:27 +00:00
|
|
|
$controller->request->params['_Token'] = array(
|
|
|
|
'key' => $token['key'],
|
|
|
|
'disabledFields' => $token['disabledFields']
|
|
|
|
);
|
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
|
2010-10-02 04:22:44 +00:00
|
|
|
* and that the token is legit/not expired. If the token is valid
|
|
|
|
* 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
|
|
|
|
* @return boolean Valid csrf token.
|
|
|
|
*/
|
|
|
|
protected function _validateCsrf($controller) {
|
|
|
|
$token = $this->Session->read('_Token');
|
2010-10-02 21:16:40 +00:00
|
|
|
$requestToken = $controller->request->data('_Token.key');
|
|
|
|
if (isset($token['csrfTokens'][$requestToken]) && $token['csrfTokens'][$requestToken] >= time()) {
|
2010-10-25 00:26:31 +00:00
|
|
|
if ($this->csrfUseOnce) {
|
|
|
|
$this->Session->delete('_Token.csrfTokens.' . $requestToken);
|
|
|
|
}
|
2010-10-01 04:13:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* @return An array of nonce => expires.
|
|
|
|
*/
|
|
|
|
protected function _expireTokens($tokens) {
|
2010-10-02 22:27:39 +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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tokens;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Calls a controller callback method
|
|
|
|
*
|
|
|
|
* @param object $controller Controller to run callback on
|
|
|
|
* @param string $method Method to execute
|
|
|
|
* @param array $params Parameters to send to method
|
|
|
|
* @return mixed Controller callback method's response
|
|
|
|
*/
|
2010-12-18 05:03:03 +00:00
|
|
|
protected function _callback($controller, $method, $params = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_callable(array($controller, $method))) {
|
|
|
|
return call_user_func_array(array(&$controller, $method), empty($params) ? null : $params);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|