SecurityComponentTest.php 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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\Network\Exception\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. $this->Controller->request['action'] = 'posted';
  216. $event = new Event('Controller.startup', $this->Controller);
  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['HTTPS'] = 'on';
  228. $_SERVER['REQUEST_METHOD'] = 'Secure';
  229. $this->Controller->request['action'] = 'posted';
  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. * testRequireSecureEmptyFail method
  237. *
  238. * @return void
  239. */
  240. public function testRequireSecureEmptyFail() {
  241. $_SERVER['HTTPS'] = 'off';
  242. $_SERVER['REQUEST_METHOD'] = 'POST';
  243. $this->Controller->request['action'] = 'posted';
  244. $event = new Event('Controller.startup', $this->Controller);
  245. $this->Controller->Security->requireSecure();
  246. $this->Controller->Security->startup($event);
  247. $this->assertTrue($this->Controller->failed);
  248. }
  249. /**
  250. * testRequireSecureEmptySucceed method
  251. *
  252. * @return void
  253. */
  254. public function testRequireSecureEmptySucceed() {
  255. $_SERVER['HTTPS'] = 'on';
  256. $_SERVER['REQUEST_METHOD'] = 'Secure';
  257. $this->Controller->request['action'] = 'posted';
  258. $event = new Event('Controller.startup', $this->Controller);
  259. $this->Controller->Security->requireSecure();
  260. $this->Controller->Security->startup($event);
  261. $this->assertFalse($this->Controller->failed);
  262. }
  263. /**
  264. * testRequireAuthFail method
  265. *
  266. * @return void
  267. */
  268. public function testRequireAuthFail() {
  269. $event = new Event('Controller.startup', $this->Controller);
  270. $_SERVER['REQUEST_METHOD'] = 'AUTH';
  271. $this->Controller->request['action'] = 'posted';
  272. $this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
  273. $this->Controller->Security->requireAuth(array('posted'));
  274. $this->Controller->Security->startup($event);
  275. $this->assertTrue($this->Controller->failed);
  276. $this->Security->session->write('_Token', array('allowedControllers' => array()));
  277. $this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
  278. $this->Controller->request['action'] = 'posted';
  279. $this->Controller->Security->requireAuth('posted');
  280. $this->Controller->Security->startup($event);
  281. $this->assertTrue($this->Controller->failed);
  282. $this->Security->session->write('_Token', array(
  283. 'allowedControllers' => array('SecurityTest'), 'allowedActions' => array('posted2')
  284. ));
  285. $this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
  286. $this->Controller->request['action'] = 'posted';
  287. $this->Controller->Security->requireAuth('posted');
  288. $this->Controller->Security->startup($event);
  289. $this->assertTrue($this->Controller->failed);
  290. }
  291. /**
  292. * testRequireAuthSucceed method
  293. *
  294. * @return void
  295. */
  296. public function testRequireAuthSucceed() {
  297. $_SERVER['REQUEST_METHOD'] = 'AUTH';
  298. $event = new Event('Controller.startup', $this->Controller);
  299. $this->Controller->request['action'] = 'posted';
  300. $this->Controller->Security->requireAuth('posted');
  301. $this->Controller->Security->startup($event);
  302. $this->assertFalse($this->Controller->failed);
  303. $this->Controller->Security->session->write('_Token', array(
  304. 'allowedControllers' => array('SecurityTest'), 'allowedActions' => array('posted')
  305. ));
  306. $this->Controller->request['controller'] = 'SecurityTest';
  307. $this->Controller->request['action'] = 'posted';
  308. $this->Controller->request->data = array(
  309. 'username' => 'willy', 'password' => 'somePass', '_Token' => ''
  310. );
  311. $this->Controller->action = 'posted';
  312. $this->Controller->Security->requireAuth('posted');
  313. $this->Controller->Security->startup($event);
  314. $this->assertFalse($this->Controller->failed);
  315. }
  316. /**
  317. * Simple hash validation test
  318. *
  319. * @return void
  320. */
  321. public function testValidatePost() {
  322. $event = new Event('Controller.startup', $this->Controller);
  323. $this->Controller->Security->startup($event);
  324. $fields = '68730b0747d4889ec2766f9117405f9635f5fd5e%3AModel.valid';
  325. $unlocked = '';
  326. $this->Controller->request->data = array(
  327. 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
  328. '_Token' => compact('fields', 'unlocked')
  329. );
  330. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  331. }
  332. /**
  333. * Test that validatePost fails if you are missing the session information.
  334. *
  335. * @return void
  336. */
  337. public function testValidatePostNoSession() {
  338. $event = new Event('Controller.startup', $this->Controller);
  339. $this->Controller->Security->startup($event);
  340. $this->Security->session->delete('_Token');
  341. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  342. $this->Controller->request->data = array(
  343. 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
  344. '_Token' => compact('fields')
  345. );
  346. $this->assertFalse($this->Controller->Security->validatePost($this->Controller));
  347. }
  348. /**
  349. * test that validatePost fails if any of its required fields are missing.
  350. *
  351. * @return void
  352. */
  353. public function testValidatePostFormHacking() {
  354. $event = new Event('Controller.startup', $this->Controller);
  355. $this->Controller->Security->startup($event);
  356. $unlocked = '';
  357. $this->Controller->request->data = array(
  358. 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
  359. '_Token' => compact('unlocked')
  360. );
  361. $result = $this->Controller->Security->validatePost($this->Controller);
  362. $this->assertFalse($result, 'validatePost passed when fields were missing. %s');
  363. }
  364. /**
  365. * Test that objects can't be passed into the serialized string. This was a vector for RFI and LFI
  366. * attacks. Thanks to Felix Wilhelm
  367. *
  368. * @return void
  369. */
  370. public function testValidatePostObjectDeserialize() {
  371. $event = new Event('Controller.startup', $this->Controller);
  372. $this->Controller->Security->startup($event);
  373. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877';
  374. $unlocked = '';
  375. // a corrupted serialized object, so we can see if it ever gets to deserialize
  376. $attack = 'O:3:"App":1:{s:5:"__map";a:1:{s:3:"foo";s:7:"Hacked!";s:1:"fail"}}';
  377. $fields .= urlencode(':' . str_rot13($attack));
  378. $this->Controller->request->data = array(
  379. 'Model' => array('username' => 'mark', 'password' => 'foo', 'valid' => '0'),
  380. '_Token' => compact('fields', 'unlocked')
  381. );
  382. $result = $this->Controller->Security->validatePost($this->Controller);
  383. $this->assertFalse($result, 'validatePost passed when key was missing. %s');
  384. }
  385. /**
  386. * Tests validation of checkbox arrays
  387. *
  388. * @return void
  389. */
  390. public function testValidatePostArray() {
  391. $event = new Event('Controller.startup', $this->Controller);
  392. $this->Controller->Security->startup($event);
  393. $fields = '8e26ef05379e5402c2c619f37ee91152333a0264%3A';
  394. $unlocked = '';
  395. $this->Controller->request->data = array(
  396. 'Model' => array('multi_field' => array('1', '3')),
  397. '_Token' => compact('fields', 'unlocked')
  398. );
  399. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  400. }
  401. /**
  402. * testValidatePostNoModel method
  403. *
  404. * @return void
  405. */
  406. public function testValidatePostNoModel() {
  407. $event = new Event('Controller.startup', $this->Controller);
  408. $this->Controller->Security->startup($event);
  409. $fields = 'a1c3724b7ba85e7022413611e30ba2c6181d5aba%3A';
  410. $unlocked = '';
  411. $this->Controller->request->data = array(
  412. 'anything' => 'some_data',
  413. '_Token' => compact('fields', 'unlocked')
  414. );
  415. $result = $this->Controller->Security->validatePost($this->Controller);
  416. $this->assertTrue($result);
  417. }
  418. /**
  419. * testValidatePostSimple method
  420. *
  421. * @return void
  422. */
  423. public function testValidatePostSimple() {
  424. $event = new Event('Controller.startup', $this->Controller);
  425. $this->Controller->Security->startup($event);
  426. $fields = 'b0914d06dfb04abf1fada53e16810e87d157950b%3A';
  427. $unlocked = '';
  428. $this->Controller->request->data = array(
  429. 'Model' => array('username' => '', 'password' => ''),
  430. '_Token' => compact('fields', 'unlocked')
  431. );
  432. $result = $this->Controller->Security->validatePost($this->Controller);
  433. $this->assertTrue($result);
  434. }
  435. /**
  436. * Tests hash validation for multiple records, including locked fields
  437. *
  438. * @return void
  439. */
  440. public function testValidatePostComplex() {
  441. $event = new Event('Controller.startup', $this->Controller);
  442. $this->Controller->Security->startup($event);
  443. $fields = 'b65c7463e44a61d8d2eaecce2c265b406c9c4742%3AAddresses.0.id%7CAddresses.1.id';
  444. $unlocked = '';
  445. $this->Controller->request->data = array(
  446. 'Addresses' => array(
  447. '0' => array(
  448. 'id' => '123456', 'title' => '', 'first_name' => '', 'last_name' => '',
  449. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  450. ),
  451. '1' => array(
  452. 'id' => '654321', 'title' => '', 'first_name' => '', 'last_name' => '',
  453. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  454. )
  455. ),
  456. '_Token' => compact('fields', 'unlocked')
  457. );
  458. $result = $this->Controller->Security->validatePost($this->Controller);
  459. $this->assertTrue($result);
  460. }
  461. /**
  462. * test ValidatePost with multiple select elements.
  463. *
  464. * @return void
  465. */
  466. public function testValidatePostMultipleSelect() {
  467. $event = new Event('Controller.startup', $this->Controller);
  468. $this->Controller->Security->startup($event);
  469. $fields = '8d8da68ba03b3d6e7e145b948abfe26741422169%3A';
  470. $unlocked = '';
  471. $this->Controller->request->data = array(
  472. 'Tag' => array('Tag' => array(1, 2)),
  473. '_Token' => compact('fields', 'unlocked'),
  474. );
  475. $result = $this->Controller->Security->validatePost($this->Controller);
  476. $this->assertTrue($result);
  477. $this->Controller->request->data = array(
  478. 'Tag' => array('Tag' => array(1, 2, 3)),
  479. '_Token' => compact('fields', 'unlocked'),
  480. );
  481. $result = $this->Controller->Security->validatePost($this->Controller);
  482. $this->assertTrue($result);
  483. $this->Controller->request->data = array(
  484. 'Tag' => array('Tag' => array(1, 2, 3, 4)),
  485. '_Token' => compact('fields', 'unlocked'),
  486. );
  487. $result = $this->Controller->Security->validatePost($this->Controller);
  488. $this->assertTrue($result);
  489. $fields = 'eae2adda1628b771a30cc133342d16220c6520fe%3A';
  490. $this->Controller->request->data = array(
  491. 'User.password' => 'bar', 'User.name' => 'foo', 'User.is_valid' => '1',
  492. 'Tag' => array('Tag' => array(1)),
  493. '_Token' => compact('fields', 'unlocked'),
  494. );
  495. $result = $this->Controller->Security->validatePost($this->Controller);
  496. $this->assertTrue($result);
  497. }
  498. /**
  499. * testValidatePostCheckbox method
  500. *
  501. * First block tests un-checked checkbox
  502. * Second block tests checked checkbox
  503. *
  504. * @return void
  505. */
  506. public function testValidatePostCheckbox() {
  507. $event = new Event('Controller.startup', $this->Controller);
  508. $this->Controller->Security->startup($event);
  509. $fields = '68730b0747d4889ec2766f9117405f9635f5fd5e%3AModel.valid';
  510. $unlocked = '';
  511. $this->Controller->request->data = array(
  512. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  513. '_Token' => compact('fields', 'unlocked')
  514. );
  515. $result = $this->Controller->Security->validatePost($this->Controller);
  516. $this->assertTrue($result);
  517. $fields = 'f63e4a69b2edd31f064e8e602a04dd59307cfe9c%3A';
  518. $this->Controller->request->data = array(
  519. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  520. '_Token' => compact('fields', 'unlocked')
  521. );
  522. $result = $this->Controller->Security->validatePost($this->Controller);
  523. $this->assertTrue($result);
  524. $this->Controller->request->data = array();
  525. $this->Controller->Security->startup($event);
  526. $this->Controller->request->data = array(
  527. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  528. '_Token' => compact('fields', 'unlocked')
  529. );
  530. $result = $this->Controller->Security->validatePost($this->Controller);
  531. $this->assertTrue($result);
  532. }
  533. /**
  534. * testValidatePostHidden method
  535. *
  536. * @return void
  537. */
  538. public function testValidatePostHidden() {
  539. $event = new Event('Controller.startup', $this->Controller);
  540. $this->Controller->Security->startup($event);
  541. $fields = '973a8939a68ac014cc6f7666cec9aa6268507350%3AModel.hidden%7CModel.other_hidden';
  542. $unlocked = '';
  543. $this->Controller->request->data = array(
  544. 'Model' => array(
  545. 'username' => '', 'password' => '', 'hidden' => '0',
  546. 'other_hidden' => 'some hidden value'
  547. ),
  548. '_Token' => compact('fields', 'unlocked')
  549. );
  550. $result = $this->Controller->Security->validatePost($this->Controller);
  551. $this->assertTrue($result);
  552. }
  553. /**
  554. * testValidatePostWithDisabledFields method
  555. *
  556. * @return void
  557. */
  558. public function testValidatePostWithDisabledFields() {
  559. $event = new Event('Controller.startup', $this->Controller);
  560. $this->Controller->Security->config('disabledFields', ['Model.username', 'Model.password']);
  561. $this->Controller->Security->startup($event);
  562. $fields = '1c59acfbca98bd870c11fb544d545cbf23215880%3AModel.hidden';
  563. $unlocked = '';
  564. $this->Controller->request->data = array(
  565. 'Model' => array(
  566. 'username' => '', 'password' => '', 'hidden' => '0'
  567. ),
  568. '_Token' => compact('fields', 'unlocked')
  569. );
  570. $result = $this->Controller->Security->validatePost($this->Controller);
  571. $this->assertTrue($result);
  572. }
  573. /**
  574. * test validating post data with posted unlocked fields.
  575. *
  576. * @return void
  577. */
  578. public function testValidatePostDisabledFieldsInData() {
  579. $event = new Event('Controller.startup', $this->Controller);
  580. $this->Controller->Security->startup($event);
  581. $unlocked = 'Model.username';
  582. $fields = array('Model.hidden', 'Model.password');
  583. $fields = urlencode(Security::hash('/articles/index' . serialize($fields) . $unlocked . Configure::read('Security.salt')));
  584. $this->Controller->request->data = array(
  585. 'Model' => array(
  586. 'username' => 'mark',
  587. 'password' => 'sekret',
  588. 'hidden' => '0'
  589. ),
  590. '_Token' => compact('fields', 'unlocked')
  591. );
  592. $result = $this->Controller->Security->validatePost($this->Controller);
  593. $this->assertTrue($result);
  594. }
  595. /**
  596. * test that missing 'unlocked' input causes failure
  597. *
  598. * @return void
  599. */
  600. public function testValidatePostFailNoDisabled() {
  601. $event = new Event('Controller.startup', $this->Controller);
  602. $this->Controller->Security->startup($event);
  603. $fields = array('Model.hidden', 'Model.password', 'Model.username');
  604. $fields = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt')));
  605. $this->Controller->request->data = array(
  606. 'Model' => array(
  607. 'username' => 'mark',
  608. 'password' => 'sekret',
  609. 'hidden' => '0'
  610. ),
  611. '_Token' => compact('fields')
  612. );
  613. $result = $this->Controller->Security->validatePost($this->Controller);
  614. $this->assertFalse($result);
  615. }
  616. /**
  617. * Test that validatePost fails when unlocked fields are changed.
  618. *
  619. * @return void
  620. */
  621. public function testValidatePostFailDisabledFieldTampering() {
  622. $event = new Event('Controller.startup', $this->Controller);
  623. $this->Controller->Security->startup($event);
  624. $unlocked = 'Model.username';
  625. $fields = array('Model.hidden', 'Model.password');
  626. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Configure::read('Security.salt')));
  627. // Tamper the values.
  628. $unlocked = 'Model.username|Model.password';
  629. $this->Controller->request->data = array(
  630. 'Model' => array(
  631. 'username' => 'mark',
  632. 'password' => 'sekret',
  633. 'hidden' => '0'
  634. ),
  635. '_Token' => compact('fields', 'unlocked')
  636. );
  637. $result = $this->Controller->Security->validatePost($this->Controller);
  638. $this->assertFalse($result);
  639. }
  640. /**
  641. * testValidateHiddenMultipleModel method
  642. *
  643. * @return void
  644. */
  645. public function testValidateHiddenMultipleModel() {
  646. $event = new Event('Controller.startup', $this->Controller);
  647. $this->Controller->Security->startup($event);
  648. $fields = '075ca6c26c38a09a78d871201df89faf52cbbeb8%3AModel.valid%7CModel2.valid%7CModel3.valid';
  649. $unlocked = '';
  650. $this->Controller->request->data = array(
  651. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  652. 'Model2' => array('valid' => '0'),
  653. 'Model3' => array('valid' => '0'),
  654. '_Token' => compact('fields', 'unlocked')
  655. );
  656. $result = $this->Controller->Security->validatePost($this->Controller);
  657. $this->assertTrue($result);
  658. }
  659. /**
  660. * testValidateHasManyModel method
  661. *
  662. * @return void
  663. */
  664. public function testValidateHasManyModel() {
  665. $event = new Event('Controller.startup', $this->Controller);
  666. $this->Controller->Security->startup($event);
  667. $fields = '24a753fb62ef7839389987b58e3f7108f564e529%3AModel.0.hidden%7CModel.0.valid';
  668. $fields .= '%7CModel.1.hidden%7CModel.1.valid';
  669. $unlocked = '';
  670. $this->Controller->request->data = array(
  671. 'Model' => array(
  672. array(
  673. 'username' => 'username', 'password' => 'password',
  674. 'hidden' => 'value', 'valid' => '0'
  675. ),
  676. array(
  677. 'username' => 'username', 'password' => 'password',
  678. 'hidden' => 'value', 'valid' => '0'
  679. )
  680. ),
  681. '_Token' => compact('fields', 'unlocked')
  682. );
  683. $result = $this->Controller->Security->validatePost($this->Controller);
  684. $this->assertTrue($result);
  685. }
  686. /**
  687. * testValidateHasManyRecordsPass method
  688. *
  689. * @return void
  690. */
  691. public function testValidateHasManyRecordsPass() {
  692. $event = new Event('Controller.startup', $this->Controller);
  693. $this->Controller->Security->startup($event);
  694. $fields = '8f7d82bf7656cf068822d9bdab109ebed1be1825%3AAddress.0.id%7CAddress.0.primary%7C';
  695. $fields .= 'Address.1.id%7CAddress.1.primary';
  696. $unlocked = '';
  697. $this->Controller->request->data = array(
  698. 'Address' => array(
  699. 0 => array(
  700. 'id' => '123',
  701. 'title' => 'home',
  702. 'first_name' => 'Bilbo',
  703. 'last_name' => 'Baggins',
  704. 'address' => '23 Bag end way',
  705. 'city' => 'the shire',
  706. 'phone' => 'N/A',
  707. 'primary' => '1',
  708. ),
  709. 1 => array(
  710. 'id' => '124',
  711. 'title' => 'home',
  712. 'first_name' => 'Frodo',
  713. 'last_name' => 'Baggins',
  714. 'address' => '50 Bag end way',
  715. 'city' => 'the shire',
  716. 'phone' => 'N/A',
  717. 'primary' => '1'
  718. )
  719. ),
  720. '_Token' => compact('fields', 'unlocked')
  721. );
  722. $result = $this->Controller->Security->validatePost($this->Controller);
  723. $this->assertTrue($result);
  724. }
  725. /**
  726. * Test that values like Foo.0.1
  727. *
  728. * @return void
  729. */
  730. public function testValidateNestedNumericSets() {
  731. $event = new Event('Controller.startup', $this->Controller);
  732. $this->Controller->Security->startup($event);
  733. $unlocked = '';
  734. $hashFields = array('TaxonomyData');
  735. $fields = urlencode(Security::hash('/articles/index' . serialize($hashFields) . $unlocked . Configure::read('Security.salt')));
  736. $this->Controller->request->data = array(
  737. 'TaxonomyData' => array(
  738. 1 => array(array(2)),
  739. 2 => array(array(3))
  740. ),
  741. '_Token' => compact('fields', 'unlocked')
  742. );
  743. $result = $this->Controller->Security->validatePost($this->Controller);
  744. $this->assertTrue($result);
  745. }
  746. /**
  747. * testValidateHasManyRecords method
  748. *
  749. * validatePost should fail, hidden fields have been changed.
  750. *
  751. * @return void
  752. */
  753. public function testValidateHasManyRecordsFail() {
  754. $event = new Event('Controller.startup', $this->Controller);
  755. $this->Controller->Security->startup($event);
  756. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
  757. $fields .= 'Address.1.id%7CAddress.1.primary';
  758. $unlocked = '';
  759. $this->Controller->request->data = array(
  760. 'Address' => array(
  761. 0 => array(
  762. 'id' => '123',
  763. 'title' => 'home',
  764. 'first_name' => 'Bilbo',
  765. 'last_name' => 'Baggins',
  766. 'address' => '23 Bag end way',
  767. 'city' => 'the shire',
  768. 'phone' => 'N/A',
  769. 'primary' => '5',
  770. ),
  771. 1 => array(
  772. 'id' => '124',
  773. 'title' => 'home',
  774. 'first_name' => 'Frodo',
  775. 'last_name' => 'Baggins',
  776. 'address' => '50 Bag end way',
  777. 'city' => 'the shire',
  778. 'phone' => 'N/A',
  779. 'primary' => '1'
  780. )
  781. ),
  782. '_Token' => compact('fields', 'unlocked')
  783. );
  784. $result = $this->Controller->Security->validatePost($this->Controller);
  785. $this->assertFalse($result);
  786. }
  787. /**
  788. * testFormDisabledFields method
  789. *
  790. * @return void
  791. */
  792. public function testFormDisabledFields() {
  793. $event = new Event('Controller.startup', $this->Controller);
  794. $this->Controller->Security->startup($event);
  795. $fields = '9da2b3fa2b5b8ac0bfbc1bbce145e58059629125%3An%3A0%3A%7B%7D';
  796. $unlocked = '';
  797. $this->Controller->request->data = array(
  798. 'MyModel' => array('name' => 'some data'),
  799. '_Token' => compact('fields', 'unlocked')
  800. );
  801. $result = $this->Controller->Security->validatePost($this->Controller);
  802. $this->assertFalse($result);
  803. $this->Controller->Security->startup($event);
  804. $this->Controller->Security->config('disabledFields', ['MyModel.name']);
  805. $this->Controller->request->data = array(
  806. 'MyModel' => array('name' => 'some data'),
  807. '_Token' => compact('fields', 'unlocked')
  808. );
  809. $result = $this->Controller->Security->validatePost($this->Controller);
  810. $this->assertTrue($result);
  811. }
  812. /**
  813. * test validatePost with radio buttons
  814. *
  815. * @return void
  816. */
  817. public function testValidatePostRadio() {
  818. $event = new Event('Controller.startup', $this->Controller);
  819. $this->Controller->Security->startup($event);
  820. $fields = 'c2226a8879c3f4b513691295fc2519a29c44c8bb%3An%3A0%3A%7B%7D';
  821. $unlocked = '';
  822. $this->Controller->request->data = array(
  823. '_Token' => compact('fields', 'unlocked')
  824. );
  825. $result = $this->Controller->Security->validatePost($this->Controller);
  826. $this->assertFalse($result);
  827. $this->Controller->request->data = array(
  828. '_Token' => compact('fields', 'unlocked'),
  829. 'Test' => array('test' => '')
  830. );
  831. $result = $this->Controller->Security->validatePost($this->Controller);
  832. $this->assertTrue($result);
  833. $this->Controller->request->data = array(
  834. '_Token' => compact('fields', 'unlocked'),
  835. 'Test' => array('test' => '1')
  836. );
  837. $result = $this->Controller->Security->validatePost($this->Controller);
  838. $this->assertTrue($result);
  839. $this->Controller->request->data = array(
  840. '_Token' => compact('fields', 'unlocked'),
  841. 'Test' => array('test' => '2')
  842. );
  843. $result = $this->Controller->Security->validatePost($this->Controller);
  844. $this->assertTrue($result);
  845. }
  846. /**
  847. * test validatePost uses here() as a hash input.
  848. *
  849. * @return void
  850. */
  851. public function testValidatePostUrlAsHashInput() {
  852. $event = new Event('Controller.startup', $this->Controller);
  853. $this->Security->startup($event);
  854. $fields = 'b0914d06dfb04abf1fada53e16810e87d157950b%3A';
  855. $unlocked = '';
  856. $this->Controller->request->data = array(
  857. 'Model' => array('username' => '', 'password' => ''),
  858. '_Token' => compact('fields', 'unlocked')
  859. );
  860. $this->assertTrue($this->Security->validatePost($this->Controller));
  861. $request = $this->getMock('Cake\Network\Request', ['here']);
  862. $request->expects($this->at(0))
  863. ->method('here')
  864. ->will($this->returnValue('/posts/index?page=1'));
  865. $request->expects($this->at(1))
  866. ->method('here')
  867. ->will($this->returnValue('/posts/edit/1'));
  868. $request->data = $this->Controller->request->data;
  869. $this->Controller->request = $request;
  870. $this->assertFalse($this->Security->validatePost($this->Controller));
  871. $this->assertFalse($this->Security->validatePost($this->Controller));
  872. }
  873. /**
  874. * test that blackhole doesn't delete the _Token session key so repeat data submissions
  875. * stay blackholed.
  876. *
  877. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/214
  878. * @return void
  879. */
  880. public function testBlackHoleNotDeletingSessionInformation() {
  881. $event = new Event('Controller.startup', $this->Controller);
  882. $this->Controller->Security->startup($event);
  883. $this->Controller->Security->blackHole($this->Controller, 'auth');
  884. $this->assertTrue($this->Controller->Security->session->check('_Token'), '_Token was deleted by blackHole %s');
  885. }
  886. /**
  887. * Test generateToken()
  888. *
  889. * @return void
  890. */
  891. public function testGenerateToken() {
  892. $request = $this->Controller->request;
  893. $this->Security->generateToken($request);
  894. $this->assertNotEmpty($request->params['_Token']);
  895. $this->assertTrue(isset($request->params['_Token']['unlockedFields']));
  896. }
  897. /**
  898. * Test unlocked actions
  899. *
  900. * @return void
  901. */
  902. public function testUnlockedActions() {
  903. $_SERVER['REQUEST_METHOD'] = 'POST';
  904. $event = new Event('Controller.startup', $this->Controller);
  905. $this->Controller->request->data = array('data');
  906. $this->Controller->Security->unlockedActions = 'index';
  907. $this->Controller->Security->blackHoleCallback = null;
  908. $result = $this->Controller->Security->startup($event);
  909. $this->assertNull($result);
  910. }
  911. }