SecurityComponentTest.php 29 KB

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