2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* SecurityComponentTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
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
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5435
|
2010-10-03 16:27:27 +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('SecurityComponent', 'Controller/Component');
|
2010-12-09 05:55:24 +00:00
|
|
|
App::uses('Controller', 'Controller');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-07-04 14:18:01 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* TestSecurityComponent
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2008-07-04 14:18:01 +00:00
|
|
|
*/
|
|
|
|
class TestSecurityComponent extends SecurityComponent {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-18 17:55:58 +00:00
|
|
|
/**
|
|
|
|
* validatePost method
|
|
|
|
*
|
|
|
|
* @param Controller $controller
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function validatePost($controller) {
|
2008-09-16 01:39:20 +00:00
|
|
|
return $this->_validatePost($controller);
|
2008-07-04 14:18:01 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* SecurityTestController
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class SecurityTestController extends Controller {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* name property
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var string 'SecurityTest'
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = 'SecurityTest';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* components property
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $components = array('Session', 'TestSecurity');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* failed property
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var bool false
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $failed = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-10 17:18:00 +00:00
|
|
|
/**
|
|
|
|
* Used for keeping track of headers in test
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $testHeaders = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* fail method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function fail() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->failed = true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* redirect method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
|
|
|
* @param mixed $option
|
|
|
|
* @param mixed $code
|
|
|
|
* @param mixed $exit
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function redirect($url, $status = null, $exit = true) {
|
2010-12-18 05:03:03 +00:00
|
|
|
return $status;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-10 17:18:00 +00:00
|
|
|
/**
|
|
|
|
* Conveinence method for header()
|
|
|
|
*
|
|
|
|
* @param string $status
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function header($status) {
|
2008-11-10 17:18:00 +00:00
|
|
|
$this->testHeaders[] = $status;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* SecurityComponentTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class SecurityComponentTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-18 17:55:58 +00:00
|
|
|
/**
|
|
|
|
* Controller property
|
|
|
|
*
|
|
|
|
* @var SecurityTestController
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $Controller;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-18 17:55:58 +00:00
|
|
|
/**
|
|
|
|
* oldSalt property
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $oldSalt;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* setUp method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2010-09-26 01:36:49 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
2010-09-15 02:20:30 +00:00
|
|
|
$request = new CakeRequest('posts/index', false);
|
|
|
|
$request->addParams(array('controller' => 'posts', 'action' => 'index'));
|
|
|
|
$this->Controller = new SecurityTestController($request);
|
2010-07-05 00:34:07 +00:00
|
|
|
$this->Controller->Components->init($this->Controller);
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->Security = $this->Controller->TestSecurity;
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->blackHoleCallback = 'fail';
|
2010-09-30 04:06:38 +00:00
|
|
|
$this->Security = $this->Controller->Security;
|
2010-10-01 04:13:34 +00:00
|
|
|
$this->Security->csrfCheck = false;
|
2010-09-26 01:36:49 +00:00
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
Configure::write('Security.salt', 'foo!');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* Tear-down method. Resets environment state.
|
2008-09-24 23:02:14 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function tearDown() {
|
2010-09-26 01:36:49 +00:00
|
|
|
parent::tearDown();
|
2009-10-21 11:00:51 +00:00
|
|
|
$this->Controller->Session->delete('_Token');
|
2008-09-24 23:02:14 +00:00
|
|
|
unset($this->Controller->Security);
|
|
|
|
unset($this->Controller->Component);
|
|
|
|
unset($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-12-17 03:39:01 +00:00
|
|
|
/**
|
|
|
|
* test that initalize can set properties.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testConstructorSettingProperties() {
|
2009-12-17 03:39:01 +00:00
|
|
|
$settings = array(
|
|
|
|
'requirePost' => array('edit', 'update'),
|
|
|
|
'requireSecure' => array('update_account'),
|
|
|
|
'requireGet' => array('index'),
|
|
|
|
'validatePost' => false,
|
|
|
|
);
|
2010-07-31 19:49:10 +00:00
|
|
|
$Security = new SecurityComponent($this->Controller->Components, $settings);
|
2009-12-17 03:39:01 +00:00
|
|
|
$this->Controller->Security->initialize($this->Controller, $settings);
|
2010-07-31 19:49:10 +00:00
|
|
|
$this->assertEqual($Security->requirePost, $settings['requirePost']);
|
|
|
|
$this->assertEqual($Security->requireSecure, $settings['requireSecure']);
|
|
|
|
$this->assertEqual($Security->requireGet, $settings['requireGet']);
|
|
|
|
$this->assertEqual($Security->validatePost, $settings['validatePost']);
|
2009-12-17 03:39:01 +00:00
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testStartup method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testStartup() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$result = $this->Controller->params['_Token']['key'];
|
|
|
|
$this->assertNotNull($result);
|
|
|
|
$this->assertTrue($this->Controller->Session->check('_Token'));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequirePostFail method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequirePostFail() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'posted';
|
2009-12-02 22:56:52 +00:00
|
|
|
$this->Controller->Security->requirePost(array('posted'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertTrue($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequirePostSucceed method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequirePostSucceed() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'posted';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->requirePost('posted');
|
2010-10-01 04:13:34 +00:00
|
|
|
$this->Security->startup($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequireSecureFail method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequireSecureFail() {
|
2009-10-31 17:41:16 +00:00
|
|
|
$_SERVER['HTTPS'] = 'off';
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'posted';
|
2009-12-02 22:56:52 +00:00
|
|
|
$this->Controller->Security->requireSecure(array('posted'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertTrue($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequireSecureSucceed method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequireSecureSucceed() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'Secure';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'posted';
|
2008-10-23 03:18:08 +00:00
|
|
|
$_SERVER['HTTPS'] = 'on';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->requireSecure('posted');
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertFalse($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequireAuthFail method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequireAuthFail() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'AUTH';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'posted';
|
|
|
|
$this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
|
2009-12-02 22:56:52 +00:00
|
|
|
$this->Controller->Security->requireAuth(array('posted'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertTrue($this->Controller->failed);
|
|
|
|
|
2010-09-30 04:26:44 +00:00
|
|
|
$this->Controller->Session->write('_Token', array('allowedControllers' => array()));
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
|
|
|
|
$this->Controller->request['action'] = 'posted';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->requireAuth('posted');
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertTrue($this->Controller->failed);
|
|
|
|
|
2010-09-30 04:26:44 +00:00
|
|
|
$this->Controller->Session->write('_Token', array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'allowedControllers' => array('SecurityTest'), 'allowedActions' => array('posted2')
|
2010-09-30 04:26:44 +00:00
|
|
|
));
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
|
|
|
|
$this->Controller->request['action'] = 'posted';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->requireAuth('posted');
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertTrue($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequireAuthSucceed method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequireAuthSucceed() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'AUTH';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'posted';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->requireAuth('posted');
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertFalse($this->Controller->failed);
|
|
|
|
|
2010-09-30 04:26:44 +00:00
|
|
|
$this->Controller->Security->Session->write('_Token', array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'allowedControllers' => array('SecurityTest'), 'allowedActions' => array('posted')
|
2010-09-30 04:26:44 +00:00
|
|
|
));
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['controller'] = 'SecurityTest';
|
|
|
|
$this->Controller->request['action'] = 'posted';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'username' => 'willy', 'password' => 'somePass', '_Token' => ''
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->action = 'posted';
|
|
|
|
$this->Controller->Security->requireAuth('posted');
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertFalse($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequirePostSucceedWrongMethod method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequirePostSucceedWrongMethod() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'getted';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->requirePost('posted');
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertFalse($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequireGetFail method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequireGetFail() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'getted';
|
2009-12-02 22:56:52 +00:00
|
|
|
$this->Controller->Security->requireGet(array('getted'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertTrue($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequireGetSucceed method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequireGetSucceed() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'getted';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->requireGet('getted');
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertFalse($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequireGetSucceedWrongMethod method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequireGetSucceedWrongMethod() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'posted';
|
2010-10-01 04:13:34 +00:00
|
|
|
$this->Security->requireGet('getted');
|
|
|
|
$this->Security->startup($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequirePutFail method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequirePutFail() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'putted';
|
2009-12-02 22:56:52 +00:00
|
|
|
$this->Controller->Security->requirePut(array('putted'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertTrue($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequirePutSucceed method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequirePutSucceed() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'putted';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->requirePut('putted');
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertFalse($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequirePutSucceedWrongMethod method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequirePutSucceedWrongMethod() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'posted';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->requirePut('putted');
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertFalse($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequireDeleteFail method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequireDeleteFail() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'deleted';
|
2009-12-02 22:56:52 +00:00
|
|
|
$this->Controller->Security->requireDelete(array('deleted', 'other_method'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertTrue($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequireDeleteSucceed method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequireDeleteSucceed() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'DELETE';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'deleted';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->requireDelete('deleted');
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertFalse($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequireDeleteSucceedWrongMethod method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequireDeleteSucceedWrongMethod() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'posted';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->requireDelete('deleted');
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertFalse($this->Controller->failed);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
/**
|
|
|
|
* Simple hash validation test
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidatePost() {
|
2008-09-24 23:02:14 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-12-11 03:24:31 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-09-24 23:02:14 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
|
|
|
$this->assertTrue($this->Controller->Security->validatePost($this->Controller));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-11-08 19:12:18 +00:00
|
|
|
/**
|
|
|
|
* test that validatePost fails if any of its required fields are missing.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidatePostFormHacking() {
|
2009-11-08 19:12:18 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$key = $this->Controller->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2009-11-08 19:12:18 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2009-11-08 19:12:18 +00:00
|
|
|
'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'unlocked')
|
2009-11-08 19:12:18 +00:00
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertFalse($result, 'validatePost passed when fields were missing. %s');
|
|
|
|
}
|
2010-10-02 21:16:40 +00:00
|
|
|
|
2010-11-08 01:53:04 +00:00
|
|
|
/**
|
|
|
|
* Test that objects can't be passed into the serialized string. This was a vector for RFI and LFI
|
|
|
|
* attacks. Thanks to Felix Wilhelm
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidatePostObjectDeserialize() {
|
2010-11-08 01:53:04 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-11-17 02:48:13 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2010-11-08 01:53:04 +00:00
|
|
|
$fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2010-11-08 01:53:04 +00:00
|
|
|
|
|
|
|
// a corrupted serialized object, so we can see if it ever gets to deserialize
|
|
|
|
$attack = 'O:3:"App":1:{s:5:"__map";a:1:{s:3:"foo";s:7:"Hacked!";s:1:"fail"}}';
|
|
|
|
$fields .= urlencode(':' . str_rot13($attack));
|
|
|
|
|
2010-11-17 02:48:13 +00:00
|
|
|
$this->Controller->request->data = array(
|
2010-11-08 01:53:04 +00:00
|
|
|
'Model' => array('username' => 'mark', 'password' => 'foo', 'valid' => '0'),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2010-11-08 01:53:04 +00:00
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertFalse($result, 'validatePost passed when key was missing. %s');
|
|
|
|
}
|
|
|
|
|
2008-12-23 08:13:00 +00:00
|
|
|
/**
|
|
|
|
* Tests validation of checkbox arrays
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidatePostArray() {
|
2008-12-23 08:13:00 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-12-11 03:24:31 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-12-23 08:13:00 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-12-23 08:13:00 +00:00
|
|
|
'Model' => array('multi_field' => array('1', '3')),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-12-23 08:13:00 +00:00
|
|
|
);
|
|
|
|
$this->assertTrue($this->Controller->Security->validatePost($this->Controller));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testValidatePostNoModel method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidatePostNoModel() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-12-11 03:24:31 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = '540ac9c60d323c22bafe997b72c0790f39a8bdef%3A';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'anything' => 'some_data',
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-07-04 14:18:01 +00:00
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testValidatePostSimple method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidatePostSimple() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-12-11 03:24:31 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = '69f493434187b867ea14b901fdf58b55d27c935d%3A';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = $data = array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'Model' => array('username' => '', 'password' => ''),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-07-04 14:18:01 +00:00
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
2008-09-24 23:02:14 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
/**
|
|
|
|
* Tests hash validation for multiple records, including locked fields
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidatePostComplex() {
|
2008-09-24 23:02:14 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-12-11 03:24:31 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-09-24 23:02:14 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'Addresses' => array(
|
|
|
|
'0' => array(
|
|
|
|
'id' => '123456', 'title' => '', 'first_name' => '', 'last_name' => '',
|
|
|
|
'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
|
|
|
|
),
|
|
|
|
'1' => array(
|
|
|
|
'id' => '654321', 'title' => '', 'first_name' => '', 'last_name' => '',
|
|
|
|
'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
|
|
|
|
)
|
|
|
|
),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
2008-10-23 03:18:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-23 03:18:08 +00:00
|
|
|
/**
|
|
|
|
* test ValidatePost with multiple select elements.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidatePostMultipleSelect() {
|
2008-10-23 03:18:08 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-12-11 03:24:31 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = '422cde416475abc171568be690a98cad20e66079%3A';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-10-23 03:18:08 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-10-23 03:18:08 +00:00
|
|
|
'Tag' => array('Tag' => array(1, 2)),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked'),
|
2008-10-23 03:18:08 +00:00
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-10-23 03:18:08 +00:00
|
|
|
'Tag' => array('Tag' => array(1, 2, 3)),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked'),
|
2008-10-23 03:18:08 +00:00
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-10-23 03:18:08 +00:00
|
|
|
'Tag' => array('Tag' => array(1, 2, 3, 4)),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked'),
|
2008-10-23 03:18:08 +00:00
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
2008-10-29 06:55:42 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = '19464422eafe977ee729c59222af07f983010c5f%3A';
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-10-29 06:55:42 +00:00
|
|
|
'User.password' => 'bar', 'User.name' => 'foo', 'User.is_valid' => '1',
|
2011-06-10 02:10:43 +00:00
|
|
|
'Tag' => array('Tag' => array(1)),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked'),
|
2008-10-29 06:55:42 +00:00
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
2008-09-24 23:02:14 +00:00
|
|
|
$this->assertTrue($result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testValidatePostCheckbox method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-08-29 03:11:35 +00:00
|
|
|
* First block tests un-checked checkbox
|
2008-09-12 05:11:34 +00:00
|
|
|
* Second block tests checked checkbox
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidatePostCheckbox() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
|
|
|
|
2008-07-04 14:18:01 +00:00
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = '874439ca69f89b4c4a5f50fb9c36ff56a28f5d42%3A';
|
2008-09-24 23:02:14 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertTrue($result);
|
2008-09-12 05:11:34 +00:00
|
|
|
|
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array();
|
2008-08-29 03:11:35 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2008-08-29 03:11:35 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = $data = array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-08-29 03:11:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertTrue($result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testValidatePostHidden method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidatePostHidden() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = '51ccd8cb0997c7b3d4523ecde5a109318405ef8c%3AModel.hidden%7CModel.other_hidden';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'Model' => array(
|
|
|
|
'username' => '', 'password' => '', 'hidden' => '0',
|
|
|
|
'other_hidden' => 'some hidden value'
|
|
|
|
),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
2008-07-04 14:18:01 +00:00
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testValidatePostWithDisabledFields method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidatePostWithDisabledFields() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->disabledFields = array('Model.username', 'Model.password');
|
2008-09-24 23:02:14 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = 'ef1082968c449397bcd849f963636864383278b1%3AModel.hidden';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'Model' => array(
|
|
|
|
'username' => '', 'password' => '', 'hidden' => '0'
|
|
|
|
),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('fields', 'key', 'unlocked')
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
|
|
|
|
2008-07-04 14:18:01 +00:00
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-06-10 02:48:47 +00:00
|
|
|
/**
|
2011-06-15 02:01:59 +00:00
|
|
|
* test validating post data with posted unlocked fields.
|
2011-06-10 02:48:47 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidatePostDisabledFieldsInData() {
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = 'Model.username';
|
2011-06-10 02:48:47 +00:00
|
|
|
$fields = array('Model.hidden', 'Model.password');
|
2011-06-15 02:01:59 +00:00
|
|
|
$fields = urlencode(Security::hash(serialize($fields) . $unlocked . Configure::read('Security.salt')));
|
2011-06-10 02:48:47 +00:00
|
|
|
|
|
|
|
$this->Controller->request->data = array(
|
|
|
|
'Model' => array(
|
|
|
|
'username' => 'mark',
|
|
|
|
'password' => 'sekret',
|
|
|
|
'hidden' => '0'
|
|
|
|
),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('fields', 'key', 'unlocked')
|
2011-06-10 02:48:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-06-15 02:01:59 +00:00
|
|
|
* test that missing 'unlocked' input causes failure
|
2011-06-10 02:48:47 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidatePostFailNoDisabled() {
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
|
|
|
$fields = array('Model.hidden', 'Model.password', 'Model.username');
|
|
|
|
$fields = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt')));
|
|
|
|
|
|
|
|
$this->Controller->request->data = array(
|
|
|
|
'Model' => array(
|
|
|
|
'username' => 'mark',
|
|
|
|
'password' => 'sekret',
|
|
|
|
'hidden' => '0'
|
|
|
|
),
|
|
|
|
'_Token' => compact('fields', 'key')
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-06-15 02:01:59 +00:00
|
|
|
* Test that validatePost fails when unlocked fields are changed.
|
2011-06-10 02:48:47 +00:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public function testValidatePostFailDisabledFieldTampering() {
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = 'Model.username';
|
2011-06-10 02:48:47 +00:00
|
|
|
$fields = array('Model.hidden', 'Model.password');
|
2011-06-15 02:01:59 +00:00
|
|
|
$fields = urlencode(Security::hash(serialize($fields) . $unlocked . Configure::read('Security.salt')));
|
2011-06-10 02:48:47 +00:00
|
|
|
|
|
|
|
// Tamper the values.
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = 'Model.username|Model.password';
|
2011-06-10 02:48:47 +00:00
|
|
|
|
|
|
|
$this->Controller->request->data = array(
|
|
|
|
'Model' => array(
|
|
|
|
'username' => 'mark',
|
|
|
|
'password' => 'sekret',
|
|
|
|
'hidden' => '0'
|
|
|
|
),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('fields', 'key', 'unlocked')
|
2011-06-10 02:48:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testValidateHiddenMultipleModel method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidateHiddenMultipleModel() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = 'a2d01072dc4660eea9d15007025f35a7a5b58e18%3AModel.valid%7CModel2.valid%7CModel3.valid';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-09-24 23:02:14 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
|
|
|
|
'Model2' => array('valid' => '0'),
|
|
|
|
'Model3' => array('valid' => '0'),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
2008-07-04 14:18:01 +00:00
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testValidateHasManyModel method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidateHasManyModel() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
|
|
|
|
$fields .= '%7CModel.1.hidden%7CModel.1.valid';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-05-30 11:40:08 +00:00
|
|
|
'Model' => array(
|
2008-09-24 23:02:14 +00:00
|
|
|
array(
|
|
|
|
'username' => 'username', 'password' => 'password',
|
|
|
|
'hidden' => 'value', 'valid' => '0'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'username' => 'username', 'password' => 'password',
|
|
|
|
'hidden' => 'value', 'valid' => '0'
|
|
|
|
)
|
|
|
|
),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-07-04 14:18:01 +00:00
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-08-29 03:11:35 +00:00
|
|
|
/**
|
|
|
|
* testValidateHasManyRecordsPass method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidateHasManyRecordsPass() {
|
2008-08-29 03:11:35 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
|
|
|
|
$fields .= 'Address.1.id%7CAddress.1.primary';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-08-29 03:11:35 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-08-29 03:11:35 +00:00
|
|
|
'Address' => array(
|
|
|
|
0 => array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'id' => '123',
|
2008-08-29 03:11:35 +00:00
|
|
|
'title' => 'home',
|
|
|
|
'first_name' => 'Bilbo',
|
|
|
|
'last_name' => 'Baggins',
|
|
|
|
'address' => '23 Bag end way',
|
|
|
|
'city' => 'the shire',
|
|
|
|
'phone' => 'N/A',
|
|
|
|
'primary' => '1',
|
|
|
|
),
|
|
|
|
1 => array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'id' => '124',
|
2008-08-29 03:11:35 +00:00
|
|
|
'title' => 'home',
|
|
|
|
'first_name' => 'Frodo',
|
|
|
|
'last_name' => 'Baggins',
|
|
|
|
'address' => '50 Bag end way',
|
2008-09-12 05:11:34 +00:00
|
|
|
'city' => 'the shire',
|
2008-08-29 03:11:35 +00:00
|
|
|
'phone' => 'N/A',
|
2008-09-24 23:02:14 +00:00
|
|
|
'primary' => '1'
|
2008-08-29 03:11:35 +00:00
|
|
|
)
|
|
|
|
),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-08-29 03:11:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-08-29 03:11:35 +00:00
|
|
|
/**
|
|
|
|
* testValidateHasManyRecords method
|
|
|
|
*
|
|
|
|
* validatePost should fail, hidden fields have been changed.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidateHasManyRecordsFail() {
|
2008-08-29 03:11:35 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
|
|
|
|
$fields .= 'Address.1.id%7CAddress.1.primary';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-08-29 03:11:35 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-08-29 03:11:35 +00:00
|
|
|
'Address' => array(
|
|
|
|
0 => array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'id' => '123',
|
2008-08-29 03:11:35 +00:00
|
|
|
'title' => 'home',
|
|
|
|
'first_name' => 'Bilbo',
|
|
|
|
'last_name' => 'Baggins',
|
|
|
|
'address' => '23 Bag end way',
|
|
|
|
'city' => 'the shire',
|
|
|
|
'phone' => 'N/A',
|
2008-09-24 23:02:14 +00:00
|
|
|
'primary' => '5',
|
2008-08-29 03:11:35 +00:00
|
|
|
),
|
|
|
|
1 => array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'id' => '124',
|
2008-08-29 03:11:35 +00:00
|
|
|
'title' => 'home',
|
|
|
|
'first_name' => 'Frodo',
|
|
|
|
'last_name' => 'Baggins',
|
|
|
|
'address' => '50 Bag end way',
|
2008-09-12 05:11:34 +00:00
|
|
|
'city' => 'the shire',
|
2008-08-29 03:11:35 +00:00
|
|
|
'phone' => 'N/A',
|
2008-09-24 23:02:14 +00:00
|
|
|
'primary' => '1'
|
2008-08-29 03:11:35 +00:00
|
|
|
)
|
|
|
|
),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-08-29 03:11:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-07-05 10:27:29 +00:00
|
|
|
/**
|
|
|
|
* testFormDisabledFields method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testFormDisabledFields() {
|
2008-07-05 10:27:29 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2008-09-24 23:02:14 +00:00
|
|
|
$fields = '11842060341b9d0fc3808b90ba29fdea7054d6ad%3An%3A0%3A%7B%7D';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-07-05 10:27:29 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'MyModel' => array('name' => 'some data'),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
2008-07-05 10:27:29 +00:00
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->Controller->Security->disabledFields = array('MyModel.name');
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2008-07-05 10:27:29 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'MyModel' => array('name' => 'some data'),
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
2008-07-05 10:27:29 +00:00
|
|
|
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertTrue($result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-10 17:18:00 +00:00
|
|
|
/**
|
2008-11-25 16:32:54 +00:00
|
|
|
* testRadio method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRadio() {
|
2008-11-25 16:32:54 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2008-11-25 16:32:54 +00:00
|
|
|
$fields = '575ef54ca4fc8cab468d6d898e9acd3a9671c17e%3An%3A0%3A%7B%7D';
|
2011-06-15 02:01:59 +00:00
|
|
|
$unlocked = '';
|
2008-11-25 16:32:54 +00:00
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked')
|
2008-11-25 16:32:54 +00:00
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked'),
|
2008-11-25 16:32:54 +00:00
|
|
|
'Test' => array('test' => '')
|
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked'),
|
2008-11-25 16:32:54 +00:00
|
|
|
'Test' => array('test' => '1')
|
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2011-06-15 02:01:59 +00:00
|
|
|
'_Token' => compact('key', 'fields', 'unlocked'),
|
2008-11-25 16:32:54 +00:00
|
|
|
'Test' => array('test' => '2')
|
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-12-19 00:05:33 +00:00
|
|
|
/**
|
|
|
|
* test that a requestAction's controller will have the _Token appended to
|
|
|
|
* the params.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @see http://cakephp.lighthouseapp.com/projects/42648/tickets/68
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSettingTokenForRequestAction() {
|
2009-12-19 00:05:33 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-05-29 03:57:43 +00:00
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
2009-12-19 00:05:33 +00:00
|
|
|
|
|
|
|
$this->Controller->params['requested'] = 1;
|
2010-05-29 03:57:43 +00:00
|
|
|
unset($this->Controller->request->params['_Token']);
|
2009-12-19 00:05:33 +00:00
|
|
|
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->assertEqual($this->Controller->request->params['_Token']['key'], $key);
|
2009-12-19 00:05:33 +00:00
|
|
|
}
|
2010-01-26 18:59:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test that blackhole doesn't delete the _Token session key so repeat data submissions
|
|
|
|
* stay blackholed.
|
|
|
|
*
|
|
|
|
* @link http://cakephp.lighthouseapp.com/projects/42648/tickets/214
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testBlackHoleNotDeletingSessionInformation() {
|
2010-01-26 18:59:26 +00:00
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
|
|
|
|
$this->Controller->Security->blackHole($this->Controller, 'auth');
|
|
|
|
$this->assertTrue($this->Controller->Security->Session->check('_Token'), '_Token was deleted by blackHole %s');
|
|
|
|
}
|
2010-09-30 04:06:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test setting
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCsrfSettings() {
|
2010-09-30 04:06:38 +00:00
|
|
|
$this->Security->validatePost = false;
|
2010-09-30 04:18:25 +00:00
|
|
|
$this->Security->csrfCheck = true;
|
2010-09-30 04:06:38 +00:00
|
|
|
$this->Security->csrfExpires = '+10 minutes';
|
|
|
|
$this->Security->startup($this->Controller);
|
2010-09-30 04:26:44 +00:00
|
|
|
|
2010-09-30 04:06:38 +00:00
|
|
|
$token = $this->Security->Session->read('_Token');
|
2010-09-30 04:18:25 +00:00
|
|
|
$this->assertEquals(count($token['csrfTokens']), 1, 'Missing the csrf token.');
|
|
|
|
$this->assertEquals(strtotime('+10 minutes'), current($token['csrfTokens']), 'Token expiry does not match');
|
2011-06-20 05:42:25 +00:00
|
|
|
$this->assertEquals(array('key', 'unlockedFields'), array_keys($this->Controller->request->params['_Token']), 'Keys don not match');
|
2010-09-30 04:26:44 +00:00
|
|
|
}
|
2010-10-01 04:13:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test setting multiple nonces, when startup() is called more than once, (ie more than one request.)
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCsrfSettingMultipleNonces() {
|
2010-09-30 04:26:44 +00:00
|
|
|
$this->Security->validatePost = false;
|
|
|
|
$this->Security->csrfCheck = true;
|
|
|
|
$this->Security->csrfExpires = '+10 minutes';
|
|
|
|
$this->Security->startup($this->Controller);
|
|
|
|
$this->Security->startup($this->Controller);
|
|
|
|
|
|
|
|
$token = $this->Security->Session->read('_Token');
|
|
|
|
$this->assertEquals(count($token['csrfTokens']), 2, 'Missing the csrf token.');
|
|
|
|
foreach ($token['csrfTokens'] as $key => $expires) {
|
|
|
|
$this->assertEquals(strtotime('+10 minutes'), $expires, 'Token expiry does not match');
|
|
|
|
}
|
2010-09-30 04:06:38 +00:00
|
|
|
}
|
2010-10-01 04:13:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test that nonces are consumed by form submits.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCsrfNonceConsumption() {
|
2010-10-01 04:13:34 +00:00
|
|
|
$this->Security->validatePost = false;
|
|
|
|
$this->Security->csrfCheck = true;
|
|
|
|
$this->Security->csrfExpires = '+10 minutes';
|
|
|
|
|
|
|
|
$this->Security->Session->write('_Token.csrfTokens', array('nonce1' => strtotime('+10 minutes')));
|
|
|
|
|
|
|
|
$this->Controller->request = $this->getMock('CakeRequest', array('is'));
|
|
|
|
$this->Controller->request->expects($this->once())->method('is')
|
|
|
|
->with('post')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->Controller->request->params['action'] = 'index';
|
|
|
|
$this->Controller->request->data = array(
|
|
|
|
'_Token' => array(
|
2010-10-02 21:16:40 +00:00
|
|
|
'key' => 'nonce1'
|
2010-10-01 04:13:34 +00:00
|
|
|
),
|
|
|
|
'Post' => array(
|
|
|
|
'title' => 'Woot'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->Security->startup($this->Controller);
|
|
|
|
$token = $this->Security->Session->read('_Token');
|
|
|
|
$this->assertFalse(isset($token['csrfTokens']['nonce1']), 'Token was not consumed');
|
|
|
|
}
|
2010-10-02 04:20:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test that expired values in the csrfTokens are cleaned up.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCsrfNonceVacuum() {
|
2010-10-02 04:20:58 +00:00
|
|
|
$this->Security->validatePost = false;
|
|
|
|
$this->Security->csrfCheck = true;
|
|
|
|
$this->Security->csrfExpires = '+10 minutes';
|
|
|
|
|
|
|
|
$this->Security->Session->write('_Token.csrfTokens', array(
|
2010-10-02 22:27:39 +00:00
|
|
|
'valid' => strtotime('+30 minutes'),
|
2010-10-02 04:20:58 +00:00
|
|
|
'poof' => strtotime('-11 minutes'),
|
|
|
|
'dust' => strtotime('-20 minutes')
|
|
|
|
));
|
|
|
|
$this->Security->startup($this->Controller);
|
|
|
|
$tokens = $this->Security->Session->read('_Token.csrfTokens');
|
2010-10-02 22:27:39 +00:00
|
|
|
$this->assertEquals(2, count($tokens), 'Too many tokens left behind');
|
|
|
|
$this->assertNotEmpty('valid', $tokens, 'Valid token was removed.');
|
|
|
|
|
2010-10-02 04:20:58 +00:00
|
|
|
}
|
2010-10-02 21:16:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test that when the key is missing the request is blackHoled
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCsrfBlackHoleOnKeyMismatch() {
|
2010-10-02 21:16:40 +00:00
|
|
|
$this->Security->validatePost = false;
|
|
|
|
$this->Security->csrfCheck = true;
|
|
|
|
$this->Security->csrfExpires = '+10 minutes';
|
|
|
|
|
|
|
|
$this->Security->Session->write('_Token.csrfTokens', array('nonce1' => strtotime('+10 minutes')));
|
|
|
|
|
|
|
|
$this->Controller->request = $this->getMock('CakeRequest', array('is'));
|
|
|
|
$this->Controller->request->expects($this->once())->method('is')
|
|
|
|
->with('post')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->Controller->request->params['action'] = 'index';
|
|
|
|
$this->Controller->request->data = array(
|
|
|
|
'_Token' => array(
|
|
|
|
'key' => 'not the right value'
|
|
|
|
),
|
|
|
|
'Post' => array(
|
|
|
|
'title' => 'Woot'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->Security->startup($this->Controller);
|
|
|
|
$this->assertTrue($this->Controller->failed, 'fail() was not called.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that when the key is missing the request is blackHoled
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCsrfBlackHoleOnExpiredKey() {
|
2010-10-02 21:16:40 +00:00
|
|
|
$this->Security->validatePost = false;
|
|
|
|
$this->Security->csrfCheck = true;
|
|
|
|
$this->Security->csrfExpires = '+10 minutes';
|
|
|
|
|
|
|
|
$this->Security->Session->write('_Token.csrfTokens', array('nonce1' => strtotime('-5 minutes')));
|
|
|
|
|
|
|
|
$this->Controller->request = $this->getMock('CakeRequest', array('is'));
|
|
|
|
$this->Controller->request->expects($this->once())->method('is')
|
|
|
|
->with('post')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->Controller->request->params['action'] = 'index';
|
|
|
|
$this->Controller->request->data = array(
|
|
|
|
'_Token' => array(
|
|
|
|
'key' => 'nonce1'
|
|
|
|
),
|
|
|
|
'Post' => array(
|
|
|
|
'title' => 'Woot'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->Security->startup($this->Controller);
|
|
|
|
$this->assertTrue($this->Controller->failed, 'fail() was not called.');
|
|
|
|
}
|
2010-10-25 00:26:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test that csrfUseOnce = false works.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCsrfNotUseOnce() {
|
2010-10-25 00:26:31 +00:00
|
|
|
$this->Security->validatePost = false;
|
|
|
|
$this->Security->csrfCheck = true;
|
|
|
|
$this->Security->csrfUseOnce = false;
|
|
|
|
$this->Security->csrfExpires = '+10 minutes';
|
|
|
|
|
|
|
|
// Generate one token
|
|
|
|
$this->Security->startup($this->Controller);
|
|
|
|
$token = $this->Security->Session->read('_Token.csrfTokens');
|
|
|
|
$this->assertEquals(1, count($token), 'Should only be one token.');
|
|
|
|
|
|
|
|
$this->Security->startup($this->Controller);
|
|
|
|
$token2 = $this->Security->Session->read('_Token.csrfTokens');
|
|
|
|
$this->assertEquals(1, count($token2), 'Should only be one token.');
|
|
|
|
$this->assertEquals($token, $token2, 'Tokens should not be different.');
|
2011-05-31 01:48:26 +00:00
|
|
|
|
|
|
|
$key = $this->Controller->request->params['_Token']['key'];
|
|
|
|
$this->assertEquals(array($key), array_keys($token), '_Token.key and csrfToken do not match request will blackhole.');
|
2010-10-25 00:26:31 +00:00
|
|
|
}
|
2010-10-25 00:57:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ensure that longer session tokens are not consumed
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCsrfNotUseOnceValidationLeavingToken() {
|
2010-10-25 00:57:12 +00:00
|
|
|
$this->Security->validatePost = false;
|
|
|
|
$this->Security->csrfCheck = true;
|
|
|
|
$this->Security->csrfUseOnce = false;
|
|
|
|
$this->Security->csrfExpires = '+10 minutes';
|
|
|
|
|
|
|
|
$this->Security->Session->write('_Token.csrfTokens', array('nonce1' => strtotime('+10 minutes')));
|
|
|
|
|
|
|
|
$this->Controller->request = $this->getMock('CakeRequest', array('is'));
|
|
|
|
$this->Controller->request->expects($this->once())->method('is')
|
|
|
|
->with('post')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->Controller->request->params['action'] = 'index';
|
|
|
|
$this->Controller->request->data = array(
|
|
|
|
'_Token' => array(
|
|
|
|
'key' => 'nonce1'
|
|
|
|
),
|
|
|
|
'Post' => array(
|
|
|
|
'title' => 'Woot'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->Security->startup($this->Controller);
|
|
|
|
$token = $this->Security->Session->read('_Token');
|
|
|
|
$this->assertTrue(isset($token['csrfTokens']['nonce1']), 'Token was consumed');
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|