SecurityComponentTest.php 48 KB

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