PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/TestCase/Controller/Component/SecurityComponentTest.php

https://github.com/binondord/cakephp
PHP | 1156 lines | 706 code | 127 blank | 323 comment | 0 complexity | f03e6a42b11f89731f6733293946b1ef MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Controller\Component;
  16. use Cake\Controller\Component\SecurityComponent;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\Configure;
  19. use Cake\Event\Event;
  20. use Cake\Network\Request;
  21. use Cake\Network\Session;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\Utility\Security;
  24. /**
  25. * TestSecurityComponent
  26. *
  27. */
  28. class TestSecurityComponent extends SecurityComponent
  29. {
  30. /**
  31. * validatePost method
  32. *
  33. * @param Controller $controller
  34. * @return bool
  35. */
  36. public function validatePost(Controller $controller)
  37. {
  38. return $this->_validatePost($controller);
  39. }
  40. }
  41. /**
  42. * SecurityTestController
  43. *
  44. */
  45. class SecurityTestController extends Controller
  46. {
  47. /**
  48. * components property
  49. *
  50. * @var array
  51. */
  52. public $components = [
  53. 'TestSecurity' => ['className' => 'Cake\Test\TestCase\Controller\Component\TestSecurityComponent']
  54. ];
  55. /**
  56. * failed property
  57. *
  58. * @var bool
  59. */
  60. public $failed = false;
  61. /**
  62. * Used for keeping track of headers in test
  63. *
  64. * @var array
  65. */
  66. public $testHeaders = [];
  67. /**
  68. * fail method
  69. *
  70. * @return void
  71. */
  72. public function fail()
  73. {
  74. $this->failed = true;
  75. }
  76. /**
  77. * redirect method
  78. *
  79. * @param string|array $url
  80. * @param mixed $status
  81. * @param mixed $exit
  82. * @return void
  83. */
  84. public function redirect($url, $status = null, $exit = true)
  85. {
  86. return $status;
  87. }
  88. /**
  89. * Convenience method for header()
  90. *
  91. * @param string $status
  92. * @return void
  93. */
  94. public function header($status)
  95. {
  96. $this->testHeaders[] = $status;
  97. }
  98. }
  99. /**
  100. * SecurityComponentTest class
  101. *
  102. */
  103. class SecurityComponentTest extends TestCase
  104. {
  105. /**
  106. * Controller property
  107. *
  108. * @var SecurityTestController
  109. */
  110. public $Controller;
  111. /**
  112. * oldSalt property
  113. *
  114. * @var string
  115. */
  116. public $oldSalt;
  117. /**
  118. * setUp method
  119. *
  120. * @return void
  121. */
  122. public function setUp()
  123. {
  124. parent::setUp();
  125. $session = new Session();
  126. $request = $this->getMock('Cake\Network\Request', ['here'], ['posts/index']);
  127. $request->addParams(['controller' => 'posts', 'action' => 'index']);
  128. $request->session($session);
  129. $request->expects($this->any())
  130. ->method('here')
  131. ->will($this->returnValue('/articles/index'));
  132. $this->Controller = new SecurityTestController($request);
  133. $this->Controller->Security = $this->Controller->TestSecurity;
  134. $this->Controller->Security->config('blackHoleCallback', 'fail');
  135. $this->Security = $this->Controller->Security;
  136. $this->Security->session = $session;
  137. Security::salt('foo!');
  138. }
  139. /**
  140. * Tear-down method. Resets environment state.
  141. *
  142. * @return void
  143. */
  144. public function tearDown()
  145. {
  146. parent::tearDown();
  147. $this->Security->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 \Cake\Network\Exception\BadRequestException
  157. * @return void
  158. * @triggers Controller.startup $Controller, $this->Controller
  159. */
  160. public function testBlackholeWithBrokenCallback()
  161. {
  162. $request = new Request([
  163. 'url' => 'posts/index',
  164. 'session' => $this->Security->session
  165. ]);
  166. $request->addParams([
  167. 'controller' => 'posts',
  168. 'action' => 'index'
  169. ]);
  170. $Controller = new \TestApp\Controller\SomePagesController($request);
  171. $event = new Event('Controller.startup', $Controller, $this->Controller);
  172. $Security = new SecurityComponent($Controller->components());
  173. $Security->config('blackHoleCallback', '_fail');
  174. $Security->startup($event);
  175. $Security->blackHole($Controller, 'csrf');
  176. }
  177. /**
  178. * Ensure that directly requesting the blackholeCallback as the controller
  179. * action results in an exception.
  180. *
  181. * @return void
  182. * @triggers Controller.startup $this->Controller
  183. */
  184. public function testExceptionWhenActionIsBlackholeCallback()
  185. {
  186. $this->Controller->request->addParams([
  187. 'controller' => 'posts',
  188. 'action' => 'fail'
  189. ]);
  190. $event = new Event('Controller.startup', $this->Controller);
  191. $this->assertFalse($this->Controller->failed);
  192. $this->Controller->Security->startup($event);
  193. $this->assertTrue($this->Controller->failed, 'Request was blackholed.');
  194. }
  195. /**
  196. * test that initialize can set properties.
  197. *
  198. * @return void
  199. */
  200. public function testConstructorSettingProperties()
  201. {
  202. $settings = [
  203. 'requireSecure' => ['update_account'],
  204. 'validatePost' => false,
  205. ];
  206. $Security = new SecurityComponent($this->Controller->components(), $settings);
  207. $this->assertEquals($Security->validatePost, $settings['validatePost']);
  208. }
  209. /**
  210. * testStartup method
  211. *
  212. * @return void
  213. * @triggers Controller.startup $this->Controller
  214. */
  215. public function testStartup()
  216. {
  217. $event = new Event('Controller.startup', $this->Controller);
  218. $this->Controller->Security->startup($event);
  219. $this->assertTrue($this->Security->session->check('_Token'));
  220. }
  221. /**
  222. * testRequireSecureFail method
  223. *
  224. * @return void
  225. * @triggers Controller.startup $this->Controller
  226. */
  227. public function testRequireSecureFail()
  228. {
  229. $_SERVER['HTTPS'] = 'off';
  230. $_SERVER['REQUEST_METHOD'] = 'POST';
  231. $this->Controller->request['action'] = 'posted';
  232. $event = new Event('Controller.startup', $this->Controller);
  233. $this->Controller->Security->requireSecure(['posted']);
  234. $this->Controller->Security->startup($event);
  235. $this->assertTrue($this->Controller->failed);
  236. }
  237. /**
  238. * testRequireSecureSucceed method
  239. *
  240. * @return void
  241. * @triggers Controller.startup $this->Controller
  242. */
  243. public function testRequireSecureSucceed()
  244. {
  245. $_SERVER['HTTPS'] = 'on';
  246. $_SERVER['REQUEST_METHOD'] = 'Secure';
  247. $this->Controller->request['action'] = 'posted';
  248. $event = new Event('Controller.startup', $this->Controller);
  249. $this->Controller->Security->requireSecure('posted');
  250. $this->Controller->Security->startup($event);
  251. $this->assertFalse($this->Controller->failed);
  252. }
  253. /**
  254. * testRequireSecureEmptyFail method
  255. *
  256. * @return void
  257. * @triggers Controller.startup $this->Controller
  258. */
  259. public function testRequireSecureEmptyFail()
  260. {
  261. $_SERVER['HTTPS'] = 'off';
  262. $_SERVER['REQUEST_METHOD'] = 'POST';
  263. $this->Controller->request['action'] = 'posted';
  264. $event = new Event('Controller.startup', $this->Controller);
  265. $this->Controller->Security->requireSecure();
  266. $this->Controller->Security->startup($event);
  267. $this->assertTrue($this->Controller->failed);
  268. }
  269. /**
  270. * testRequireSecureEmptySucceed method
  271. *
  272. * @return void
  273. * @triggers Controller.startup $this->Controller
  274. */
  275. public function testRequireSecureEmptySucceed()
  276. {
  277. $_SERVER['HTTPS'] = 'on';
  278. $_SERVER['REQUEST_METHOD'] = 'Secure';
  279. $this->Controller->request['action'] = 'posted';
  280. $event = new Event('Controller.startup', $this->Controller);
  281. $this->Controller->Security->requireSecure();
  282. $this->Controller->Security->startup($event);
  283. $this->assertFalse($this->Controller->failed);
  284. }
  285. /**
  286. * testRequireAuthFail method
  287. *
  288. * @return void
  289. * @triggers Controller.startup $this->Controller
  290. */
  291. public function testRequireAuthFail()
  292. {
  293. $event = new Event('Controller.startup', $this->Controller);
  294. $_SERVER['REQUEST_METHOD'] = 'AUTH';
  295. $this->Controller->request['action'] = 'posted';
  296. $this->Controller->request->data = ['username' => 'willy', 'password' => 'somePass'];
  297. $this->Controller->Security->requireAuth(['posted']);
  298. $this->Controller->Security->startup($event);
  299. $this->assertTrue($this->Controller->failed);
  300. $this->Security->session->write('_Token', ['allowedControllers' => []]);
  301. $this->Controller->request->data = ['username' => 'willy', 'password' => 'somePass'];
  302. $this->Controller->request['action'] = 'posted';
  303. $this->Controller->Security->requireAuth('posted');
  304. $this->Controller->Security->startup($event);
  305. $this->assertTrue($this->Controller->failed);
  306. $this->Security->session->write('_Token', [
  307. 'allowedControllers' => ['SecurityTest'], 'allowedActions' => ['posted2']
  308. ]);
  309. $this->Controller->request->data = ['username' => 'willy', 'password' => 'somePass'];
  310. $this->Controller->request['action'] = 'posted';
  311. $this->Controller->Security->requireAuth('posted');
  312. $this->Controller->Security->startup($event);
  313. $this->assertTrue($this->Controller->failed);
  314. }
  315. /**
  316. * testRequireAuthSucceed method
  317. *
  318. * @return void
  319. * @triggers Controller.startup $this->Controller
  320. */
  321. public function testRequireAuthSucceed()
  322. {
  323. $_SERVER['REQUEST_METHOD'] = 'AUTH';
  324. $event = new Event('Controller.startup', $this->Controller);
  325. $this->Controller->request['action'] = 'posted';
  326. $this->Controller->Security->requireAuth('posted');
  327. $this->Controller->Security->startup($event);
  328. $this->assertFalse($this->Controller->failed);
  329. $this->Controller->Security->session->write('_Token', [
  330. 'allowedControllers' => ['SecurityTest'], 'allowedActions' => ['posted']
  331. ]);
  332. $this->Controller->request['controller'] = 'SecurityTest';
  333. $this->Controller->request['action'] = 'posted';
  334. $this->Controller->request->data = [
  335. 'username' => 'willy', 'password' => 'somePass', '_Token' => ''
  336. ];
  337. $this->Controller->action = 'posted';
  338. $this->Controller->Security->requireAuth('posted');
  339. $this->Controller->Security->startup($event);
  340. $this->assertFalse($this->Controller->failed);
  341. }
  342. /**
  343. * Simple hash validation test
  344. *
  345. * @return void
  346. * @triggers Controller.startup $this->Controller
  347. */
  348. public function testValidatePost()
  349. {
  350. $event = new Event('Controller.startup', $this->Controller);
  351. $this->Controller->Security->startup($event);
  352. $fields = '68730b0747d4889ec2766f9117405f9635f5fd5e%3AModel.valid';
  353. $unlocked = '';
  354. $this->Controller->request->data = [
  355. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  356. '_Token' => compact('fields', 'unlocked')
  357. ];
  358. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  359. }
  360. /**
  361. * Test that validatePost fails if you are missing the session information.
  362. *
  363. * @return void
  364. * @triggers Controller.startup $this->Controller
  365. */
  366. public function testValidatePostNoSession()
  367. {
  368. $event = new Event('Controller.startup', $this->Controller);
  369. $this->Controller->Security->startup($event);
  370. $this->Security->session->delete('_Token');
  371. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  372. $this->Controller->request->data = [
  373. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  374. '_Token' => compact('fields')
  375. ];
  376. $this->assertFalse($this->Controller->Security->validatePost($this->Controller));
  377. }
  378. /**
  379. * test that validatePost fails if any of its required fields are missing.
  380. *
  381. * @return void
  382. * @triggers Controller.startup $this->Controller
  383. */
  384. public function testValidatePostFormHacking()
  385. {
  386. $event = new Event('Controller.startup', $this->Controller);
  387. $this->Controller->Security->startup($event);
  388. $unlocked = '';
  389. $this->Controller->request->data = [
  390. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  391. '_Token' => compact('unlocked')
  392. ];
  393. $result = $this->Controller->Security->validatePost($this->Controller);
  394. $this->assertFalse($result, 'validatePost passed when fields were missing. %s');
  395. }
  396. /**
  397. * Test that objects can't be passed into the serialized string. This was a vector for RFI and LFI
  398. * attacks. Thanks to Felix Wilhelm
  399. *
  400. * @return void
  401. * @triggers Controller.startup $this->Controller
  402. */
  403. public function testValidatePostObjectDeserialize()
  404. {
  405. $event = new Event('Controller.startup', $this->Controller);
  406. $this->Controller->Security->startup($event);
  407. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877';
  408. $unlocked = '';
  409. // a corrupted serialized object, so we can see if it ever gets to deserialize
  410. $attack = 'O:3:"App":1:{s:5:"__map";a:1:{s:3:"foo";s:7:"Hacked!";s:1:"fail"}}';
  411. $fields .= urlencode(':' . str_rot13($attack));
  412. $this->Controller->request->data = [
  413. 'Model' => ['username' => 'mark', 'password' => 'foo', 'valid' => '0'],
  414. '_Token' => compact('fields', 'unlocked')
  415. ];
  416. $result = $this->Controller->Security->validatePost($this->Controller);
  417. $this->assertFalse($result, 'validatePost passed when key was missing. %s');
  418. }
  419. /**
  420. * Tests validation post data ignores `_csrfToken`.
  421. *
  422. * @return void
  423. * @triggers Controller.startup $this->Controller
  424. */
  425. public function testValidatePostIgnoresCsrfToken()
  426. {
  427. $event = new Event('Controller.startup', $this->Controller);
  428. $this->Controller->Security->startup($event);
  429. $fields = '8e26ef05379e5402c2c619f37ee91152333a0264%3A';
  430. $unlocked = '';
  431. $this->Controller->request->data = [
  432. '_csrfToken' => 'abc123',
  433. 'Model' => ['multi_field' => ['1', '3']],
  434. '_Token' => compact('fields', 'unlocked')
  435. ];
  436. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  437. }
  438. /**
  439. * Tests validation of checkbox arrays
  440. *
  441. * @return void
  442. * @triggers Controller.startup $this->Controller
  443. */
  444. public function testValidatePostArray()
  445. {
  446. $event = new Event('Controller.startup', $this->Controller);
  447. $this->Controller->Security->startup($event);
  448. $fields = '8e26ef05379e5402c2c619f37ee91152333a0264%3A';
  449. $unlocked = '';
  450. $this->Controller->request->data = [
  451. 'Model' => ['multi_field' => ['1', '3']],
  452. '_Token' => compact('fields', 'unlocked')
  453. ];
  454. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  455. $this->Controller->request->data = [
  456. 'Model' => ['multi_field' => [12 => '1', 20 => '3']],
  457. '_Token' => compact('fields', 'unlocked')
  458. ];
  459. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  460. }
  461. /**
  462. * Tests validation of integer field names.
  463. *
  464. * @return void
  465. */
  466. public function testValidateIntFieldName()
  467. {
  468. $event = new Event('Controller.startup', $this->Controller);
  469. $this->Controller->Security->startup($event);
  470. $fields = '4a221010dd7a23f7166cb10c38bc21d81341c387%3A';
  471. $unlocked = '';
  472. $this->Controller->request->data = [
  473. 1 => 'value,',
  474. '_Token' => compact('fields', 'unlocked')
  475. ];
  476. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  477. }
  478. /**
  479. * testValidatePostNoModel method
  480. *
  481. * @return void
  482. * @triggers Controller.startup $this->Controller
  483. */
  484. public function testValidatePostNoModel()
  485. {
  486. $event = new Event('Controller.startup', $this->Controller);
  487. $this->Controller->Security->startup($event);
  488. $fields = 'a1c3724b7ba85e7022413611e30ba2c6181d5aba%3A';
  489. $unlocked = '';
  490. $this->Controller->request->data = [
  491. 'anything' => 'some_data',
  492. '_Token' => compact('fields', 'unlocked')
  493. ];
  494. $result = $this->Controller->Security->validatePost($this->Controller);
  495. $this->assertTrue($result);
  496. }
  497. /**
  498. * testValidatePostSimple method
  499. *
  500. * @return void
  501. * @triggers Controller.startup $this->Controller
  502. */
  503. public function testValidatePostSimple()
  504. {
  505. $event = new Event('Controller.startup', $this->Controller);
  506. $this->Controller->Security->startup($event);
  507. $fields = 'b0914d06dfb04abf1fada53e16810e87d157950b%3A';
  508. $unlocked = '';
  509. $this->Controller->request->data = [
  510. 'Model' => ['username' => '', 'password' => ''],
  511. '_Token' => compact('fields', 'unlocked')
  512. ];
  513. $result = $this->Controller->Security->validatePost($this->Controller);
  514. $this->assertTrue($result);
  515. }
  516. /**
  517. * Tests hash validation for multiple records, including locked fields
  518. *
  519. * @return void
  520. * @triggers Controller.startup $this->Controller
  521. */
  522. public function testValidatePostComplex()
  523. {
  524. $event = new Event('Controller.startup', $this->Controller);
  525. $this->Controller->Security->startup($event);
  526. $fields = 'b65c7463e44a61d8d2eaecce2c265b406c9c4742%3AAddresses.0.id%7CAddresses.1.id';
  527. $unlocked = '';
  528. $this->Controller->request->data = [
  529. 'Addresses' => [
  530. '0' => [
  531. 'id' => '123456', 'title' => '', 'first_name' => '', 'last_name' => '',
  532. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  533. ],
  534. '1' => [
  535. 'id' => '654321', 'title' => '', 'first_name' => '', 'last_name' => '',
  536. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  537. ]
  538. ],
  539. '_Token' => compact('fields', 'unlocked')
  540. ];
  541. $result = $this->Controller->Security->validatePost($this->Controller);
  542. $this->assertTrue($result);
  543. }
  544. /**
  545. * test ValidatePost with multiple select elements.
  546. *
  547. * @return void
  548. * @triggers Controller.startup $this->Controller
  549. */
  550. public function testValidatePostMultipleSelect()
  551. {
  552. $event = new Event('Controller.startup', $this->Controller);
  553. $this->Controller->Security->startup($event);
  554. $fields = '8d8da68ba03b3d6e7e145b948abfe26741422169%3A';
  555. $unlocked = '';
  556. $this->Controller->request->data = [
  557. 'Tag' => ['Tag' => [1, 2]],
  558. '_Token' => compact('fields', 'unlocked'),
  559. ];
  560. $result = $this->Controller->Security->validatePost($this->Controller);
  561. $this->assertTrue($result);
  562. $this->Controller->request->data = [
  563. 'Tag' => ['Tag' => [1, 2, 3]],
  564. '_Token' => compact('fields', 'unlocked'),
  565. ];
  566. $result = $this->Controller->Security->validatePost($this->Controller);
  567. $this->assertTrue($result);
  568. $this->Controller->request->data = [
  569. 'Tag' => ['Tag' => [1, 2, 3, 4]],
  570. '_Token' => compact('fields', 'unlocked'),
  571. ];
  572. $result = $this->Controller->Security->validatePost($this->Controller);
  573. $this->assertTrue($result);
  574. $fields = 'eae2adda1628b771a30cc133342d16220c6520fe%3A';
  575. $this->Controller->request->data = [
  576. 'User.password' => 'bar', 'User.name' => 'foo', 'User.is_valid' => '1',
  577. 'Tag' => ['Tag' => [1]],
  578. '_Token' => compact('fields', 'unlocked'),
  579. ];
  580. $result = $this->Controller->Security->validatePost($this->Controller);
  581. $this->assertTrue($result);
  582. }
  583. /**
  584. * testValidatePostCheckbox method
  585. *
  586. * First block tests un-checked checkbox
  587. * Second block tests checked checkbox
  588. *
  589. * @return void
  590. * @triggers Controller.startup $this->Controller
  591. */
  592. public function testValidatePostCheckbox()
  593. {
  594. $event = new Event('Controller.startup', $this->Controller);
  595. $this->Controller->Security->startup($event);
  596. $fields = '68730b0747d4889ec2766f9117405f9635f5fd5e%3AModel.valid';
  597. $unlocked = '';
  598. $this->Controller->request->data = [
  599. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  600. '_Token' => compact('fields', 'unlocked')
  601. ];
  602. $result = $this->Controller->Security->validatePost($this->Controller);
  603. $this->assertTrue($result);
  604. $fields = 'f63e4a69b2edd31f064e8e602a04dd59307cfe9c%3A';
  605. $this->Controller->request->data = [
  606. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  607. '_Token' => compact('fields', 'unlocked')
  608. ];
  609. $result = $this->Controller->Security->validatePost($this->Controller);
  610. $this->assertTrue($result);
  611. $this->Controller->request->data = [];
  612. $this->Controller->Security->startup($event);
  613. $this->Controller->request->data = [
  614. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  615. '_Token' => compact('fields', 'unlocked')
  616. ];
  617. $result = $this->Controller->Security->validatePost($this->Controller);
  618. $this->assertTrue($result);
  619. }
  620. /**
  621. * testValidatePostHidden method
  622. *
  623. * @return void
  624. * @triggers Controller.startup $this->Controller
  625. */
  626. public function testValidatePostHidden()
  627. {
  628. $event = new Event('Controller.startup', $this->Controller);
  629. $this->Controller->Security->startup($event);
  630. $fields = '973a8939a68ac014cc6f7666cec9aa6268507350%3AModel.hidden%7CModel.other_hidden';
  631. $unlocked = '';
  632. $this->Controller->request->data = [
  633. 'Model' => [
  634. 'username' => '', 'password' => '', 'hidden' => '0',
  635. 'other_hidden' => 'some hidden value'
  636. ],
  637. '_Token' => compact('fields', 'unlocked')
  638. ];
  639. $result = $this->Controller->Security->validatePost($this->Controller);
  640. $this->assertTrue($result);
  641. }
  642. /**
  643. * testValidatePostWithDisabledFields method
  644. *
  645. * @return void
  646. * @triggers Controller.startup $this->Controller
  647. */
  648. public function testValidatePostWithDisabledFields()
  649. {
  650. $event = new Event('Controller.startup', $this->Controller);
  651. $this->Controller->Security->config('disabledFields', ['Model.username', 'Model.password']);
  652. $this->Controller->Security->startup($event);
  653. $fields = '1c59acfbca98bd870c11fb544d545cbf23215880%3AModel.hidden';
  654. $unlocked = '';
  655. $this->Controller->request->data = [
  656. 'Model' => [
  657. 'username' => '', 'password' => '', 'hidden' => '0'
  658. ],
  659. '_Token' => compact('fields', 'unlocked')
  660. ];
  661. $result = $this->Controller->Security->validatePost($this->Controller);
  662. $this->assertTrue($result);
  663. }
  664. /**
  665. * test validating post data with posted unlocked fields.
  666. *
  667. * @return void
  668. * @triggers Controller.startup $this->Controller
  669. */
  670. public function testValidatePostDisabledFieldsInData()
  671. {
  672. $event = new Event('Controller.startup', $this->Controller);
  673. $this->Controller->Security->startup($event);
  674. $unlocked = 'Model.username';
  675. $fields = ['Model.hidden', 'Model.password'];
  676. $fields = urlencode(Security::hash('/articles/index' . serialize($fields) . $unlocked . Security::salt()));
  677. $this->Controller->request->data = [
  678. 'Model' => [
  679. 'username' => 'mark',
  680. 'password' => 'sekret',
  681. 'hidden' => '0'
  682. ],
  683. '_Token' => compact('fields', 'unlocked')
  684. ];
  685. $result = $this->Controller->Security->validatePost($this->Controller);
  686. $this->assertTrue($result);
  687. }
  688. /**
  689. * test that missing 'unlocked' input causes failure
  690. *
  691. * @return void
  692. * @triggers Controller.startup $this->Controller
  693. */
  694. public function testValidatePostFailNoDisabled()
  695. {
  696. $event = new Event('Controller.startup', $this->Controller);
  697. $this->Controller->Security->startup($event);
  698. $fields = ['Model.hidden', 'Model.password', 'Model.username'];
  699. $fields = urlencode(Security::hash(serialize($fields) . Security::salt()));
  700. $this->Controller->request->data = [
  701. 'Model' => [
  702. 'username' => 'mark',
  703. 'password' => 'sekret',
  704. 'hidden' => '0'
  705. ],
  706. '_Token' => compact('fields')
  707. ];
  708. $result = $this->Controller->Security->validatePost($this->Controller);
  709. $this->assertFalse($result);
  710. }
  711. /**
  712. * Test that validatePost fails when unlocked fields are changed.
  713. *
  714. * @return void
  715. * @triggers Controller.startup $this->Controller
  716. */
  717. public function testValidatePostFailDisabledFieldTampering()
  718. {
  719. $event = new Event('Controller.startup', $this->Controller);
  720. $this->Controller->Security->startup($event);
  721. $unlocked = 'Model.username';
  722. $fields = ['Model.hidden', 'Model.password'];
  723. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::salt()));
  724. // Tamper the values.
  725. $unlocked = 'Model.username|Model.password';
  726. $this->Controller->request->data = [
  727. 'Model' => [
  728. 'username' => 'mark',
  729. 'password' => 'sekret',
  730. 'hidden' => '0'
  731. ],
  732. '_Token' => compact('fields', 'unlocked')
  733. ];
  734. $result = $this->Controller->Security->validatePost($this->Controller);
  735. $this->assertFalse($result);
  736. }
  737. /**
  738. * testValidateHiddenMultipleModel method
  739. *
  740. * @return void
  741. * @triggers Controller.startup $this->Controller
  742. */
  743. public function testValidateHiddenMultipleModel()
  744. {
  745. $event = new Event('Controller.startup', $this->Controller);
  746. $this->Controller->Security->startup($event);
  747. $fields = '075ca6c26c38a09a78d871201df89faf52cbbeb8%3AModel.valid%7CModel2.valid%7CModel3.valid';
  748. $unlocked = '';
  749. $this->Controller->request->data = [
  750. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  751. 'Model2' => ['valid' => '0'],
  752. 'Model3' => ['valid' => '0'],
  753. '_Token' => compact('fields', 'unlocked')
  754. ];
  755. $result = $this->Controller->Security->validatePost($this->Controller);
  756. $this->assertTrue($result);
  757. }
  758. /**
  759. * testValidateHasManyModel method
  760. *
  761. * @return void
  762. * @triggers Controller.startup $this->Controller
  763. */
  764. public function testValidateHasManyModel()
  765. {
  766. $event = new Event('Controller.startup', $this->Controller);
  767. $this->Controller->Security->startup($event);
  768. $fields = '24a753fb62ef7839389987b58e3f7108f564e529%3AModel.0.hidden%7CModel.0.valid';
  769. $fields .= '%7CModel.1.hidden%7CModel.1.valid';
  770. $unlocked = '';
  771. $this->Controller->request->data = [
  772. 'Model' => [
  773. [
  774. 'username' => 'username', 'password' => 'password',
  775. 'hidden' => 'value', 'valid' => '0'
  776. ],
  777. [
  778. 'username' => 'username', 'password' => 'password',
  779. 'hidden' => 'value', 'valid' => '0'
  780. ]
  781. ],
  782. '_Token' => compact('fields', 'unlocked')
  783. ];
  784. $result = $this->Controller->Security->validatePost($this->Controller);
  785. $this->assertTrue($result);
  786. }
  787. /**
  788. * testValidateHasManyRecordsPass method
  789. *
  790. * @return void
  791. * @triggers Controller.startup $this->Controller
  792. */
  793. public function testValidateHasManyRecordsPass()
  794. {
  795. $event = new Event('Controller.startup', $this->Controller);
  796. $this->Controller->Security->startup($event);
  797. $fields = '8f7d82bf7656cf068822d9bdab109ebed1be1825%3AAddress.0.id%7CAddress.0.primary%7C';
  798. $fields .= 'Address.1.id%7CAddress.1.primary';
  799. $unlocked = '';
  800. $this->Controller->request->data = [
  801. 'Address' => [
  802. 0 => [
  803. 'id' => '123',
  804. 'title' => 'home',
  805. 'first_name' => 'Bilbo',
  806. 'last_name' => 'Baggins',
  807. 'address' => '23 Bag end way',
  808. 'city' => 'the shire',
  809. 'phone' => 'N/A',
  810. 'primary' => '1',
  811. ],
  812. 1 => [
  813. 'id' => '124',
  814. 'title' => 'home',
  815. 'first_name' => 'Frodo',
  816. 'last_name' => 'Baggins',
  817. 'address' => '50 Bag end way',
  818. 'city' => 'the shire',
  819. 'phone' => 'N/A',
  820. 'primary' => '1'
  821. ]
  822. ],
  823. '_Token' => compact('fields', 'unlocked')
  824. ];
  825. $result = $this->Controller->Security->validatePost($this->Controller);
  826. $this->assertTrue($result);
  827. }
  828. /**
  829. * Test that values like Foo.0.1
  830. *
  831. * @return void
  832. * @triggers Controller.startup $this->Controller
  833. */
  834. public function testValidateNestedNumericSets()
  835. {
  836. $event = new Event('Controller.startup', $this->Controller);
  837. $this->Controller->Security->startup($event);
  838. $unlocked = '';
  839. $hashFields = ['TaxonomyData'];
  840. $fields = urlencode(Security::hash('/articles/index' . serialize($hashFields) . $unlocked . Security::salt()));
  841. $this->Controller->request->data = [
  842. 'TaxonomyData' => [
  843. 1 => [[2]],
  844. 2 => [[3]]
  845. ],
  846. '_Token' => compact('fields', 'unlocked')
  847. ];
  848. $result = $this->Controller->Security->validatePost($this->Controller);
  849. $this->assertTrue($result);
  850. }
  851. /**
  852. * testValidateHasManyRecords method
  853. *
  854. * validatePost should fail, hidden fields have been changed.
  855. *
  856. * @return void
  857. * @triggers Controller.startup $this->Controller
  858. */
  859. public function testValidateHasManyRecordsFail()
  860. {
  861. $event = new Event('Controller.startup', $this->Controller);
  862. $this->Controller->Security->startup($event);
  863. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
  864. $fields .= 'Address.1.id%7CAddress.1.primary';
  865. $unlocked = '';
  866. $this->Controller->request->data = [
  867. 'Address' => [
  868. 0 => [
  869. 'id' => '123',
  870. 'title' => 'home',
  871. 'first_name' => 'Bilbo',
  872. 'last_name' => 'Baggins',
  873. 'address' => '23 Bag end way',
  874. 'city' => 'the shire',
  875. 'phone' => 'N/A',
  876. 'primary' => '5',
  877. ],
  878. 1 => [
  879. 'id' => '124',
  880. 'title' => 'home',
  881. 'first_name' => 'Frodo',
  882. 'last_name' => 'Baggins',
  883. 'address' => '50 Bag end way',
  884. 'city' => 'the shire',
  885. 'phone' => 'N/A',
  886. 'primary' => '1'
  887. ]
  888. ],
  889. '_Token' => compact('fields', 'unlocked')
  890. ];
  891. $result = $this->Controller->Security->validatePost($this->Controller);
  892. $this->assertFalse($result);
  893. }
  894. /**
  895. * testFormDisabledFields method
  896. *
  897. * @return void
  898. * @triggers Controller.startup $this->Controller
  899. */
  900. public function testFormDisabledFields()
  901. {
  902. $event = new Event('Controller.startup', $this->Controller);
  903. $this->Controller->Security->startup($event);
  904. $fields = '9da2b3fa2b5b8ac0bfbc1bbce145e58059629125%3An%3A0%3A%7B%7D';
  905. $unlocked = '';
  906. $this->Controller->request->data = [
  907. 'MyModel' => ['name' => 'some data'],
  908. '_Token' => compact('fields', 'unlocked')
  909. ];
  910. $result = $this->Controller->Security->validatePost($this->Controller);
  911. $this->assertFalse($result);
  912. $this->Controller->Security->startup($event);
  913. $this->Controller->Security->config('disabledFields', ['MyModel.name']);
  914. $this->Controller->request->data = [
  915. 'MyModel' => ['name' => 'some data'],
  916. '_Token' => compact('fields', 'unlocked')
  917. ];
  918. $result = $this->Controller->Security->validatePost($this->Controller);
  919. $this->assertTrue($result);
  920. }
  921. /**
  922. * test validatePost with radio buttons
  923. *
  924. * @return void
  925. * @triggers Controller.startup $this->Controller
  926. */
  927. public function testValidatePostRadio()
  928. {
  929. $event = new Event('Controller.startup', $this->Controller);
  930. $this->Controller->Security->startup($event);
  931. $fields = 'c2226a8879c3f4b513691295fc2519a29c44c8bb%3An%3A0%3A%7B%7D';
  932. $unlocked = '';
  933. $this->Controller->request->data = [
  934. '_Token' => compact('fields', 'unlocked')
  935. ];
  936. $result = $this->Controller->Security->validatePost($this->Controller);
  937. $this->assertFalse($result);
  938. $this->Controller->request->data = [
  939. '_Token' => compact('fields', 'unlocked'),
  940. 'Test' => ['test' => '']
  941. ];
  942. $result = $this->Controller->Security->validatePost($this->Controller);
  943. $this->assertTrue($result);
  944. $this->Controller->request->data = [
  945. '_Token' => compact('fields', 'unlocked'),
  946. 'Test' => ['test' => '1']
  947. ];
  948. $result = $this->Controller->Security->validatePost($this->Controller);
  949. $this->assertTrue($result);
  950. $this->Controller->request->data = [
  951. '_Token' => compact('fields', 'unlocked'),
  952. 'Test' => ['test' => '2']
  953. ];
  954. $result = $this->Controller->Security->validatePost($this->Controller);
  955. $this->assertTrue($result);
  956. }
  957. /**
  958. * test validatePost uses here() as a hash input.
  959. *
  960. * @return void
  961. * @triggers Controller.startup $this->Controller
  962. */
  963. public function testValidatePostUrlAsHashInput()
  964. {
  965. $event = new Event('Controller.startup', $this->Controller);
  966. $this->Security->startup($event);
  967. $fields = 'b0914d06dfb04abf1fada53e16810e87d157950b%3A';
  968. $unlocked = '';
  969. $this->Controller->request->data = [
  970. 'Model' => ['username' => '', 'password' => ''],
  971. '_Token' => compact('fields', 'unlocked')
  972. ];
  973. $this->assertTrue($this->Security->validatePost($this->Controller));
  974. $request = $this->getMock('Cake\Network\Request', ['here']);
  975. $request->expects($this->at(0))
  976. ->method('here')
  977. ->will($this->returnValue('/posts/index?page=1'));
  978. $request->expects($this->at(1))
  979. ->method('here')
  980. ->will($this->returnValue('/posts/edit/1'));
  981. $request->data = $this->Controller->request->data;
  982. $this->Controller->request = $request;
  983. $this->assertFalse($this->Security->validatePost($this->Controller));
  984. $this->assertFalse($this->Security->validatePost($this->Controller));
  985. }
  986. /**
  987. * test that blackhole doesn't delete the _Token session key so repeat data submissions
  988. * stay blackholed.
  989. *
  990. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/214
  991. * @return void
  992. * @triggers Controller.startup $this->Controller
  993. */
  994. public function testBlackHoleNotDeletingSessionInformation()
  995. {
  996. $event = new Event('Controller.startup', $this->Controller);
  997. $this->Controller->Security->startup($event);
  998. $this->Controller->Security->blackHole($this->Controller, 'auth');
  999. $this->assertTrue($this->Controller->Security->session->check('_Token'), '_Token was deleted by blackHole %s');
  1000. }
  1001. /**
  1002. * Test generateToken()
  1003. *
  1004. * @return void
  1005. */
  1006. public function testGenerateToken()
  1007. {
  1008. $request = $this->Controller->request;
  1009. $this->Security->generateToken($request);
  1010. $this->assertNotEmpty($request->params['_Token']);
  1011. $this->assertTrue(isset($request->params['_Token']['unlockedFields']));
  1012. }
  1013. /**
  1014. * Test unlocked actions
  1015. *
  1016. * @return void
  1017. * @triggers Controller.startup $this->Controller
  1018. */
  1019. public function testUnlockedActions()
  1020. {
  1021. $_SERVER['REQUEST_METHOD'] = 'POST';
  1022. $event = new Event('Controller.startup', $this->Controller);
  1023. $this->Controller->request->data = ['data'];
  1024. $this->Controller->Security->unlockedActions = 'index';
  1025. $this->Controller->Security->blackHoleCallback = null;
  1026. $result = $this->Controller->Security->startup($event);
  1027. $this->assertNull($result);
  1028. }
  1029. }