SecurityComponentTest.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Controller\Component;
  17. use Cake\Controller\Component\SecurityComponent;
  18. use Cake\Controller\Controller;
  19. use Cake\Controller\Exception\SecurityException;
  20. use Cake\Core\Configure;
  21. use Cake\Event\Event;
  22. use Cake\Http\Response;
  23. use Cake\Http\ServerRequest;
  24. use Cake\Http\Session;
  25. use Cake\Routing\Router;
  26. use Cake\TestSuite\TestCase;
  27. use Cake\Utility\Security;
  28. /**
  29. * TestSecurityComponent
  30. */
  31. class TestSecurityComponent extends SecurityComponent
  32. {
  33. /**
  34. * validatePost method
  35. *
  36. * @param Controller $controller
  37. * @return bool
  38. */
  39. public function validatePost(Controller $controller): bool
  40. {
  41. return $this->_validatePost($controller);
  42. }
  43. /**
  44. * authRequired method
  45. *
  46. * @param Controller $controller
  47. * @return bool
  48. */
  49. public function authRequired(Controller $controller): bool
  50. {
  51. return $this->_authRequired($controller);
  52. }
  53. }
  54. /**
  55. * SecurityTestController
  56. */
  57. class SecurityTestController extends Controller
  58. {
  59. /**
  60. * failed property
  61. *
  62. * @var bool
  63. */
  64. public $failed = false;
  65. /**
  66. * Used for keeping track of headers in test
  67. *
  68. * @var array
  69. */
  70. public $testHeaders = [];
  71. public function initialize(): void
  72. {
  73. $this->loadComponent('TestSecurity', ['className' => 'Cake\Test\TestCase\Controller\Component\TestSecurityComponent']);
  74. }
  75. /**
  76. * fail method
  77. *
  78. * @return void
  79. */
  80. public function fail(): void
  81. {
  82. $this->failed = true;
  83. }
  84. /**
  85. * redirect method
  86. *
  87. * @param string|array $url
  88. * @param int $status
  89. * @return \Cake\Http\Response|null
  90. */
  91. public function redirect($url, int $status = null): ?Response
  92. {
  93. return $status;
  94. }
  95. /**
  96. * Convenience method for header()
  97. *
  98. * @param string $status
  99. * @return void
  100. */
  101. public function header(string $status): void
  102. {
  103. $this->testHeaders[] = $status;
  104. }
  105. }
  106. /**
  107. * SecurityComponentTest class
  108. *
  109. * @property SecurityComponent Security
  110. * @property SecurityTestController Controller
  111. */
  112. class SecurityComponentTest extends TestCase
  113. {
  114. /**
  115. * SERVER variable backup.
  116. *
  117. * @var array
  118. */
  119. protected $server = [];
  120. /**
  121. * Controller property
  122. *
  123. * @var SecurityTestController
  124. */
  125. public $Controller;
  126. /**
  127. * oldSalt property
  128. *
  129. * @var string
  130. */
  131. public $oldSalt;
  132. /**
  133. * setUp method
  134. *
  135. * Initializes environment state.
  136. *
  137. * @return void
  138. */
  139. public function setUp(): void
  140. {
  141. parent::setUp();
  142. $this->server = $_SERVER;
  143. $session = new Session();
  144. $request = new ServerRequest([
  145. 'url' => '/articles/index',
  146. 'session' => $session,
  147. 'params' => ['controller' => 'articles', 'action' => 'index']
  148. ]);
  149. $this->Controller = new SecurityTestController($request);
  150. $this->Controller->Security = $this->Controller->TestSecurity;
  151. $this->Controller->Security->setConfig('blackHoleCallback', 'fail');
  152. $this->Security = $this->Controller->Security;
  153. Security::setSalt('foo!');
  154. }
  155. /**
  156. *
  157. * Resets environment state.
  158. *
  159. * @return void
  160. */
  161. public function tearDown(): void
  162. {
  163. parent::tearDown();
  164. $_SERVER = $this->server;
  165. unset($this->Controller->Security);
  166. unset($this->Controller->Component);
  167. unset($this->Controller);
  168. }
  169. public function validatePost($expectedException = 'SecurityException', $expectedExceptionMessage = null)
  170. {
  171. try {
  172. return $this->Controller->Security->validatePost($this->Controller);
  173. } catch (SecurityException $ex) {
  174. $this->assertInstanceOf('Cake\\Controller\\Exception\\' . $expectedException, $ex);
  175. $this->assertEquals($expectedExceptionMessage, $ex->getMessage());
  176. return false;
  177. }
  178. }
  179. /**
  180. * testBlackholeWithBrokenCallback method
  181. *
  182. * Test that requests are still blackholed when controller has incorrect
  183. * visibility keyword in the blackhole callback.
  184. *
  185. * @return void
  186. * @triggers Controller.startup $Controller, $this->Controller
  187. */
  188. public function testBlackholeWithBrokenCallback(): void
  189. {
  190. $this->expectException(\Cake\Http\Exception\BadRequestException::class);
  191. $request = new ServerRequest([
  192. 'url' => 'posts/index',
  193. 'session' => new Session(),
  194. 'params' => [
  195. 'controller' => 'posts',
  196. 'action' => 'index'
  197. ]
  198. ]);
  199. $Controller = new \TestApp\Controller\SomePagesController($request);
  200. $event = new Event('Controller.startup', $Controller);
  201. $Security = new SecurityComponent($Controller->components());
  202. $Security->setConfig('blackHoleCallback', '_fail');
  203. $Security->startup($event);
  204. $Security->blackHole($Controller, 'csrf');
  205. }
  206. /**
  207. * testExceptionWhenActionIsBlackholeCallback method
  208. *
  209. * Ensure that directly requesting the blackholeCallback as the controller
  210. * action results in an exception.
  211. *
  212. * @return void
  213. * @triggers Controller.startup $this->Controller
  214. */
  215. public function testExceptionWhenActionIsBlackholeCallback(): void
  216. {
  217. $this->Controller->setRequest($this->Controller->getRequest()
  218. ->withParam('controller', 'posts')
  219. ->withParam('action', 'fail'));
  220. $event = new Event('Controller.startup', $this->Controller);
  221. $this->assertFalse($this->Controller->failed);
  222. $this->Controller->Security->startup($event);
  223. $this->assertTrue($this->Controller->failed, 'Request was blackholed.');
  224. }
  225. /**
  226. * testConstructorSettingProperties method
  227. *
  228. * Test that initialize can set properties.
  229. *
  230. * @return void
  231. */
  232. public function testConstructorSettingProperties(): void
  233. {
  234. $settings = [
  235. 'requireSecure' => ['update_account'],
  236. 'validatePost' => false,
  237. ];
  238. $Security = new SecurityComponent($this->Controller->components(), $settings);
  239. $this->assertEquals($Security->validatePost, $settings['validatePost']);
  240. }
  241. /**
  242. * testStartup method
  243. *
  244. * @return void
  245. * @triggers Controller.startup $this->Controller
  246. */
  247. public function testStartup(): void
  248. {
  249. $event = new Event('Controller.startup', $this->Controller);
  250. $this->Controller->Security->startup($event);
  251. $this->assertTrue($this->Controller->getRequest()->getSession()->check('_Token'));
  252. }
  253. /**
  254. * testRequireSecureFail method
  255. *
  256. * @return void
  257. * @triggers Controller.startup $this->Controller
  258. */
  259. public function testRequireSecureFail(): void
  260. {
  261. $this->Controller->setRequest($this->Controller->getRequest()
  262. ->withParam('action', 'posted')
  263. ->withEnv('HTTPS', 'off')
  264. ->withEnv('REQUEST_METHOD', 'POST'));
  265. $event = new Event('Controller.startup', $this->Controller);
  266. $this->Controller->Security->requireSecure(['posted']);
  267. $this->Controller->Security->startup($event);
  268. $this->assertTrue($this->Controller->failed);
  269. }
  270. /**
  271. * testRequireSecureSucceed method
  272. *
  273. * @return void
  274. * @triggers Controller.startup $this->Controller
  275. */
  276. public function testRequireSecureSucceed(): void
  277. {
  278. $this->Controller->setRequest($this->Controller->getRequest()
  279. ->withParam('action', 'posted')
  280. ->withEnv('HTTPS', 'on')
  281. ->withEnv('REQUEST_METHOD', 'Secure'));
  282. $event = new Event('Controller.startup', $this->Controller);
  283. $this->Controller->Security->requireSecure('posted');
  284. $this->Controller->Security->startup($event);
  285. $this->assertFalse($this->Controller->failed);
  286. }
  287. /**
  288. * testRequireSecureEmptyFail method
  289. *
  290. * @return void
  291. * @triggers Controller.startup $this->Controller
  292. */
  293. public function testRequireSecureEmptyFail(): void
  294. {
  295. $this->Controller->setRequest($this->Controller->getRequest()
  296. ->withParam('action', 'posted')
  297. ->withEnv('HTTPS', 'off')
  298. ->withEnv('REQUEST_METHOD', 'POST'));
  299. $event = new Event('Controller.startup', $this->Controller);
  300. $this->Controller->Security->requireSecure();
  301. $this->Controller->Security->startup($event);
  302. $this->assertTrue($this->Controller->failed);
  303. }
  304. /**
  305. * testRequireSecureEmptySucceed method
  306. *
  307. * @return void
  308. * @triggers Controller.startup $this->Controller
  309. */
  310. public function testRequireSecureEmptySucceed(): void
  311. {
  312. $this->Controller->setRequest($this->Controller->getRequest()
  313. ->withParam('action', 'posted')
  314. ->withEnv('HTTPS', 'on')
  315. ->withEnv('REQUEST_METHOD', 'Secure'));
  316. $event = new Event('Controller.startup', $this->Controller);
  317. $this->Controller->Security->requireSecure();
  318. $this->Controller->Security->startup($event);
  319. $this->assertFalse($this->Controller->failed);
  320. }
  321. /**
  322. * testValidatePost method
  323. *
  324. * Simple hash validation test
  325. *
  326. * @return void
  327. * @triggers Controller.startup $this->Controller
  328. */
  329. public function testValidatePost(): void
  330. {
  331. $event = new Event('Controller.startup', $this->Controller);
  332. $this->Security->startup($event);
  333. $fields = '4697b45f7f430ff3ab73018c20f315eecb0ba5a6%3AModel.valid';
  334. $unlocked = '';
  335. $debug = '';
  336. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  337. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  338. '_Token' => compact('fields', 'unlocked', 'debug'),
  339. ]));
  340. $this->assertTrue($this->validatePost());
  341. }
  342. /**
  343. * testValidatePostOnGetWithData method
  344. *
  345. * Test that validatePost fires on GET with request data.
  346. * This could happen when method overriding is used.
  347. *
  348. * @return void
  349. * @triggers Controller.startup $this->Controller
  350. */
  351. public function testValidatePostOnGetWithData(): void
  352. {
  353. $event = new Event('Controller.startup', $this->Controller);
  354. $this->Security->startup($event);
  355. $fields = 'an-invalid-token';
  356. $unlocked = '';
  357. $debug = urlencode(json_encode([
  358. 'some-action',
  359. [],
  360. []
  361. ]));
  362. $this->Controller->setRequest($this->Controller->getRequest()
  363. ->withEnv('REQUEST_METHOD', 'GET')
  364. ->withData('Model', ['username' => 'nate', 'password' => 'foo', 'valid' => '0'])
  365. ->withData('_Token', compact('fields', 'unlocked', 'debug')));
  366. $this->Security->startup($event);
  367. $this->assertTrue($this->Controller->failed);
  368. }
  369. /**
  370. * testValidatePostNoSession method
  371. *
  372. * Test that validatePost fails if you are missing the session information.
  373. *
  374. * @return void
  375. * @triggers Controller.startup $this->Controller
  376. */
  377. public function testValidatePostNoSession(): void
  378. {
  379. $event = new Event('Controller.startup', $this->Controller);
  380. $this->Security->startup($event);
  381. $this->Controller->getRequest()->getSession()->delete('_Token');
  382. $unlocked = '';
  383. $debug = urlencode(json_encode([
  384. '/articles/index',
  385. [],
  386. []
  387. ]));
  388. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  389. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  390. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  391. '_Token' => compact('fields', 'unlocked', 'debug')
  392. ]));
  393. $this->assertFalse($this->validatePost('AuthSecurityException', 'Unexpected field \'Model.password\' in POST data, Unexpected field \'Model.username\' in POST data'));
  394. }
  395. /**
  396. * testValidatePostNoUnlockedInRequestData method
  397. *
  398. * Test that validatePost fails if you are missing unlocked in request data.
  399. *
  400. * @return void
  401. * @triggers Controller.startup $this->Controller
  402. */
  403. public function testValidatePostNoUnlockedInRequestData(): void
  404. {
  405. $event = new Event('Controller.startup', $this->Controller);
  406. $this->Security->startup($event);
  407. $this->Controller->getRequest()->getSession()->delete('_Token');
  408. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  409. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  410. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  411. '_Token' => compact('fields')
  412. ]));
  413. $this->assertFalse($this->validatePost('AuthSecurityException', '\'_Token.unlocked\' was not found in request data.'));
  414. }
  415. /**
  416. * testValidatePostFormHacking method
  417. *
  418. * Test that validatePost fails if any of its required fields are missing.
  419. *
  420. * @return void
  421. * @triggers Controller.startup $this->Controller
  422. */
  423. public function testValidatePostFormHacking(): void
  424. {
  425. $event = new Event('Controller.startup', $this->Controller);
  426. $this->Security->startup($event);
  427. $unlocked = '';
  428. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  429. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  430. '_Token' => compact('unlocked')
  431. ]));
  432. $result = $this->validatePost('AuthSecurityException', '\'_Token.fields\' was not found in request data.');
  433. $this->assertFalse($result, 'validatePost passed when fields were missing. %s');
  434. }
  435. /**
  436. * testValidatePostEmptyForm method
  437. *
  438. * Test that validatePost fails if empty form is submitted.
  439. *
  440. * @return void
  441. * @triggers Controller.startup $this->Controller
  442. */
  443. public function testValidatePostEmptyForm(): void
  444. {
  445. $this->Controller->setRequest($this->Controller->getRequest()
  446. ->withEnv('REQUEST_METHOD', 'POST')
  447. ->withParsedBody([]));
  448. $event = new Event('Controller.startup', $this->Controller);
  449. $this->Security->startup($event);
  450. $result = $this->validatePost('AuthSecurityException', '\'_Token\' was not found in request data.');
  451. $this->assertFalse($result, 'validatePost passed when empty form is submitted');
  452. }
  453. /**
  454. * testValidatePostObjectDeserialize
  455. *
  456. * Test that objects can't be passed into the serialized string. This was a vector for RFI and LFI
  457. * attacks. Thanks to Felix Wilhelm
  458. *
  459. * @return void
  460. * @triggers Controller.startup $this->Controller
  461. */
  462. public function testValidatePostObjectDeserialize(): void
  463. {
  464. $event = new Event('Controller.startup', $this->Controller);
  465. $this->Security->startup($event);
  466. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877';
  467. $unlocked = '';
  468. $debug = urlencode(json_encode([
  469. '/articles/index',
  470. ['Model.password', 'Model.username', 'Model.valid'],
  471. []
  472. ]));
  473. // a corrupted serialized object, so we can see if it ever gets to deserialize
  474. $attack = 'O:3:"App":1:{s:5:"__map";a:1:{s:3:"foo";s:7:"Hacked!";s:1:"fail"}}';
  475. $fields .= urlencode(':' . str_rot13($attack));
  476. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  477. 'Model' => ['username' => 'mark', 'password' => 'foo', 'valid' => '0'],
  478. '_Token' => compact('fields', 'unlocked', 'debug')
  479. ]));
  480. $result = $this->validatePost('SecurityException', 'Bad Request');
  481. $this->assertFalse($result, 'validatePost passed when key was missing. %s');
  482. }
  483. /**
  484. * testValidatePostIgnoresCsrfToken method
  485. *
  486. * Tests validation post data ignores `_csrfToken`.
  487. *
  488. * @return void
  489. * @triggers Controller.startup $this->Controller
  490. */
  491. public function testValidatePostIgnoresCsrfToken(): void
  492. {
  493. $event = new Event('Controller.startup', $this->Controller);
  494. $this->Security->startup($event);
  495. $fields = 'f95b472a63f1d883b9eaacaf8a8e36e325e3fe82%3A';
  496. $unlocked = '';
  497. $debug = 'not used';
  498. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  499. '_csrfToken' => 'abc123',
  500. 'Model' => ['multi_field' => ['1', '3']],
  501. '_Token' => compact('fields', 'unlocked', 'debug')
  502. ]));
  503. $this->assertTrue($this->validatePost());
  504. }
  505. /**
  506. * testValidatePostArray method
  507. *
  508. * Tests validation of checkbox arrays.
  509. *
  510. * @return void
  511. * @triggers Controller.startup $this->Controller
  512. */
  513. public function testValidatePostArray(): void
  514. {
  515. $event = new Event('Controller.startup', $this->Controller);
  516. $this->Security->startup($event);
  517. $fields = 'f95b472a63f1d883b9eaacaf8a8e36e325e3fe82%3A';
  518. $unlocked = '';
  519. $debug = urlencode(json_encode([
  520. 'some-action',
  521. [],
  522. []
  523. ]));
  524. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  525. 'Model' => ['multi_field' => ['1', '3']],
  526. '_Token' => compact('fields', 'unlocked', 'debug')
  527. ]));
  528. $this->assertTrue($this->validatePost());
  529. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  530. 'Model' => ['multi_field' => [12 => '1', 20 => '3']],
  531. '_Token' => compact('fields', 'unlocked', 'debug')
  532. ]));
  533. $this->assertTrue($this->validatePost());
  534. }
  535. /**
  536. * testValidateIntFieldName method
  537. *
  538. * Tests validation of integer field names.
  539. *
  540. * @return void
  541. */
  542. public function testValidateIntFieldName(): void
  543. {
  544. $event = new Event('Controller.startup', $this->Controller);
  545. $this->Security->startup($event);
  546. $fields = '11f87a5962db9ac26405e460cd3063bb6ff76cf8%3A';
  547. $unlocked = '';
  548. $debug = urlencode(json_encode([
  549. 'some-action',
  550. [],
  551. []
  552. ]));
  553. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  554. 1 => 'value,',
  555. '_Token' => compact('fields', 'unlocked', 'debug')
  556. ]));
  557. $this->assertTrue($this->validatePost());
  558. }
  559. /**
  560. * testValidatePostNoModel method
  561. *
  562. * @return void
  563. * @triggers Controller.startup $this->Controller
  564. */
  565. public function testValidatePostNoModel(): void
  566. {
  567. $event = new Event('Controller.startup', $this->Controller);
  568. $this->Security->startup($event);
  569. $fields = 'a2a942f587deb20e90241c51b59d901d8a7f796b%3A';
  570. $unlocked = '';
  571. $debug = 'not used';
  572. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  573. 'anything' => 'some_data',
  574. '_Token' => compact('fields', 'unlocked', 'debug')
  575. ]));
  576. $result = $this->validatePost();
  577. $this->assertTrue($result);
  578. }
  579. /**
  580. * testValidatePostSimple method
  581. *
  582. * @return void
  583. * @triggers Controller.startup $this->Controller
  584. */
  585. public function testValidatePostSimple(): void
  586. {
  587. $event = new Event('Controller.startup', $this->Controller);
  588. $this->Security->startup($event);
  589. $fields = 'de2ca3670dd06c29558dd98482c8739e86da2c7c%3A';
  590. $unlocked = '';
  591. $debug = 'not used';
  592. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  593. 'Model' => ['username' => '', 'password' => ''],
  594. '_Token' => compact('fields', 'unlocked', 'debug')
  595. ]));
  596. $result = $this->validatePost();
  597. $this->assertTrue($result);
  598. }
  599. /**
  600. * test validatePost uses full URL
  601. *
  602. * @return void
  603. * @triggers Controller.startup $this->Controller
  604. */
  605. public function testValidatePostSubdirectory(): void
  606. {
  607. // set the base path.
  608. $this->Controller->setRequest($this->Controller->getRequest()
  609. ->withAttribute('base', 'subdir')
  610. ->withAttributE('webroot', 'subdir/'));
  611. Router::pushRequest($this->Controller->getRequest());
  612. $event = new Event('Controller.startup', $this->Controller);
  613. $this->Security->startup($event);
  614. // Differs from testValidatePostSimple because of base url
  615. $fields = 'cc9b6af3f33147235ae8f8037b0a71399a2425f2%3A';
  616. $unlocked = '';
  617. $debug = '';
  618. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  619. 'Model' => ['username' => '', 'password' => ''],
  620. '_Token' => compact('fields', 'unlocked', 'debug')
  621. ]));
  622. $result = $this->validatePost();
  623. $this->assertTrue($result);
  624. }
  625. /**
  626. * testValidatePostComplex method
  627. *
  628. * Tests hash validation for multiple records, including locked fields.
  629. *
  630. * @return void
  631. * @triggers Controller.startup $this->Controller
  632. */
  633. public function testValidatePostComplex(): void
  634. {
  635. $event = new Event('Controller.startup', $this->Controller);
  636. $this->Security->startup($event);
  637. $fields = 'b00b7e5c2e3bf8bc474fb7cfde6f9c2aa06ab9bc%3AAddresses.0.id%7CAddresses.1.id';
  638. $unlocked = '';
  639. $debug = 'not used';
  640. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  641. 'Addresses' => [
  642. '0' => [
  643. 'id' => '123456', 'title' => '', 'first_name' => '', 'last_name' => '',
  644. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  645. ],
  646. '1' => [
  647. 'id' => '654321', 'title' => '', 'first_name' => '', 'last_name' => '',
  648. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  649. ]
  650. ],
  651. '_Token' => compact('fields', 'unlocked', 'debug')
  652. ]));
  653. $result = $this->validatePost();
  654. $this->assertTrue($result);
  655. }
  656. /**
  657. * testValidatePostMultipleSelect method
  658. *
  659. * Test ValidatePost with multiple select elements.
  660. *
  661. * @return void
  662. * @triggers Controller.startup $this->Controller
  663. */
  664. public function testValidatePostMultipleSelect(): void
  665. {
  666. $event = new Event('Controller.startup', $this->Controller);
  667. $this->Security->startup($event);
  668. $fields = '28dd05f0af314050784b18b3366857e8e8c78e73%3A';
  669. $unlocked = '';
  670. $debug = 'not used';
  671. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  672. 'Tag' => ['Tag' => [1, 2]],
  673. '_Token' => compact('fields', 'unlocked', 'debug'),
  674. ]));
  675. $result = $this->validatePost();
  676. $this->assertTrue($result);
  677. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  678. 'Tag' => ['Tag' => [1, 2, 3]],
  679. '_Token' => compact('fields', 'unlocked', 'debug'),
  680. ]));
  681. $result = $this->validatePost();
  682. $this->assertTrue($result);
  683. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  684. 'Tag' => ['Tag' => [1, 2, 3, 4]],
  685. '_Token' => compact('fields', 'unlocked', 'debug'),
  686. ]));
  687. $result = $this->validatePost();
  688. $this->assertTrue($result);
  689. $fields = '1e4c9269b64756e9b141d364497c5f037b428a37%3A';
  690. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  691. 'User.password' => 'bar', 'User.name' => 'foo', 'User.is_valid' => '1',
  692. 'Tag' => ['Tag' => [1]],
  693. '_Token' => compact('fields', 'unlocked', 'debug'),
  694. ]));
  695. $result = $this->validatePost();
  696. $this->assertTrue($result);
  697. }
  698. /**
  699. * testValidatePostCheckbox method
  700. *
  701. * First block tests un-checked checkbox
  702. * Second block tests checked checkbox
  703. *
  704. * @return void
  705. * @triggers Controller.startup $this->Controller
  706. */
  707. public function testValidatePostCheckbox(): void
  708. {
  709. $event = new Event('Controller.startup', $this->Controller);
  710. $this->Security->startup($event);
  711. $fields = '4697b45f7f430ff3ab73018c20f315eecb0ba5a6%3AModel.valid';
  712. $unlocked = '';
  713. $debug = 'not used';
  714. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  715. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  716. '_Token' => compact('fields', 'unlocked', 'debug'),
  717. ]));
  718. $result = $this->validatePost();
  719. $this->assertTrue($result);
  720. $fields = '3f368401f9a8610bcace7746039651066cdcdc38%3A';
  721. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  722. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  723. '_Token' => compact('fields', 'unlocked', 'debug'),
  724. ]));
  725. $result = $this->validatePost();
  726. $this->assertTrue($result);
  727. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([]));
  728. $this->Security->startup($event);
  729. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  730. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  731. '_Token' => compact('fields', 'unlocked', 'debug'),
  732. ]));
  733. $result = $this->validatePost();
  734. $this->assertTrue($result);
  735. }
  736. /**
  737. * testValidatePostHidden method
  738. *
  739. * @return void
  740. * @triggers Controller.startup $this->Controller
  741. */
  742. public function testValidatePostHidden(): void
  743. {
  744. $event = new Event('Controller.startup', $this->Controller);
  745. $this->Security->startup($event);
  746. $fields = '96e61bded2b62b0c420116a0eb06a3b3acddb8f1%3AModel.hidden%7CModel.other_hidden';
  747. $unlocked = '';
  748. $debug = 'not used';
  749. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  750. 'Model' => [
  751. 'username' => '', 'password' => '', 'hidden' => '0',
  752. 'other_hidden' => 'some hidden value'
  753. ],
  754. '_Token' => compact('fields', 'unlocked', 'debug'),
  755. ]));
  756. $result = $this->validatePost();
  757. $this->assertTrue($result);
  758. }
  759. /**
  760. * testValidatePostWithDisabledFields method
  761. *
  762. * @return void
  763. * @triggers Controller.startup $this->Controller
  764. */
  765. public function testValidatePostWithDisabledFields(): void
  766. {
  767. $event = new Event('Controller.startup', $this->Controller);
  768. $this->Security->setConfig('disabledFields', ['Model.username', 'Model.password']);
  769. $this->Security->startup($event);
  770. $fields = '0313460e1136e1227044399fe10a6edc0cbca279%3AModel.hidden';
  771. $unlocked = '';
  772. $debug = 'not used';
  773. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  774. 'Model' => [
  775. 'username' => '', 'password' => '', 'hidden' => '0'
  776. ],
  777. '_Token' => compact('fields', 'unlocked', 'debug'),
  778. ]));
  779. $result = $this->validatePost();
  780. $this->assertTrue($result);
  781. }
  782. /**
  783. * testValidatePostDisabledFieldsInData method
  784. *
  785. * Test validating post data with posted unlocked fields.
  786. *
  787. * @return void
  788. * @triggers Controller.startup $this->Controller
  789. */
  790. public function testValidatePostDisabledFieldsInData(): void
  791. {
  792. $event = new Event('Controller.startup', $this->Controller);
  793. $this->Security->startup($event);
  794. $unlocked = 'Model.username';
  795. $fields = ['Model.hidden', 'Model.password'];
  796. $fields = urlencode(
  797. hash_hmac('sha1', '/articles/index' . serialize($fields) . $unlocked . 'cli', Security::getSalt())
  798. );
  799. $debug = 'not used';
  800. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  801. 'Model' => [
  802. 'username' => 'mark',
  803. 'password' => 'sekret',
  804. 'hidden' => '0'
  805. ],
  806. '_Token' => compact('fields', 'unlocked', 'debug'),
  807. ]));
  808. $result = $this->validatePost();
  809. $this->assertTrue($result);
  810. }
  811. /**
  812. * testValidatePostFailNoDisabled method
  813. *
  814. * Test that missing 'unlocked' input causes failure.
  815. *
  816. * @return void
  817. * @triggers Controller.startup $this->Controller
  818. */
  819. public function testValidatePostFailNoDisabled(): void
  820. {
  821. $event = new Event('Controller.startup', $this->Controller);
  822. $this->Security->startup($event);
  823. $fields = ['Model.hidden', 'Model.password', 'Model.username'];
  824. $fields = urlencode(Security::hash(serialize($fields) . Security::getSalt()));
  825. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  826. 'Model' => [
  827. 'username' => 'mark',
  828. 'password' => 'sekret',
  829. 'hidden' => '0'
  830. ],
  831. '_Token' => compact('fields')
  832. ]));
  833. $result = $this->validatePost('SecurityException', '\'_Token.unlocked\' was not found in request data.');
  834. $this->assertFalse($result);
  835. }
  836. /**
  837. * testValidatePostFailNoDebug method
  838. *
  839. * Test that missing 'debug' input causes failure.
  840. *
  841. * @return void
  842. * @triggers Controller.startup $this->Controller
  843. */
  844. public function testValidatePostFailNoDebug(): void
  845. {
  846. $event = new Event('Controller.startup', $this->Controller);
  847. $this->Security->startup($event);
  848. $fields = ['Model.hidden', 'Model.password', 'Model.username'];
  849. $fields = urlencode(Security::hash(serialize($fields) . Security::getSalt()));
  850. $unlocked = '';
  851. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  852. 'Model' => [
  853. 'username' => 'mark',
  854. 'password' => 'sekret',
  855. 'hidden' => '0'
  856. ],
  857. '_Token' => compact('fields', 'unlocked')
  858. ]));
  859. $result = $this->validatePost('SecurityException', '\'_Token.debug\' was not found in request data.');
  860. $this->assertFalse($result);
  861. }
  862. /**
  863. * testValidatePostFailNoDebugMode method
  864. *
  865. * Test that missing 'debug' input is not the problem when debug mode disabled.
  866. *
  867. * @return void
  868. * @triggers Controller.startup $this->Controller
  869. */
  870. public function testValidatePostFailNoDebugMode(): void
  871. {
  872. $event = new Event('Controller.startup', $this->Controller);
  873. $this->Security->startup($event);
  874. $fields = ['Model.hidden', 'Model.password', 'Model.username'];
  875. $fields = urlencode(Security::hash(serialize($fields) . Security::getSalt()));
  876. $unlocked = '';
  877. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  878. 'Model' => [
  879. 'username' => 'mark',
  880. 'password' => 'sekret',
  881. 'hidden' => '0'
  882. ],
  883. '_Token' => compact('fields', 'unlocked')
  884. ]));
  885. Configure::write('debug', false);
  886. $result = $this->validatePost('SecurityException', 'The request has been black-holed');
  887. }
  888. /**
  889. * testValidatePostFailDisabledFieldTampering method
  890. *
  891. * Test that validatePost fails when unlocked fields are changed.
  892. *
  893. * @return void
  894. * @triggers Controller.startup $this->Controller
  895. */
  896. public function testValidatePostFailDisabledFieldTampering(): void
  897. {
  898. $event = new Event('Controller.startup', $this->Controller);
  899. $this->Security->startup($event);
  900. $unlocked = 'Model.username';
  901. $fields = ['Model.hidden', 'Model.password'];
  902. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::getSalt()));
  903. $debug = urlencode(json_encode([
  904. '/articles/index',
  905. ['Model.hidden', 'Model.password'],
  906. ['Model.username']
  907. ]));
  908. // Tamper the values.
  909. $unlocked = 'Model.username|Model.password';
  910. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  911. 'Model' => [
  912. 'username' => 'mark',
  913. 'password' => 'sekret',
  914. 'hidden' => '0'
  915. ],
  916. '_Token' => compact('fields', 'unlocked', 'debug')
  917. ]));
  918. $result = $this->validatePost('SecurityException', 'Missing field \'Model.password\' in POST data, Unexpected unlocked field \'Model.password\' in POST data');
  919. $this->assertFalse($result);
  920. }
  921. /**
  922. * testValidateHiddenMultipleModel method
  923. *
  924. * @return void
  925. * @triggers Controller.startup $this->Controller
  926. */
  927. public function testValidateHiddenMultipleModel(): void
  928. {
  929. $event = new Event('Controller.startup', $this->Controller);
  930. $this->Security->startup($event);
  931. $fields = '642b7a6db3b848fab88952b86ea36c572f93df40%3AModel.valid%7CModel2.valid%7CModel3.valid';
  932. $unlocked = '';
  933. $debug = 'not used';
  934. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  935. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  936. 'Model2' => ['valid' => '0'],
  937. 'Model3' => ['valid' => '0'],
  938. '_Token' => compact('fields', 'unlocked', 'debug'),
  939. ]));
  940. $result = $this->validatePost();
  941. $this->assertTrue($result);
  942. }
  943. /**
  944. * testValidateHasManyModel method
  945. *
  946. * @return void
  947. * @triggers Controller.startup $this->Controller
  948. */
  949. public function testValidateHasManyModel(): void
  950. {
  951. $event = new Event('Controller.startup', $this->Controller);
  952. $this->Security->startup($event);
  953. $fields = '792324c8a374772ad82acfb28f0e77e70f8ed3af%3AModel.0.hidden%7CModel.0.valid';
  954. $fields .= '%7CModel.1.hidden%7CModel.1.valid';
  955. $unlocked = '';
  956. $debug = 'not used';
  957. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  958. 'Model' => [
  959. [
  960. 'username' => 'username', 'password' => 'password',
  961. 'hidden' => 'value', 'valid' => '0'
  962. ],
  963. [
  964. 'username' => 'username', 'password' => 'password',
  965. 'hidden' => 'value', 'valid' => '0'
  966. ]
  967. ],
  968. '_Token' => compact('fields', 'unlocked', 'debug'),
  969. ]));
  970. $result = $this->validatePost();
  971. $this->assertTrue($result);
  972. }
  973. /**
  974. * testValidateHasManyRecordsPass method
  975. *
  976. * @return void
  977. * @triggers Controller.startup $this->Controller
  978. */
  979. public function testValidateHasManyRecordsPass(): void
  980. {
  981. $event = new Event('Controller.startup', $this->Controller);
  982. $this->Security->startup($event);
  983. $fields = '7f4bff67558e25ebeea44c84ea4befa8d50b080c%3AAddress.0.id%7CAddress.0.primary%7C';
  984. $fields .= 'Address.1.id%7CAddress.1.primary';
  985. $unlocked = '';
  986. $debug = 'not used';
  987. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  988. 'Address' => [
  989. 0 => [
  990. 'id' => '123',
  991. 'title' => 'home',
  992. 'first_name' => 'Bilbo',
  993. 'last_name' => 'Baggins',
  994. 'address' => '23 Bag end way',
  995. 'city' => 'the shire',
  996. 'phone' => 'N/A',
  997. 'primary' => '1',
  998. ],
  999. 1 => [
  1000. 'id' => '124',
  1001. 'title' => 'home',
  1002. 'first_name' => 'Frodo',
  1003. 'last_name' => 'Baggins',
  1004. 'address' => '50 Bag end way',
  1005. 'city' => 'the shire',
  1006. 'phone' => 'N/A',
  1007. 'primary' => '1'
  1008. ]
  1009. ],
  1010. '_Token' => compact('fields', 'unlocked', 'debug'),
  1011. ]));
  1012. $result = $this->validatePost();
  1013. $this->assertTrue($result);
  1014. }
  1015. /**
  1016. * testValidateNestedNumericSets method
  1017. *
  1018. * Test that values like Foo.0.1
  1019. *
  1020. * @return void
  1021. * @triggers Controller.startup $this->Controller
  1022. */
  1023. public function testValidateNestedNumericSets(): void
  1024. {
  1025. $event = new Event('Controller.startup', $this->Controller);
  1026. $this->Security->startup($event);
  1027. $unlocked = '';
  1028. $hashFields = ['TaxonomyData'];
  1029. $fields = urlencode(
  1030. hash_hmac('sha1', '/articles/index' . serialize($hashFields) . $unlocked . 'cli', Security::getSalt())
  1031. );
  1032. $debug = 'not used';
  1033. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1034. 'TaxonomyData' => [
  1035. 1 => [[2]],
  1036. 2 => [[3]]
  1037. ],
  1038. '_Token' => compact('fields', 'unlocked', 'debug'),
  1039. ]));
  1040. $result = $this->validatePost();
  1041. $this->assertTrue($result);
  1042. }
  1043. /**
  1044. * testValidateHasManyRecords method
  1045. *
  1046. * validatePost should fail, hidden fields have been changed.
  1047. *
  1048. * @return void
  1049. * @triggers Controller.startup $this->Controller
  1050. */
  1051. public function testValidateHasManyRecordsFail(): void
  1052. {
  1053. $event = new Event('Controller.startup', $this->Controller);
  1054. $this->Security->startup($event);
  1055. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
  1056. $fields .= 'Address.1.id%7CAddress.1.primary';
  1057. $unlocked = '';
  1058. $debug = urlencode(json_encode([
  1059. '/articles/index',
  1060. [
  1061. 'Address.0.address',
  1062. 'Address.0.city',
  1063. 'Address.0.first_name',
  1064. 'Address.0.last_name',
  1065. 'Address.0.phone',
  1066. 'Address.0.title',
  1067. 'Address.1.address',
  1068. 'Address.1.city',
  1069. 'Address.1.first_name',
  1070. 'Address.1.last_name',
  1071. 'Address.1.phone',
  1072. 'Address.1.title',
  1073. 'Address.0.id' => '123',
  1074. 'Address.0.primary' => '5',
  1075. 'Address.1.id' => '124',
  1076. 'Address.1.primary' => '1'
  1077. ],
  1078. []
  1079. ]));
  1080. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1081. 'Address' => [
  1082. 0 => [
  1083. 'id' => '123',
  1084. 'title' => 'home',
  1085. 'first_name' => 'Bilbo',
  1086. 'last_name' => 'Baggins',
  1087. 'address' => '23 Bag end way',
  1088. 'city' => 'the shire',
  1089. 'phone' => 'N/A',
  1090. 'primary' => '5',
  1091. ],
  1092. 1 => [
  1093. 'id' => '124',
  1094. 'title' => 'home',
  1095. 'first_name' => 'Frodo',
  1096. 'last_name' => 'Baggins',
  1097. 'address' => '50 Bag end way',
  1098. 'city' => 'the shire',
  1099. 'phone' => 'N/A',
  1100. 'primary' => '1'
  1101. ]
  1102. ],
  1103. '_Token' => compact('fields', 'unlocked', 'debug'),
  1104. ]));
  1105. $result = $this->validatePost('SecurityException', 'Bad Request');
  1106. $this->assertFalse($result);
  1107. }
  1108. /**
  1109. * testFormDisabledFields method
  1110. *
  1111. * @return void
  1112. * @triggers Controller.startup $this->Controller
  1113. */
  1114. public function testFormDisabledFields(): void
  1115. {
  1116. $event = new Event('Controller.startup', $this->Controller);
  1117. $this->Security->startup($event);
  1118. $fields = '4eaf5c6b93d5140c24171d5fdce16ed5b904f288%3An%3A0%3A%7B%7D';
  1119. $unlocked = '';
  1120. $debug = urlencode(json_encode([
  1121. '/articles/index',
  1122. [],
  1123. []
  1124. ]));
  1125. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1126. 'MyModel' => ['name' => 'some data'],
  1127. '_Token' => compact('fields', 'unlocked', 'debug'),
  1128. ]));
  1129. $result = $this->validatePost('SecurityException', 'Unexpected field \'MyModel.name\' in POST data');
  1130. $this->assertFalse($result);
  1131. $this->Security->startup($event);
  1132. $this->Security->setConfig('disabledFields', ['MyModel.name']);
  1133. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1134. 'MyModel' => ['name' => 'some data'],
  1135. '_Token' => compact('fields', 'unlocked', 'debug'),
  1136. ]));
  1137. $result = $this->validatePost();
  1138. $this->assertTrue($result);
  1139. }
  1140. /**
  1141. * testValidatePostRadio method
  1142. *
  1143. * Test validatePost with radio buttons.
  1144. *
  1145. * @return void
  1146. * @triggers Controller.startup $this->Controller
  1147. */
  1148. public function testValidatePostRadio(): void
  1149. {
  1150. $event = new Event('Controller.startup', $this->Controller);
  1151. $this->Security->startup($event);
  1152. $fields = 'a709dfdee0a0cce52c4c964a1b8a56159bb081b4%3An%3A0%3A%7B%7D';
  1153. $unlocked = '';
  1154. $debug = urlencode(json_encode([
  1155. '/articles/index',
  1156. [],
  1157. []
  1158. ]));
  1159. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1160. '_Token' => compact('fields', 'unlocked', 'debug'),
  1161. ]));
  1162. $result = $this->validatePost('SecurityException', 'Bad Request');
  1163. $this->assertFalse($result);
  1164. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1165. '_Token' => compact('fields', 'unlocked', 'debug'),
  1166. 'Test' => ['test' => '']
  1167. ]));
  1168. $result = $this->validatePost();
  1169. $this->assertTrue($result);
  1170. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1171. '_Token' => compact('fields', 'unlocked', 'debug'),
  1172. 'Test' => ['test' => '1']
  1173. ]));
  1174. $result = $this->validatePost();
  1175. $this->assertTrue($result);
  1176. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1177. '_Token' => compact('fields', 'unlocked', 'debug'),
  1178. 'Test' => ['test' => '2']
  1179. ]));
  1180. $result = $this->validatePost();
  1181. $this->assertTrue($result);
  1182. }
  1183. /**
  1184. * testValidatePostUrlAsHashInput method
  1185. *
  1186. * Test validatePost uses here() as a hash input.
  1187. *
  1188. * @return void
  1189. * @triggers Controller.startup $this->Controller
  1190. */
  1191. public function testValidatePostUrlAsHashInput(): void
  1192. {
  1193. $event = new Event('Controller.startup', $this->Controller);
  1194. $this->Security->startup($event);
  1195. $fields = 'de2ca3670dd06c29558dd98482c8739e86da2c7c%3A';
  1196. $unlocked = '';
  1197. $debug = urlencode(json_encode([
  1198. 'another-url',
  1199. ['Model.username', 'Model.password'],
  1200. []
  1201. ]));
  1202. $this->Controller->setRequest($this->Controller->getRequest()
  1203. ->withData('Model', ['username' => '', 'password' => ''])
  1204. ->withData('_Token', compact('fields', 'unlocked', 'debug')));
  1205. $this->assertTrue($this->validatePost());
  1206. $this->Controller->setRequest($this->Controller->getRequest()
  1207. ->withRequestTarget('/posts/index?page=1'));
  1208. $this->assertFalse($this->validatePost(
  1209. 'SecurityException',
  1210. 'URL mismatch in POST data (expected \'another-url\' but found \'/posts/index?page=1\')'
  1211. ));
  1212. $this->Controller->setRequest($this->Controller->getRequest()
  1213. ->withRequestTarget('/posts/edit/1'));
  1214. $this->assertFalse($this->validatePost(
  1215. 'SecurityException',
  1216. 'URL mismatch in POST data (expected \'another-url\' but found \'/posts/edit/1\')'
  1217. ));
  1218. }
  1219. /**
  1220. * testBlackHoleNotDeletingSessionInformation method
  1221. *
  1222. * Test that blackhole doesn't delete the _Token session key so repeat data submissions
  1223. * stay blackholed.
  1224. *
  1225. * @return void
  1226. * @triggers Controller.startup $this->Controller
  1227. */
  1228. public function testBlackHoleNotDeletingSessionInformation(): void
  1229. {
  1230. $event = new Event('Controller.startup', $this->Controller);
  1231. $this->Security->startup($event);
  1232. $this->Security->blackHole($this->Controller, 'auth');
  1233. $this->assertTrue(
  1234. $this->Controller->getRequest()->getSession()->check('_Token'),
  1235. '_Token was deleted by blackHole %s'
  1236. );
  1237. }
  1238. /**
  1239. * testGenerateToken method
  1240. *
  1241. * Test generateToken().
  1242. *
  1243. * @return void
  1244. */
  1245. public function testGenerateToken(): void
  1246. {
  1247. $request = $this->Controller->getRequest();
  1248. $request = $this->Security->generateToken($request);
  1249. $this->assertNotEmpty($request->getParam('_Token'));
  1250. $this->assertSame([], $request->getParam('_Token.unlockedFields'));
  1251. }
  1252. /**
  1253. * testUnlockedActions method
  1254. *
  1255. * Test unlocked actions.
  1256. *
  1257. * @return void
  1258. * @triggers Controller.startup $this->Controller
  1259. */
  1260. public function testUnlockedActions(): void
  1261. {
  1262. $_SERVER['REQUEST_METHOD'] = 'POST';
  1263. $event = new Event('Controller.startup', $this->Controller);
  1264. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody(['data']));
  1265. $this->Security->unlockedActions = 'index';
  1266. $this->Security->blackHoleCallback = null;
  1267. $result = $this->Controller->Security->startup($event);
  1268. $this->assertNull($result);
  1269. }
  1270. /**
  1271. * testValidatePostDebugFormat method
  1272. *
  1273. * Test that debug token format is right.
  1274. *
  1275. * @return void
  1276. * @triggers Controller.startup $this->Controller
  1277. */
  1278. public function testValidatePostDebugFormat(): void
  1279. {
  1280. $event = new Event('Controller.startup', $this->Controller);
  1281. $this->Security->startup($event);
  1282. $unlocked = 'Model.username';
  1283. $fields = ['Model.hidden', 'Model.password'];
  1284. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::getSalt()));
  1285. $debug = urlencode(json_encode([
  1286. '/articles/index',
  1287. ['Model.hidden', 'Model.password'],
  1288. ['Model.username'],
  1289. ['not expected']
  1290. ]));
  1291. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1292. 'Model' => [
  1293. 'username' => 'mark',
  1294. 'password' => 'sekret',
  1295. 'hidden' => '0'
  1296. ],
  1297. '_Token' => compact('fields', 'unlocked', 'debug')
  1298. ]));
  1299. $result = $this->validatePost('SecurityException', 'Invalid security debug token.');
  1300. $this->assertFalse($result);
  1301. $debug = urlencode(json_encode('not an array'));
  1302. $result = $this->validatePost('SecurityException', 'Invalid security debug token.');
  1303. $this->assertFalse($result);
  1304. }
  1305. /**
  1306. * testBlackholeThrowsException method
  1307. *
  1308. * Test blackhole will now throw passed exception if debug enabled.
  1309. *
  1310. * @return void
  1311. */
  1312. public function testBlackholeThrowsException(): void
  1313. {
  1314. $this->expectException(\Cake\Controller\Exception\SecurityException::class);
  1315. $this->expectExceptionMessage('error description');
  1316. $this->Security->setConfig('blackHoleCallback', '');
  1317. $this->Security->blackHole($this->Controller, 'auth', new SecurityException('error description'));
  1318. }
  1319. /**
  1320. * testBlackholeThrowsBadRequest method
  1321. *
  1322. * Test blackhole will throw BadRequest if debug disabled.
  1323. *
  1324. * @return void
  1325. */
  1326. public function testBlackholeThrowsBadRequest(): void
  1327. {
  1328. $this->Security->setConfig('blackHoleCallback', '');
  1329. $message = '';
  1330. Configure::write('debug', false);
  1331. try {
  1332. $this->Security->blackHole($this->Controller, 'auth', new SecurityException('error description'));
  1333. } catch (SecurityException $ex) {
  1334. $message = $ex->getMessage();
  1335. $reason = $ex->getReason();
  1336. }
  1337. $this->assertEquals('The request has been black-holed', $message);
  1338. $this->assertEquals('error description', $reason);
  1339. }
  1340. /**
  1341. * testValidatePostFailTampering method
  1342. *
  1343. * Test that validatePost fails with tampered fields and explanation.
  1344. *
  1345. * @return void
  1346. * @triggers Controller.startup $this->Controller
  1347. */
  1348. public function testValidatePostFailTampering(): void
  1349. {
  1350. $event = new Event('Controller.startup', $this->Controller);
  1351. $this->Security->startup($event);
  1352. $unlocked = '';
  1353. $fields = ['Model.hidden' => 'value', 'Model.id' => '1'];
  1354. $debug = urlencode(json_encode([
  1355. '/articles/index',
  1356. $fields,
  1357. []
  1358. ]));
  1359. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::getSalt()));
  1360. $fields .= urlencode(':Model.hidden|Model.id');
  1361. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1362. 'Model' => [
  1363. 'hidden' => 'tampered',
  1364. 'id' => '1',
  1365. ],
  1366. '_Token' => compact('fields', 'unlocked', 'debug')
  1367. ]));
  1368. $result = $this->validatePost('SecurityException', 'Tampered field \'Model.hidden\' in POST data (expected value \'value\' but found \'tampered\')');
  1369. $this->assertFalse($result);
  1370. }
  1371. /**
  1372. * testValidatePostFailTamperingMutatedIntoArray method
  1373. *
  1374. * Test that validatePost fails with tampered fields and explanation.
  1375. *
  1376. * @return void
  1377. * @triggers Controller.startup $this->Controller
  1378. */
  1379. public function testValidatePostFailTamperingMutatedIntoArray(): void
  1380. {
  1381. $event = new Event('Controller.startup', $this->Controller);
  1382. $this->Security->startup($event);
  1383. $unlocked = '';
  1384. $fields = ['Model.hidden' => 'value', 'Model.id' => '1'];
  1385. $debug = urlencode(json_encode([
  1386. '/articles/index',
  1387. $fields,
  1388. []
  1389. ]));
  1390. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::getSalt()));
  1391. $fields .= urlencode(':Model.hidden|Model.id');
  1392. $this->Controller->setRequest($this->Controller->getRequest()->withData('Model', [
  1393. 'hidden' => ['some-key' => 'some-value'],
  1394. 'id' => '1',
  1395. ])->withData('_Token', compact('fields', 'unlocked', 'debug')));
  1396. $result = $this->validatePost(
  1397. 'SecurityException',
  1398. 'Unexpected field \'Model.hidden.some-key\' in POST data, Missing field \'Model.hidden\' in POST data'
  1399. );
  1400. $this->assertFalse($result);
  1401. }
  1402. /**
  1403. * testValidatePostUnexpectedDebugToken method
  1404. *
  1405. * Test that debug token should not be sent if debug is disabled.
  1406. *
  1407. * @return void
  1408. * @triggers Controller.startup $this->Controller
  1409. */
  1410. public function testValidatePostUnexpectedDebugToken(): void
  1411. {
  1412. $event = new Event('Controller.startup', $this->Controller);
  1413. $this->Security->startup($event);
  1414. $unlocked = '';
  1415. $fields = ['Model.hidden' => 'value', 'Model.id' => '1'];
  1416. $debug = urlencode(json_encode([
  1417. '/articles/index',
  1418. $fields,
  1419. []
  1420. ]));
  1421. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::getSalt()));
  1422. $fields .= urlencode(':Model.hidden|Model.id');
  1423. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1424. 'Model' => [
  1425. 'hidden' => ['some-key' => 'some-value'],
  1426. 'id' => '1',
  1427. ],
  1428. '_Token' => compact('fields', 'unlocked', 'debug')
  1429. ]));
  1430. Configure::write('debug', false);
  1431. $result = $this->validatePost('SecurityException', 'Unexpected \'_Token.debug\' found in request data');
  1432. $this->assertFalse($result);
  1433. }
  1434. }