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>
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, 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
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, 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
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs.controller.components
|
|
|
|
* @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
|
|
|
*/
|
2010-08-28 04:01:41 +00:00
|
|
|
App::import('Controller', 'Controller', false);
|
2008-07-21 02:40:58 +00:00
|
|
|
App::import('Component', 'Security');
|
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
|
|
|
*
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs.controller.components
|
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
|
|
|
|
*/
|
2010-12-18 05:03:03 +00:00
|
|
|
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
|
|
|
*
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs.controller.components
|
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'
|
|
|
|
* @access public
|
|
|
|
*/
|
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
|
|
|
|
* @access public
|
|
|
|
*/
|
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
|
|
|
|
* @access public
|
|
|
|
*/
|
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
|
|
|
|
* @access public
|
|
|
|
*/
|
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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function fail() {
|
|
|
|
$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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-12-18 05:03:03 +00:00
|
|
|
function redirect($url, $status = null, $exit = true) {
|
|
|
|
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
|
|
|
*
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs.controller.components
|
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
|
|
|
|
* @access public
|
|
|
|
*/
|
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
|
|
|
|
* @access public
|
|
|
|
*/
|
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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-26 01:36:49 +00:00
|
|
|
function setUp() {
|
|
|
|
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
|
|
|
|
*/
|
2010-09-26 01:36:49 +00:00
|
|
|
function tearDown() {
|
|
|
|
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
|
|
|
|
*/
|
2010-07-31 19:49:10 +00:00
|
|
|
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,
|
|
|
|
'loginUsers' => array(
|
|
|
|
'mark' => 'password'
|
|
|
|
),
|
|
|
|
'requireLogin' => array('login'),
|
|
|
|
);
|
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']);
|
|
|
|
$this->assertEqual($Security->loginUsers, $settings['loginUsers']);
|
|
|
|
$this->assertEqual($Security->requireLogin, $settings['requireLogin']);
|
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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testStartup() {
|
|
|
|
$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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequirePostFail() {
|
|
|
|
$_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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequirePostSucceed() {
|
|
|
|
$_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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequireSecureSucceed() {
|
|
|
|
$_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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequireAuthFail() {
|
|
|
|
$_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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequireAuthSucceed() {
|
|
|
|
$_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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequirePostSucceedWrongMethod() {
|
|
|
|
$_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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequireGetFail() {
|
|
|
|
$_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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequireGetSucceed() {
|
|
|
|
$_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
|
|
|
/**
|
|
|
|
* testRequireLogin method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequireLogin() {
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'posted';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->requireLogin(
|
|
|
|
'posted',
|
|
|
|
array('type' => 'basic', 'users' => array('admin' => 'password'))
|
|
|
|
);
|
2008-06-20 20:17:23 +00:00
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'admin';
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'password';
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertFalse($this->Controller->failed);
|
|
|
|
|
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'posted';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->requireLogin(
|
2009-12-02 22:56:52 +00:00
|
|
|
array('posted'),
|
2008-05-30 11:40:08 +00:00
|
|
|
array('type' => 'basic', 'users' => array('admin' => 'password'))
|
|
|
|
);
|
2008-06-20 20:17:23 +00:00
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'admin2';
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'password';
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$this->assertTrue($this->Controller->failed);
|
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'posted';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->Controller->Security->requireLogin(
|
|
|
|
'posted',
|
|
|
|
array('type' => 'basic', 'users' => array('admin' => 'password'))
|
|
|
|
);
|
2008-06-20 20:17:23 +00:00
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'admin';
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'password2';
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* testDigestAuth method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testDigestAuth() {
|
2009-08-03 19:40:02 +00:00
|
|
|
$skip = $this->skipIf((version_compare(PHP_VERSION, '5.1') == -1) XOR (!function_exists('apache_request_headers')),
|
|
|
|
"%s Cannot run Digest Auth test for PHP versions < 5.1"
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($skip) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request['action'] = 'posted';
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['PHP_AUTH_DIGEST'] = $digest = <<<DIGEST
|
|
|
|
Digest username="Mufasa",
|
|
|
|
realm="testrealm@host.com",
|
|
|
|
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
|
|
|
|
uri="/dir/index.html",
|
|
|
|
qop=auth,
|
|
|
|
nc=00000001,
|
|
|
|
cnonce="0a4f113b",
|
|
|
|
response="460d0d3c6867c2f1ab85b1ada1aece48",
|
|
|
|
opaque="5ccc069c403ebaf9f0171e9517f40e41"
|
|
|
|
DIGEST;
|
2008-09-24 23:02:14 +00:00
|
|
|
$this->Controller->Security->requireLogin('posted', array(
|
|
|
|
'type' => 'digest', 'users' => array('Mufasa' => 'password'),
|
|
|
|
'realm' => 'testrealm@host.com'
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
$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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequireGetSucceedWrongMethod() {
|
|
|
|
$_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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequirePutFail() {
|
|
|
|
$_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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequirePutSucceed() {
|
|
|
|
$_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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequirePutSucceedWrongMethod() {
|
|
|
|
$_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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequireDeleteFail() {
|
|
|
|
$_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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequireDeleteSucceed() {
|
|
|
|
$_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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequireDeleteSucceedWrongMethod() {
|
|
|
|
$_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-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequireLoginSettings method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequireLoginSettings() {
|
|
|
|
$this->Controller->Security->requireLogin(
|
|
|
|
'add', 'edit',
|
|
|
|
array('type' => 'basic', 'users' => array('admin' => 'password'))
|
|
|
|
);
|
|
|
|
$this->assertEqual($this->Controller->Security->requireLogin, array('add', 'edit'));
|
|
|
|
$this->assertEqual($this->Controller->Security->loginUsers, array('admin' => 'password'));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequireLoginAllActions method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testRequireLoginAllActions() {
|
|
|
|
$this->Controller->Security->requireLogin(
|
|
|
|
array('type' => 'basic', 'users' => array('admin' => 'password'))
|
|
|
|
);
|
|
|
|
$this->assertEqual($this->Controller->Security->requireLogin, array('*'));
|
|
|
|
$this->assertEqual($this->Controller->Security->loginUsers, array('admin' => 'password'));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
/**
|
|
|
|
* Simple hash validation test
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testValidatePost() {
|
|
|
|
$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';
|
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'),
|
|
|
|
'_Token' => compact('key', 'fields')
|
|
|
|
);
|
|
|
|
$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
|
|
|
*/
|
2009-11-08 19:12:18 +00:00
|
|
|
function testValidatePostFormHacking() {
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$key = $this->Controller->params['_Token']['key'];
|
2010-11-21 04:42:54 +00:00
|
|
|
$fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
|
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'),
|
|
|
|
'_Token' => compact('key')
|
|
|
|
);
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function testValidatePostObjectDeserialize() {
|
|
|
|
$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';
|
|
|
|
|
|
|
|
// 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'),
|
|
|
|
'_Token' => compact('key', 'fields')
|
|
|
|
);
|
|
|
|
$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
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testValidatePostArray() {
|
|
|
|
$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';
|
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')),
|
|
|
|
'_Token' => compact('key', 'fields')
|
|
|
|
);
|
|
|
|
$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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testValidatePostNoModel() {
|
|
|
|
$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';
|
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',
|
|
|
|
'_Token' => compact('key', 'fields')
|
|
|
|
);
|
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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testValidatePostSimple() {
|
|
|
|
$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';
|
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' => ''),
|
|
|
|
'_Token' => compact('key', 'fields')
|
|
|
|
);
|
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
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testValidatePostComplex() {
|
|
|
|
$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';
|
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' => ''
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'_Token' => compact('key', 'fields')
|
|
|
|
);
|
|
|
|
$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
|
|
|
*/
|
2008-10-23 03:18:08 +00:00
|
|
|
function testValidatePostMultipleSelect() {
|
|
|
|
$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';
|
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)),
|
|
|
|
'_Token' => compact('key', 'fields'),
|
|
|
|
);
|
|
|
|
$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)),
|
|
|
|
'_Token' => compact('key', 'fields'),
|
|
|
|
);
|
|
|
|
$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)),
|
|
|
|
'_Token' => compact('key', 'fields'),
|
|
|
|
);
|
|
|
|
$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',
|
|
|
|
'Tag' => array('Tag' => array(1)), '_Token' => compact('key', 'fields'),
|
|
|
|
);
|
|
|
|
$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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testValidatePostCheckbox() {
|
|
|
|
$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';
|
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'),
|
|
|
|
'_Token' => compact('key', 'fields')
|
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'),
|
|
|
|
'_Token' => compact('key', 'fields')
|
|
|
|
);
|
|
|
|
|
|
|
|
$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'),
|
|
|
|
'_Token' => compact('key', 'fields')
|
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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testValidatePostHidden() {
|
|
|
|
$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';
|
|
|
|
$fields .= '';
|
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'
|
|
|
|
),
|
|
|
|
'_Token' => compact('key', 'fields')
|
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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testValidatePostWithDisabledFields() {
|
|
|
|
$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';
|
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'
|
|
|
|
),
|
|
|
|
'_Token' => compact('fields', 'key')
|
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
|
|
|
/**
|
|
|
|
* testValidateHiddenMultipleModel method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testValidateHiddenMultipleModel() {
|
|
|
|
$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';
|
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'),
|
|
|
|
'_Token' => compact('key', 'fields')
|
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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testValidateHasManyModel() {
|
|
|
|
$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';
|
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'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'_Token' => compact('key', 'fields')
|
|
|
|
);
|
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
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testValidateHasManyRecordsPass() {
|
|
|
|
$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';
|
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
|
|
|
)
|
|
|
|
),
|
2008-09-24 23:02:14 +00:00
|
|
|
'_Token' => compact('key', 'fields')
|
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.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testValidateHasManyRecordsFail() {
|
|
|
|
$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';
|
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
|
|
|
)
|
|
|
|
),
|
2008-09-24 23:02:14 +00:00
|
|
|
'_Token' => compact('key', 'fields')
|
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-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testLoginRequest method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testLoginRequest() {
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$realm = 'cakephp.org';
|
|
|
|
$options = array('realm' => $realm, 'type' => 'basic');
|
|
|
|
$result = $this->Controller->Security->loginRequest($options);
|
|
|
|
$expected = 'WWW-Authenticate: Basic realm="'.$realm.'"';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$options = array('realm' => $realm, 'type' => 'digest');
|
|
|
|
$result = $this->Controller->Security->loginRequest($options);
|
|
|
|
$this->assertPattern('/realm="'.$realm.'"/', $result);
|
|
|
|
$this->assertPattern('/qop="auth"/', $result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testGenerateDigestResponseHash method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testGenerateDigestResponseHash() {
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$realm = 'cakephp.org';
|
|
|
|
$loginData = array('realm' => $realm, 'users' => array('Willy Smith' => 'password'));
|
|
|
|
$this->Controller->Security->requireLogin($loginData);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'username' => 'Willy Smith',
|
|
|
|
'password' => 'password',
|
|
|
|
'nonce' => String::uuid(),
|
|
|
|
'nc' => 1,
|
|
|
|
'cnonce' => 1,
|
|
|
|
'realm' => $realm,
|
|
|
|
'uri' => 'path_to_identifier',
|
|
|
|
'qop' => 'testme'
|
|
|
|
);
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
|
|
|
|
|
|
$result = $this->Controller->Security->generateDigestResponseHash($data);
|
2008-06-20 20:17:23 +00:00
|
|
|
$expected = md5(
|
2008-09-24 23:02:14 +00:00
|
|
|
md5($data['username'] . ':' . $loginData['realm'] . ':' . $data['password']) . ':' .
|
|
|
|
$data['nonce'] . ':' . $data['nc'] . ':' . $data['cnonce'] . ':' . $data['qop'] . ':' .
|
2008-06-20 20:17:23 +00:00
|
|
|
md5(env('REQUEST_METHOD') . ':' . $data['uri'])
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertIdentical($result, $expected);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testLoginCredentials method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testLoginCredentials() {
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = $user = 'Willy Test';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = $pw = 'some password for the nice test';
|
2008-05-31 03:54:22 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $this->Controller->Security->loginCredentials('basic');
|
|
|
|
$expected = array('username' => $user, 'password' => $pw);
|
|
|
|
$this->assertIdentical($result, $expected);
|
|
|
|
|
2008-09-12 05:11:34 +00:00
|
|
|
if (version_compare(PHP_VERSION, '5.1') != -1) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['PHP_AUTH_DIGEST'] = $digest = <<<DIGEST
|
|
|
|
Digest username="Mufasa",
|
|
|
|
realm="testrealm@host.com",
|
|
|
|
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
|
|
|
|
uri="/dir/index.html",
|
|
|
|
qop=auth,
|
|
|
|
nc=00000001,
|
|
|
|
cnonce="0a4f113b",
|
|
|
|
response="6629fae49393a05397450978507c4ef1",
|
|
|
|
opaque="5ccc069c403ebaf9f0171e9517f40e41"
|
|
|
|
DIGEST;
|
|
|
|
$expected = array(
|
|
|
|
'username' => 'Mufasa',
|
2010-10-12 02:35:47 +00:00
|
|
|
'realm' => 'testrealm@host.com',
|
2008-05-30 11:40:08 +00:00
|
|
|
'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
|
|
|
|
'uri' => '/dir/index.html',
|
|
|
|
'qop' => 'auth',
|
|
|
|
'nc' => '00000001',
|
|
|
|
'cnonce' => '0a4f113b',
|
|
|
|
'response' => '6629fae49393a05397450978507c4ef1',
|
|
|
|
'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
|
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->loginCredentials('digest');
|
|
|
|
$this->assertIdentical($result, $expected);
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testParseDigestAuthData method
|
2008-07-04 14:18:01 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testParseDigestAuthData() {
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$digest = <<<DIGEST
|
|
|
|
Digest username="Mufasa",
|
|
|
|
realm="testrealm@host.com",
|
|
|
|
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
|
|
|
|
uri="/dir/index.html",
|
|
|
|
qop=auth,
|
|
|
|
nc=00000001,
|
|
|
|
cnonce="0a4f113b",
|
|
|
|
response="6629fae49393a05397450978507c4ef1",
|
|
|
|
opaque="5ccc069c403ebaf9f0171e9517f40e41"
|
|
|
|
DIGEST;
|
|
|
|
$expected = array(
|
|
|
|
'username' => 'Mufasa',
|
2010-10-12 02:35:47 +00:00
|
|
|
'realm' => 'testrealm@host.com',
|
2008-05-30 11:40:08 +00:00
|
|
|
'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
|
|
|
|
'uri' => '/dir/index.html',
|
|
|
|
'qop' => 'auth',
|
|
|
|
'nc' => '00000001',
|
|
|
|
'cnonce' => '0a4f113b',
|
|
|
|
'response' => '6629fae49393a05397450978507c4ef1',
|
|
|
|
'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
|
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->parseDigestAuthData($digest);
|
|
|
|
$this->assertIdentical($result, $expected);
|
|
|
|
|
|
|
|
$result = $this->Controller->Security->parseDigestAuthData('');
|
|
|
|
$this->assertNull($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-12 02:35:47 +00:00
|
|
|
/**
|
|
|
|
* test parsing digest information with email addresses
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testParseDigestAuthEmailAddress() {
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
$digest = <<<DIGEST
|
|
|
|
Digest username="mark@example.com",
|
|
|
|
realm="testrealm@host.com",
|
|
|
|
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
|
|
|
|
uri="/dir/index.html",
|
|
|
|
qop=auth,
|
|
|
|
nc=00000001,
|
|
|
|
cnonce="0a4f113b",
|
|
|
|
response="6629fae49393a05397450978507c4ef1",
|
|
|
|
opaque="5ccc069c403ebaf9f0171e9517f40e41"
|
|
|
|
DIGEST;
|
|
|
|
$expected = array(
|
|
|
|
'username' => 'mark@example.com',
|
|
|
|
'realm' => 'testrealm@host.com',
|
|
|
|
'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
|
|
|
|
'uri' => '/dir/index.html',
|
|
|
|
'qop' => 'auth',
|
|
|
|
'nc' => '00000001',
|
|
|
|
'cnonce' => '0a4f113b',
|
|
|
|
'response' => '6629fae49393a05397450978507c4ef1',
|
|
|
|
'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
|
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->parseDigestAuthData($digest);
|
|
|
|
$this->assertIdentical($result, $expected);
|
|
|
|
}
|
|
|
|
|
2008-07-05 10:27:29 +00:00
|
|
|
/**
|
|
|
|
* testFormDisabledFields method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testFormDisabledFields() {
|
|
|
|
$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';
|
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'),
|
|
|
|
'_Token' => compact('key', 'fields')
|
|
|
|
);
|
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'),
|
|
|
|
'_Token' => compact('key', 'fields')
|
|
|
|
);
|
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
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testRadio() {
|
|
|
|
$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';
|
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-11-25 16:32:54 +00:00
|
|
|
'_Token' => compact('key', 'fields')
|
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
2010-05-29 03:57:43 +00:00
|
|
|
$this->Controller->request->data = array(
|
2008-11-25 16:32:54 +00:00
|
|
|
'_Token' => compact('key', 'fields'),
|
|
|
|
'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(
|
2008-11-25 16:32:54 +00:00
|
|
|
'_Token' => compact('key', 'fields'),
|
|
|
|
'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(
|
2008-11-25 16:32:54 +00:00
|
|
|
'_Token' => compact('key', 'fields'),
|
|
|
|
'Test' => array('test' => '2')
|
|
|
|
);
|
|
|
|
$result = $this->Controller->Security->validatePost($this->Controller);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-25 16:32:54 +00:00
|
|
|
/**
|
2008-11-10 17:18:00 +00:00
|
|
|
* testInvalidAuthHeaders method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testInvalidAuthHeaders() {
|
|
|
|
$this->Controller->Security->blackHoleCallback = null;
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'admin';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'password';
|
|
|
|
$realm = 'cakephp.org';
|
|
|
|
$loginData = array('type' => 'basic', 'realm' => $realm);
|
|
|
|
$this->Controller->Security->requireLogin($loginData);
|
|
|
|
$this->Controller->Security->startup($this->Controller);
|
|
|
|
|
|
|
|
$expected = 'WWW-Authenticate: Basic realm="'.$realm.'"';
|
|
|
|
$this->assertEqual(count($this->Controller->testHeaders), 1);
|
|
|
|
$this->assertEqual(current($this->Controller->testHeaders), $expected);
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
function testSettingTokenForRequestAction() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function testBlackHoleNotDeletingSessionInformation() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function testCsrfSettings() {
|
|
|
|
$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');
|
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
|
|
|
|
*/
|
2010-09-30 04:26:44 +00:00
|
|
|
function testCsrfSettingMultipleNonces() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function testCsrfNonceConsumption() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function testCsrfNonceVacuum() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function testCsrfBlackHoleOnKeyMismatch() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function testCsrfBlackHoleOnExpiredKey() {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function testCsrfNotUseOnce() {
|
|
|
|
$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.');
|
|
|
|
}
|
2010-10-25 00:57:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ensure that longer session tokens are not consumed
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testCsrfNotUseOnceValidationLeavingToken() {
|
|
|
|
$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
|
|
|
}
|