SecurityComponentTest.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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(string $expectedException = 'SecurityException', ?string $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. * testValidatePostFormTampering 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 testValidatePostFormTampering(): 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. $this->assertFalse($result);
  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. * Test that invalid types cause failures.
  833. *
  834. * @return void
  835. */
  836. public function testValidatePostFailArrayData()
  837. {
  838. $event = new Event('Controller.startup', $this->Controller);
  839. $this->Security->startup($event);
  840. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  841. 'Model' => [
  842. 'username' => 'mark',
  843. 'password' => 'sekret',
  844. ],
  845. '_Token' => [
  846. 'fields' => [],
  847. 'unlocked' => '',
  848. ],
  849. ]));
  850. Configure::write('debug', false);
  851. $result = $this->validatePost('SecurityException', "'_Token.fields' is invalid.");
  852. $this->assertFalse($result);
  853. }
  854. /**
  855. * testValidateHiddenMultipleModel method
  856. *
  857. * @return void
  858. * @triggers Controller.startup $this->Controller
  859. */
  860. public function testValidateHiddenMultipleModel(): void
  861. {
  862. $event = new Event('Controller.startup', $this->Controller);
  863. $this->Security->startup($event);
  864. $fields = '642b7a6db3b848fab88952b86ea36c572f93df40%3AModel.valid%7CModel2.valid%7CModel3.valid';
  865. $unlocked = '';
  866. $debug = 'not used';
  867. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  868. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  869. 'Model2' => ['valid' => '0'],
  870. 'Model3' => ['valid' => '0'],
  871. '_Token' => compact('fields', 'unlocked', 'debug'),
  872. ]));
  873. $result = $this->validatePost();
  874. $this->assertNull($result);
  875. }
  876. /**
  877. * testValidateHasManyModel method
  878. *
  879. * @return void
  880. * @triggers Controller.startup $this->Controller
  881. */
  882. public function testValidateHasManyModel(): void
  883. {
  884. $event = new Event('Controller.startup', $this->Controller);
  885. $this->Security->startup($event);
  886. $fields = '792324c8a374772ad82acfb28f0e77e70f8ed3af%3AModel.0.hidden%7CModel.0.valid';
  887. $fields .= '%7CModel.1.hidden%7CModel.1.valid';
  888. $unlocked = '';
  889. $debug = 'not used';
  890. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  891. 'Model' => [
  892. [
  893. 'username' => 'username', 'password' => 'password',
  894. 'hidden' => 'value', 'valid' => '0',
  895. ],
  896. [
  897. 'username' => 'username', 'password' => 'password',
  898. 'hidden' => 'value', 'valid' => '0',
  899. ],
  900. ],
  901. '_Token' => compact('fields', 'unlocked', 'debug'),
  902. ]));
  903. $result = $this->validatePost();
  904. $this->assertNull($result);
  905. }
  906. /**
  907. * testValidateHasManyRecordsPass method
  908. *
  909. * @return void
  910. * @triggers Controller.startup $this->Controller
  911. */
  912. public function testValidateHasManyRecordsPass(): void
  913. {
  914. $event = new Event('Controller.startup', $this->Controller);
  915. $this->Security->startup($event);
  916. $fields = '7f4bff67558e25ebeea44c84ea4befa8d50b080c%3AAddress.0.id%7CAddress.0.primary%7C';
  917. $fields .= 'Address.1.id%7CAddress.1.primary';
  918. $unlocked = '';
  919. $debug = 'not used';
  920. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  921. 'Address' => [
  922. 0 => [
  923. 'id' => '123',
  924. 'title' => 'home',
  925. 'first_name' => 'Bilbo',
  926. 'last_name' => 'Baggins',
  927. 'address' => '23 Bag end way',
  928. 'city' => 'the shire',
  929. 'phone' => 'N/A',
  930. 'primary' => '1',
  931. ],
  932. 1 => [
  933. 'id' => '124',
  934. 'title' => 'home',
  935. 'first_name' => 'Frodo',
  936. 'last_name' => 'Baggins',
  937. 'address' => '50 Bag end way',
  938. 'city' => 'the shire',
  939. 'phone' => 'N/A',
  940. 'primary' => '1',
  941. ],
  942. ],
  943. '_Token' => compact('fields', 'unlocked', 'debug'),
  944. ]));
  945. $result = $this->validatePost();
  946. $this->assertNull($result);
  947. }
  948. /**
  949. * testValidateNestedNumericSets method
  950. *
  951. * Test that values like Foo.0.1
  952. *
  953. * @return void
  954. * @triggers Controller.startup $this->Controller
  955. */
  956. public function testValidateNestedNumericSets(): void
  957. {
  958. $event = new Event('Controller.startup', $this->Controller);
  959. $this->Security->startup($event);
  960. $unlocked = '';
  961. $hashFields = ['TaxonomyData'];
  962. $fields = urlencode(
  963. hash_hmac('sha1', '/articles/index' . serialize($hashFields) . $unlocked . 'cli', Security::getSalt())
  964. );
  965. $debug = 'not used';
  966. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  967. 'TaxonomyData' => [
  968. 1 => [[2]],
  969. 2 => [[3]],
  970. ],
  971. '_Token' => compact('fields', 'unlocked', 'debug'),
  972. ]));
  973. $result = $this->validatePost();
  974. $this->assertNull($result);
  975. }
  976. /**
  977. * testValidateHasManyRecords method
  978. *
  979. * validatePost should fail, hidden fields have been changed.
  980. *
  981. * @return void
  982. * @triggers Controller.startup $this->Controller
  983. */
  984. public function testValidateHasManyRecordsFail(): void
  985. {
  986. $event = new Event('Controller.startup', $this->Controller);
  987. $this->Security->startup($event);
  988. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
  989. $fields .= 'Address.1.id%7CAddress.1.primary';
  990. $unlocked = '';
  991. $debug = urlencode(json_encode([
  992. '/articles/index',
  993. [
  994. 'Address.0.address',
  995. 'Address.0.city',
  996. 'Address.0.first_name',
  997. 'Address.0.last_name',
  998. 'Address.0.phone',
  999. 'Address.0.title',
  1000. 'Address.1.address',
  1001. 'Address.1.city',
  1002. 'Address.1.first_name',
  1003. 'Address.1.last_name',
  1004. 'Address.1.phone',
  1005. 'Address.1.title',
  1006. 'Address.0.id' => '123',
  1007. 'Address.0.primary' => '5',
  1008. 'Address.1.id' => '124',
  1009. 'Address.1.primary' => '1',
  1010. ],
  1011. [],
  1012. ]));
  1013. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1014. 'Address' => [
  1015. 0 => [
  1016. 'id' => '123',
  1017. 'title' => 'home',
  1018. 'first_name' => 'Bilbo',
  1019. 'last_name' => 'Baggins',
  1020. 'address' => '23 Bag end way',
  1021. 'city' => 'the shire',
  1022. 'phone' => 'N/A',
  1023. 'primary' => '5',
  1024. ],
  1025. 1 => [
  1026. 'id' => '124',
  1027. 'title' => 'home',
  1028. 'first_name' => 'Frodo',
  1029. 'last_name' => 'Baggins',
  1030. 'address' => '50 Bag end way',
  1031. 'city' => 'the shire',
  1032. 'phone' => 'N/A',
  1033. 'primary' => '1',
  1034. ],
  1035. ],
  1036. '_Token' => compact('fields', 'unlocked', 'debug'),
  1037. ]));
  1038. $result = $this->validatePost('SecurityException', 'Bad Request');
  1039. $this->assertFalse($result);
  1040. }
  1041. /**
  1042. * testValidatePostRadio method
  1043. *
  1044. * Test validatePost with radio buttons.
  1045. *
  1046. * @return void
  1047. * @triggers Controller.startup $this->Controller
  1048. */
  1049. public function testValidatePostRadio(): void
  1050. {
  1051. $event = new Event('Controller.startup', $this->Controller);
  1052. $this->Security->startup($event);
  1053. $fields = 'a709dfdee0a0cce52c4c964a1b8a56159bb081b4%3An%3A0%3A%7B%7D';
  1054. $unlocked = '';
  1055. $debug = urlencode(json_encode([
  1056. '/articles/index',
  1057. [],
  1058. [],
  1059. ]));
  1060. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1061. '_Token' => compact('fields', 'unlocked', 'debug'),
  1062. ]));
  1063. $result = $this->validatePost('SecurityException', 'Bad Request');
  1064. $this->assertFalse($result);
  1065. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1066. '_Token' => compact('fields', 'unlocked', 'debug'),
  1067. 'Test' => ['test' => ''],
  1068. ]));
  1069. $result = $this->validatePost();
  1070. $this->assertNull($result);
  1071. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1072. '_Token' => compact('fields', 'unlocked', 'debug'),
  1073. 'Test' => ['test' => '1'],
  1074. ]));
  1075. $result = $this->validatePost();
  1076. $this->assertNull($result);
  1077. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1078. '_Token' => compact('fields', 'unlocked', 'debug'),
  1079. 'Test' => ['test' => '2'],
  1080. ]));
  1081. $result = $this->validatePost();
  1082. $this->assertNull($result);
  1083. }
  1084. /**
  1085. * testValidatePostUrlAsHashInput method
  1086. *
  1087. * Test validatePost uses here() as a hash input.
  1088. *
  1089. * @return void
  1090. * @triggers Controller.startup $this->Controller
  1091. */
  1092. public function testValidatePostUrlAsHashInput(): void
  1093. {
  1094. $event = new Event('Controller.startup', $this->Controller);
  1095. $this->Security->startup($event);
  1096. $fields = 'de2ca3670dd06c29558dd98482c8739e86da2c7c%3A';
  1097. $unlocked = '';
  1098. $debug = urlencode(json_encode([
  1099. 'another-url',
  1100. ['Model.username', 'Model.password'],
  1101. [],
  1102. ]));
  1103. $this->Controller->setRequest($this->Controller->getRequest()
  1104. ->withData('Model', ['username' => '', 'password' => ''])
  1105. ->withData('_Token', compact('fields', 'unlocked', 'debug')));
  1106. $this->assertNull($this->validatePost());
  1107. $this->Controller->setRequest($this->Controller->getRequest()
  1108. ->withRequestTarget('/posts/index?page=1'));
  1109. $this->assertFalse($this->validatePost(
  1110. 'SecurityException',
  1111. 'URL mismatch in POST data (expected \'another-url\' but found \'/posts/index?page=1\')'
  1112. ));
  1113. $this->Controller->setRequest($this->Controller->getRequest()
  1114. ->withRequestTarget('/posts/edit/1'));
  1115. $this->assertFalse($this->validatePost(
  1116. 'SecurityException',
  1117. 'URL mismatch in POST data (expected \'another-url\' but found \'/posts/edit/1\')'
  1118. ));
  1119. }
  1120. /**
  1121. * testGenerateToken method
  1122. *
  1123. * Test generateToken().
  1124. *
  1125. * @return void
  1126. */
  1127. public function testGenerateToken(): void
  1128. {
  1129. $request = $this->Controller->getRequest();
  1130. $request = $this->Security->generateToken($request);
  1131. $securityToken = $request->getAttribute('formTokenData');
  1132. $this->assertNotEmpty($securityToken);
  1133. $this->assertSame([], $securityToken['unlockedFields']);
  1134. }
  1135. /**
  1136. * testUnlockedActions method
  1137. *
  1138. * Test unlocked actions.
  1139. *
  1140. * @return void
  1141. * @triggers Controller.startup $this->Controller
  1142. */
  1143. public function testUnlockedActions(): void
  1144. {
  1145. $_SERVER['REQUEST_METHOD'] = 'POST';
  1146. $event = new Event('Controller.startup', $this->Controller);
  1147. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody(['data']));
  1148. $this->Security->unlockedActions = 'index';
  1149. $this->Security->blackHoleCallback = null;
  1150. $result = $this->Controller->Security->startup($event);
  1151. $this->assertNull($result);
  1152. }
  1153. /**
  1154. * testValidatePostDebugFormat method
  1155. *
  1156. * Test that debug token format is right.
  1157. *
  1158. * @return void
  1159. * @triggers Controller.startup $this->Controller
  1160. */
  1161. public function testValidatePostDebugFormat(): void
  1162. {
  1163. $event = new Event('Controller.startup', $this->Controller);
  1164. $this->Security->startup($event);
  1165. $unlocked = 'Model.username';
  1166. $fields = ['Model.hidden', 'Model.password'];
  1167. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::getSalt()));
  1168. $debug = urlencode(json_encode([
  1169. '/articles/index',
  1170. ['Model.hidden', 'Model.password'],
  1171. ['Model.username'],
  1172. ['not expected'],
  1173. ]));
  1174. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1175. 'Model' => [
  1176. 'username' => 'mark',
  1177. 'password' => 'sekret',
  1178. 'hidden' => '0',
  1179. ],
  1180. '_Token' => compact('fields', 'unlocked', 'debug'),
  1181. ]));
  1182. $result = $this->validatePost('SecurityException', 'Invalid security debug token.');
  1183. $this->assertFalse($result);
  1184. $debug = urlencode(json_encode('not an array'));
  1185. $result = $this->validatePost('SecurityException', 'Invalid security debug token.');
  1186. $this->assertFalse($result);
  1187. }
  1188. /**
  1189. * testBlackholeThrowsException method
  1190. *
  1191. * Test blackhole will now throw passed exception if debug enabled.
  1192. *
  1193. * @return void
  1194. */
  1195. public function testBlackholeThrowsException(): void
  1196. {
  1197. $this->expectException(\Cake\Controller\Exception\SecurityException::class);
  1198. $this->expectExceptionMessage('error description');
  1199. $this->Security->setConfig('blackHoleCallback', '');
  1200. $this->Security->blackHole($this->Controller, 'auth', new SecurityException('error description'));
  1201. }
  1202. /**
  1203. * testBlackholeThrowsBadRequest method
  1204. *
  1205. * Test blackhole will throw BadRequest if debug disabled.
  1206. *
  1207. * @return void
  1208. */
  1209. public function testBlackholeThrowsBadRequest(): void
  1210. {
  1211. $this->Security->setConfig('blackHoleCallback', '');
  1212. $message = '';
  1213. Configure::write('debug', false);
  1214. try {
  1215. $this->Security->blackHole($this->Controller, 'auth', new SecurityException('error description'));
  1216. } catch (SecurityException $ex) {
  1217. $message = $ex->getMessage();
  1218. $reason = $ex->getReason();
  1219. }
  1220. $this->assertSame('The request has been black-holed', $message);
  1221. $this->assertSame('error description', $reason);
  1222. }
  1223. /**
  1224. * testValidatePostFailTampering method
  1225. *
  1226. * Test that validatePost fails with tampered fields and explanation.
  1227. *
  1228. * @return void
  1229. * @triggers Controller.startup $this->Controller
  1230. */
  1231. public function testValidatePostFailTampering(): void
  1232. {
  1233. $event = new Event('Controller.startup', $this->Controller);
  1234. $this->Security->startup($event);
  1235. $unlocked = '';
  1236. $fields = ['Model.hidden' => 'value', 'Model.id' => '1'];
  1237. $debug = urlencode(json_encode([
  1238. '/articles/index',
  1239. $fields,
  1240. [],
  1241. ]));
  1242. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::getSalt()));
  1243. $fields .= urlencode(':Model.hidden|Model.id');
  1244. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1245. 'Model' => [
  1246. 'hidden' => 'tampered',
  1247. 'id' => '1',
  1248. ],
  1249. '_Token' => compact('fields', 'unlocked', 'debug'),
  1250. ]));
  1251. $result = $this->validatePost('SecurityException', 'Tampered field \'Model.hidden\' in POST data (expected value \'value\' but found \'tampered\')');
  1252. $this->assertFalse($result);
  1253. }
  1254. /**
  1255. * testValidatePostFailTamperingMutatedIntoArray method
  1256. *
  1257. * Test that validatePost fails with tampered fields and explanation.
  1258. *
  1259. * @return void
  1260. * @triggers Controller.startup $this->Controller
  1261. */
  1262. public function testValidatePostFailTamperingMutatedIntoArray(): void
  1263. {
  1264. $event = new Event('Controller.startup', $this->Controller);
  1265. $this->Security->startup($event);
  1266. $unlocked = '';
  1267. $fields = ['Model.hidden' => 'value', 'Model.id' => '1'];
  1268. $debug = urlencode(json_encode([
  1269. '/articles/index',
  1270. $fields,
  1271. [],
  1272. ]));
  1273. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::getSalt()));
  1274. $fields .= urlencode(':Model.hidden|Model.id');
  1275. $this->Controller->setRequest($this->Controller->getRequest()->withData('Model', [
  1276. 'hidden' => ['some-key' => 'some-value'],
  1277. 'id' => '1',
  1278. ])->withData('_Token', compact('fields', 'unlocked', 'debug')));
  1279. $result = $this->validatePost(
  1280. 'SecurityException',
  1281. 'Unexpected field \'Model.hidden.some-key\' in POST data, Missing field \'Model.hidden\' in POST data'
  1282. );
  1283. $this->assertFalse($result);
  1284. }
  1285. /**
  1286. * testValidatePostUnexpectedDebugToken method
  1287. *
  1288. * Test that debug token should not be sent if debug is disabled.
  1289. *
  1290. * @return void
  1291. * @triggers Controller.startup $this->Controller
  1292. */
  1293. public function testValidatePostUnexpectedDebugToken(): void
  1294. {
  1295. $event = new Event('Controller.startup', $this->Controller);
  1296. $this->Security->startup($event);
  1297. $unlocked = '';
  1298. $fields = ['Model.hidden' => 'value', 'Model.id' => '1'];
  1299. $debug = urlencode(json_encode([
  1300. '/articles/index',
  1301. $fields,
  1302. [],
  1303. ]));
  1304. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::getSalt()));
  1305. $fields .= urlencode(':Model.hidden|Model.id');
  1306. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  1307. 'Model' => [
  1308. 'hidden' => ['some-key' => 'some-value'],
  1309. 'id' => '1',
  1310. ],
  1311. '_Token' => compact('fields', 'unlocked', 'debug'),
  1312. ]));
  1313. Configure::write('debug', false);
  1314. $result = $this->validatePost('SecurityException', 'Unexpected \'_Token.debug\' found in request data');
  1315. $this->assertFalse($result);
  1316. }
  1317. }