SecurityComponentTest.php 30 KB

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