SecurityComponentTest.php 53 KB

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