SecurityComponentTest.php 48 KB

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