| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004 |
- <?php
- /**
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @since 1.2.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Controller\Component;
- use Cake\Controller\Component\SecurityComponent;
- use Cake\Controller\Controller;
- use Cake\Core\Configure;
- use Cake\Event\Event;
- use Cake\Network\Request;
- use Cake\Network\Session;
- use Cake\TestSuite\TestCase;
- use Cake\Utility\Security;
- /**
- * TestSecurityComponent
- *
- */
- class TestSecurityComponent extends SecurityComponent {
- /**
- * validatePost method
- *
- * @param Controller $controller
- * @return bool
- */
- public function validatePost(Controller $controller) {
- return $this->_validatePost($controller);
- }
- }
- /**
- * SecurityTestController
- *
- */
- class SecurityTestController extends Controller {
- /**
- * components property
- *
- * @var array
- */
- public $components = array(
- 'Session',
- 'TestSecurity' => array('className' => 'Cake\Test\TestCase\Controller\Component\TestSecurityComponent')
- );
- /**
- * failed property
- *
- * @var bool
- */
- public $failed = false;
- /**
- * Used for keeping track of headers in test
- *
- * @var array
- */
- public $testHeaders = array();
- /**
- * fail method
- *
- * @return void
- */
- public function fail() {
- $this->failed = true;
- }
- /**
- * redirect method
- *
- * @param string|array $url
- * @param mixed $status
- * @param mixed $exit
- * @return void
- */
- public function redirect($url, $status = null, $exit = true) {
- return $status;
- }
- /**
- * Convenience method for header()
- *
- * @param string $status
- * @return void
- */
- public function header($status) {
- $this->testHeaders[] = $status;
- }
- }
- /**
- * SecurityComponentTest class
- *
- */
- class SecurityComponentTest extends TestCase {
- /**
- * Controller property
- *
- * @var SecurityTestController
- */
- public $Controller;
- /**
- * oldSalt property
- *
- * @var string
- */
- public $oldSalt;
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $session = new Session();
- $request = $this->getMock('Cake\Network\Request', ['here'], ['posts/index']);
- $request->addParams(array('controller' => 'posts', 'action' => 'index'));
- $request->session($session);
- $request->expects($this->any())
- ->method('here')
- ->will($this->returnValue('/articles/index'));
- $this->Controller = new SecurityTestController($request);
- $this->Controller->constructClasses();
- $this->Controller->Security = $this->Controller->TestSecurity;
- $this->Controller->Security->config('blackHoleCallback', 'fail');
- $this->Security = $this->Controller->Security;
- $this->Security->session = $session;
- Configure::write('Security.salt', 'foo!');
- }
- /**
- * Tear-down method. Resets environment state.
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- $this->Security->session->delete('_Token');
- unset($this->Controller->Security);
- unset($this->Controller->Component);
- unset($this->Controller);
- }
- /**
- * Test that requests are still blackholed when controller has incorrect
- * visibility keyword in the blackhole callback
- *
- * @expectedException \Cake\Error\BadRequestException
- * @return void
- */
- public function testBlackholeWithBrokenCallback() {
- $request = new Request([
- 'url' => 'posts/index',
- 'session' => $this->Security->session
- ]);
- $request->addParams([
- 'controller' => 'posts',
- 'action' => 'index'
- ]);
- $Controller = new \TestApp\Controller\SomePagesController($request);
- $event = new Event('Controller.startup', $Controller, $this->Controller);
- $Security = new SecurityComponent($Controller->components());
- $Security->config('blackHoleCallback', '_fail');
- $Security->startup($event);
- $Security->blackHole($Controller, 'csrf');
- }
- /**
- * Ensure that directly requesting the blackholeCallback as the controller
- * action results in an exception.
- *
- * @return void
- */
- public function testExceptionWhenActionIsBlackholeCallback() {
- $this->Controller->request->addParams(array(
- 'controller' => 'posts',
- 'action' => 'fail'
- ));
- $event = new Event('Controller.startup', $this->Controller);
- $this->assertFalse($this->Controller->failed);
- $this->Controller->Security->startup($event);
- $this->assertTrue($this->Controller->failed, 'Request was blackholed.');
- }
- /**
- * test that initialize can set properties.
- *
- * @return void
- */
- public function testConstructorSettingProperties() {
- $settings = array(
- 'requireSecure' => array('update_account'),
- 'validatePost' => false,
- );
- $Security = new SecurityComponent($this->Controller->components(), $settings);
- $this->assertEquals($Security->validatePost, $settings['validatePost']);
- }
- /**
- * testStartup method
- *
- * @return void
- */
- public function testStartup() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $this->assertTrue($this->Security->session->check('_Token'));
- }
- /**
- * testRequireSecureFail method
- *
- * @return void
- */
- public function testRequireSecureFail() {
- $_SERVER['HTTPS'] = 'off';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->request['action'] = 'posted';
- $this->Controller->Security->requireSecure(array('posted'));
- $this->Controller->Security->startup($event);
- $this->assertTrue($this->Controller->failed);
- }
- /**
- * testRequireSecureSucceed method
- *
- * @return void
- */
- public function testRequireSecureSucceed() {
- $_SERVER['REQUEST_METHOD'] = 'Secure';
- $this->Controller->request['action'] = 'posted';
- $_SERVER['HTTPS'] = 'on';
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->requireSecure('posted');
- $this->Controller->Security->startup($event);
- $this->assertFalse($this->Controller->failed);
- }
- /**
- * testRequireAuthFail method
- *
- * @return void
- */
- public function testRequireAuthFail() {
- $event = new Event('Controller.startup', $this->Controller);
- $_SERVER['REQUEST_METHOD'] = 'AUTH';
- $this->Controller->request['action'] = 'posted';
- $this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
- $this->Controller->Security->requireAuth(array('posted'));
- $this->Controller->Security->startup($event);
- $this->assertTrue($this->Controller->failed);
- $this->Security->session->write('_Token', array('allowedControllers' => array()));
- $this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
- $this->Controller->request['action'] = 'posted';
- $this->Controller->Security->requireAuth('posted');
- $this->Controller->Security->startup($event);
- $this->assertTrue($this->Controller->failed);
- $this->Security->session->write('_Token', array(
- 'allowedControllers' => array('SecurityTest'), 'allowedActions' => array('posted2')
- ));
- $this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
- $this->Controller->request['action'] = 'posted';
- $this->Controller->Security->requireAuth('posted');
- $this->Controller->Security->startup($event);
- $this->assertTrue($this->Controller->failed);
- }
- /**
- * testRequireAuthSucceed method
- *
- * @return void
- */
- public function testRequireAuthSucceed() {
- $_SERVER['REQUEST_METHOD'] = 'AUTH';
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->request['action'] = 'posted';
- $this->Controller->Security->requireAuth('posted');
- $this->Controller->Security->startup($event);
- $this->assertFalse($this->Controller->failed);
- $this->Controller->Security->session->write('_Token', array(
- 'allowedControllers' => array('SecurityTest'), 'allowedActions' => array('posted')
- ));
- $this->Controller->request['controller'] = 'SecurityTest';
- $this->Controller->request['action'] = 'posted';
- $this->Controller->request->data = array(
- 'username' => 'willy', 'password' => 'somePass', '_Token' => ''
- );
- $this->Controller->action = 'posted';
- $this->Controller->Security->requireAuth('posted');
- $this->Controller->Security->startup($event);
- $this->assertFalse($this->Controller->failed);
- }
- /**
- * Simple hash validation test
- *
- * @return void
- */
- public function testValidatePost() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = '68730b0747d4889ec2766f9117405f9635f5fd5e%3AModel.valid';
- $unlocked = '';
- $this->Controller->request->data = array(
- 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
- '_Token' => compact('fields', 'unlocked')
- );
- $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
- }
- /**
- * Test that validatePost fails if you are missing the session information.
- *
- * @return void
- */
- public function testValidatePostNoSession() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $this->Security->session->delete('_Token');
- $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
- $this->Controller->request->data = array(
- 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
- '_Token' => compact('fields')
- );
- $this->assertFalse($this->Controller->Security->validatePost($this->Controller));
- }
- /**
- * test that validatePost fails if any of its required fields are missing.
- *
- * @return void
- */
- public function testValidatePostFormHacking() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $unlocked = '';
- $this->Controller->request->data = array(
- 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
- '_Token' => compact('unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertFalse($result, 'validatePost passed when fields were missing. %s');
- }
- /**
- * 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
- */
- public function testValidatePostObjectDeserialize() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877';
- $unlocked = '';
- // 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));
- $this->Controller->request->data = array(
- 'Model' => array('username' => 'mark', 'password' => 'foo', 'valid' => '0'),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertFalse($result, 'validatePost passed when key was missing. %s');
- }
- /**
- * Tests validation of checkbox arrays
- *
- * @return void
- */
- public function testValidatePostArray() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = '8e26ef05379e5402c2c619f37ee91152333a0264%3A';
- $unlocked = '';
- $this->Controller->request->data = array(
- 'Model' => array('multi_field' => array('1', '3')),
- '_Token' => compact('fields', 'unlocked')
- );
- $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
- }
- /**
- * testValidatePostNoModel method
- *
- * @return void
- */
- public function testValidatePostNoModel() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = 'a1c3724b7ba85e7022413611e30ba2c6181d5aba%3A';
- $unlocked = '';
- $this->Controller->request->data = array(
- 'anything' => 'some_data',
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- }
- /**
- * testValidatePostSimple method
- *
- * @return void
- */
- public function testValidatePostSimple() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = 'b0914d06dfb04abf1fada53e16810e87d157950b%3A';
- $unlocked = '';
- $this->Controller->request->data = array(
- 'Model' => array('username' => '', 'password' => ''),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- }
- /**
- * Tests hash validation for multiple records, including locked fields
- *
- * @return void
- */
- public function testValidatePostComplex() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = 'b65c7463e44a61d8d2eaecce2c265b406c9c4742%3AAddresses.0.id%7CAddresses.1.id';
- $unlocked = '';
- $this->Controller->request->data = array(
- '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('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- }
- /**
- * test ValidatePost with multiple select elements.
- *
- * @return void
- */
- public function testValidatePostMultipleSelect() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = '8d8da68ba03b3d6e7e145b948abfe26741422169%3A';
- $unlocked = '';
- $this->Controller->request->data = array(
- 'Tag' => array('Tag' => array(1, 2)),
- '_Token' => compact('fields', 'unlocked'),
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- $this->Controller->request->data = array(
- 'Tag' => array('Tag' => array(1, 2, 3)),
- '_Token' => compact('fields', 'unlocked'),
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- $this->Controller->request->data = array(
- 'Tag' => array('Tag' => array(1, 2, 3, 4)),
- '_Token' => compact('fields', 'unlocked'),
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- $fields = 'eae2adda1628b771a30cc133342d16220c6520fe%3A';
- $this->Controller->request->data = array(
- 'User.password' => 'bar', 'User.name' => 'foo', 'User.is_valid' => '1',
- 'Tag' => array('Tag' => array(1)),
- '_Token' => compact('fields', 'unlocked'),
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- }
- /**
- * testValidatePostCheckbox method
- *
- * First block tests un-checked checkbox
- * Second block tests checked checkbox
- *
- * @return void
- */
- public function testValidatePostCheckbox() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = '68730b0747d4889ec2766f9117405f9635f5fd5e%3AModel.valid';
- $unlocked = '';
- $this->Controller->request->data = array(
- 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- $fields = 'f63e4a69b2edd31f064e8e602a04dd59307cfe9c%3A';
- $this->Controller->request->data = array(
- 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- $this->Controller->request->data = array();
- $this->Controller->Security->startup($event);
- $this->Controller->request->data = array(
- 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- }
- /**
- * testValidatePostHidden method
- *
- * @return void
- */
- public function testValidatePostHidden() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = '973a8939a68ac014cc6f7666cec9aa6268507350%3AModel.hidden%7CModel.other_hidden';
- $unlocked = '';
- $this->Controller->request->data = array(
- 'Model' => array(
- 'username' => '', 'password' => '', 'hidden' => '0',
- 'other_hidden' => 'some hidden value'
- ),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- }
- /**
- * testValidatePostWithDisabledFields method
- *
- * @return void
- */
- public function testValidatePostWithDisabledFields() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->config('disabledFields', ['Model.username', 'Model.password']);
- $this->Controller->Security->startup($event);
- $fields = '1c59acfbca98bd870c11fb544d545cbf23215880%3AModel.hidden';
- $unlocked = '';
- $this->Controller->request->data = array(
- 'Model' => array(
- 'username' => '', 'password' => '', 'hidden' => '0'
- ),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- }
- /**
- * test validating post data with posted unlocked fields.
- *
- * @return void
- */
- public function testValidatePostDisabledFieldsInData() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $unlocked = 'Model.username';
- $fields = array('Model.hidden', 'Model.password');
- $fields = urlencode(Security::hash('/articles/index' . serialize($fields) . $unlocked . Configure::read('Security.salt')));
- $this->Controller->request->data = array(
- 'Model' => array(
- 'username' => 'mark',
- 'password' => 'sekret',
- 'hidden' => '0'
- ),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- }
- /**
- * test that missing 'unlocked' input causes failure
- *
- * @return void
- */
- public function testValidatePostFailNoDisabled() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $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')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertFalse($result);
- }
- /**
- * Test that validatePost fails when unlocked fields are changed.
- *
- * @return void
- */
- public function testValidatePostFailDisabledFieldTampering() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $unlocked = 'Model.username';
- $fields = array('Model.hidden', 'Model.password');
- $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Configure::read('Security.salt')));
- // Tamper the values.
- $unlocked = 'Model.username|Model.password';
- $this->Controller->request->data = array(
- 'Model' => array(
- 'username' => 'mark',
- 'password' => 'sekret',
- 'hidden' => '0'
- ),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertFalse($result);
- }
- /**
- * testValidateHiddenMultipleModel method
- *
- * @return void
- */
- public function testValidateHiddenMultipleModel() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = '075ca6c26c38a09a78d871201df89faf52cbbeb8%3AModel.valid%7CModel2.valid%7CModel3.valid';
- $unlocked = '';
- $this->Controller->request->data = array(
- 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
- 'Model2' => array('valid' => '0'),
- 'Model3' => array('valid' => '0'),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- }
- /**
- * testValidateHasManyModel method
- *
- * @return void
- */
- public function testValidateHasManyModel() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = '24a753fb62ef7839389987b58e3f7108f564e529%3AModel.0.hidden%7CModel.0.valid';
- $fields .= '%7CModel.1.hidden%7CModel.1.valid';
- $unlocked = '';
- $this->Controller->request->data = array(
- 'Model' => array(
- array(
- 'username' => 'username', 'password' => 'password',
- 'hidden' => 'value', 'valid' => '0'
- ),
- array(
- 'username' => 'username', 'password' => 'password',
- 'hidden' => 'value', 'valid' => '0'
- )
- ),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- }
- /**
- * testValidateHasManyRecordsPass method
- *
- * @return void
- */
- public function testValidateHasManyRecordsPass() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = '8f7d82bf7656cf068822d9bdab109ebed1be1825%3AAddress.0.id%7CAddress.0.primary%7C';
- $fields .= 'Address.1.id%7CAddress.1.primary';
- $unlocked = '';
- $this->Controller->request->data = array(
- 'Address' => array(
- 0 => array(
- 'id' => '123',
- 'title' => 'home',
- 'first_name' => 'Bilbo',
- 'last_name' => 'Baggins',
- 'address' => '23 Bag end way',
- 'city' => 'the shire',
- 'phone' => 'N/A',
- 'primary' => '1',
- ),
- 1 => array(
- 'id' => '124',
- 'title' => 'home',
- 'first_name' => 'Frodo',
- 'last_name' => 'Baggins',
- 'address' => '50 Bag end way',
- 'city' => 'the shire',
- 'phone' => 'N/A',
- 'primary' => '1'
- )
- ),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- }
- /**
- * Test that values like Foo.0.1
- *
- * @return void
- */
- public function testValidateNestedNumericSets() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $unlocked = '';
- $hashFields = array('TaxonomyData');
- $fields = urlencode(Security::hash('/articles/index' . serialize($hashFields) . $unlocked . Configure::read('Security.salt')));
- $this->Controller->request->data = array(
- 'TaxonomyData' => array(
- 1 => array(array(2)),
- 2 => array(array(3))
- ),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- }
- /**
- * testValidateHasManyRecords method
- *
- * validatePost should fail, hidden fields have been changed.
- *
- * @return void
- */
- public function testValidateHasManyRecordsFail() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
- $fields .= 'Address.1.id%7CAddress.1.primary';
- $unlocked = '';
- $this->Controller->request->data = array(
- 'Address' => array(
- 0 => array(
- 'id' => '123',
- 'title' => 'home',
- 'first_name' => 'Bilbo',
- 'last_name' => 'Baggins',
- 'address' => '23 Bag end way',
- 'city' => 'the shire',
- 'phone' => 'N/A',
- 'primary' => '5',
- ),
- 1 => array(
- 'id' => '124',
- 'title' => 'home',
- 'first_name' => 'Frodo',
- 'last_name' => 'Baggins',
- 'address' => '50 Bag end way',
- 'city' => 'the shire',
- 'phone' => 'N/A',
- 'primary' => '1'
- )
- ),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertFalse($result);
- }
- /**
- * testFormDisabledFields method
- *
- * @return void
- */
- public function testFormDisabledFields() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = '9da2b3fa2b5b8ac0bfbc1bbce145e58059629125%3An%3A0%3A%7B%7D';
- $unlocked = '';
- $this->Controller->request->data = array(
- 'MyModel' => array('name' => 'some data'),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertFalse($result);
- $this->Controller->Security->startup($event);
- $this->Controller->Security->config('disabledFields', ['MyModel.name']);
- $this->Controller->request->data = array(
- 'MyModel' => array('name' => 'some data'),
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- }
- /**
- * test validatePost with radio buttons
- *
- * @return void
- */
- public function testValidatePostRadio() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $fields = 'c2226a8879c3f4b513691295fc2519a29c44c8bb%3An%3A0%3A%7B%7D';
- $unlocked = '';
- $this->Controller->request->data = array(
- '_Token' => compact('fields', 'unlocked')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertFalse($result);
- $this->Controller->request->data = array(
- '_Token' => compact('fields', 'unlocked'),
- 'Test' => array('test' => '')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- $this->Controller->request->data = array(
- '_Token' => compact('fields', 'unlocked'),
- 'Test' => array('test' => '1')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- $this->Controller->request->data = array(
- '_Token' => compact('fields', 'unlocked'),
- 'Test' => array('test' => '2')
- );
- $result = $this->Controller->Security->validatePost($this->Controller);
- $this->assertTrue($result);
- }
- /**
- * test validatePost uses here() as a hash input.
- *
- * @return void
- */
- public function testValidatePostUrlAsHashInput() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Security->startup($event);
- $fields = 'b0914d06dfb04abf1fada53e16810e87d157950b%3A';
- $unlocked = '';
- $this->Controller->request->data = array(
- 'Model' => array('username' => '', 'password' => ''),
- '_Token' => compact('fields', 'unlocked')
- );
- $this->assertTrue($this->Security->validatePost($this->Controller));
- $request = $this->getMock('Cake\Network\Request', ['here']);
- $request->expects($this->at(0))
- ->method('here')
- ->will($this->returnValue('/posts/index?page=1'));
- $request->expects($this->at(1))
- ->method('here')
- ->will($this->returnValue('/posts/edit/1'));
- $request->data = $this->Controller->request->data;
- $this->Controller->request = $request;
- $this->assertFalse($this->Security->validatePost($this->Controller));
- $this->assertFalse($this->Security->validatePost($this->Controller));
- }
- /**
- * test that blackhole doesn't delete the _Token session key so repeat data submissions
- * stay blackholed.
- *
- * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/214
- * @return void
- */
- public function testBlackHoleNotDeletingSessionInformation() {
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->Security->startup($event);
- $this->Controller->Security->blackHole($this->Controller, 'auth');
- $this->assertTrue($this->Controller->Security->session->check('_Token'), '_Token was deleted by blackHole %s');
- }
- /**
- * Test generateToken()
- *
- * @return void
- */
- public function testGenerateToken() {
- $request = $this->Controller->request;
- $this->Security->generateToken($request);
- $this->assertNotEmpty($request->params['_Token']);
- $this->assertTrue(isset($request->params['_Token']['unlockedFields']));
- }
- /**
- * Test unlocked actions
- *
- * @return void
- */
- public function testUnlockedActions() {
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $event = new Event('Controller.startup', $this->Controller);
- $this->Controller->request->data = array('data');
- $this->Controller->Security->unlockedActions = 'index';
- $this->Controller->Security->blackHoleCallback = null;
- $result = $this->Controller->Security->startup($event);
- $this->assertNull($result);
- }
- }
|