SecurityComponentTest.php 29 KB

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