SecurityComponentTest.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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. /**
  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. }
  456. /**
  457. * Tests validation of integer field names.
  458. *
  459. * @return void
  460. */
  461. public function testValidateIntFieldName()
  462. {
  463. $event = new Event('Controller.startup', $this->Controller);
  464. $this->Controller->Security->startup($event);
  465. $fields = '4a221010dd7a23f7166cb10c38bc21d81341c387%3A';
  466. $unlocked = '';
  467. $this->Controller->request->data = [
  468. 1 => 'value,',
  469. '_Token' => compact('fields', 'unlocked')
  470. ];
  471. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  472. }
  473. /**
  474. * testValidatePostNoModel method
  475. *
  476. * @return void
  477. * @triggers Controller.startup $this->Controller
  478. */
  479. public function testValidatePostNoModel()
  480. {
  481. $event = new Event('Controller.startup', $this->Controller);
  482. $this->Controller->Security->startup($event);
  483. $fields = 'a1c3724b7ba85e7022413611e30ba2c6181d5aba%3A';
  484. $unlocked = '';
  485. $this->Controller->request->data = [
  486. 'anything' => 'some_data',
  487. '_Token' => compact('fields', 'unlocked')
  488. ];
  489. $result = $this->Controller->Security->validatePost($this->Controller);
  490. $this->assertTrue($result);
  491. }
  492. /**
  493. * testValidatePostSimple method
  494. *
  495. * @return void
  496. * @triggers Controller.startup $this->Controller
  497. */
  498. public function testValidatePostSimple()
  499. {
  500. $event = new Event('Controller.startup', $this->Controller);
  501. $this->Controller->Security->startup($event);
  502. $fields = 'b0914d06dfb04abf1fada53e16810e87d157950b%3A';
  503. $unlocked = '';
  504. $this->Controller->request->data = [
  505. 'Model' => ['username' => '', 'password' => ''],
  506. '_Token' => compact('fields', 'unlocked')
  507. ];
  508. $result = $this->Controller->Security->validatePost($this->Controller);
  509. $this->assertTrue($result);
  510. }
  511. /**
  512. * Tests hash validation for multiple records, including locked fields
  513. *
  514. * @return void
  515. * @triggers Controller.startup $this->Controller
  516. */
  517. public function testValidatePostComplex()
  518. {
  519. $event = new Event('Controller.startup', $this->Controller);
  520. $this->Controller->Security->startup($event);
  521. $fields = 'b65c7463e44a61d8d2eaecce2c265b406c9c4742%3AAddresses.0.id%7CAddresses.1.id';
  522. $unlocked = '';
  523. $this->Controller->request->data = [
  524. 'Addresses' => [
  525. '0' => [
  526. 'id' => '123456', 'title' => '', 'first_name' => '', 'last_name' => '',
  527. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  528. ],
  529. '1' => [
  530. 'id' => '654321', 'title' => '', 'first_name' => '', 'last_name' => '',
  531. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  532. ]
  533. ],
  534. '_Token' => compact('fields', 'unlocked')
  535. ];
  536. $result = $this->Controller->Security->validatePost($this->Controller);
  537. $this->assertTrue($result);
  538. }
  539. /**
  540. * test ValidatePost with multiple select elements.
  541. *
  542. * @return void
  543. * @triggers Controller.startup $this->Controller
  544. */
  545. public function testValidatePostMultipleSelect()
  546. {
  547. $event = new Event('Controller.startup', $this->Controller);
  548. $this->Controller->Security->startup($event);
  549. $fields = '8d8da68ba03b3d6e7e145b948abfe26741422169%3A';
  550. $unlocked = '';
  551. $this->Controller->request->data = [
  552. 'Tag' => ['Tag' => [1, 2]],
  553. '_Token' => compact('fields', 'unlocked'),
  554. ];
  555. $result = $this->Controller->Security->validatePost($this->Controller);
  556. $this->assertTrue($result);
  557. $this->Controller->request->data = [
  558. 'Tag' => ['Tag' => [1, 2, 3]],
  559. '_Token' => compact('fields', 'unlocked'),
  560. ];
  561. $result = $this->Controller->Security->validatePost($this->Controller);
  562. $this->assertTrue($result);
  563. $this->Controller->request->data = [
  564. 'Tag' => ['Tag' => [1, 2, 3, 4]],
  565. '_Token' => compact('fields', 'unlocked'),
  566. ];
  567. $result = $this->Controller->Security->validatePost($this->Controller);
  568. $this->assertTrue($result);
  569. $fields = 'eae2adda1628b771a30cc133342d16220c6520fe%3A';
  570. $this->Controller->request->data = [
  571. 'User.password' => 'bar', 'User.name' => 'foo', 'User.is_valid' => '1',
  572. 'Tag' => ['Tag' => [1]],
  573. '_Token' => compact('fields', 'unlocked'),
  574. ];
  575. $result = $this->Controller->Security->validatePost($this->Controller);
  576. $this->assertTrue($result);
  577. }
  578. /**
  579. * testValidatePostCheckbox method
  580. *
  581. * First block tests un-checked checkbox
  582. * Second block tests checked checkbox
  583. *
  584. * @return void
  585. * @triggers Controller.startup $this->Controller
  586. */
  587. public function testValidatePostCheckbox()
  588. {
  589. $event = new Event('Controller.startup', $this->Controller);
  590. $this->Controller->Security->startup($event);
  591. $fields = '68730b0747d4889ec2766f9117405f9635f5fd5e%3AModel.valid';
  592. $unlocked = '';
  593. $this->Controller->request->data = [
  594. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  595. '_Token' => compact('fields', 'unlocked')
  596. ];
  597. $result = $this->Controller->Security->validatePost($this->Controller);
  598. $this->assertTrue($result);
  599. $fields = 'f63e4a69b2edd31f064e8e602a04dd59307cfe9c%3A';
  600. $this->Controller->request->data = [
  601. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  602. '_Token' => compact('fields', 'unlocked')
  603. ];
  604. $result = $this->Controller->Security->validatePost($this->Controller);
  605. $this->assertTrue($result);
  606. $this->Controller->request->data = [];
  607. $this->Controller->Security->startup($event);
  608. $this->Controller->request->data = [
  609. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  610. '_Token' => compact('fields', 'unlocked')
  611. ];
  612. $result = $this->Controller->Security->validatePost($this->Controller);
  613. $this->assertTrue($result);
  614. }
  615. /**
  616. * testValidatePostHidden method
  617. *
  618. * @return void
  619. * @triggers Controller.startup $this->Controller
  620. */
  621. public function testValidatePostHidden()
  622. {
  623. $event = new Event('Controller.startup', $this->Controller);
  624. $this->Controller->Security->startup($event);
  625. $fields = '973a8939a68ac014cc6f7666cec9aa6268507350%3AModel.hidden%7CModel.other_hidden';
  626. $unlocked = '';
  627. $this->Controller->request->data = [
  628. 'Model' => [
  629. 'username' => '', 'password' => '', 'hidden' => '0',
  630. 'other_hidden' => 'some hidden value'
  631. ],
  632. '_Token' => compact('fields', 'unlocked')
  633. ];
  634. $result = $this->Controller->Security->validatePost($this->Controller);
  635. $this->assertTrue($result);
  636. }
  637. /**
  638. * testValidatePostWithDisabledFields method
  639. *
  640. * @return void
  641. * @triggers Controller.startup $this->Controller
  642. */
  643. public function testValidatePostWithDisabledFields()
  644. {
  645. $event = new Event('Controller.startup', $this->Controller);
  646. $this->Controller->Security->config('disabledFields', ['Model.username', 'Model.password']);
  647. $this->Controller->Security->startup($event);
  648. $fields = '1c59acfbca98bd870c11fb544d545cbf23215880%3AModel.hidden';
  649. $unlocked = '';
  650. $this->Controller->request->data = [
  651. 'Model' => [
  652. 'username' => '', 'password' => '', 'hidden' => '0'
  653. ],
  654. '_Token' => compact('fields', 'unlocked')
  655. ];
  656. $result = $this->Controller->Security->validatePost($this->Controller);
  657. $this->assertTrue($result);
  658. }
  659. /**
  660. * test validating post data with posted unlocked fields.
  661. *
  662. * @return void
  663. * @triggers Controller.startup $this->Controller
  664. */
  665. public function testValidatePostDisabledFieldsInData()
  666. {
  667. $event = new Event('Controller.startup', $this->Controller);
  668. $this->Controller->Security->startup($event);
  669. $unlocked = 'Model.username';
  670. $fields = ['Model.hidden', 'Model.password'];
  671. $fields = urlencode(Security::hash('/articles/index' . serialize($fields) . $unlocked . Security::salt()));
  672. $this->Controller->request->data = [
  673. 'Model' => [
  674. 'username' => 'mark',
  675. 'password' => 'sekret',
  676. 'hidden' => '0'
  677. ],
  678. '_Token' => compact('fields', 'unlocked')
  679. ];
  680. $result = $this->Controller->Security->validatePost($this->Controller);
  681. $this->assertTrue($result);
  682. }
  683. /**
  684. * test that missing 'unlocked' input causes failure
  685. *
  686. * @return void
  687. * @triggers Controller.startup $this->Controller
  688. */
  689. public function testValidatePostFailNoDisabled()
  690. {
  691. $event = new Event('Controller.startup', $this->Controller);
  692. $this->Controller->Security->startup($event);
  693. $fields = ['Model.hidden', 'Model.password', 'Model.username'];
  694. $fields = urlencode(Security::hash(serialize($fields) . Security::salt()));
  695. $this->Controller->request->data = [
  696. 'Model' => [
  697. 'username' => 'mark',
  698. 'password' => 'sekret',
  699. 'hidden' => '0'
  700. ],
  701. '_Token' => compact('fields')
  702. ];
  703. $result = $this->Controller->Security->validatePost($this->Controller);
  704. $this->assertFalse($result);
  705. }
  706. /**
  707. * Test that validatePost fails when unlocked fields are changed.
  708. *
  709. * @return void
  710. * @triggers Controller.startup $this->Controller
  711. */
  712. public function testValidatePostFailDisabledFieldTampering()
  713. {
  714. $event = new Event('Controller.startup', $this->Controller);
  715. $this->Controller->Security->startup($event);
  716. $unlocked = 'Model.username';
  717. $fields = ['Model.hidden', 'Model.password'];
  718. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::salt()));
  719. // Tamper the values.
  720. $unlocked = 'Model.username|Model.password';
  721. $this->Controller->request->data = [
  722. 'Model' => [
  723. 'username' => 'mark',
  724. 'password' => 'sekret',
  725. 'hidden' => '0'
  726. ],
  727. '_Token' => compact('fields', 'unlocked')
  728. ];
  729. $result = $this->Controller->Security->validatePost($this->Controller);
  730. $this->assertFalse($result);
  731. }
  732. /**
  733. * testValidateHiddenMultipleModel method
  734. *
  735. * @return void
  736. * @triggers Controller.startup $this->Controller
  737. */
  738. public function testValidateHiddenMultipleModel()
  739. {
  740. $event = new Event('Controller.startup', $this->Controller);
  741. $this->Controller->Security->startup($event);
  742. $fields = '075ca6c26c38a09a78d871201df89faf52cbbeb8%3AModel.valid%7CModel2.valid%7CModel3.valid';
  743. $unlocked = '';
  744. $this->Controller->request->data = [
  745. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  746. 'Model2' => ['valid' => '0'],
  747. 'Model3' => ['valid' => '0'],
  748. '_Token' => compact('fields', 'unlocked')
  749. ];
  750. $result = $this->Controller->Security->validatePost($this->Controller);
  751. $this->assertTrue($result);
  752. }
  753. /**
  754. * testValidateHasManyModel method
  755. *
  756. * @return void
  757. * @triggers Controller.startup $this->Controller
  758. */
  759. public function testValidateHasManyModel()
  760. {
  761. $event = new Event('Controller.startup', $this->Controller);
  762. $this->Controller->Security->startup($event);
  763. $fields = '24a753fb62ef7839389987b58e3f7108f564e529%3AModel.0.hidden%7CModel.0.valid';
  764. $fields .= '%7CModel.1.hidden%7CModel.1.valid';
  765. $unlocked = '';
  766. $this->Controller->request->data = [
  767. 'Model' => [
  768. [
  769. 'username' => 'username', 'password' => 'password',
  770. 'hidden' => 'value', 'valid' => '0'
  771. ],
  772. [
  773. 'username' => 'username', 'password' => 'password',
  774. 'hidden' => 'value', 'valid' => '0'
  775. ]
  776. ],
  777. '_Token' => compact('fields', 'unlocked')
  778. ];
  779. $result = $this->Controller->Security->validatePost($this->Controller);
  780. $this->assertTrue($result);
  781. }
  782. /**
  783. * testValidateHasManyRecordsPass method
  784. *
  785. * @return void
  786. * @triggers Controller.startup $this->Controller
  787. */
  788. public function testValidateHasManyRecordsPass()
  789. {
  790. $event = new Event('Controller.startup', $this->Controller);
  791. $this->Controller->Security->startup($event);
  792. $fields = '8f7d82bf7656cf068822d9bdab109ebed1be1825%3AAddress.0.id%7CAddress.0.primary%7C';
  793. $fields .= 'Address.1.id%7CAddress.1.primary';
  794. $unlocked = '';
  795. $this->Controller->request->data = [
  796. 'Address' => [
  797. 0 => [
  798. 'id' => '123',
  799. 'title' => 'home',
  800. 'first_name' => 'Bilbo',
  801. 'last_name' => 'Baggins',
  802. 'address' => '23 Bag end way',
  803. 'city' => 'the shire',
  804. 'phone' => 'N/A',
  805. 'primary' => '1',
  806. ],
  807. 1 => [
  808. 'id' => '124',
  809. 'title' => 'home',
  810. 'first_name' => 'Frodo',
  811. 'last_name' => 'Baggins',
  812. 'address' => '50 Bag end way',
  813. 'city' => 'the shire',
  814. 'phone' => 'N/A',
  815. 'primary' => '1'
  816. ]
  817. ],
  818. '_Token' => compact('fields', 'unlocked')
  819. ];
  820. $result = $this->Controller->Security->validatePost($this->Controller);
  821. $this->assertTrue($result);
  822. }
  823. /**
  824. * Test that values like Foo.0.1
  825. *
  826. * @return void
  827. * @triggers Controller.startup $this->Controller
  828. */
  829. public function testValidateNestedNumericSets()
  830. {
  831. $event = new Event('Controller.startup', $this->Controller);
  832. $this->Controller->Security->startup($event);
  833. $unlocked = '';
  834. $hashFields = ['TaxonomyData'];
  835. $fields = urlencode(Security::hash('/articles/index' . serialize($hashFields) . $unlocked . Security::salt()));
  836. $this->Controller->request->data = [
  837. 'TaxonomyData' => [
  838. 1 => [[2]],
  839. 2 => [[3]]
  840. ],
  841. '_Token' => compact('fields', 'unlocked')
  842. ];
  843. $result = $this->Controller->Security->validatePost($this->Controller);
  844. $this->assertTrue($result);
  845. }
  846. /**
  847. * testValidateHasManyRecords method
  848. *
  849. * validatePost should fail, hidden fields have been changed.
  850. *
  851. * @return void
  852. * @triggers Controller.startup $this->Controller
  853. */
  854. public function testValidateHasManyRecordsFail()
  855. {
  856. $event = new Event('Controller.startup', $this->Controller);
  857. $this->Controller->Security->startup($event);
  858. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
  859. $fields .= 'Address.1.id%7CAddress.1.primary';
  860. $unlocked = '';
  861. $this->Controller->request->data = [
  862. 'Address' => [
  863. 0 => [
  864. 'id' => '123',
  865. 'title' => 'home',
  866. 'first_name' => 'Bilbo',
  867. 'last_name' => 'Baggins',
  868. 'address' => '23 Bag end way',
  869. 'city' => 'the shire',
  870. 'phone' => 'N/A',
  871. 'primary' => '5',
  872. ],
  873. 1 => [
  874. 'id' => '124',
  875. 'title' => 'home',
  876. 'first_name' => 'Frodo',
  877. 'last_name' => 'Baggins',
  878. 'address' => '50 Bag end way',
  879. 'city' => 'the shire',
  880. 'phone' => 'N/A',
  881. 'primary' => '1'
  882. ]
  883. ],
  884. '_Token' => compact('fields', 'unlocked')
  885. ];
  886. $result = $this->Controller->Security->validatePost($this->Controller);
  887. $this->assertFalse($result);
  888. }
  889. /**
  890. * testFormDisabledFields method
  891. *
  892. * @return void
  893. * @triggers Controller.startup $this->Controller
  894. */
  895. public function testFormDisabledFields()
  896. {
  897. $event = new Event('Controller.startup', $this->Controller);
  898. $this->Controller->Security->startup($event);
  899. $fields = '9da2b3fa2b5b8ac0bfbc1bbce145e58059629125%3An%3A0%3A%7B%7D';
  900. $unlocked = '';
  901. $this->Controller->request->data = [
  902. 'MyModel' => ['name' => 'some data'],
  903. '_Token' => compact('fields', 'unlocked')
  904. ];
  905. $result = $this->Controller->Security->validatePost($this->Controller);
  906. $this->assertFalse($result);
  907. $this->Controller->Security->startup($event);
  908. $this->Controller->Security->config('disabledFields', ['MyModel.name']);
  909. $this->Controller->request->data = [
  910. 'MyModel' => ['name' => 'some data'],
  911. '_Token' => compact('fields', 'unlocked')
  912. ];
  913. $result = $this->Controller->Security->validatePost($this->Controller);
  914. $this->assertTrue($result);
  915. }
  916. /**
  917. * test validatePost with radio buttons
  918. *
  919. * @return void
  920. * @triggers Controller.startup $this->Controller
  921. */
  922. public function testValidatePostRadio()
  923. {
  924. $event = new Event('Controller.startup', $this->Controller);
  925. $this->Controller->Security->startup($event);
  926. $fields = 'c2226a8879c3f4b513691295fc2519a29c44c8bb%3An%3A0%3A%7B%7D';
  927. $unlocked = '';
  928. $this->Controller->request->data = [
  929. '_Token' => compact('fields', 'unlocked')
  930. ];
  931. $result = $this->Controller->Security->validatePost($this->Controller);
  932. $this->assertFalse($result);
  933. $this->Controller->request->data = [
  934. '_Token' => compact('fields', 'unlocked'),
  935. 'Test' => ['test' => '']
  936. ];
  937. $result = $this->Controller->Security->validatePost($this->Controller);
  938. $this->assertTrue($result);
  939. $this->Controller->request->data = [
  940. '_Token' => compact('fields', 'unlocked'),
  941. 'Test' => ['test' => '1']
  942. ];
  943. $result = $this->Controller->Security->validatePost($this->Controller);
  944. $this->assertTrue($result);
  945. $this->Controller->request->data = [
  946. '_Token' => compact('fields', 'unlocked'),
  947. 'Test' => ['test' => '2']
  948. ];
  949. $result = $this->Controller->Security->validatePost($this->Controller);
  950. $this->assertTrue($result);
  951. }
  952. /**
  953. * test validatePost uses here() as a hash input.
  954. *
  955. * @return void
  956. * @triggers Controller.startup $this->Controller
  957. */
  958. public function testValidatePostUrlAsHashInput()
  959. {
  960. $event = new Event('Controller.startup', $this->Controller);
  961. $this->Security->startup($event);
  962. $fields = 'b0914d06dfb04abf1fada53e16810e87d157950b%3A';
  963. $unlocked = '';
  964. $this->Controller->request->data = [
  965. 'Model' => ['username' => '', 'password' => ''],
  966. '_Token' => compact('fields', 'unlocked')
  967. ];
  968. $this->assertTrue($this->Security->validatePost($this->Controller));
  969. $request = $this->getMock('Cake\Network\Request', ['here']);
  970. $request->expects($this->at(0))
  971. ->method('here')
  972. ->will($this->returnValue('/posts/index?page=1'));
  973. $request->expects($this->at(1))
  974. ->method('here')
  975. ->will($this->returnValue('/posts/edit/1'));
  976. $request->data = $this->Controller->request->data;
  977. $this->Controller->request = $request;
  978. $this->assertFalse($this->Security->validatePost($this->Controller));
  979. $this->assertFalse($this->Security->validatePost($this->Controller));
  980. }
  981. /**
  982. * test that blackhole doesn't delete the _Token session key so repeat data submissions
  983. * stay blackholed.
  984. *
  985. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/214
  986. * @return void
  987. * @triggers Controller.startup $this->Controller
  988. */
  989. public function testBlackHoleNotDeletingSessionInformation()
  990. {
  991. $event = new Event('Controller.startup', $this->Controller);
  992. $this->Controller->Security->startup($event);
  993. $this->Controller->Security->blackHole($this->Controller, 'auth');
  994. $this->assertTrue($this->Controller->Security->session->check('_Token'), '_Token was deleted by blackHole %s');
  995. }
  996. /**
  997. * Test generateToken()
  998. *
  999. * @return void
  1000. */
  1001. public function testGenerateToken()
  1002. {
  1003. $request = $this->Controller->request;
  1004. $this->Security->generateToken($request);
  1005. $this->assertNotEmpty($request->params['_Token']);
  1006. $this->assertTrue(isset($request->params['_Token']['unlockedFields']));
  1007. }
  1008. /**
  1009. * Test unlocked actions
  1010. *
  1011. * @return void
  1012. * @triggers Controller.startup $this->Controller
  1013. */
  1014. public function testUnlockedActions()
  1015. {
  1016. $_SERVER['REQUEST_METHOD'] = 'POST';
  1017. $event = new Event('Controller.startup', $this->Controller);
  1018. $this->Controller->request->data = ['data'];
  1019. $this->Controller->Security->unlockedActions = 'index';
  1020. $this->Controller->Security->blackHoleCallback = null;
  1021. $result = $this->Controller->Security->startup($event);
  1022. $this->assertNull($result);
  1023. }
  1024. }