SecurityComponentTest.php 56 KB

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