SecurityComponentTest.php 55 KB

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