SecurityComponentTest.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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\Core\Configure;
  19. use Cake\Event\Event;
  20. use Cake\Network\Request;
  21. use Cake\Network\Session;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\Utility\Security;
  24. /**
  25. * TestSecurityComponent
  26. *
  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. /**
  42. * SecurityTestController
  43. *
  44. */
  45. class SecurityTestController extends Controller
  46. {
  47. /**
  48. * components property
  49. *
  50. * @var array
  51. */
  52. public $components = [
  53. 'TestSecurity' => ['className' => 'Cake\Test\TestCase\Controller\Component\TestSecurityComponent']
  54. ];
  55. /**
  56. * failed property
  57. *
  58. * @var bool
  59. */
  60. public $failed = false;
  61. /**
  62. * Used for keeping track of headers in test
  63. *
  64. * @var array
  65. */
  66. public $testHeaders = [];
  67. /**
  68. * fail method
  69. *
  70. * @return void
  71. */
  72. public function fail()
  73. {
  74. $this->failed = true;
  75. }
  76. /**
  77. * redirect method
  78. *
  79. * @param string|array $url
  80. * @param mixed $status
  81. * @param mixed $exit
  82. * @return void
  83. */
  84. public function redirect($url, $status = null, $exit = true)
  85. {
  86. return $status;
  87. }
  88. /**
  89. * Convenience method for header()
  90. *
  91. * @param string $status
  92. * @return void
  93. */
  94. public function header($status)
  95. {
  96. $this->testHeaders[] = $status;
  97. }
  98. }
  99. /**
  100. * SecurityComponentTest class
  101. *
  102. */
  103. class SecurityComponentTest extends TestCase
  104. {
  105. /**
  106. * Controller property
  107. *
  108. * @var SecurityTestController
  109. */
  110. public $Controller;
  111. /**
  112. * oldSalt property
  113. *
  114. * @var string
  115. */
  116. public $oldSalt;
  117. /**
  118. * setUp method
  119. *
  120. * @return void
  121. */
  122. public function setUp()
  123. {
  124. parent::setUp();
  125. $session = new Session();
  126. $request = $this->getMock('Cake\Network\Request', ['here'], ['posts/index']);
  127. $request->addParams(['controller' => 'posts', 'action' => 'index']);
  128. $request->session($session);
  129. $request->expects($this->any())
  130. ->method('here')
  131. ->will($this->returnValue('/articles/index'));
  132. $this->Controller = new SecurityTestController($request);
  133. $this->Controller->Security = $this->Controller->TestSecurity;
  134. $this->Controller->Security->config('blackHoleCallback', 'fail');
  135. $this->Security = $this->Controller->Security;
  136. $this->Security->session = $session;
  137. Security::salt('foo!');
  138. }
  139. /**
  140. * Tear-down method. Resets environment state.
  141. *
  142. * @return void
  143. */
  144. public function tearDown()
  145. {
  146. parent::tearDown();
  147. $this->Security->session->delete('_Token');
  148. unset($this->Controller->Security);
  149. unset($this->Controller->Component);
  150. unset($this->Controller);
  151. }
  152. /**
  153. * Test that requests are still blackholed when controller has incorrect
  154. * visibility keyword in the blackhole callback
  155. *
  156. * @expectedException \Cake\Network\Exception\BadRequestException
  157. * @return void
  158. * @triggers Controller.startup $Controller, $this->Controller
  159. */
  160. public function testBlackholeWithBrokenCallback()
  161. {
  162. $request = new Request([
  163. 'url' => 'posts/index',
  164. 'session' => $this->Security->session
  165. ]);
  166. $request->addParams([
  167. 'controller' => 'posts',
  168. 'action' => 'index'
  169. ]);
  170. $Controller = new \TestApp\Controller\SomePagesController($request);
  171. $event = new Event('Controller.startup', $Controller, $this->Controller);
  172. $Security = new SecurityComponent($Controller->components());
  173. $Security->config('blackHoleCallback', '_fail');
  174. $Security->startup($event);
  175. $Security->blackHole($Controller, 'csrf');
  176. }
  177. /**
  178. * Ensure that directly requesting the blackholeCallback as the controller
  179. * action results in an exception.
  180. *
  181. * @return void
  182. * @triggers Controller.startup $this->Controller
  183. */
  184. public function testExceptionWhenActionIsBlackholeCallback()
  185. {
  186. $this->Controller->request->addParams([
  187. 'controller' => 'posts',
  188. 'action' => 'fail'
  189. ]);
  190. $event = new Event('Controller.startup', $this->Controller);
  191. $this->assertFalse($this->Controller->failed);
  192. $this->Controller->Security->startup($event);
  193. $this->assertTrue($this->Controller->failed, 'Request was blackholed.');
  194. }
  195. /**
  196. * test that initialize can set properties.
  197. *
  198. * @return void
  199. */
  200. public function testConstructorSettingProperties()
  201. {
  202. $settings = [
  203. 'requireSecure' => ['update_account'],
  204. 'validatePost' => false,
  205. ];
  206. $Security = new SecurityComponent($this->Controller->components(), $settings);
  207. $this->assertEquals($Security->validatePost, $settings['validatePost']);
  208. }
  209. /**
  210. * testStartup method
  211. *
  212. * @return void
  213. * @triggers Controller.startup $this->Controller
  214. */
  215. public function testStartup()
  216. {
  217. $event = new Event('Controller.startup', $this->Controller);
  218. $this->Controller->Security->startup($event);
  219. $this->assertTrue($this->Security->session->check('_Token'));
  220. }
  221. /**
  222. * testRequireSecureFail method
  223. *
  224. * @return void
  225. * @triggers Controller.startup $this->Controller
  226. */
  227. public function testRequireSecureFail()
  228. {
  229. $_SERVER['HTTPS'] = 'off';
  230. $_SERVER['REQUEST_METHOD'] = 'POST';
  231. $this->Controller->request['action'] = 'posted';
  232. $event = new Event('Controller.startup', $this->Controller);
  233. $this->Controller->Security->requireSecure(['posted']);
  234. $this->Controller->Security->startup($event);
  235. $this->assertTrue($this->Controller->failed);
  236. }
  237. /**
  238. * testRequireSecureSucceed method
  239. *
  240. * @return void
  241. * @triggers Controller.startup $this->Controller
  242. */
  243. public function testRequireSecureSucceed()
  244. {
  245. $_SERVER['HTTPS'] = 'on';
  246. $_SERVER['REQUEST_METHOD'] = 'Secure';
  247. $this->Controller->request['action'] = 'posted';
  248. $event = new Event('Controller.startup', $this->Controller);
  249. $this->Controller->Security->requireSecure('posted');
  250. $this->Controller->Security->startup($event);
  251. $this->assertFalse($this->Controller->failed);
  252. }
  253. /**
  254. * testRequireSecureEmptyFail method
  255. *
  256. * @return void
  257. * @triggers Controller.startup $this->Controller
  258. */
  259. public function testRequireSecureEmptyFail()
  260. {
  261. $_SERVER['HTTPS'] = 'off';
  262. $_SERVER['REQUEST_METHOD'] = 'POST';
  263. $this->Controller->request['action'] = 'posted';
  264. $event = new Event('Controller.startup', $this->Controller);
  265. $this->Controller->Security->requireSecure();
  266. $this->Controller->Security->startup($event);
  267. $this->assertTrue($this->Controller->failed);
  268. }
  269. /**
  270. * testRequireSecureEmptySucceed method
  271. *
  272. * @return void
  273. * @triggers Controller.startup $this->Controller
  274. */
  275. public function testRequireSecureEmptySucceed()
  276. {
  277. $_SERVER['HTTPS'] = 'on';
  278. $_SERVER['REQUEST_METHOD'] = 'Secure';
  279. $this->Controller->request['action'] = 'posted';
  280. $event = new Event('Controller.startup', $this->Controller);
  281. $this->Controller->Security->requireSecure();
  282. $this->Controller->Security->startup($event);
  283. $this->assertFalse($this->Controller->failed);
  284. }
  285. /**
  286. * testRequireAuthFail method
  287. *
  288. * @return void
  289. * @triggers Controller.startup $this->Controller
  290. */
  291. public function testRequireAuthFail()
  292. {
  293. $event = new Event('Controller.startup', $this->Controller);
  294. $_SERVER['REQUEST_METHOD'] = 'AUTH';
  295. $this->Controller->request['action'] = 'posted';
  296. $this->Controller->request->data = ['username' => 'willy', 'password' => 'somePass'];
  297. $this->Controller->Security->requireAuth(['posted']);
  298. $this->Controller->Security->startup($event);
  299. $this->assertTrue($this->Controller->failed);
  300. $this->Security->session->write('_Token', ['allowedControllers' => []]);
  301. $this->Controller->request->data = ['username' => 'willy', 'password' => 'somePass'];
  302. $this->Controller->request['action'] = 'posted';
  303. $this->Controller->Security->requireAuth('posted');
  304. $this->Controller->Security->startup($event);
  305. $this->assertTrue($this->Controller->failed);
  306. $this->Security->session->write('_Token', [
  307. 'allowedControllers' => ['SecurityTest'], 'allowedActions' => ['posted2']
  308. ]);
  309. $this->Controller->request->data = ['username' => 'willy', 'password' => 'somePass'];
  310. $this->Controller->request['action'] = 'posted';
  311. $this->Controller->Security->requireAuth('posted');
  312. $this->Controller->Security->startup($event);
  313. $this->assertTrue($this->Controller->failed);
  314. }
  315. /**
  316. * testRequireAuthSucceed method
  317. *
  318. * @return void
  319. * @triggers Controller.startup $this->Controller
  320. */
  321. public function testRequireAuthSucceed()
  322. {
  323. $_SERVER['REQUEST_METHOD'] = 'AUTH';
  324. $this->Controller->Security->config('validatePost', false);
  325. $event = new Event('Controller.startup', $this->Controller);
  326. $this->Controller->request['action'] = 'posted';
  327. $this->Controller->Security->requireAuth('posted');
  328. $this->Controller->Security->startup($event);
  329. $this->assertFalse($this->Controller->failed);
  330. $this->Controller->Security->session->write('_Token', [
  331. 'allowedControllers' => ['SecurityTest'],
  332. 'allowedActions' => ['posted'],
  333. ]);
  334. $this->Controller->request['controller'] = 'SecurityTest';
  335. $this->Controller->request['action'] = 'posted';
  336. $this->Controller->request->data = [
  337. 'username' => 'willy',
  338. 'password' => 'somePass',
  339. '_Token' => ''
  340. ];
  341. $this->Controller->action = 'posted';
  342. $this->Controller->Security->requireAuth('posted');
  343. $this->Controller->Security->startup($event);
  344. $this->assertFalse($this->Controller->failed);
  345. }
  346. /**
  347. * Simple hash validation test
  348. *
  349. * @return void
  350. * @triggers Controller.startup $this->Controller
  351. */
  352. public function testValidatePost()
  353. {
  354. $event = new Event('Controller.startup', $this->Controller);
  355. $this->Controller->Security->startup($event);
  356. $fields = '68730b0747d4889ec2766f9117405f9635f5fd5e%3AModel.valid';
  357. $unlocked = '';
  358. $this->Controller->request->data = [
  359. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  360. '_Token' => compact('fields', 'unlocked')
  361. ];
  362. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  363. }
  364. /**
  365. * Test that validatePost fires on GET with request data.
  366. * This could happen when method overriding is used.
  367. *
  368. * @return void
  369. * @triggers Controller.startup $this->Controller
  370. */
  371. public function testValidatePostOnGetWithData()
  372. {
  373. $event = new Event('Controller.startup', $this->Controller);
  374. $this->Controller->Security->startup($event);
  375. $fields = 'an-invalid-token';
  376. $unlocked = '';
  377. $this->Controller->request->env('REQUEST_METHOD', 'GET');
  378. $this->Controller->request->data = [
  379. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  380. '_Token' => compact('fields', 'unlocked')
  381. ];
  382. $this->Controller->Security->startup($event);
  383. $this->assertTrue($this->Controller->failed);
  384. }
  385. /**
  386. * Test that validatePost fails if you are missing the session information.
  387. *
  388. * @return void
  389. * @triggers Controller.startup $this->Controller
  390. */
  391. public function testValidatePostNoSession()
  392. {
  393. $event = new Event('Controller.startup', $this->Controller);
  394. $this->Controller->Security->startup($event);
  395. $this->Security->session->delete('_Token');
  396. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  397. $this->Controller->request->data = [
  398. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  399. '_Token' => compact('fields')
  400. ];
  401. $this->assertFalse($this->Controller->Security->validatePost($this->Controller));
  402. }
  403. /**
  404. * test that validatePost fails if any of its required fields are missing.
  405. *
  406. * @return void
  407. * @triggers Controller.startup $this->Controller
  408. */
  409. public function testValidatePostFormHacking()
  410. {
  411. $event = new Event('Controller.startup', $this->Controller);
  412. $this->Controller->Security->startup($event);
  413. $unlocked = '';
  414. $this->Controller->request->data = [
  415. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  416. '_Token' => compact('unlocked')
  417. ];
  418. $result = $this->Controller->Security->validatePost($this->Controller);
  419. $this->assertFalse($result, 'validatePost passed when fields were missing. %s');
  420. }
  421. /**
  422. * Test that objects can't be passed into the serialized string. This was a vector for RFI and LFI
  423. * attacks. Thanks to Felix Wilhelm
  424. *
  425. * @return void
  426. * @triggers Controller.startup $this->Controller
  427. */
  428. public function testValidatePostObjectDeserialize()
  429. {
  430. $event = new Event('Controller.startup', $this->Controller);
  431. $this->Controller->Security->startup($event);
  432. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877';
  433. $unlocked = '';
  434. // a corrupted serialized object, so we can see if it ever gets to deserialize
  435. $attack = 'O:3:"App":1:{s:5:"__map";a:1:{s:3:"foo";s:7:"Hacked!";s:1:"fail"}}';
  436. $fields .= urlencode(':' . str_rot13($attack));
  437. $this->Controller->request->data = [
  438. 'Model' => ['username' => 'mark', 'password' => 'foo', 'valid' => '0'],
  439. '_Token' => compact('fields', 'unlocked')
  440. ];
  441. $result = $this->Controller->Security->validatePost($this->Controller);
  442. $this->assertFalse($result, 'validatePost passed when key was missing. %s');
  443. }
  444. /**
  445. * Tests validation post data ignores `_csrfToken`.
  446. *
  447. * @return void
  448. * @triggers Controller.startup $this->Controller
  449. */
  450. public function testValidatePostIgnoresCsrfToken()
  451. {
  452. $event = new Event('Controller.startup', $this->Controller);
  453. $this->Controller->Security->startup($event);
  454. $fields = '8e26ef05379e5402c2c619f37ee91152333a0264%3A';
  455. $unlocked = '';
  456. $this->Controller->request->data = [
  457. '_csrfToken' => 'abc123',
  458. 'Model' => ['multi_field' => ['1', '3']],
  459. '_Token' => compact('fields', 'unlocked')
  460. ];
  461. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  462. }
  463. /**
  464. * Tests validation of checkbox arrays
  465. *
  466. * @return void
  467. * @triggers Controller.startup $this->Controller
  468. */
  469. public function testValidatePostArray()
  470. {
  471. $event = new Event('Controller.startup', $this->Controller);
  472. $this->Controller->Security->startup($event);
  473. $fields = '8e26ef05379e5402c2c619f37ee91152333a0264%3A';
  474. $unlocked = '';
  475. $this->Controller->request->data = [
  476. 'Model' => ['multi_field' => ['1', '3']],
  477. '_Token' => compact('fields', 'unlocked')
  478. ];
  479. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  480. $this->Controller->request->data = [
  481. 'Model' => ['multi_field' => [12 => '1', 20 => '3']],
  482. '_Token' => compact('fields', 'unlocked')
  483. ];
  484. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  485. }
  486. /**
  487. * Tests validation of integer field names.
  488. *
  489. * @return void
  490. */
  491. public function testValidateIntFieldName()
  492. {
  493. $event = new Event('Controller.startup', $this->Controller);
  494. $this->Controller->Security->startup($event);
  495. $fields = '4a221010dd7a23f7166cb10c38bc21d81341c387%3A';
  496. $unlocked = '';
  497. $this->Controller->request->data = [
  498. 1 => 'value,',
  499. '_Token' => compact('fields', 'unlocked')
  500. ];
  501. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  502. }
  503. /**
  504. * testValidatePostNoModel method
  505. *
  506. * @return void
  507. * @triggers Controller.startup $this->Controller
  508. */
  509. public function testValidatePostNoModel()
  510. {
  511. $event = new Event('Controller.startup', $this->Controller);
  512. $this->Controller->Security->startup($event);
  513. $fields = 'a1c3724b7ba85e7022413611e30ba2c6181d5aba%3A';
  514. $unlocked = '';
  515. $this->Controller->request->data = [
  516. 'anything' => 'some_data',
  517. '_Token' => compact('fields', 'unlocked')
  518. ];
  519. $result = $this->Controller->Security->validatePost($this->Controller);
  520. $this->assertTrue($result);
  521. }
  522. /**
  523. * testValidatePostSimple method
  524. *
  525. * @return void
  526. * @triggers Controller.startup $this->Controller
  527. */
  528. public function testValidatePostSimple()
  529. {
  530. $event = new Event('Controller.startup', $this->Controller);
  531. $this->Controller->Security->startup($event);
  532. $fields = 'b0914d06dfb04abf1fada53e16810e87d157950b%3A';
  533. $unlocked = '';
  534. $this->Controller->request->data = [
  535. 'Model' => ['username' => '', 'password' => ''],
  536. '_Token' => compact('fields', 'unlocked')
  537. ];
  538. $result = $this->Controller->Security->validatePost($this->Controller);
  539. $this->assertTrue($result);
  540. }
  541. /**
  542. * Tests hash validation for multiple records, including locked fields
  543. *
  544. * @return void
  545. * @triggers Controller.startup $this->Controller
  546. */
  547. public function testValidatePostComplex()
  548. {
  549. $event = new Event('Controller.startup', $this->Controller);
  550. $this->Controller->Security->startup($event);
  551. $fields = 'b65c7463e44a61d8d2eaecce2c265b406c9c4742%3AAddresses.0.id%7CAddresses.1.id';
  552. $unlocked = '';
  553. $this->Controller->request->data = [
  554. 'Addresses' => [
  555. '0' => [
  556. 'id' => '123456', 'title' => '', 'first_name' => '', 'last_name' => '',
  557. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  558. ],
  559. '1' => [
  560. 'id' => '654321', 'title' => '', 'first_name' => '', 'last_name' => '',
  561. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  562. ]
  563. ],
  564. '_Token' => compact('fields', 'unlocked')
  565. ];
  566. $result = $this->Controller->Security->validatePost($this->Controller);
  567. $this->assertTrue($result);
  568. }
  569. /**
  570. * test ValidatePost with multiple select elements.
  571. *
  572. * @return void
  573. * @triggers Controller.startup $this->Controller
  574. */
  575. public function testValidatePostMultipleSelect()
  576. {
  577. $event = new Event('Controller.startup', $this->Controller);
  578. $this->Controller->Security->startup($event);
  579. $fields = '8d8da68ba03b3d6e7e145b948abfe26741422169%3A';
  580. $unlocked = '';
  581. $this->Controller->request->data = [
  582. 'Tag' => ['Tag' => [1, 2]],
  583. '_Token' => compact('fields', 'unlocked'),
  584. ];
  585. $result = $this->Controller->Security->validatePost($this->Controller);
  586. $this->assertTrue($result);
  587. $this->Controller->request->data = [
  588. 'Tag' => ['Tag' => [1, 2, 3]],
  589. '_Token' => compact('fields', 'unlocked'),
  590. ];
  591. $result = $this->Controller->Security->validatePost($this->Controller);
  592. $this->assertTrue($result);
  593. $this->Controller->request->data = [
  594. 'Tag' => ['Tag' => [1, 2, 3, 4]],
  595. '_Token' => compact('fields', 'unlocked'),
  596. ];
  597. $result = $this->Controller->Security->validatePost($this->Controller);
  598. $this->assertTrue($result);
  599. $fields = 'eae2adda1628b771a30cc133342d16220c6520fe%3A';
  600. $this->Controller->request->data = [
  601. 'User.password' => 'bar', 'User.name' => 'foo', 'User.is_valid' => '1',
  602. 'Tag' => ['Tag' => [1]],
  603. '_Token' => compact('fields', 'unlocked'),
  604. ];
  605. $result = $this->Controller->Security->validatePost($this->Controller);
  606. $this->assertTrue($result);
  607. }
  608. /**
  609. * testValidatePostCheckbox method
  610. *
  611. * First block tests un-checked checkbox
  612. * Second block tests checked checkbox
  613. *
  614. * @return void
  615. * @triggers Controller.startup $this->Controller
  616. */
  617. public function testValidatePostCheckbox()
  618. {
  619. $event = new Event('Controller.startup', $this->Controller);
  620. $this->Controller->Security->startup($event);
  621. $fields = '68730b0747d4889ec2766f9117405f9635f5fd5e%3AModel.valid';
  622. $unlocked = '';
  623. $this->Controller->request->data = [
  624. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  625. '_Token' => compact('fields', 'unlocked')
  626. ];
  627. $result = $this->Controller->Security->validatePost($this->Controller);
  628. $this->assertTrue($result);
  629. $fields = 'f63e4a69b2edd31f064e8e602a04dd59307cfe9c%3A';
  630. $this->Controller->request->data = [
  631. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  632. '_Token' => compact('fields', 'unlocked')
  633. ];
  634. $result = $this->Controller->Security->validatePost($this->Controller);
  635. $this->assertTrue($result);
  636. $this->Controller->request->data = [];
  637. $this->Controller->Security->startup($event);
  638. $this->Controller->request->data = [
  639. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  640. '_Token' => compact('fields', 'unlocked')
  641. ];
  642. $result = $this->Controller->Security->validatePost($this->Controller);
  643. $this->assertTrue($result);
  644. }
  645. /**
  646. * testValidatePostHidden method
  647. *
  648. * @return void
  649. * @triggers Controller.startup $this->Controller
  650. */
  651. public function testValidatePostHidden()
  652. {
  653. $event = new Event('Controller.startup', $this->Controller);
  654. $this->Controller->Security->startup($event);
  655. $fields = '973a8939a68ac014cc6f7666cec9aa6268507350%3AModel.hidden%7CModel.other_hidden';
  656. $unlocked = '';
  657. $this->Controller->request->data = [
  658. 'Model' => [
  659. 'username' => '', 'password' => '', 'hidden' => '0',
  660. 'other_hidden' => 'some hidden value'
  661. ],
  662. '_Token' => compact('fields', 'unlocked')
  663. ];
  664. $result = $this->Controller->Security->validatePost($this->Controller);
  665. $this->assertTrue($result);
  666. }
  667. /**
  668. * testValidatePostWithDisabledFields method
  669. *
  670. * @return void
  671. * @triggers Controller.startup $this->Controller
  672. */
  673. public function testValidatePostWithDisabledFields()
  674. {
  675. $event = new Event('Controller.startup', $this->Controller);
  676. $this->Controller->Security->config('disabledFields', ['Model.username', 'Model.password']);
  677. $this->Controller->Security->startup($event);
  678. $fields = '1c59acfbca98bd870c11fb544d545cbf23215880%3AModel.hidden';
  679. $unlocked = '';
  680. $this->Controller->request->data = [
  681. 'Model' => [
  682. 'username' => '', 'password' => '', 'hidden' => '0'
  683. ],
  684. '_Token' => compact('fields', 'unlocked')
  685. ];
  686. $result = $this->Controller->Security->validatePost($this->Controller);
  687. $this->assertTrue($result);
  688. }
  689. /**
  690. * test validating post data with posted unlocked fields.
  691. *
  692. * @return void
  693. * @triggers Controller.startup $this->Controller
  694. */
  695. public function testValidatePostDisabledFieldsInData()
  696. {
  697. $event = new Event('Controller.startup', $this->Controller);
  698. $this->Controller->Security->startup($event);
  699. $unlocked = 'Model.username';
  700. $fields = ['Model.hidden', 'Model.password'];
  701. $fields = urlencode(Security::hash('/articles/index' . serialize($fields) . $unlocked . Security::salt()));
  702. $this->Controller->request->data = [
  703. 'Model' => [
  704. 'username' => 'mark',
  705. 'password' => 'sekret',
  706. 'hidden' => '0'
  707. ],
  708. '_Token' => compact('fields', 'unlocked')
  709. ];
  710. $result = $this->Controller->Security->validatePost($this->Controller);
  711. $this->assertTrue($result);
  712. }
  713. /**
  714. * test that missing 'unlocked' input causes failure
  715. *
  716. * @return void
  717. * @triggers Controller.startup $this->Controller
  718. */
  719. public function testValidatePostFailNoDisabled()
  720. {
  721. $event = new Event('Controller.startup', $this->Controller);
  722. $this->Controller->Security->startup($event);
  723. $fields = ['Model.hidden', 'Model.password', 'Model.username'];
  724. $fields = urlencode(Security::hash(serialize($fields) . Security::salt()));
  725. $this->Controller->request->data = [
  726. 'Model' => [
  727. 'username' => 'mark',
  728. 'password' => 'sekret',
  729. 'hidden' => '0'
  730. ],
  731. '_Token' => compact('fields')
  732. ];
  733. $result = $this->Controller->Security->validatePost($this->Controller);
  734. $this->assertFalse($result);
  735. }
  736. /**
  737. * Test that validatePost fails when unlocked fields are changed.
  738. *
  739. * @return void
  740. * @triggers Controller.startup $this->Controller
  741. */
  742. public function testValidatePostFailDisabledFieldTampering()
  743. {
  744. $event = new Event('Controller.startup', $this->Controller);
  745. $this->Controller->Security->startup($event);
  746. $unlocked = 'Model.username';
  747. $fields = ['Model.hidden', 'Model.password'];
  748. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::salt()));
  749. // Tamper the values.
  750. $unlocked = 'Model.username|Model.password';
  751. $this->Controller->request->data = [
  752. 'Model' => [
  753. 'username' => 'mark',
  754. 'password' => 'sekret',
  755. 'hidden' => '0'
  756. ],
  757. '_Token' => compact('fields', 'unlocked')
  758. ];
  759. $result = $this->Controller->Security->validatePost($this->Controller);
  760. $this->assertFalse($result);
  761. }
  762. /**
  763. * testValidateHiddenMultipleModel method
  764. *
  765. * @return void
  766. * @triggers Controller.startup $this->Controller
  767. */
  768. public function testValidateHiddenMultipleModel()
  769. {
  770. $event = new Event('Controller.startup', $this->Controller);
  771. $this->Controller->Security->startup($event);
  772. $fields = '075ca6c26c38a09a78d871201df89faf52cbbeb8%3AModel.valid%7CModel2.valid%7CModel3.valid';
  773. $unlocked = '';
  774. $this->Controller->request->data = [
  775. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  776. 'Model2' => ['valid' => '0'],
  777. 'Model3' => ['valid' => '0'],
  778. '_Token' => compact('fields', 'unlocked')
  779. ];
  780. $result = $this->Controller->Security->validatePost($this->Controller);
  781. $this->assertTrue($result);
  782. }
  783. /**
  784. * testValidateHasManyModel method
  785. *
  786. * @return void
  787. * @triggers Controller.startup $this->Controller
  788. */
  789. public function testValidateHasManyModel()
  790. {
  791. $event = new Event('Controller.startup', $this->Controller);
  792. $this->Controller->Security->startup($event);
  793. $fields = '24a753fb62ef7839389987b58e3f7108f564e529%3AModel.0.hidden%7CModel.0.valid';
  794. $fields .= '%7CModel.1.hidden%7CModel.1.valid';
  795. $unlocked = '';
  796. $this->Controller->request->data = [
  797. 'Model' => [
  798. [
  799. 'username' => 'username', 'password' => 'password',
  800. 'hidden' => 'value', 'valid' => '0'
  801. ],
  802. [
  803. 'username' => 'username', 'password' => 'password',
  804. 'hidden' => 'value', 'valid' => '0'
  805. ]
  806. ],
  807. '_Token' => compact('fields', 'unlocked')
  808. ];
  809. $result = $this->Controller->Security->validatePost($this->Controller);
  810. $this->assertTrue($result);
  811. }
  812. /**
  813. * testValidateHasManyRecordsPass method
  814. *
  815. * @return void
  816. * @triggers Controller.startup $this->Controller
  817. */
  818. public function testValidateHasManyRecordsPass()
  819. {
  820. $event = new Event('Controller.startup', $this->Controller);
  821. $this->Controller->Security->startup($event);
  822. $fields = '8f7d82bf7656cf068822d9bdab109ebed1be1825%3AAddress.0.id%7CAddress.0.primary%7C';
  823. $fields .= 'Address.1.id%7CAddress.1.primary';
  824. $unlocked = '';
  825. $this->Controller->request->data = [
  826. 'Address' => [
  827. 0 => [
  828. 'id' => '123',
  829. 'title' => 'home',
  830. 'first_name' => 'Bilbo',
  831. 'last_name' => 'Baggins',
  832. 'address' => '23 Bag end way',
  833. 'city' => 'the shire',
  834. 'phone' => 'N/A',
  835. 'primary' => '1',
  836. ],
  837. 1 => [
  838. 'id' => '124',
  839. 'title' => 'home',
  840. 'first_name' => 'Frodo',
  841. 'last_name' => 'Baggins',
  842. 'address' => '50 Bag end way',
  843. 'city' => 'the shire',
  844. 'phone' => 'N/A',
  845. 'primary' => '1'
  846. ]
  847. ],
  848. '_Token' => compact('fields', 'unlocked')
  849. ];
  850. $result = $this->Controller->Security->validatePost($this->Controller);
  851. $this->assertTrue($result);
  852. }
  853. /**
  854. * Test that values like Foo.0.1
  855. *
  856. * @return void
  857. * @triggers Controller.startup $this->Controller
  858. */
  859. public function testValidateNestedNumericSets()
  860. {
  861. $event = new Event('Controller.startup', $this->Controller);
  862. $this->Controller->Security->startup($event);
  863. $unlocked = '';
  864. $hashFields = ['TaxonomyData'];
  865. $fields = urlencode(Security::hash('/articles/index' . serialize($hashFields) . $unlocked . Security::salt()));
  866. $this->Controller->request->data = [
  867. 'TaxonomyData' => [
  868. 1 => [[2]],
  869. 2 => [[3]]
  870. ],
  871. '_Token' => compact('fields', 'unlocked')
  872. ];
  873. $result = $this->Controller->Security->validatePost($this->Controller);
  874. $this->assertTrue($result);
  875. }
  876. /**
  877. * testValidateHasManyRecords method
  878. *
  879. * validatePost should fail, hidden fields have been changed.
  880. *
  881. * @return void
  882. * @triggers Controller.startup $this->Controller
  883. */
  884. public function testValidateHasManyRecordsFail()
  885. {
  886. $event = new Event('Controller.startup', $this->Controller);
  887. $this->Controller->Security->startup($event);
  888. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
  889. $fields .= 'Address.1.id%7CAddress.1.primary';
  890. $unlocked = '';
  891. $this->Controller->request->data = [
  892. 'Address' => [
  893. 0 => [
  894. 'id' => '123',
  895. 'title' => 'home',
  896. 'first_name' => 'Bilbo',
  897. 'last_name' => 'Baggins',
  898. 'address' => '23 Bag end way',
  899. 'city' => 'the shire',
  900. 'phone' => 'N/A',
  901. 'primary' => '5',
  902. ],
  903. 1 => [
  904. 'id' => '124',
  905. 'title' => 'home',
  906. 'first_name' => 'Frodo',
  907. 'last_name' => 'Baggins',
  908. 'address' => '50 Bag end way',
  909. 'city' => 'the shire',
  910. 'phone' => 'N/A',
  911. 'primary' => '1'
  912. ]
  913. ],
  914. '_Token' => compact('fields', 'unlocked')
  915. ];
  916. $result = $this->Controller->Security->validatePost($this->Controller);
  917. $this->assertFalse($result);
  918. }
  919. /**
  920. * testFormDisabledFields method
  921. *
  922. * @return void
  923. * @triggers Controller.startup $this->Controller
  924. */
  925. public function testFormDisabledFields()
  926. {
  927. $event = new Event('Controller.startup', $this->Controller);
  928. $this->Controller->Security->startup($event);
  929. $fields = '9da2b3fa2b5b8ac0bfbc1bbce145e58059629125%3An%3A0%3A%7B%7D';
  930. $unlocked = '';
  931. $this->Controller->request->data = [
  932. 'MyModel' => ['name' => 'some data'],
  933. '_Token' => compact('fields', 'unlocked')
  934. ];
  935. $result = $this->Controller->Security->validatePost($this->Controller);
  936. $this->assertFalse($result);
  937. $this->Controller->Security->startup($event);
  938. $this->Controller->Security->config('disabledFields', ['MyModel.name']);
  939. $this->Controller->request->data = [
  940. 'MyModel' => ['name' => 'some data'],
  941. '_Token' => compact('fields', 'unlocked')
  942. ];
  943. $result = $this->Controller->Security->validatePost($this->Controller);
  944. $this->assertTrue($result);
  945. }
  946. /**
  947. * test validatePost with radio buttons
  948. *
  949. * @return void
  950. * @triggers Controller.startup $this->Controller
  951. */
  952. public function testValidatePostRadio()
  953. {
  954. $event = new Event('Controller.startup', $this->Controller);
  955. $this->Controller->Security->startup($event);
  956. $fields = 'c2226a8879c3f4b513691295fc2519a29c44c8bb%3An%3A0%3A%7B%7D';
  957. $unlocked = '';
  958. $this->Controller->request->data = [
  959. '_Token' => compact('fields', 'unlocked')
  960. ];
  961. $result = $this->Controller->Security->validatePost($this->Controller);
  962. $this->assertFalse($result);
  963. $this->Controller->request->data = [
  964. '_Token' => compact('fields', 'unlocked'),
  965. 'Test' => ['test' => '']
  966. ];
  967. $result = $this->Controller->Security->validatePost($this->Controller);
  968. $this->assertTrue($result);
  969. $this->Controller->request->data = [
  970. '_Token' => compact('fields', 'unlocked'),
  971. 'Test' => ['test' => '1']
  972. ];
  973. $result = $this->Controller->Security->validatePost($this->Controller);
  974. $this->assertTrue($result);
  975. $this->Controller->request->data = [
  976. '_Token' => compact('fields', 'unlocked'),
  977. 'Test' => ['test' => '2']
  978. ];
  979. $result = $this->Controller->Security->validatePost($this->Controller);
  980. $this->assertTrue($result);
  981. }
  982. /**
  983. * test validatePost uses here() as a hash input.
  984. *
  985. * @return void
  986. * @triggers Controller.startup $this->Controller
  987. */
  988. public function testValidatePostUrlAsHashInput()
  989. {
  990. $event = new Event('Controller.startup', $this->Controller);
  991. $this->Security->startup($event);
  992. $fields = 'b0914d06dfb04abf1fada53e16810e87d157950b%3A';
  993. $unlocked = '';
  994. $this->Controller->request->data = [
  995. 'Model' => ['username' => '', 'password' => ''],
  996. '_Token' => compact('fields', 'unlocked')
  997. ];
  998. $this->assertTrue($this->Security->validatePost($this->Controller));
  999. $request = $this->getMock('Cake\Network\Request', ['here']);
  1000. $request->expects($this->at(0))
  1001. ->method('here')
  1002. ->will($this->returnValue('/posts/index?page=1'));
  1003. $request->expects($this->at(1))
  1004. ->method('here')
  1005. ->will($this->returnValue('/posts/edit/1'));
  1006. $request->data = $this->Controller->request->data;
  1007. $this->Controller->request = $request;
  1008. $this->assertFalse($this->Security->validatePost($this->Controller));
  1009. $this->assertFalse($this->Security->validatePost($this->Controller));
  1010. }
  1011. /**
  1012. * test that blackhole doesn't delete the _Token session key so repeat data submissions
  1013. * stay blackholed.
  1014. *
  1015. * @return void
  1016. * @triggers Controller.startup $this->Controller
  1017. */
  1018. public function testBlackHoleNotDeletingSessionInformation()
  1019. {
  1020. $event = new Event('Controller.startup', $this->Controller);
  1021. $this->Controller->Security->startup($event);
  1022. $this->Controller->Security->blackHole($this->Controller, 'auth');
  1023. $this->assertTrue($this->Controller->Security->session->check('_Token'), '_Token was deleted by blackHole %s');
  1024. }
  1025. /**
  1026. * Test generateToken()
  1027. *
  1028. * @return void
  1029. */
  1030. public function testGenerateToken()
  1031. {
  1032. $request = $this->Controller->request;
  1033. $this->Security->generateToken($request);
  1034. $this->assertNotEmpty($request->params['_Token']);
  1035. $this->assertTrue(isset($request->params['_Token']['unlockedFields']));
  1036. }
  1037. /**
  1038. * Test unlocked actions
  1039. *
  1040. * @return void
  1041. * @triggers Controller.startup $this->Controller
  1042. */
  1043. public function testUnlockedActions()
  1044. {
  1045. $_SERVER['REQUEST_METHOD'] = 'POST';
  1046. $event = new Event('Controller.startup', $this->Controller);
  1047. $this->Controller->request->data = ['data'];
  1048. $this->Controller->Security->unlockedActions = 'index';
  1049. $this->Controller->Security->blackHoleCallback = null;
  1050. $result = $this->Controller->Security->startup($event);
  1051. $this->assertNull($result);
  1052. }
  1053. }