SecurityComponentTest.php 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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('SecurityException', '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('SecurityException', '\'_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('SecurityException', '\'_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. $debug = Configure::read('debug');
  860. Configure::write('debug', false);
  861. $result = $this->validatePost();
  862. $this->assertFalse($result);
  863. Configure::write('debug', $debug);
  864. }
  865. /**
  866. * Test that validatePost fails when unlocked fields are changed.
  867. *
  868. * @return void
  869. * @triggers Controller.startup $this->Controller
  870. */
  871. public function testValidatePostFailDisabledFieldTampering()
  872. {
  873. $event = new Event('Controller.startup', $this->Controller);
  874. $this->Controller->Security->startup($event);
  875. $unlocked = 'Model.username';
  876. $fields = ['Model.hidden', 'Model.password'];
  877. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::salt()));
  878. $debug = urlencode(json_encode([
  879. '/articles/index',
  880. ['Model.hidden', 'Model.password'],
  881. ['Model.username']
  882. ]));
  883. // Tamper the values.
  884. $unlocked = 'Model.username|Model.password';
  885. $this->Controller->request->data = [
  886. 'Model' => [
  887. 'username' => 'mark',
  888. 'password' => 'sekret',
  889. 'hidden' => '0'
  890. ],
  891. '_Token' => compact('fields', 'unlocked', 'debug')
  892. ];
  893. $result = $this->validatePost('SecurityException', 'Missing field \'Model.password\' in POST data, Unexpected unlocked field \'Model.password\' in POST data');
  894. $this->assertFalse($result);
  895. }
  896. /**
  897. * testValidateHiddenMultipleModel method
  898. *
  899. * @return void
  900. * @triggers Controller.startup $this->Controller
  901. */
  902. public function testValidateHiddenMultipleModel()
  903. {
  904. $event = new Event('Controller.startup', $this->Controller);
  905. $this->Controller->Security->startup($event);
  906. $fields = '075ca6c26c38a09a78d871201df89faf52cbbeb8%3AModel.valid%7CModel2.valid%7CModel3.valid';
  907. $unlocked = '';
  908. $debug = 'not used';
  909. $this->Controller->request->data = [
  910. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  911. 'Model2' => ['valid' => '0'],
  912. 'Model3' => ['valid' => '0'],
  913. '_Token' => compact('fields', 'unlocked', 'debug'),
  914. ];
  915. $result = $this->validatePost();
  916. $this->assertTrue($result);
  917. }
  918. /**
  919. * testValidateHasManyModel method
  920. *
  921. * @return void
  922. * @triggers Controller.startup $this->Controller
  923. */
  924. public function testValidateHasManyModel()
  925. {
  926. $event = new Event('Controller.startup', $this->Controller);
  927. $this->Controller->Security->startup($event);
  928. $fields = '24a753fb62ef7839389987b58e3f7108f564e529%3AModel.0.hidden%7CModel.0.valid';
  929. $fields .= '%7CModel.1.hidden%7CModel.1.valid';
  930. $unlocked = '';
  931. $debug = 'not used';
  932. $this->Controller->request->data = [
  933. 'Model' => [
  934. [
  935. 'username' => 'username', 'password' => 'password',
  936. 'hidden' => 'value', 'valid' => '0'
  937. ],
  938. [
  939. 'username' => 'username', 'password' => 'password',
  940. 'hidden' => 'value', 'valid' => '0'
  941. ]
  942. ],
  943. '_Token' => compact('fields', 'unlocked', 'debug'),
  944. ];
  945. $result = $this->validatePost();
  946. $this->assertTrue($result);
  947. }
  948. /**
  949. * testValidateHasManyRecordsPass method
  950. *
  951. * @return void
  952. * @triggers Controller.startup $this->Controller
  953. */
  954. public function testValidateHasManyRecordsPass()
  955. {
  956. $event = new Event('Controller.startup', $this->Controller);
  957. $this->Controller->Security->startup($event);
  958. $fields = '8f7d82bf7656cf068822d9bdab109ebed1be1825%3AAddress.0.id%7CAddress.0.primary%7C';
  959. $fields .= 'Address.1.id%7CAddress.1.primary';
  960. $unlocked = '';
  961. $debug = 'not used';
  962. $this->Controller->request->data = [
  963. 'Address' => [
  964. 0 => [
  965. 'id' => '123',
  966. 'title' => 'home',
  967. 'first_name' => 'Bilbo',
  968. 'last_name' => 'Baggins',
  969. 'address' => '23 Bag end way',
  970. 'city' => 'the shire',
  971. 'phone' => 'N/A',
  972. 'primary' => '1',
  973. ],
  974. 1 => [
  975. 'id' => '124',
  976. 'title' => 'home',
  977. 'first_name' => 'Frodo',
  978. 'last_name' => 'Baggins',
  979. 'address' => '50 Bag end way',
  980. 'city' => 'the shire',
  981. 'phone' => 'N/A',
  982. 'primary' => '1'
  983. ]
  984. ],
  985. '_Token' => compact('fields', 'unlocked', 'debug'),
  986. ];
  987. $result = $this->validatePost();
  988. $this->assertTrue($result);
  989. }
  990. /**
  991. * Test that values like Foo.0.1
  992. *
  993. * @return void
  994. * @triggers Controller.startup $this->Controller
  995. */
  996. public function testValidateNestedNumericSets()
  997. {
  998. $event = new Event('Controller.startup', $this->Controller);
  999. $this->Controller->Security->startup($event);
  1000. $unlocked = '';
  1001. $hashFields = ['TaxonomyData'];
  1002. $fields = urlencode(Security::hash('/articles/index' . serialize($hashFields) . $unlocked . Security::salt()));
  1003. $debug = 'not used';
  1004. $this->Controller->request->data = [
  1005. 'TaxonomyData' => [
  1006. 1 => [[2]],
  1007. 2 => [[3]]
  1008. ],
  1009. '_Token' => compact('fields', 'unlocked', 'debug'),
  1010. ];
  1011. $result = $this->validatePost();
  1012. $this->assertTrue($result);
  1013. }
  1014. /**
  1015. * testValidateHasManyRecords method
  1016. *
  1017. * validatePost should fail, hidden fields have been changed.
  1018. *
  1019. * @return void
  1020. * @triggers Controller.startup $this->Controller
  1021. */
  1022. public function testValidateHasManyRecordsFail()
  1023. {
  1024. $event = new Event('Controller.startup', $this->Controller);
  1025. $this->Controller->Security->startup($event);
  1026. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
  1027. $fields .= 'Address.1.id%7CAddress.1.primary';
  1028. $unlocked = '';
  1029. $debug = urlencode(json_encode([
  1030. '/articles/index',
  1031. [
  1032. 'Address.0.address',
  1033. 'Address.0.city',
  1034. 'Address.0.first_name',
  1035. 'Address.0.last_name',
  1036. 'Address.0.phone',
  1037. 'Address.0.title',
  1038. 'Address.1.address',
  1039. 'Address.1.city',
  1040. 'Address.1.first_name',
  1041. 'Address.1.last_name',
  1042. 'Address.1.phone',
  1043. 'Address.1.title',
  1044. 'Address.0.id' => '123',
  1045. 'Address.0.primary' => '5',
  1046. 'Address.1.id' => '124',
  1047. 'Address.1.primary' => '1'
  1048. ],
  1049. []
  1050. ]));
  1051. $this->Controller->request->data = [
  1052. 'Address' => [
  1053. 0 => [
  1054. 'id' => '123',
  1055. 'title' => 'home',
  1056. 'first_name' => 'Bilbo',
  1057. 'last_name' => 'Baggins',
  1058. 'address' => '23 Bag end way',
  1059. 'city' => 'the shire',
  1060. 'phone' => 'N/A',
  1061. 'primary' => '5',
  1062. ],
  1063. 1 => [
  1064. 'id' => '124',
  1065. 'title' => 'home',
  1066. 'first_name' => 'Frodo',
  1067. 'last_name' => 'Baggins',
  1068. 'address' => '50 Bag end way',
  1069. 'city' => 'the shire',
  1070. 'phone' => 'N/A',
  1071. 'primary' => '1'
  1072. ]
  1073. ],
  1074. '_Token' => compact('fields', 'unlocked', 'debug'),
  1075. ];
  1076. $result = $this->validatePost('SecurityException', 'Bad Request');
  1077. $this->assertFalse($result);
  1078. }
  1079. /**
  1080. * testFormDisabledFields method
  1081. *
  1082. * @return void
  1083. * @triggers Controller.startup $this->Controller
  1084. */
  1085. public function testFormDisabledFields()
  1086. {
  1087. $event = new Event('Controller.startup', $this->Controller);
  1088. $this->Controller->Security->startup($event);
  1089. $fields = '9da2b3fa2b5b8ac0bfbc1bbce145e58059629125%3An%3A0%3A%7B%7D';
  1090. $unlocked = '';
  1091. $debug = urlencode(json_encode([
  1092. '/articles/index',
  1093. [],
  1094. []
  1095. ]));
  1096. $this->Controller->request->data = [
  1097. 'MyModel' => ['name' => 'some data'],
  1098. '_Token' => compact('fields', 'unlocked', 'debug'),
  1099. ];
  1100. $result = $this->validatePost('SecurityException', 'Unexpected field \'MyModel.name\' in POST data');
  1101. $this->assertFalse($result);
  1102. $this->Controller->Security->startup($event);
  1103. $this->Controller->Security->config('disabledFields', ['MyModel.name']);
  1104. $this->Controller->request->data = [
  1105. 'MyModel' => ['name' => 'some data'],
  1106. '_Token' => compact('fields', 'unlocked', 'debug'),
  1107. ];
  1108. $result = $this->validatePost();
  1109. $this->assertTrue($result);
  1110. }
  1111. /**
  1112. * test validatePost with radio buttons
  1113. *
  1114. * @return void
  1115. * @triggers Controller.startup $this->Controller
  1116. */
  1117. public function testValidatePostRadio()
  1118. {
  1119. $event = new Event('Controller.startup', $this->Controller);
  1120. $this->Controller->Security->startup($event);
  1121. $fields = 'c2226a8879c3f4b513691295fc2519a29c44c8bb%3An%3A0%3A%7B%7D';
  1122. $unlocked = '';
  1123. $debug = urlencode(json_encode([
  1124. '/articles/index',
  1125. [],
  1126. []
  1127. ]));
  1128. $this->Controller->request->data = [
  1129. '_Token' => compact('fields', 'unlocked', 'debug'),
  1130. ];
  1131. $result = $this->validatePost('SecurityException', 'Bad Request');
  1132. $this->assertFalse($result);
  1133. $this->Controller->request->data = [
  1134. '_Token' => compact('fields', 'unlocked', 'debug'),
  1135. 'Test' => ['test' => '']
  1136. ];
  1137. $result = $this->validatePost();
  1138. $this->assertTrue($result);
  1139. $this->Controller->request->data = [
  1140. '_Token' => compact('fields', 'unlocked', 'debug'),
  1141. 'Test' => ['test' => '1']
  1142. ];
  1143. $result = $this->validatePost();
  1144. $this->assertTrue($result);
  1145. $this->Controller->request->data = [
  1146. '_Token' => compact('fields', 'unlocked', 'debug'),
  1147. 'Test' => ['test' => '2']
  1148. ];
  1149. $result = $this->validatePost();
  1150. $this->assertTrue($result);
  1151. }
  1152. /**
  1153. * test validatePost uses here() as a hash input.
  1154. *
  1155. * @return void
  1156. * @triggers Controller.startup $this->Controller
  1157. */
  1158. public function testValidatePostUrlAsHashInput()
  1159. {
  1160. $event = new Event('Controller.startup', $this->Controller);
  1161. $this->Security->startup($event);
  1162. $fields = 'b0914d06dfb04abf1fada53e16810e87d157950b%3A';
  1163. $unlocked = '';
  1164. $debug = urlencode(json_encode([
  1165. 'another-url',
  1166. ['Model.username', 'Model.password'],
  1167. []
  1168. ]));
  1169. $this->Controller->request->data = [
  1170. 'Model' => ['username' => '', 'password' => ''],
  1171. '_Token' => compact('fields', 'unlocked', 'debug')
  1172. ];
  1173. $this->assertTrue($this->validatePost());
  1174. $request = $this->getMock('Cake\Network\Request', ['here']);
  1175. $request->expects($this->at(0))
  1176. ->method('here')
  1177. ->will($this->returnValue('/posts/index?page=1'));
  1178. $request->expects($this->at(1))
  1179. ->method('here')
  1180. ->will($this->returnValue('/posts/edit/1'));
  1181. $request->data = $this->Controller->request->data;
  1182. $this->Controller->request = $request;
  1183. $this->assertFalse($this->validatePost('SecurityException', 'URL mismatch in POST data (expected \'another-url\' but found \'/posts/index?page=1\')'));
  1184. $this->assertFalse($this->validatePost('SecurityException', 'URL mismatch in POST data (expected \'another-url\' but found \'/posts/edit/1\')'));
  1185. }
  1186. /**
  1187. * test that blackhole doesn't delete the _Token session key so repeat data submissions
  1188. * stay blackholed.
  1189. *
  1190. * @return void
  1191. * @triggers Controller.startup $this->Controller
  1192. */
  1193. public function testBlackHoleNotDeletingSessionInformation()
  1194. {
  1195. $event = new Event('Controller.startup', $this->Controller);
  1196. $this->Controller->Security->startup($event);
  1197. $this->Controller->Security->blackHole($this->Controller, 'auth');
  1198. $this->assertTrue($this->Controller->Security->session->check('_Token'), '_Token was deleted by blackHole %s');
  1199. }
  1200. /**
  1201. * Test generateToken()
  1202. *
  1203. * @return void
  1204. */
  1205. public function testGenerateToken()
  1206. {
  1207. $request = $this->Controller->request;
  1208. $this->Security->generateToken($request);
  1209. $this->assertNotEmpty($request->params['_Token']);
  1210. $this->assertTrue(isset($request->params['_Token']['unlockedFields']));
  1211. }
  1212. /**
  1213. * Test unlocked actions
  1214. *
  1215. * @return void
  1216. * @triggers Controller.startup $this->Controller
  1217. */
  1218. public function testUnlockedActions()
  1219. {
  1220. $_SERVER['REQUEST_METHOD'] = 'POST';
  1221. $event = new Event('Controller.startup', $this->Controller);
  1222. $this->Controller->request->data = ['data'];
  1223. $this->Controller->Security->unlockedActions = 'index';
  1224. $this->Controller->Security->blackHoleCallback = null;
  1225. $result = $this->Controller->Security->startup($event);
  1226. $this->assertNull($result);
  1227. }
  1228. /**
  1229. * Test that debug token format is right
  1230. *
  1231. * @return void
  1232. * @triggers Controller.startup $this->Controller
  1233. */
  1234. public function testValidatePostDebugFormat()
  1235. {
  1236. $event = new Event('Controller.startup', $this->Controller);
  1237. $this->Controller->Security->startup($event);
  1238. $unlocked = 'Model.username';
  1239. $fields = ['Model.hidden', 'Model.password'];
  1240. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::salt()));
  1241. $debug = urlencode(json_encode([
  1242. '/articles/index',
  1243. ['Model.hidden', 'Model.password'],
  1244. ['Model.username'],
  1245. ['not expected']
  1246. ]));
  1247. $this->Controller->request->data = [
  1248. 'Model' => [
  1249. 'username' => 'mark',
  1250. 'password' => 'sekret',
  1251. 'hidden' => '0'
  1252. ],
  1253. '_Token' => compact('fields', 'unlocked', 'debug')
  1254. ];
  1255. $result = $this->validatePost('SecurityException', 'Invalid security debug token.');
  1256. $this->assertFalse($result);
  1257. $debug = urlencode(json_encode('not an array'));
  1258. $result = $this->validatePost('SecurityException', 'Invalid security debug token.');
  1259. $this->assertFalse($result);
  1260. }
  1261. /**
  1262. * test blackhole will now throw passed exception if debug enabled
  1263. *
  1264. * @expectedException Cake\Controller\Exception\SecurityException
  1265. * @expectedExceptionMessage error description
  1266. * @return void
  1267. */
  1268. public function testBlackholeThrowsException()
  1269. {
  1270. $this->Security->config('blackHoleCallback', '');
  1271. $this->Security->blackHole($this->Controller, 'auth', new SecurityException('error description'));
  1272. }
  1273. /**
  1274. * test blackhole will throw BadRequest if debug disabled
  1275. *
  1276. * @return void
  1277. */
  1278. public function testBlackholeThrowsBadRequest()
  1279. {
  1280. $this->Security->config('blackHoleCallback', '');
  1281. $debug = Configure::read('debug');
  1282. $message = '';
  1283. Configure::write('debug', false);
  1284. try {
  1285. $this->Security->blackHole($this->Controller, 'auth', new SecurityException('error description'));
  1286. } catch (BadRequestException $ex) {
  1287. $message = $ex->getMessage();
  1288. }
  1289. Configure::write('debug', $debug);
  1290. $this->assertEquals('The request has been black-holed', $message);
  1291. }
  1292. /**
  1293. * Test that validatePost fails with tampered fields and explanation
  1294. *
  1295. * @return void
  1296. * @triggers Controller.startup $this->Controller
  1297. */
  1298. public function testValidatePostFailTampering()
  1299. {
  1300. $event = new Event('Controller.startup', $this->Controller);
  1301. $this->Controller->Security->startup($event);
  1302. $unlocked = '';
  1303. $fields = ['Model.hidden' => 'value', 'Model.id' => '1'];
  1304. $debug = urlencode(json_encode([
  1305. '/articles/index',
  1306. $fields,
  1307. []
  1308. ]));
  1309. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::salt()));
  1310. $fields .= urlencode(':Model.hidden|Model.id');
  1311. $this->Controller->request->data = [
  1312. 'Model' => [
  1313. 'hidden' => 'tampered',
  1314. 'id' => '1',
  1315. ],
  1316. '_Token' => compact('fields', 'unlocked', 'debug')
  1317. ];
  1318. $result = $this->validatePost('SecurityException', 'Tampered field \'Model.hidden\' in POST data (expected value \'value\' but found \'tampered\')');
  1319. $this->assertFalse($result);
  1320. }
  1321. /**
  1322. * Auth required throws exception token not found
  1323. *
  1324. * @return void
  1325. * @expectedException Cake\Controller\Exception\AuthSecurityException
  1326. * @expectedExceptionMessage '_Token' was not found in request data.
  1327. * @triggers Controller.startup $this->Controller
  1328. */
  1329. public function testAuthRequiredThrowsExceptionTokenNotFoundPost()
  1330. {
  1331. $this->Controller->Security->config('requireAuth', ['protected']);
  1332. $this->Controller->request->params['action'] = 'protected';
  1333. $this->Controller->request->data = 'notEmpty';
  1334. $event = new Event('Controller.startup', $this->Controller);
  1335. $this->Controller->Security->startup($event);
  1336. $this->Controller->Security->authRequired($this->Controller);
  1337. }
  1338. /**
  1339. * Auth required throws exception token not found in Session
  1340. *
  1341. * @return void
  1342. * @expectedException Cake\Controller\Exception\AuthSecurityException
  1343. * @expectedExceptionMessage '_Token' was not found in session.
  1344. * @triggers Controller.startup $this->Controller
  1345. */
  1346. public function testAuthRequiredThrowsExceptionTokenNotFoundSession()
  1347. {
  1348. $this->Controller->Security->config('requireAuth', ['protected']);
  1349. $this->Controller->request->params['action'] = 'protected';
  1350. $this->Controller->request->data = ['_Token' => 'not empty'];
  1351. $event = new Event('Controller.startup', $this->Controller);
  1352. $this->Controller->Security->startup($event);
  1353. $this->Controller->Security->authRequired($this->Controller);
  1354. }
  1355. /**
  1356. * Auth required throws exception controller not allowed
  1357. *
  1358. * @return void
  1359. * @expectedException Cake\Controller\Exception\AuthSecurityException
  1360. * @expectedExceptionMessage Controller 'NotAllowed' was not found in allowed controllers: 'Allowed, AnotherAllowed'.
  1361. * @triggers Controller.startup $this->Controller
  1362. */
  1363. public function testAuthRequiredThrowsExceptionControllerNotAllowed()
  1364. {
  1365. $this->Controller->Security->config('requireAuth', ['protected']);
  1366. $this->Controller->request->params['controller'] = 'NotAllowed';
  1367. $this->Controller->request->params['action'] = 'protected';
  1368. $this->Controller->request->data = ['_Token' => 'not empty'];
  1369. $this->Controller->request->session()->write('_Token', [
  1370. 'allowedControllers' => ['Allowed', 'AnotherAllowed']
  1371. ]);
  1372. $event = new Event('Controller.startup', $this->Controller);
  1373. $this->Controller->Security->startup($event);
  1374. $this->Controller->Security->authRequired($this->Controller);
  1375. }
  1376. /**
  1377. * Auth required throws exception controller not allowed
  1378. *
  1379. * @return void
  1380. * @expectedException Cake\Controller\Exception\AuthSecurityException
  1381. * @expectedExceptionMessage Action 'NotAllowed::protected' was not found in allowed actions: 'index, view'.
  1382. * @triggers Controller.startup $this->Controller
  1383. */
  1384. public function testAuthRequiredThrowsExceptionActionNotAllowed()
  1385. {
  1386. $this->Controller->Security->config('requireAuth', ['protected']);
  1387. $this->Controller->request->params['controller'] = 'NotAllowed';
  1388. $this->Controller->request->params['action'] = 'protected';
  1389. $this->Controller->request->data = ['_Token' => 'not empty'];
  1390. $this->Controller->request->session()->write('_Token', [
  1391. 'allowedActions' => ['index', 'view']
  1392. ]);
  1393. $event = new Event('Controller.startup', $this->Controller);
  1394. $this->Controller->Security->startup($event);
  1395. $this->Controller->Security->authRequired($this->Controller);
  1396. }
  1397. /**
  1398. * Auth required throws exception controller not allowed
  1399. *
  1400. * @return void
  1401. * @triggers Controller.startup $this->Controller
  1402. */
  1403. public function testAuthRequired()
  1404. {
  1405. $this->Controller->Security->config('requireAuth', ['protected']);
  1406. $this->Controller->request->params['controller'] = 'Allowed';
  1407. $this->Controller->request->params['action'] = 'protected';
  1408. $this->Controller->request->data = ['_Token' => 'not empty'];
  1409. $this->Controller->request->session()->write('_Token', [
  1410. 'allowedActions' => ['protected'],
  1411. 'allowedControllers' => ['Allowed'],
  1412. ]);
  1413. $event = new Event('Controller.startup', $this->Controller);
  1414. $this->Controller->Security->startup($event);
  1415. $this->assertTrue($this->Controller->Security->authRequired($this->Controller));
  1416. }
  1417. }