SecurityComponentTest.php 29 KB

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