PageRenderTime 64ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Controller/Component/SecurityComponentTest.php

https://bitbucket.org/matchs/gameshop
PHP | 1406 lines | 846 code | 167 blank | 393 comment | 1 complexity | 9b590e0cb1b67173447fa1dfa8119472 MD5 | raw file
  1. <?php
  2. /**
  3. * SecurityComponentTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.Controller.Component
  17. * @since CakePHP(tm) v 1.2.0.5435
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('SecurityComponent', 'Controller/Component');
  21. App::uses('Controller', 'Controller');
  22. /**
  23. * TestSecurityComponent
  24. *
  25. * @package Cake.Test.Case.Controller.Component
  26. */
  27. class TestSecurityComponent extends SecurityComponent {
  28. /**
  29. * validatePost method
  30. *
  31. * @param Controller $controller
  32. * @return boolean
  33. */
  34. public function validatePost(Controller $controller) {
  35. return $this->_validatePost($controller);
  36. }
  37. }
  38. /**
  39. * SecurityTestController
  40. *
  41. * @package Cake.Test.Case.Controller.Component
  42. */
  43. class SecurityTestController extends Controller {
  44. /**
  45. * name property
  46. *
  47. * @var string 'SecurityTest'
  48. */
  49. public $name = 'SecurityTest';
  50. /**
  51. * components property
  52. *
  53. * @var array
  54. */
  55. public $components = array('Session', 'TestSecurity');
  56. /**
  57. * failed property
  58. *
  59. * @var boolean false
  60. */
  61. public $failed = false;
  62. /**
  63. * Used for keeping track of headers in test
  64. *
  65. * @var array
  66. */
  67. public $testHeaders = array();
  68. /**
  69. * fail method
  70. *
  71. * @return void
  72. */
  73. public function fail() {
  74. $this->failed = true;
  75. }
  76. /**
  77. * redirect method
  78. *
  79. * @param string|array $url
  80. * @param mixed $code
  81. * @param mixed $exit
  82. * @return void
  83. */
  84. public function redirect($url, $status = null, $exit = true) {
  85. return $status;
  86. }
  87. /**
  88. * Convenience method for header()
  89. *
  90. * @param string $status
  91. * @return void
  92. */
  93. public function header($status) {
  94. $this->testHeaders[] = $status;
  95. }
  96. }
  97. class BrokenCallbackController extends Controller {
  98. public $name = 'UncallableCallback';
  99. public $components = array('Session', 'TestSecurity');
  100. public function index() {
  101. }
  102. protected function _fail() {
  103. }
  104. }
  105. /**
  106. * SecurityComponentTest class
  107. *
  108. * @package Cake.Test.Case.Controller.Component
  109. */
  110. class SecurityComponentTest extends CakeTestCase {
  111. /**
  112. * Controller property
  113. *
  114. * @var SecurityTestController
  115. */
  116. public $Controller;
  117. /**
  118. * oldSalt property
  119. *
  120. * @var string
  121. */
  122. public $oldSalt;
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp() {
  129. parent::setUp();
  130. $request = new CakeRequest('posts/index', false);
  131. $request->addParams(array('controller' => 'posts', 'action' => 'index'));
  132. $this->Controller = new SecurityTestController($request);
  133. $this->Controller->Components->init($this->Controller);
  134. $this->Controller->Security = $this->Controller->TestSecurity;
  135. $this->Controller->Security->blackHoleCallback = 'fail';
  136. $this->Security = $this->Controller->Security;
  137. $this->Security->csrfCheck = false;
  138. Configure::write('Security.salt', 'foo!');
  139. }
  140. /**
  141. * Tear-down method. Resets environment state.
  142. *
  143. * @return void
  144. */
  145. public function tearDown() {
  146. parent::tearDown();
  147. $this->Controller->Session->delete('_Token');
  148. unset($this->Controller->Security);
  149. unset($this->Controller->Component);
  150. unset($this->Controller);
  151. }
  152. /**
  153. * Test that requests are still blackholed when controller has incorrect
  154. * visibility keyword in the blackhole callback
  155. *
  156. * @expectedException BadRequestException
  157. */
  158. public function testBlackholeWithBrokenCallback() {
  159. $request = new CakeRequest('posts/index', false);
  160. $request->addParams(array(
  161. 'controller' => 'posts', 'action' => 'index')
  162. );
  163. $this->Controller = new BrokenCallbackController($request);
  164. $this->Controller->Components->init($this->Controller);
  165. $this->Controller->Security = $this->Controller->TestSecurity;
  166. $this->Controller->Security->blackHoleCallback = '_fail';
  167. $this->Controller->Security->startup($this->Controller);
  168. $this->Controller->Security->blackHole($this->Controller, 'csrf');
  169. }
  170. /**
  171. * Ensure that directly requesting the blackholeCallback as the controller
  172. * action results in an exception.
  173. *
  174. * @return void
  175. */
  176. public function testExceptionWhenActionIsBlackholeCallback() {
  177. $this->Controller->request->addParams(array(
  178. 'controller' => 'posts',
  179. 'action' => 'fail'
  180. ));
  181. $this->assertFalse($this->Controller->failed);
  182. $this->Controller->Security->startup($this->Controller);
  183. $this->assertTrue($this->Controller->failed, 'Request was blackholed.');
  184. }
  185. /**
  186. * test that initialize can set properties.
  187. *
  188. * @return void
  189. */
  190. public function testConstructorSettingProperties() {
  191. $settings = array(
  192. 'requirePost' => array('edit', 'update'),
  193. 'requireSecure' => array('update_account'),
  194. 'requireGet' => array('index'),
  195. 'validatePost' => false,
  196. );
  197. $Security = new SecurityComponent($this->Controller->Components, $settings);
  198. $this->Controller->Security->initialize($this->Controller, $settings);
  199. $this->assertEquals($Security->requirePost, $settings['requirePost']);
  200. $this->assertEquals($Security->requireSecure, $settings['requireSecure']);
  201. $this->assertEquals($Security->requireGet, $settings['requireGet']);
  202. $this->assertEquals($Security->validatePost, $settings['validatePost']);
  203. }
  204. /**
  205. * testStartup method
  206. *
  207. * @return void
  208. */
  209. public function testStartup() {
  210. $this->Controller->Security->startup($this->Controller);
  211. $result = $this->Controller->params['_Token']['key'];
  212. $this->assertNotNull($result);
  213. $this->assertTrue($this->Controller->Session->check('_Token'));
  214. }
  215. /**
  216. * testRequirePostFail method
  217. *
  218. * @return void
  219. */
  220. public function testRequirePostFail() {
  221. $_SERVER['REQUEST_METHOD'] = 'GET';
  222. $this->Controller->request['action'] = 'posted';
  223. $this->Controller->Security->requirePost(array('posted'));
  224. $this->Controller->Security->startup($this->Controller);
  225. $this->assertTrue($this->Controller->failed);
  226. }
  227. /**
  228. * testRequirePostSucceed method
  229. *
  230. * @return void
  231. */
  232. public function testRequirePostSucceed() {
  233. $_SERVER['REQUEST_METHOD'] = 'POST';
  234. $this->Controller->request['action'] = 'posted';
  235. $this->Controller->Security->requirePost('posted');
  236. $this->Security->startup($this->Controller);
  237. $this->assertFalse($this->Controller->failed);
  238. }
  239. /**
  240. * testRequireSecureFail method
  241. *
  242. * @return void
  243. */
  244. public function testRequireSecureFail() {
  245. $_SERVER['HTTPS'] = 'off';
  246. $_SERVER['REQUEST_METHOD'] = 'POST';
  247. $this->Controller->request['action'] = 'posted';
  248. $this->Controller->Security->requireSecure(array('posted'));
  249. $this->Controller->Security->startup($this->Controller);
  250. $this->assertTrue($this->Controller->failed);
  251. }
  252. /**
  253. * testRequireSecureSucceed method
  254. *
  255. * @return void
  256. */
  257. public function testRequireSecureSucceed() {
  258. $_SERVER['REQUEST_METHOD'] = 'Secure';
  259. $this->Controller->request['action'] = 'posted';
  260. $_SERVER['HTTPS'] = 'on';
  261. $this->Controller->Security->requireSecure('posted');
  262. $this->Controller->Security->startup($this->Controller);
  263. $this->assertFalse($this->Controller->failed);
  264. }
  265. /**
  266. * testRequireAuthFail method
  267. *
  268. * @return void
  269. */
  270. public function testRequireAuthFail() {
  271. $_SERVER['REQUEST_METHOD'] = 'AUTH';
  272. $this->Controller->request['action'] = 'posted';
  273. $this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
  274. $this->Controller->Security->requireAuth(array('posted'));
  275. $this->Controller->Security->startup($this->Controller);
  276. $this->assertTrue($this->Controller->failed);
  277. $this->Controller->Session->write('_Token', array('allowedControllers' => array()));
  278. $this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
  279. $this->Controller->request['action'] = 'posted';
  280. $this->Controller->Security->requireAuth('posted');
  281. $this->Controller->Security->startup($this->Controller);
  282. $this->assertTrue($this->Controller->failed);
  283. $this->Controller->Session->write('_Token', array(
  284. 'allowedControllers' => array('SecurityTest'), 'allowedActions' => array('posted2')
  285. ));
  286. $this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
  287. $this->Controller->request['action'] = 'posted';
  288. $this->Controller->Security->requireAuth('posted');
  289. $this->Controller->Security->startup($this->Controller);
  290. $this->assertTrue($this->Controller->failed);
  291. }
  292. /**
  293. * testRequireAuthSucceed method
  294. *
  295. * @return void
  296. */
  297. public function testRequireAuthSucceed() {
  298. $_SERVER['REQUEST_METHOD'] = 'AUTH';
  299. $this->Controller->request['action'] = 'posted';
  300. $this->Controller->Security->requireAuth('posted');
  301. $this->Controller->Security->startup($this->Controller);
  302. $this->assertFalse($this->Controller->failed);
  303. $this->Controller->Security->Session->write('_Token', array(
  304. 'allowedControllers' => array('SecurityTest'), 'allowedActions' => array('posted')
  305. ));
  306. $this->Controller->request['controller'] = 'SecurityTest';
  307. $this->Controller->request['action'] = 'posted';
  308. $this->Controller->request->data = array(
  309. 'username' => 'willy', 'password' => 'somePass', '_Token' => ''
  310. );
  311. $this->Controller->action = 'posted';
  312. $this->Controller->Security->requireAuth('posted');
  313. $this->Controller->Security->startup($this->Controller);
  314. $this->assertFalse($this->Controller->failed);
  315. }
  316. /**
  317. * testRequirePostSucceedWrongMethod method
  318. *
  319. * @return void
  320. */
  321. public function testRequirePostSucceedWrongMethod() {
  322. $_SERVER['REQUEST_METHOD'] = 'GET';
  323. $this->Controller->request['action'] = 'getted';
  324. $this->Controller->Security->requirePost('posted');
  325. $this->Controller->Security->startup($this->Controller);
  326. $this->assertFalse($this->Controller->failed);
  327. }
  328. /**
  329. * testRequireGetFail method
  330. *
  331. * @return void
  332. */
  333. public function testRequireGetFail() {
  334. $_SERVER['REQUEST_METHOD'] = 'POST';
  335. $this->Controller->request['action'] = 'getted';
  336. $this->Controller->Security->requireGet(array('getted'));
  337. $this->Controller->Security->startup($this->Controller);
  338. $this->assertTrue($this->Controller->failed);
  339. }
  340. /**
  341. * testRequireGetSucceed method
  342. *
  343. * @return void
  344. */
  345. public function testRequireGetSucceed() {
  346. $_SERVER['REQUEST_METHOD'] = 'GET';
  347. $this->Controller->request['action'] = 'getted';
  348. $this->Controller->Security->requireGet('getted');
  349. $this->Controller->Security->startup($this->Controller);
  350. $this->assertFalse($this->Controller->failed);
  351. }
  352. /**
  353. * testRequireGetSucceedWrongMethod method
  354. *
  355. * @return void
  356. */
  357. public function testRequireGetSucceedWrongMethod() {
  358. $_SERVER['REQUEST_METHOD'] = 'POST';
  359. $this->Controller->request['action'] = 'posted';
  360. $this->Security->requireGet('getted');
  361. $this->Security->startup($this->Controller);
  362. $this->assertFalse($this->Controller->failed);
  363. }
  364. /**
  365. * testRequirePutFail method
  366. *
  367. * @return void
  368. */
  369. public function testRequirePutFail() {
  370. $_SERVER['REQUEST_METHOD'] = 'POST';
  371. $this->Controller->request['action'] = 'putted';
  372. $this->Controller->Security->requirePut(array('putted'));
  373. $this->Controller->Security->startup($this->Controller);
  374. $this->assertTrue($this->Controller->failed);
  375. }
  376. /**
  377. * testRequirePutSucceed method
  378. *
  379. * @return void
  380. */
  381. public function testRequirePutSucceed() {
  382. $_SERVER['REQUEST_METHOD'] = 'PUT';
  383. $this->Controller->request['action'] = 'putted';
  384. $this->Controller->Security->requirePut('putted');
  385. $this->Controller->Security->startup($this->Controller);
  386. $this->assertFalse($this->Controller->failed);
  387. }
  388. /**
  389. * testRequirePutSucceedWrongMethod method
  390. *
  391. * @return void
  392. */
  393. public function testRequirePutSucceedWrongMethod() {
  394. $_SERVER['REQUEST_METHOD'] = 'POST';
  395. $this->Controller->request['action'] = 'posted';
  396. $this->Controller->Security->requirePut('putted');
  397. $this->Controller->Security->startup($this->Controller);
  398. $this->assertFalse($this->Controller->failed);
  399. }
  400. /**
  401. * testRequireDeleteFail method
  402. *
  403. * @return void
  404. */
  405. public function testRequireDeleteFail() {
  406. $_SERVER['REQUEST_METHOD'] = 'POST';
  407. $this->Controller->request['action'] = 'deleted';
  408. $this->Controller->Security->requireDelete(array('deleted', 'other_method'));
  409. $this->Controller->Security->startup($this->Controller);
  410. $this->assertTrue($this->Controller->failed);
  411. }
  412. /**
  413. * testRequireDeleteSucceed method
  414. *
  415. * @return void
  416. */
  417. public function testRequireDeleteSucceed() {
  418. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  419. $this->Controller->request['action'] = 'deleted';
  420. $this->Controller->Security->requireDelete('deleted');
  421. $this->Controller->Security->startup($this->Controller);
  422. $this->assertFalse($this->Controller->failed);
  423. }
  424. /**
  425. * testRequireDeleteSucceedWrongMethod method
  426. *
  427. * @return void
  428. */
  429. public function testRequireDeleteSucceedWrongMethod() {
  430. $_SERVER['REQUEST_METHOD'] = 'POST';
  431. $this->Controller->request['action'] = 'posted';
  432. $this->Controller->Security->requireDelete('deleted');
  433. $this->Controller->Security->startup($this->Controller);
  434. $this->assertFalse($this->Controller->failed);
  435. }
  436. /**
  437. * Simple hash validation test
  438. *
  439. * @return void
  440. */
  441. public function testValidatePost() {
  442. $this->Controller->Security->startup($this->Controller);
  443. $key = $this->Controller->request->params['_Token']['key'];
  444. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  445. $unlocked = '';
  446. $this->Controller->request->data = array(
  447. 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
  448. '_Token' => compact('key', 'fields', 'unlocked')
  449. );
  450. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  451. }
  452. /**
  453. * Test that validatePost fails if you are missing the session information.
  454. *
  455. * @return void
  456. */
  457. public function testValidatePostNoSession() {
  458. $this->Controller->Security->startup($this->Controller);
  459. $this->Controller->Session->delete('_Token');
  460. $key = $this->Controller->params['_Token']['key'];
  461. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  462. $this->Controller->data = array(
  463. 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
  464. '_Token' => compact('key', 'fields')
  465. );
  466. $this->assertFalse($this->Controller->Security->validatePost($this->Controller));
  467. }
  468. /**
  469. * test that validatePost fails if any of its required fields are missing.
  470. *
  471. * @return void
  472. */
  473. public function testValidatePostFormHacking() {
  474. $this->Controller->Security->startup($this->Controller);
  475. $key = $this->Controller->params['_Token']['key'];
  476. $unlocked = '';
  477. $this->Controller->request->data = array(
  478. 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
  479. '_Token' => compact('key', 'unlocked')
  480. );
  481. $result = $this->Controller->Security->validatePost($this->Controller);
  482. $this->assertFalse($result, 'validatePost passed when fields were missing. %s');
  483. }
  484. /**
  485. * Test that objects can't be passed into the serialized string. This was a vector for RFI and LFI
  486. * attacks. Thanks to Felix Wilhelm
  487. *
  488. * @return void
  489. */
  490. public function testValidatePostObjectDeserialize() {
  491. $this->Controller->Security->startup($this->Controller);
  492. $key = $this->Controller->request->params['_Token']['key'];
  493. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877';
  494. $unlocked = '';
  495. // a corrupted serialized object, so we can see if it ever gets to deserialize
  496. $attack = 'O:3:"App":1:{s:5:"__map";a:1:{s:3:"foo";s:7:"Hacked!";s:1:"fail"}}';
  497. $fields .= urlencode(':' . str_rot13($attack));
  498. $this->Controller->request->data = array(
  499. 'Model' => array('username' => 'mark', 'password' => 'foo', 'valid' => '0'),
  500. '_Token' => compact('key', 'fields', 'unlocked')
  501. );
  502. $result = $this->Controller->Security->validatePost($this->Controller);
  503. $this->assertFalse($result, 'validatePost passed when key was missing. %s');
  504. }
  505. /**
  506. * Tests validation of checkbox arrays
  507. *
  508. * @return void
  509. */
  510. public function testValidatePostArray() {
  511. $this->Controller->Security->startup($this->Controller);
  512. $key = $this->Controller->request->params['_Token']['key'];
  513. $fields = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  514. $unlocked = '';
  515. $this->Controller->request->data = array(
  516. 'Model' => array('multi_field' => array('1', '3')),
  517. '_Token' => compact('key', 'fields', 'unlocked')
  518. );
  519. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  520. }
  521. /**
  522. * testValidatePostNoModel method
  523. *
  524. * @return void
  525. */
  526. public function testValidatePostNoModel() {
  527. $this->Controller->Security->startup($this->Controller);
  528. $key = $this->Controller->request->params['_Token']['key'];
  529. $fields = '540ac9c60d323c22bafe997b72c0790f39a8bdef%3A';
  530. $unlocked = '';
  531. $this->Controller->request->data = array(
  532. 'anything' => 'some_data',
  533. '_Token' => compact('key', 'fields', 'unlocked')
  534. );
  535. $result = $this->Controller->Security->validatePost($this->Controller);
  536. $this->assertTrue($result);
  537. }
  538. /**
  539. * testValidatePostSimple method
  540. *
  541. * @return void
  542. */
  543. public function testValidatePostSimple() {
  544. $this->Controller->Security->startup($this->Controller);
  545. $key = $this->Controller->request->params['_Token']['key'];
  546. $fields = '69f493434187b867ea14b901fdf58b55d27c935d%3A';
  547. $unlocked = '';
  548. $this->Controller->request->data = $data = array(
  549. 'Model' => array('username' => '', 'password' => ''),
  550. '_Token' => compact('key', 'fields', 'unlocked')
  551. );
  552. $result = $this->Controller->Security->validatePost($this->Controller);
  553. $this->assertTrue($result);
  554. }
  555. /**
  556. * Tests hash validation for multiple records, including locked fields
  557. *
  558. * @return void
  559. */
  560. public function testValidatePostComplex() {
  561. $this->Controller->Security->startup($this->Controller);
  562. $key = $this->Controller->request->params['_Token']['key'];
  563. $fields = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  564. $unlocked = '';
  565. $this->Controller->request->data = array(
  566. 'Addresses' => array(
  567. '0' => array(
  568. 'id' => '123456', 'title' => '', 'first_name' => '', 'last_name' => '',
  569. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  570. ),
  571. '1' => array(
  572. 'id' => '654321', 'title' => '', 'first_name' => '', 'last_name' => '',
  573. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  574. )
  575. ),
  576. '_Token' => compact('key', 'fields', 'unlocked')
  577. );
  578. $result = $this->Controller->Security->validatePost($this->Controller);
  579. $this->assertTrue($result);
  580. }
  581. /**
  582. * test ValidatePost with multiple select elements.
  583. *
  584. * @return void
  585. */
  586. public function testValidatePostMultipleSelect() {
  587. $this->Controller->Security->startup($this->Controller);
  588. $key = $this->Controller->request->params['_Token']['key'];
  589. $fields = '422cde416475abc171568be690a98cad20e66079%3A';
  590. $unlocked = '';
  591. $this->Controller->request->data = array(
  592. 'Tag' => array('Tag' => array(1, 2)),
  593. '_Token' => compact('key', 'fields', 'unlocked'),
  594. );
  595. $result = $this->Controller->Security->validatePost($this->Controller);
  596. $this->assertTrue($result);
  597. $this->Controller->request->data = array(
  598. 'Tag' => array('Tag' => array(1, 2, 3)),
  599. '_Token' => compact('key', 'fields', 'unlocked'),
  600. );
  601. $result = $this->Controller->Security->validatePost($this->Controller);
  602. $this->assertTrue($result);
  603. $this->Controller->request->data = array(
  604. 'Tag' => array('Tag' => array(1, 2, 3, 4)),
  605. '_Token' => compact('key', 'fields', 'unlocked'),
  606. );
  607. $result = $this->Controller->Security->validatePost($this->Controller);
  608. $this->assertTrue($result);
  609. $fields = '19464422eafe977ee729c59222af07f983010c5f%3A';
  610. $this->Controller->request->data = array(
  611. 'User.password' => 'bar', 'User.name' => 'foo', 'User.is_valid' => '1',
  612. 'Tag' => array('Tag' => array(1)),
  613. '_Token' => compact('key', 'fields', 'unlocked'),
  614. );
  615. $result = $this->Controller->Security->validatePost($this->Controller);
  616. $this->assertTrue($result);
  617. }
  618. /**
  619. * testValidatePostCheckbox method
  620. *
  621. * First block tests un-checked checkbox
  622. * Second block tests checked checkbox
  623. *
  624. * @return void
  625. */
  626. public function testValidatePostCheckbox() {
  627. $this->Controller->Security->startup($this->Controller);
  628. $key = $this->Controller->request->params['_Token']['key'];
  629. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  630. $unlocked = '';
  631. $this->Controller->request->data = array(
  632. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  633. '_Token' => compact('key', 'fields', 'unlocked')
  634. );
  635. $result = $this->Controller->Security->validatePost($this->Controller);
  636. $this->assertTrue($result);
  637. $fields = '874439ca69f89b4c4a5f50fb9c36ff56a28f5d42%3A';
  638. $this->Controller->request->data = array(
  639. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  640. '_Token' => compact('key', 'fields', 'unlocked')
  641. );
  642. $result = $this->Controller->Security->validatePost($this->Controller);
  643. $this->assertTrue($result);
  644. $this->Controller->request->data = array();
  645. $this->Controller->Security->startup($this->Controller);
  646. $key = $this->Controller->request->params['_Token']['key'];
  647. $this->Controller->request->data = array(
  648. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  649. '_Token' => compact('key', 'fields', 'unlocked')
  650. );
  651. $result = $this->Controller->Security->validatePost($this->Controller);
  652. $this->assertTrue($result);
  653. }
  654. /**
  655. * testValidatePostHidden method
  656. *
  657. * @return void
  658. */
  659. public function testValidatePostHidden() {
  660. $this->Controller->Security->startup($this->Controller);
  661. $key = $this->Controller->request->params['_Token']['key'];
  662. $fields = '51ccd8cb0997c7b3d4523ecde5a109318405ef8c%3AModel.hidden%7CModel.other_hidden';
  663. $unlocked = '';
  664. $this->Controller->request->data = array(
  665. 'Model' => array(
  666. 'username' => '', 'password' => '', 'hidden' => '0',
  667. 'other_hidden' => 'some hidden value'
  668. ),
  669. '_Token' => compact('key', 'fields', 'unlocked')
  670. );
  671. $result = $this->Controller->Security->validatePost($this->Controller);
  672. $this->assertTrue($result);
  673. }
  674. /**
  675. * testValidatePostWithDisabledFields method
  676. *
  677. * @return void
  678. */
  679. public function testValidatePostWithDisabledFields() {
  680. $this->Controller->Security->disabledFields = array('Model.username', 'Model.password');
  681. $this->Controller->Security->startup($this->Controller);
  682. $key = $this->Controller->request->params['_Token']['key'];
  683. $fields = 'ef1082968c449397bcd849f963636864383278b1%3AModel.hidden';
  684. $unlocked = '';
  685. $this->Controller->request->data = array(
  686. 'Model' => array(
  687. 'username' => '', 'password' => '', 'hidden' => '0'
  688. ),
  689. '_Token' => compact('fields', 'key', 'unlocked')
  690. );
  691. $result = $this->Controller->Security->validatePost($this->Controller);
  692. $this->assertTrue($result);
  693. }
  694. /**
  695. * test validating post data with posted unlocked fields.
  696. *
  697. * @return void
  698. */
  699. public function testValidatePostDisabledFieldsInData() {
  700. $this->Controller->Security->startup($this->Controller);
  701. $key = $this->Controller->request->params['_Token']['key'];
  702. $unlocked = 'Model.username';
  703. $fields = array('Model.hidden', 'Model.password');
  704. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Configure::read('Security.salt')));
  705. $this->Controller->request->data = array(
  706. 'Model' => array(
  707. 'username' => 'mark',
  708. 'password' => 'sekret',
  709. 'hidden' => '0'
  710. ),
  711. '_Token' => compact('fields', 'key', 'unlocked')
  712. );
  713. $result = $this->Controller->Security->validatePost($this->Controller);
  714. $this->assertTrue($result);
  715. }
  716. /**
  717. * test that missing 'unlocked' input causes failure
  718. *
  719. * @return void
  720. */
  721. public function testValidatePostFailNoDisabled() {
  722. $this->Controller->Security->startup($this->Controller);
  723. $key = $this->Controller->request->params['_Token']['key'];
  724. $fields = array('Model.hidden', 'Model.password', 'Model.username');
  725. $fields = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt')));
  726. $this->Controller->request->data = array(
  727. 'Model' => array(
  728. 'username' => 'mark',
  729. 'password' => 'sekret',
  730. 'hidden' => '0'
  731. ),
  732. '_Token' => compact('fields', 'key')
  733. );
  734. $result = $this->Controller->Security->validatePost($this->Controller);
  735. $this->assertFalse($result);
  736. }
  737. /**
  738. * Test that validatePost fails when unlocked fields are changed.
  739. *
  740. * @return
  741. */
  742. public function testValidatePostFailDisabledFieldTampering() {
  743. $this->Controller->Security->startup($this->Controller);
  744. $key = $this->Controller->request->params['_Token']['key'];
  745. $unlocked = 'Model.username';
  746. $fields = array('Model.hidden', 'Model.password');
  747. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Configure::read('Security.salt')));
  748. // Tamper the values.
  749. $unlocked = 'Model.username|Model.password';
  750. $this->Controller->request->data = array(
  751. 'Model' => array(
  752. 'username' => 'mark',
  753. 'password' => 'sekret',
  754. 'hidden' => '0'
  755. ),
  756. '_Token' => compact('fields', 'key', 'unlocked')
  757. );
  758. $result = $this->Controller->Security->validatePost($this->Controller);
  759. $this->assertFalse($result);
  760. }
  761. /**
  762. * testValidateHiddenMultipleModel method
  763. *
  764. * @return void
  765. */
  766. public function testValidateHiddenMultipleModel() {
  767. $this->Controller->Security->startup($this->Controller);
  768. $key = $this->Controller->request->params['_Token']['key'];
  769. $fields = 'a2d01072dc4660eea9d15007025f35a7a5b58e18%3AModel.valid%7CModel2.valid%7CModel3.valid';
  770. $unlocked = '';
  771. $this->Controller->request->data = array(
  772. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  773. 'Model2' => array('valid' => '0'),
  774. 'Model3' => array('valid' => '0'),
  775. '_Token' => compact('key', 'fields', 'unlocked')
  776. );
  777. $result = $this->Controller->Security->validatePost($this->Controller);
  778. $this->assertTrue($result);
  779. }
  780. /**
  781. * testValidateHasManyModel method
  782. *
  783. * @return void
  784. */
  785. public function testValidateHasManyModel() {
  786. $this->Controller->Security->startup($this->Controller);
  787. $key = $this->Controller->request->params['_Token']['key'];
  788. $fields = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  789. $fields .= '%7CModel.1.hidden%7CModel.1.valid';
  790. $unlocked = '';
  791. $this->Controller->request->data = array(
  792. 'Model' => array(
  793. array(
  794. 'username' => 'username', 'password' => 'password',
  795. 'hidden' => 'value', 'valid' => '0'
  796. ),
  797. array(
  798. 'username' => 'username', 'password' => 'password',
  799. 'hidden' => 'value', 'valid' => '0'
  800. )
  801. ),
  802. '_Token' => compact('key', 'fields', 'unlocked')
  803. );
  804. $result = $this->Controller->Security->validatePost($this->Controller);
  805. $this->assertTrue($result);
  806. }
  807. /**
  808. * testValidateHasManyRecordsPass method
  809. *
  810. * @return void
  811. */
  812. public function testValidateHasManyRecordsPass() {
  813. $this->Controller->Security->startup($this->Controller);
  814. $key = $this->Controller->request->params['_Token']['key'];
  815. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
  816. $fields .= 'Address.1.id%7CAddress.1.primary';
  817. $unlocked = '';
  818. $this->Controller->request->data = array(
  819. 'Address' => array(
  820. 0 => array(
  821. 'id' => '123',
  822. 'title' => 'home',
  823. 'first_name' => 'Bilbo',
  824. 'last_name' => 'Baggins',
  825. 'address' => '23 Bag end way',
  826. 'city' => 'the shire',
  827. 'phone' => 'N/A',
  828. 'primary' => '1',
  829. ),
  830. 1 => array(
  831. 'id' => '124',
  832. 'title' => 'home',
  833. 'first_name' => 'Frodo',
  834. 'last_name' => 'Baggins',
  835. 'address' => '50 Bag end way',
  836. 'city' => 'the shire',
  837. 'phone' => 'N/A',
  838. 'primary' => '1'
  839. )
  840. ),
  841. '_Token' => compact('key', 'fields', 'unlocked')
  842. );
  843. $result = $this->Controller->Security->validatePost($this->Controller);
  844. $this->assertTrue($result);
  845. }
  846. /**
  847. * Test that values like Foo.0.1
  848. *
  849. * @return void
  850. */
  851. public function testValidateNestedNumericSets() {
  852. $this->Controller->Security->startup($this->Controller);
  853. $key = $this->Controller->request->params['_Token']['key'];
  854. $unlocked = '';
  855. $hashFields = array('TaxonomyData');
  856. $fields = urlencode(Security::hash(serialize($hashFields) . $unlocked . Configure::read('Security.salt')));
  857. $this->Controller->request->data = array(
  858. 'TaxonomyData' => array(
  859. 1 => array(array(2)),
  860. 2 => array(array(3))
  861. ),
  862. '_Token' => compact('key', 'fields', 'unlocked')
  863. );
  864. $result = $this->Controller->Security->validatePost($this->Controller);
  865. $this->assertTrue($result);
  866. }
  867. /**
  868. * testValidateHasManyRecords method
  869. *
  870. * validatePost should fail, hidden fields have been changed.
  871. *
  872. * @return void
  873. */
  874. public function testValidateHasManyRecordsFail() {
  875. $this->Controller->Security->startup($this->Controller);
  876. $key = $this->Controller->request->params['_Token']['key'];
  877. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
  878. $fields .= 'Address.1.id%7CAddress.1.primary';
  879. $unlocked = '';
  880. $this->Controller->request->data = array(
  881. 'Address' => array(
  882. 0 => array(
  883. 'id' => '123',
  884. 'title' => 'home',
  885. 'first_name' => 'Bilbo',
  886. 'last_name' => 'Baggins',
  887. 'address' => '23 Bag end way',
  888. 'city' => 'the shire',
  889. 'phone' => 'N/A',
  890. 'primary' => '5',
  891. ),
  892. 1 => array(
  893. 'id' => '124',
  894. 'title' => 'home',
  895. 'first_name' => 'Frodo',
  896. 'last_name' => 'Baggins',
  897. 'address' => '50 Bag end way',
  898. 'city' => 'the shire',
  899. 'phone' => 'N/A',
  900. 'primary' => '1'
  901. )
  902. ),
  903. '_Token' => compact('key', 'fields', 'unlocked')
  904. );
  905. $result = $this->Controller->Security->validatePost($this->Controller);
  906. $this->assertFalse($result);
  907. }
  908. /**
  909. * testFormDisabledFields method
  910. *
  911. * @return void
  912. */
  913. public function testFormDisabledFields() {
  914. $this->Controller->Security->startup($this->Controller);
  915. $key = $this->Controller->request->params['_Token']['key'];
  916. $fields = '11842060341b9d0fc3808b90ba29fdea7054d6ad%3An%3A0%3A%7B%7D';
  917. $unlocked = '';
  918. $this->Controller->request->data = array(
  919. 'MyModel' => array('name' => 'some data'),
  920. '_Token' => compact('key', 'fields', 'unlocked')
  921. );
  922. $result = $this->Controller->Security->validatePost($this->Controller);
  923. $this->assertFalse($result);
  924. $this->Controller->Security->startup($this->Controller);
  925. $this->Controller->Security->disabledFields = array('MyModel.name');
  926. $key = $this->Controller->request->params['_Token']['key'];
  927. $this->Controller->request->data = array(
  928. 'MyModel' => array('name' => 'some data'),
  929. '_Token' => compact('key', 'fields', 'unlocked')
  930. );
  931. $result = $this->Controller->Security->validatePost($this->Controller);
  932. $this->assertTrue($result);
  933. }
  934. /**
  935. * testRadio method
  936. *
  937. * @return void
  938. */
  939. public function testRadio() {
  940. $this->Controller->Security->startup($this->Controller);
  941. $key = $this->Controller->request->params['_Token']['key'];
  942. $fields = '575ef54ca4fc8cab468d6d898e9acd3a9671c17e%3An%3A0%3A%7B%7D';
  943. $unlocked = '';
  944. $this->Controller->request->data = array(
  945. '_Token' => compact('key', 'fields', 'unlocked')
  946. );
  947. $result = $this->Controller->Security->validatePost($this->Controller);
  948. $this->assertFalse($result);
  949. $this->Controller->request->data = array(
  950. '_Token' => compact('key', 'fields', 'unlocked'),
  951. 'Test' => array('test' => '')
  952. );
  953. $result = $this->Controller->Security->validatePost($this->Controller);
  954. $this->assertTrue($result);
  955. $this->Controller->request->data = array(
  956. '_Token' => compact('key', 'fields', 'unlocked'),
  957. 'Test' => array('test' => '1')
  958. );
  959. $result = $this->Controller->Security->validatePost($this->Controller);
  960. $this->assertTrue($result);
  961. $this->Controller->request->data = array(
  962. '_Token' => compact('key', 'fields', 'unlocked'),
  963. 'Test' => array('test' => '2')
  964. );
  965. $result = $this->Controller->Security->validatePost($this->Controller);
  966. $this->assertTrue($result);
  967. }
  968. /**
  969. * test that a requestAction's controller will have the _Token appended to
  970. * the params.
  971. *
  972. * @return void
  973. * @see http://cakephp.lighthouseapp.com/projects/42648/tickets/68
  974. */
  975. public function testSettingTokenForRequestAction() {
  976. $this->Controller->Security->startup($this->Controller);
  977. $key = $this->Controller->request->params['_Token']['key'];
  978. $this->Controller->params['requested'] = 1;
  979. unset($this->Controller->request->params['_Token']);
  980. $this->Controller->Security->startup($this->Controller);
  981. $this->assertEquals($this->Controller->request->params['_Token']['key'], $key);
  982. }
  983. /**
  984. * test that blackhole doesn't delete the _Token session key so repeat data submissions
  985. * stay blackholed.
  986. *
  987. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/214
  988. * @return void
  989. */
  990. public function testBlackHoleNotDeletingSessionInformation() {
  991. $this->Controller->Security->startup($this->Controller);
  992. $this->Controller->Security->blackHole($this->Controller, 'auth');
  993. $this->assertTrue($this->Controller->Security->Session->check('_Token'), '_Token was deleted by blackHole %s');
  994. }
  995. /**
  996. * test that csrf checks are skipped for request action.
  997. *
  998. * @return void
  999. */
  1000. public function testCsrfSkipRequestAction() {
  1001. $_SERVER['REQUEST_METHOD'] = 'POST';
  1002. $this->Security->validatePost = false;
  1003. $this->Security->csrfCheck = true;
  1004. $this->Security->csrfExpires = '+10 minutes';
  1005. $this->Controller->request->params['requested'] = 1;
  1006. $this->Security->startup($this->Controller);
  1007. $this->assertFalse($this->Controller->failed, 'fail() was called.');
  1008. }
  1009. /**
  1010. * test setting
  1011. *
  1012. * @return void
  1013. */
  1014. public function testCsrfSettings() {
  1015. $this->Security->validatePost = false;
  1016. $this->Security->csrfCheck = true;
  1017. $this->Security->csrfExpires = '+10 minutes';
  1018. $this->Security->startup($this->Controller);
  1019. $token = $this->Security->Session->read('_Token');
  1020. $this->assertEquals(1, count($token['csrfTokens']), 'Missing the csrf token.');
  1021. $this->assertEquals(strtotime('+10 minutes'), current($token['csrfTokens']), 'Token expiry does not match');
  1022. $this->assertEquals(array('key', 'unlockedFields'), array_keys($this->Controller->request->params['_Token']), 'Keys don not match');
  1023. }
  1024. /**
  1025. * Test setting multiple nonces, when startup() is called more than once, (ie more than one request.)
  1026. *
  1027. * @return void
  1028. */
  1029. public function testCsrfSettingMultipleNonces() {
  1030. $this->Security->validatePost = false;
  1031. $this->Security->csrfCheck = true;
  1032. $this->Security->csrfExpires = '+10 minutes';
  1033. $csrfExpires = strtotime('+10 minutes');
  1034. $this->Security->startup($this->Controller);
  1035. $this->Security->startup($this->Controller);
  1036. $token = $this->Security->Session->read('_Token');
  1037. $this->assertEquals(2, count($token['csrfTokens']), 'Missing the csrf token.');
  1038. foreach ($token['csrfTokens'] as $expires) {
  1039. $diff = $csrfExpires - $expires;
  1040. $this->assertTrue($diff === 0 || $diff === 1, 'Token expiry does not match');
  1041. }
  1042. }
  1043. /**
  1044. * test that nonces are consumed by form submits.
  1045. *
  1046. * @return void
  1047. */
  1048. public function testCsrfNonceConsumption() {
  1049. $this->Security->validatePost = false;
  1050. $this->Security->csrfCheck = true;
  1051. $this->Security->csrfExpires = '+10 minutes';
  1052. $this->Security->Session->write('_Token.csrfTokens', array('nonce1' => strtotime('+10 minutes')));
  1053. $this->Controller->request = $this->getMock('CakeRequest', array('is'));
  1054. $this->Controller->request->expects($this->once())->method('is')
  1055. ->with('post')
  1056. ->will($this->returnValue(true));
  1057. $this->Controller->request->params['action'] = 'index';
  1058. $this->Controller->request->data = array(
  1059. '_Token' => array(
  1060. 'key' => 'nonce1'
  1061. ),
  1062. 'Post' => array(
  1063. 'title' => 'Woot'
  1064. )
  1065. );
  1066. $this->Security->startup($this->Controller);
  1067. $token = $this->Security->Session->read('_Token');
  1068. $this->assertFalse(isset($token['csrfTokens']['nonce1']), 'Token was not consumed');
  1069. }
  1070. /**
  1071. * test that expired values in the csrfTokens are cleaned up.
  1072. *
  1073. * @return void
  1074. */
  1075. public function testCsrfNonceVacuum() {
  1076. $this->Security->validatePost = false;
  1077. $this->Security->csrfCheck = true;
  1078. $this->Security->csrfExpires = '+10 minutes';
  1079. $this->Security->Session->write('_Token.csrfTokens', array(
  1080. 'valid' => strtotime('+30 minutes'),
  1081. 'poof' => strtotime('-11 minutes'),
  1082. 'dust' => strtotime('-20 minutes')
  1083. ));
  1084. $this->Security->startup($this->Controller);
  1085. $tokens = $this->Security->Session->read('_Token.csrfTokens');
  1086. $this->assertEquals(2, count($tokens), 'Too many tokens left behind');
  1087. $this->assertNotEmpty('valid', $tokens, 'Valid token was removed.');
  1088. }
  1089. /**
  1090. * test that when the key is missing the request is blackHoled
  1091. *
  1092. * @return void
  1093. */
  1094. public function testCsrfBlackHoleOnKeyMismatch() {
  1095. $this->Security->validatePost = false;
  1096. $this->Security->csrfCheck = true;
  1097. $this->Security->csrfExpires = '+10 minutes';
  1098. $this->Security->Session->write('_Token.csrfTokens', array('nonce1' => strtotime('+10 minutes')));
  1099. $this->Controller->request = $this->getMock('CakeRequest', array('is'));
  1100. $this->Controller->request->expects($this->once())->method('is')
  1101. ->with('post')
  1102. ->will($this->returnValue(true));
  1103. $this->Controller->request->params['action'] = 'index';
  1104. $this->Controller->request->data = array(
  1105. '_Token' => array(
  1106. 'key' => 'not the right value'
  1107. ),
  1108. 'Post' => array(
  1109. 'title' => 'Woot'
  1110. )
  1111. );
  1112. $this->Security->startup($this->Controller);
  1113. $this->assertTrue($this->Controller->failed, 'fail() was not called.');
  1114. }
  1115. /**
  1116. * test that when the key is missing the request is blackHoled
  1117. *
  1118. * @return void
  1119. */
  1120. public function testCsrfBlackHoleOnExpiredKey() {
  1121. $this->Security->validatePost = false;
  1122. $this->Security->csrfCheck = true;
  1123. $this->Security->csrfExpires = '+10 minutes';
  1124. $this->Security->Session->write('_Token.csrfTokens', array('nonce1' => strtotime('-5 minutes')));
  1125. $this->Controller->request = $this->getMock('CakeRequest', array('is'));
  1126. $this->Controller->request->expects($this->once())->method('is')
  1127. ->with('post')
  1128. ->will($this->returnValue(true));
  1129. $this->Controller->request->params['action'] = 'index';
  1130. $this->Controller->request->data = array(
  1131. '_Token' => array(
  1132. 'key' => 'nonce1'
  1133. ),
  1134. 'Post' => array(
  1135. 'title' => 'Woot'
  1136. )
  1137. );
  1138. $this->Security->startup($this->Controller);
  1139. $this->assertTrue($this->Controller->failed, 'fail() was not called.');
  1140. }
  1141. /**
  1142. * test that csrfUseOnce = false works.
  1143. *
  1144. * @return void
  1145. */
  1146. public function testCsrfNotUseOnce() {
  1147. $this->Security->validatePost = false;
  1148. $this->Security->csrfCheck = true;
  1149. $this->Security->csrfUseOnce = false;
  1150. $this->Security->csrfExpires = '+10 minutes';
  1151. // Generate one token
  1152. $this->Security->startup($this->Controller);
  1153. $token = $this->Security->Session->read('_Token.csrfTokens');
  1154. $this->assertEquals(1, count($token), 'Should only be one token.');
  1155. $this->Security->startup($this->Controller);
  1156. $tokenTwo = $this->Security->Session->read('_Token.csrfTokens');
  1157. $this->assertEquals(1, count($tokenTwo), 'Should only be one token.');
  1158. $this->assertEquals($token, $tokenTwo, 'Tokens should not be different.');
  1159. $key = $this->Controller->request->params['_Token']['key'];
  1160. $this->assertEquals(array($key), array_keys($token), '_Token.key and csrfToken do not match request will blackhole.');
  1161. }
  1162. /**
  1163. * ensure that longer session tokens are not consumed
  1164. *
  1165. * @return void
  1166. */
  1167. public function testCsrfNotUseOnceValidationLeavingToken() {
  1168. $this->Security->validatePost = false;
  1169. $this->Security->csrfCheck = true;
  1170. $this->Security->csrfUseOnce = false;
  1171. $this->Security->csrfExpires = '+10 minutes';
  1172. $this->Security->Session->write('_Token.csrfTokens', array('nonce1' => strtotime('+10 minutes')));
  1173. $this->Controller->request = $this->getMock('CakeRequest', array('is'));
  1174. $this->Controller->request->expects($this->once())->method('is')
  1175. ->with('post')
  1176. ->will($this->returnValue(true));
  1177. $this->Controller->request->params['action'] = 'index';
  1178. $this->Controller->request->data = array(
  1179. '_Token' => array(
  1180. 'key' => 'nonce1'
  1181. ),
  1182. 'Post' => array(
  1183. 'title' => 'Woot'
  1184. )
  1185. );
  1186. $this->Security->startup($this->Controller);
  1187. $token = $this->Security->Session->read('_Token');
  1188. $this->assertTrue(isset($token['csrfTokens']['nonce1']), 'Token was consumed');
  1189. }
  1190. /**
  1191. * Test generateToken()
  1192. *
  1193. * @return void
  1194. */
  1195. public function testGenerateToken() {
  1196. $request = $this->Controller->request;
  1197. $this->Security->generateToken($request);
  1198. $this->assertNotEmpty($request->params['_Token']);
  1199. $this->assertTrue(isset($request->params['_Token']['unlockedFields']));
  1200. $this->assertTrue(isset($request->params['_Token']['key']));
  1201. }
  1202. /**
  1203. * Test the limiting of CSRF tokens.
  1204. *
  1205. * @return void
  1206. */
  1207. public function testCsrfLimit() {
  1208. $this->Security->csrfLimit = 3;
  1209. $time = strtotime('+10 minutes');
  1210. $tokens = array(
  1211. '1' => $time,
  1212. '2' => $time,
  1213. '3' => $time,
  1214. '4' => $time,
  1215. '5' => $time,
  1216. );
  1217. $this->Security->Session->write('_Token', array('csrfTokens' => $tokens));
  1218. $this->Security->generateToken($this->Controller->request);
  1219. $result = $this->Security->Session->read('_Token.csrfTokens');
  1220. $this->assertFalse(isset($result['1']));
  1221. $this->assertFalse(isset($result['2']));
  1222. $this->assertFalse(isset($result['3']));
  1223. $this->assertTrue(isset($result['4']));
  1224. $this->assertTrue(isset($result['5']));
  1225. }
  1226. /**
  1227. * Test unlocked actions
  1228. *
  1229. * @return void
  1230. */
  1231. public function testUnlockedActions() {
  1232. $_SERVER['REQUEST_METHOD'] = 'POST';
  1233. $this->Controller->request->data = array('data');
  1234. $this->Controller->Security->unlockedActions = 'index';
  1235. $this->Controller->Security->blackHoleCallback = null;
  1236. $result = $this->Controller->Security->startup($this->Controller);
  1237. $this->assertNull($result);
  1238. }
  1239. }