SecurityComponentTest.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  1. <?php
  2. /**
  3. * SecurityComponentTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.tests.cases.libs.controller.components
  16. * @since CakePHP(tm) v 1.2.0.5435
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('SecurityComponent', 'Controller/Component');
  20. App::uses('Controller', 'Controller');
  21. /**
  22. * TestSecurityComponent
  23. *
  24. * @package cake
  25. * @package cake.tests.cases.libs.controller.components
  26. */
  27. class TestSecurityComponent extends SecurityComponent {
  28. /**
  29. * validatePost method
  30. *
  31. * @param Controller $controller
  32. * @return unknown
  33. */
  34. public function validatePost($controller) {
  35. return $this->_validatePost($controller);
  36. }
  37. }
  38. /**
  39. * SecurityTestController
  40. *
  41. * @package cake
  42. * @package cake.tests.cases.libs.controller.components
  43. */
  44. class SecurityTestController extends Controller {
  45. /**
  46. * name property
  47. *
  48. * @var string 'SecurityTest'
  49. */
  50. public $name = 'SecurityTest';
  51. /**
  52. * components property
  53. *
  54. * @var array
  55. */
  56. public $components = array('Session', 'TestSecurity');
  57. /**
  58. * failed property
  59. *
  60. * @var bool false
  61. */
  62. public $failed = false;
  63. /**
  64. * Used for keeping track of headers in test
  65. *
  66. * @var array
  67. */
  68. public $testHeaders = array();
  69. /**
  70. * fail method
  71. *
  72. * @return void
  73. */
  74. public function fail() {
  75. $this->failed = true;
  76. }
  77. /**
  78. * redirect method
  79. *
  80. * @param mixed $option
  81. * @param mixed $code
  82. * @param mixed $exit
  83. * @return void
  84. */
  85. public function redirect($url, $status = null, $exit = true) {
  86. return $status;
  87. }
  88. /**
  89. * Conveinence method for header()
  90. *
  91. * @param string $status
  92. * @return void
  93. */
  94. public function header($status) {
  95. $this->testHeaders[] = $status;
  96. }
  97. }
  98. /**
  99. * SecurityComponentTest class
  100. *
  101. * @package cake.tests.cases.libs.controller.components
  102. */
  103. class SecurityComponentTest extends CakeTestCase {
  104. /**
  105. * Controller property
  106. *
  107. * @var SecurityTestController
  108. */
  109. public $Controller;
  110. /**
  111. * oldSalt property
  112. *
  113. * @var string
  114. */
  115. public $oldSalt;
  116. /**
  117. * setUp method
  118. *
  119. * @return void
  120. */
  121. public function setUp() {
  122. parent::setUp();
  123. $request = new CakeRequest('posts/index', false);
  124. $request->addParams(array('controller' => 'posts', 'action' => 'index'));
  125. $this->Controller = new SecurityTestController($request);
  126. $this->Controller->Components->init($this->Controller);
  127. $this->Controller->Security = $this->Controller->TestSecurity;
  128. $this->Controller->Security->blackHoleCallback = 'fail';
  129. $this->Security = $this->Controller->Security;
  130. $this->Security->csrfCheck = false;
  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->Controller->Session->delete('_Token');
  141. unset($this->Controller->Security);
  142. unset($this->Controller->Component);
  143. unset($this->Controller);
  144. }
  145. /**
  146. * test that initalize can set properties.
  147. *
  148. * @return void
  149. */
  150. public function testConstructorSettingProperties() {
  151. $settings = array(
  152. 'requirePost' => array('edit', 'update'),
  153. 'requireSecure' => array('update_account'),
  154. 'requireGet' => array('index'),
  155. 'validatePost' => false,
  156. );
  157. $Security = new SecurityComponent($this->Controller->Components, $settings);
  158. $this->Controller->Security->initialize($this->Controller, $settings);
  159. $this->assertEqual($Security->requirePost, $settings['requirePost']);
  160. $this->assertEqual($Security->requireSecure, $settings['requireSecure']);
  161. $this->assertEqual($Security->requireGet, $settings['requireGet']);
  162. $this->assertEqual($Security->validatePost, $settings['validatePost']);
  163. }
  164. /**
  165. * testStartup method
  166. *
  167. * @return void
  168. */
  169. public function testStartup() {
  170. $this->Controller->Security->startup($this->Controller);
  171. $result = $this->Controller->params['_Token']['key'];
  172. $this->assertNotNull($result);
  173. $this->assertTrue($this->Controller->Session->check('_Token'));
  174. }
  175. /**
  176. * testRequirePostFail method
  177. *
  178. * @return void
  179. */
  180. public function testRequirePostFail() {
  181. $_SERVER['REQUEST_METHOD'] = 'GET';
  182. $this->Controller->request['action'] = 'posted';
  183. $this->Controller->Security->requirePost(array('posted'));
  184. $this->Controller->Security->startup($this->Controller);
  185. $this->assertTrue($this->Controller->failed);
  186. }
  187. /**
  188. * testRequirePostSucceed method
  189. *
  190. * @return void
  191. */
  192. public function testRequirePostSucceed() {
  193. $_SERVER['REQUEST_METHOD'] = 'POST';
  194. $this->Controller->request['action'] = 'posted';
  195. $this->Controller->Security->requirePost('posted');
  196. $this->Security->startup($this->Controller);
  197. $this->assertFalse($this->Controller->failed);
  198. }
  199. /**
  200. * testRequireSecureFail method
  201. *
  202. * @return void
  203. */
  204. public function testRequireSecureFail() {
  205. $_SERVER['HTTPS'] = 'off';
  206. $_SERVER['REQUEST_METHOD'] = 'POST';
  207. $this->Controller->request['action'] = 'posted';
  208. $this->Controller->Security->requireSecure(array('posted'));
  209. $this->Controller->Security->startup($this->Controller);
  210. $this->assertTrue($this->Controller->failed);
  211. }
  212. /**
  213. * testRequireSecureSucceed method
  214. *
  215. * @return void
  216. */
  217. public function testRequireSecureSucceed() {
  218. $_SERVER['REQUEST_METHOD'] = 'Secure';
  219. $this->Controller->request['action'] = 'posted';
  220. $_SERVER['HTTPS'] = 'on';
  221. $this->Controller->Security->requireSecure('posted');
  222. $this->Controller->Security->startup($this->Controller);
  223. $this->assertFalse($this->Controller->failed);
  224. }
  225. /**
  226. * testRequireAuthFail method
  227. *
  228. * @return void
  229. */
  230. public function testRequireAuthFail() {
  231. $_SERVER['REQUEST_METHOD'] = 'AUTH';
  232. $this->Controller->request['action'] = 'posted';
  233. $this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
  234. $this->Controller->Security->requireAuth(array('posted'));
  235. $this->Controller->Security->startup($this->Controller);
  236. $this->assertTrue($this->Controller->failed);
  237. $this->Controller->Session->write('_Token', array('allowedControllers' => array()));
  238. $this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
  239. $this->Controller->request['action'] = 'posted';
  240. $this->Controller->Security->requireAuth('posted');
  241. $this->Controller->Security->startup($this->Controller);
  242. $this->assertTrue($this->Controller->failed);
  243. $this->Controller->Session->write('_Token', array(
  244. 'allowedControllers' => array('SecurityTest'), 'allowedActions' => array('posted2')
  245. ));
  246. $this->Controller->request->data = array('username' => 'willy', 'password' => 'somePass');
  247. $this->Controller->request['action'] = 'posted';
  248. $this->Controller->Security->requireAuth('posted');
  249. $this->Controller->Security->startup($this->Controller);
  250. $this->assertTrue($this->Controller->failed);
  251. }
  252. /**
  253. * testRequireAuthSucceed method
  254. *
  255. * @return void
  256. */
  257. public function testRequireAuthSucceed() {
  258. $_SERVER['REQUEST_METHOD'] = 'AUTH';
  259. $this->Controller->request['action'] = 'posted';
  260. $this->Controller->Security->requireAuth('posted');
  261. $this->Controller->Security->startup($this->Controller);
  262. $this->assertFalse($this->Controller->failed);
  263. $this->Controller->Security->Session->write('_Token', array(
  264. 'allowedControllers' => array('SecurityTest'), 'allowedActions' => array('posted')
  265. ));
  266. $this->Controller->request['controller'] = 'SecurityTest';
  267. $this->Controller->request['action'] = 'posted';
  268. $this->Controller->request->data = array(
  269. 'username' => 'willy', 'password' => 'somePass', '_Token' => ''
  270. );
  271. $this->Controller->action = 'posted';
  272. $this->Controller->Security->requireAuth('posted');
  273. $this->Controller->Security->startup($this->Controller);
  274. $this->assertFalse($this->Controller->failed);
  275. }
  276. /**
  277. * testRequirePostSucceedWrongMethod method
  278. *
  279. * @return void
  280. */
  281. public function testRequirePostSucceedWrongMethod() {
  282. $_SERVER['REQUEST_METHOD'] = 'GET';
  283. $this->Controller->request['action'] = 'getted';
  284. $this->Controller->Security->requirePost('posted');
  285. $this->Controller->Security->startup($this->Controller);
  286. $this->assertFalse($this->Controller->failed);
  287. }
  288. /**
  289. * testRequireGetFail method
  290. *
  291. * @return void
  292. */
  293. public function testRequireGetFail() {
  294. $_SERVER['REQUEST_METHOD'] = 'POST';
  295. $this->Controller->request['action'] = 'getted';
  296. $this->Controller->Security->requireGet(array('getted'));
  297. $this->Controller->Security->startup($this->Controller);
  298. $this->assertTrue($this->Controller->failed);
  299. }
  300. /**
  301. * testRequireGetSucceed method
  302. *
  303. * @return void
  304. */
  305. public function testRequireGetSucceed() {
  306. $_SERVER['REQUEST_METHOD'] = 'GET';
  307. $this->Controller->request['action'] = 'getted';
  308. $this->Controller->Security->requireGet('getted');
  309. $this->Controller->Security->startup($this->Controller);
  310. $this->assertFalse($this->Controller->failed);
  311. }
  312. /**
  313. * testRequireGetSucceedWrongMethod method
  314. *
  315. * @return void
  316. */
  317. public function testRequireGetSucceedWrongMethod() {
  318. $_SERVER['REQUEST_METHOD'] = 'POST';
  319. $this->Controller->request['action'] = 'posted';
  320. $this->Security->requireGet('getted');
  321. $this->Security->startup($this->Controller);
  322. $this->assertFalse($this->Controller->failed);
  323. }
  324. /**
  325. * testRequirePutFail method
  326. *
  327. * @return void
  328. */
  329. public function testRequirePutFail() {
  330. $_SERVER['REQUEST_METHOD'] = 'POST';
  331. $this->Controller->request['action'] = 'putted';
  332. $this->Controller->Security->requirePut(array('putted'));
  333. $this->Controller->Security->startup($this->Controller);
  334. $this->assertTrue($this->Controller->failed);
  335. }
  336. /**
  337. * testRequirePutSucceed method
  338. *
  339. * @return void
  340. */
  341. public function testRequirePutSucceed() {
  342. $_SERVER['REQUEST_METHOD'] = 'PUT';
  343. $this->Controller->request['action'] = 'putted';
  344. $this->Controller->Security->requirePut('putted');
  345. $this->Controller->Security->startup($this->Controller);
  346. $this->assertFalse($this->Controller->failed);
  347. }
  348. /**
  349. * testRequirePutSucceedWrongMethod method
  350. *
  351. * @return void
  352. */
  353. public function testRequirePutSucceedWrongMethod() {
  354. $_SERVER['REQUEST_METHOD'] = 'POST';
  355. $this->Controller->request['action'] = 'posted';
  356. $this->Controller->Security->requirePut('putted');
  357. $this->Controller->Security->startup($this->Controller);
  358. $this->assertFalse($this->Controller->failed);
  359. }
  360. /**
  361. * testRequireDeleteFail method
  362. *
  363. * @return void
  364. */
  365. public function testRequireDeleteFail() {
  366. $_SERVER['REQUEST_METHOD'] = 'POST';
  367. $this->Controller->request['action'] = 'deleted';
  368. $this->Controller->Security->requireDelete(array('deleted', 'other_method'));
  369. $this->Controller->Security->startup($this->Controller);
  370. $this->assertTrue($this->Controller->failed);
  371. }
  372. /**
  373. * testRequireDeleteSucceed method
  374. *
  375. * @return void
  376. */
  377. public function testRequireDeleteSucceed() {
  378. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  379. $this->Controller->request['action'] = 'deleted';
  380. $this->Controller->Security->requireDelete('deleted');
  381. $this->Controller->Security->startup($this->Controller);
  382. $this->assertFalse($this->Controller->failed);
  383. }
  384. /**
  385. * testRequireDeleteSucceedWrongMethod method
  386. *
  387. * @return void
  388. */
  389. public function testRequireDeleteSucceedWrongMethod() {
  390. $_SERVER['REQUEST_METHOD'] = 'POST';
  391. $this->Controller->request['action'] = 'posted';
  392. $this->Controller->Security->requireDelete('deleted');
  393. $this->Controller->Security->startup($this->Controller);
  394. $this->assertFalse($this->Controller->failed);
  395. }
  396. /**
  397. * Simple hash validation test
  398. *
  399. * @return void
  400. */
  401. public function testValidatePost() {
  402. $this->Controller->Security->startup($this->Controller);
  403. $key = $this->Controller->request->params['_Token']['key'];
  404. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  405. $unlocked = '';
  406. $this->Controller->request->data = array(
  407. 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
  408. '_Token' => compact('key', 'fields', 'unlocked')
  409. );
  410. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  411. }
  412. /**
  413. * test that validatePost fails if any of its required fields are missing.
  414. *
  415. * @return void
  416. */
  417. public function testValidatePostFormHacking() {
  418. $this->Controller->Security->startup($this->Controller);
  419. $key = $this->Controller->params['_Token']['key'];
  420. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  421. $unlocked = '';
  422. $this->Controller->request->data = array(
  423. 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
  424. '_Token' => compact('key', 'unlocked')
  425. );
  426. $result = $this->Controller->Security->validatePost($this->Controller);
  427. $this->assertFalse($result, 'validatePost passed when fields were missing. %s');
  428. }
  429. /**
  430. * Test that objects can't be passed into the serialized string. This was a vector for RFI and LFI
  431. * attacks. Thanks to Felix Wilhelm
  432. *
  433. * @return void
  434. */
  435. public function testValidatePostObjectDeserialize() {
  436. $this->Controller->Security->startup($this->Controller);
  437. $key = $this->Controller->request->params['_Token']['key'];
  438. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877';
  439. $unlocked = '';
  440. // a corrupted serialized object, so we can see if it ever gets to deserialize
  441. $attack = 'O:3:"App":1:{s:5:"__map";a:1:{s:3:"foo";s:7:"Hacked!";s:1:"fail"}}';
  442. $fields .= urlencode(':' . str_rot13($attack));
  443. $this->Controller->request->data = array(
  444. 'Model' => array('username' => 'mark', 'password' => 'foo', 'valid' => '0'),
  445. '_Token' => compact('key', 'fields', 'unlocked')
  446. );
  447. $result = $this->Controller->Security->validatePost($this->Controller);
  448. $this->assertFalse($result, 'validatePost passed when key was missing. %s');
  449. }
  450. /**
  451. * Tests validation of checkbox arrays
  452. *
  453. * @return void
  454. */
  455. public function testValidatePostArray() {
  456. $this->Controller->Security->startup($this->Controller);
  457. $key = $this->Controller->request->params['_Token']['key'];
  458. $fields = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  459. $unlocked = '';
  460. $this->Controller->request->data = array(
  461. 'Model' => array('multi_field' => array('1', '3')),
  462. '_Token' => compact('key', 'fields', 'unlocked')
  463. );
  464. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  465. }
  466. /**
  467. * testValidatePostNoModel method
  468. *
  469. * @return void
  470. */
  471. public function testValidatePostNoModel() {
  472. $this->Controller->Security->startup($this->Controller);
  473. $key = $this->Controller->request->params['_Token']['key'];
  474. $fields = '540ac9c60d323c22bafe997b72c0790f39a8bdef%3A';
  475. $unlocked = '';
  476. $this->Controller->request->data = array(
  477. 'anything' => 'some_data',
  478. '_Token' => compact('key', 'fields', 'unlocked')
  479. );
  480. $result = $this->Controller->Security->validatePost($this->Controller);
  481. $this->assertTrue($result);
  482. }
  483. /**
  484. * testValidatePostSimple method
  485. *
  486. * @return void
  487. */
  488. public function testValidatePostSimple() {
  489. $this->Controller->Security->startup($this->Controller);
  490. $key = $this->Controller->request->params['_Token']['key'];
  491. $fields = '69f493434187b867ea14b901fdf58b55d27c935d%3A';
  492. $unlocked = '';
  493. $this->Controller->request->data = $data = array(
  494. 'Model' => array('username' => '', 'password' => ''),
  495. '_Token' => compact('key', 'fields', 'unlocked')
  496. );
  497. $result = $this->Controller->Security->validatePost($this->Controller);
  498. $this->assertTrue($result);
  499. }
  500. /**
  501. * Tests hash validation for multiple records, including locked fields
  502. *
  503. * @return void
  504. */
  505. public function testValidatePostComplex() {
  506. $this->Controller->Security->startup($this->Controller);
  507. $key = $this->Controller->request->params['_Token']['key'];
  508. $fields = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  509. $unlocked = '';
  510. $this->Controller->request->data = array(
  511. 'Addresses' => array(
  512. '0' => array(
  513. 'id' => '123456', 'title' => '', 'first_name' => '', 'last_name' => '',
  514. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  515. ),
  516. '1' => array(
  517. 'id' => '654321', 'title' => '', 'first_name' => '', 'last_name' => '',
  518. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  519. )
  520. ),
  521. '_Token' => compact('key', 'fields', 'unlocked')
  522. );
  523. $result = $this->Controller->Security->validatePost($this->Controller);
  524. $this->assertTrue($result);
  525. }
  526. /**
  527. * test ValidatePost with multiple select elements.
  528. *
  529. * @return void
  530. */
  531. public function testValidatePostMultipleSelect() {
  532. $this->Controller->Security->startup($this->Controller);
  533. $key = $this->Controller->request->params['_Token']['key'];
  534. $fields = '422cde416475abc171568be690a98cad20e66079%3A';
  535. $unlocked = '';
  536. $this->Controller->request->data = array(
  537. 'Tag' => array('Tag' => array(1, 2)),
  538. '_Token' => compact('key', 'fields', 'unlocked'),
  539. );
  540. $result = $this->Controller->Security->validatePost($this->Controller);
  541. $this->assertTrue($result);
  542. $this->Controller->request->data = array(
  543. 'Tag' => array('Tag' => array(1, 2, 3)),
  544. '_Token' => compact('key', 'fields', 'unlocked'),
  545. );
  546. $result = $this->Controller->Security->validatePost($this->Controller);
  547. $this->assertTrue($result);
  548. $this->Controller->request->data = array(
  549. 'Tag' => array('Tag' => array(1, 2, 3, 4)),
  550. '_Token' => compact('key', 'fields', 'unlocked'),
  551. );
  552. $result = $this->Controller->Security->validatePost($this->Controller);
  553. $this->assertTrue($result);
  554. $fields = '19464422eafe977ee729c59222af07f983010c5f%3A';
  555. $this->Controller->request->data = array(
  556. 'User.password' => 'bar', 'User.name' => 'foo', 'User.is_valid' => '1',
  557. 'Tag' => array('Tag' => array(1)),
  558. '_Token' => compact('key', 'fields', 'unlocked'),
  559. );
  560. $result = $this->Controller->Security->validatePost($this->Controller);
  561. $this->assertTrue($result);
  562. }
  563. /**
  564. * testValidatePostCheckbox method
  565. *
  566. * First block tests un-checked checkbox
  567. * Second block tests checked checkbox
  568. *
  569. * @return void
  570. */
  571. public function testValidatePostCheckbox() {
  572. $this->Controller->Security->startup($this->Controller);
  573. $key = $this->Controller->request->params['_Token']['key'];
  574. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  575. $unlocked = '';
  576. $this->Controller->request->data = array(
  577. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  578. '_Token' => compact('key', 'fields', 'unlocked')
  579. );
  580. $result = $this->Controller->Security->validatePost($this->Controller);
  581. $this->assertTrue($result);
  582. $fields = '874439ca69f89b4c4a5f50fb9c36ff56a28f5d42%3A';
  583. $this->Controller->request->data = array(
  584. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  585. '_Token' => compact('key', 'fields', 'unlocked')
  586. );
  587. $result = $this->Controller->Security->validatePost($this->Controller);
  588. $this->assertTrue($result);
  589. $this->Controller->request->data = array();
  590. $this->Controller->Security->startup($this->Controller);
  591. $key = $this->Controller->request->params['_Token']['key'];
  592. $this->Controller->request->data = $data = array(
  593. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  594. '_Token' => compact('key', 'fields', 'unlocked')
  595. );
  596. $result = $this->Controller->Security->validatePost($this->Controller);
  597. $this->assertTrue($result);
  598. }
  599. /**
  600. * testValidatePostHidden method
  601. *
  602. * @return void
  603. */
  604. public function testValidatePostHidden() {
  605. $this->Controller->Security->startup($this->Controller);
  606. $key = $this->Controller->request->params['_Token']['key'];
  607. $fields = '51ccd8cb0997c7b3d4523ecde5a109318405ef8c%3AModel.hidden%7CModel.other_hidden';
  608. $unlocked = '';
  609. $this->Controller->request->data = array(
  610. 'Model' => array(
  611. 'username' => '', 'password' => '', 'hidden' => '0',
  612. 'other_hidden' => 'some hidden value'
  613. ),
  614. '_Token' => compact('key', 'fields', 'unlocked')
  615. );
  616. $result = $this->Controller->Security->validatePost($this->Controller);
  617. $this->assertTrue($result);
  618. }
  619. /**
  620. * testValidatePostWithDisabledFields method
  621. *
  622. * @return void
  623. */
  624. public function testValidatePostWithDisabledFields() {
  625. $this->Controller->Security->disabledFields = array('Model.username', 'Model.password');
  626. $this->Controller->Security->startup($this->Controller);
  627. $key = $this->Controller->request->params['_Token']['key'];
  628. $fields = 'ef1082968c449397bcd849f963636864383278b1%3AModel.hidden';
  629. $unlocked = '';
  630. $this->Controller->request->data = array(
  631. 'Model' => array(
  632. 'username' => '', 'password' => '', 'hidden' => '0'
  633. ),
  634. '_Token' => compact('fields', 'key', 'unlocked')
  635. );
  636. $result = $this->Controller->Security->validatePost($this->Controller);
  637. $this->assertTrue($result);
  638. }
  639. /**
  640. * test validating post data with posted unlocked fields.
  641. *
  642. * @return void
  643. */
  644. public function testValidatePostDisabledFieldsInData() {
  645. $this->Controller->Security->startup($this->Controller);
  646. $key = $this->Controller->request->params['_Token']['key'];
  647. $unlocked = 'Model.username';
  648. $fields = array('Model.hidden', 'Model.password');
  649. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Configure::read('Security.salt')));
  650. $this->Controller->request->data = array(
  651. 'Model' => array(
  652. 'username' => 'mark',
  653. 'password' => 'sekret',
  654. 'hidden' => '0'
  655. ),
  656. '_Token' => compact('fields', 'key', 'unlocked')
  657. );
  658. $result = $this->Controller->Security->validatePost($this->Controller);
  659. $this->assertTrue($result);
  660. }
  661. /**
  662. * test that missing 'unlocked' input causes failure
  663. *
  664. * @return void
  665. */
  666. public function testValidatePostFailNoDisabled() {
  667. $this->Controller->Security->startup($this->Controller);
  668. $key = $this->Controller->request->params['_Token']['key'];
  669. $fields = array('Model.hidden', 'Model.password', 'Model.username');
  670. $fields = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt')));
  671. $this->Controller->request->data = array(
  672. 'Model' => array(
  673. 'username' => 'mark',
  674. 'password' => 'sekret',
  675. 'hidden' => '0'
  676. ),
  677. '_Token' => compact('fields', 'key')
  678. );
  679. $result = $this->Controller->Security->validatePost($this->Controller);
  680. $this->assertFalse($result);
  681. }
  682. /**
  683. * Test that validatePost fails when unlocked fields are changed.
  684. *
  685. * @return
  686. */
  687. public function testValidatePostFailDisabledFieldTampering() {
  688. $this->Controller->Security->startup($this->Controller);
  689. $key = $this->Controller->request->params['_Token']['key'];
  690. $unlocked = 'Model.username';
  691. $fields = array('Model.hidden', 'Model.password');
  692. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Configure::read('Security.salt')));
  693. // Tamper the values.
  694. $unlocked = 'Model.username|Model.password';
  695. $this->Controller->request->data = array(
  696. 'Model' => array(
  697. 'username' => 'mark',
  698. 'password' => 'sekret',
  699. 'hidden' => '0'
  700. ),
  701. '_Token' => compact('fields', 'key', 'unlocked')
  702. );
  703. $result = $this->Controller->Security->validatePost($this->Controller);
  704. $this->assertFalse($result);
  705. }
  706. /**
  707. * testValidateHiddenMultipleModel method
  708. *
  709. * @return void
  710. */
  711. public function testValidateHiddenMultipleModel() {
  712. $this->Controller->Security->startup($this->Controller);
  713. $key = $this->Controller->request->params['_Token']['key'];
  714. $fields = 'a2d01072dc4660eea9d15007025f35a7a5b58e18%3AModel.valid%7CModel2.valid%7CModel3.valid';
  715. $unlocked = '';
  716. $this->Controller->request->data = array(
  717. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  718. 'Model2' => array('valid' => '0'),
  719. 'Model3' => array('valid' => '0'),
  720. '_Token' => compact('key', 'fields', 'unlocked')
  721. );
  722. $result = $this->Controller->Security->validatePost($this->Controller);
  723. $this->assertTrue($result);
  724. }
  725. /**
  726. * testValidateHasManyModel method
  727. *
  728. * @return void
  729. */
  730. public function testValidateHasManyModel() {
  731. $this->Controller->Security->startup($this->Controller);
  732. $key = $this->Controller->request->params['_Token']['key'];
  733. $fields = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  734. $fields .= '%7CModel.1.hidden%7CModel.1.valid';
  735. $unlocked = '';
  736. $this->Controller->request->data = array(
  737. 'Model' => array(
  738. array(
  739. 'username' => 'username', 'password' => 'password',
  740. 'hidden' => 'value', 'valid' => '0'
  741. ),
  742. array(
  743. 'username' => 'username', 'password' => 'password',
  744. 'hidden' => 'value', 'valid' => '0'
  745. )
  746. ),
  747. '_Token' => compact('key', 'fields', 'unlocked')
  748. );
  749. $result = $this->Controller->Security->validatePost($this->Controller);
  750. $this->assertTrue($result);
  751. }
  752. /**
  753. * testValidateHasManyRecordsPass method
  754. *
  755. * @return void
  756. */
  757. public function testValidateHasManyRecordsPass() {
  758. $this->Controller->Security->startup($this->Controller);
  759. $key = $this->Controller->request->params['_Token']['key'];
  760. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
  761. $fields .= 'Address.1.id%7CAddress.1.primary';
  762. $unlocked = '';
  763. $this->Controller->request->data = array(
  764. 'Address' => array(
  765. 0 => array(
  766. 'id' => '123',
  767. 'title' => 'home',
  768. 'first_name' => 'Bilbo',
  769. 'last_name' => 'Baggins',
  770. 'address' => '23 Bag end way',
  771. 'city' => 'the shire',
  772. 'phone' => 'N/A',
  773. 'primary' => '1',
  774. ),
  775. 1 => array(
  776. 'id' => '124',
  777. 'title' => 'home',
  778. 'first_name' => 'Frodo',
  779. 'last_name' => 'Baggins',
  780. 'address' => '50 Bag end way',
  781. 'city' => 'the shire',
  782. 'phone' => 'N/A',
  783. 'primary' => '1'
  784. )
  785. ),
  786. '_Token' => compact('key', 'fields', 'unlocked')
  787. );
  788. $result = $this->Controller->Security->validatePost($this->Controller);
  789. $this->assertTrue($result);
  790. }
  791. /**
  792. * testValidateHasManyRecords method
  793. *
  794. * validatePost should fail, hidden fields have been changed.
  795. *
  796. * @return void
  797. */
  798. public function testValidateHasManyRecordsFail() {
  799. $this->Controller->Security->startup($this->Controller);
  800. $key = $this->Controller->request->params['_Token']['key'];
  801. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
  802. $fields .= 'Address.1.id%7CAddress.1.primary';
  803. $unlocked = '';
  804. $this->Controller->request->data = array(
  805. 'Address' => array(
  806. 0 => array(
  807. 'id' => '123',
  808. 'title' => 'home',
  809. 'first_name' => 'Bilbo',
  810. 'last_name' => 'Baggins',
  811. 'address' => '23 Bag end way',
  812. 'city' => 'the shire',
  813. 'phone' => 'N/A',
  814. 'primary' => '5',
  815. ),
  816. 1 => array(
  817. 'id' => '124',
  818. 'title' => 'home',
  819. 'first_name' => 'Frodo',
  820. 'last_name' => 'Baggins',
  821. 'address' => '50 Bag end way',
  822. 'city' => 'the shire',
  823. 'phone' => 'N/A',
  824. 'primary' => '1'
  825. )
  826. ),
  827. '_Token' => compact('key', 'fields', 'unlocked')
  828. );
  829. $result = $this->Controller->Security->validatePost($this->Controller);
  830. $this->assertFalse($result);
  831. }
  832. /**
  833. * testFormDisabledFields method
  834. *
  835. * @return void
  836. */
  837. public function testFormDisabledFields() {
  838. $this->Controller->Security->startup($this->Controller);
  839. $key = $this->Controller->request->params['_Token']['key'];
  840. $fields = '11842060341b9d0fc3808b90ba29fdea7054d6ad%3An%3A0%3A%7B%7D';
  841. $unlocked = '';
  842. $this->Controller->request->data = array(
  843. 'MyModel' => array('name' => 'some data'),
  844. '_Token' => compact('key', 'fields', 'unlocked')
  845. );
  846. $result = $this->Controller->Security->validatePost($this->Controller);
  847. $this->assertFalse($result);
  848. $this->Controller->Security->startup($this->Controller);
  849. $this->Controller->Security->disabledFields = array('MyModel.name');
  850. $key = $this->Controller->request->params['_Token']['key'];
  851. $this->Controller->request->data = array(
  852. 'MyModel' => array('name' => 'some data'),
  853. '_Token' => compact('key', 'fields', 'unlocked')
  854. );
  855. $result = $this->Controller->Security->validatePost($this->Controller);
  856. $this->assertTrue($result);
  857. }
  858. /**
  859. * testRadio method
  860. *
  861. * @return void
  862. */
  863. public function testRadio() {
  864. $this->Controller->Security->startup($this->Controller);
  865. $key = $this->Controller->request->params['_Token']['key'];
  866. $fields = '575ef54ca4fc8cab468d6d898e9acd3a9671c17e%3An%3A0%3A%7B%7D';
  867. $unlocked = '';
  868. $this->Controller->request->data = array(
  869. '_Token' => compact('key', 'fields', 'unlocked')
  870. );
  871. $result = $this->Controller->Security->validatePost($this->Controller);
  872. $this->assertFalse($result);
  873. $this->Controller->request->data = array(
  874. '_Token' => compact('key', 'fields', 'unlocked'),
  875. 'Test' => array('test' => '')
  876. );
  877. $result = $this->Controller->Security->validatePost($this->Controller);
  878. $this->assertTrue($result);
  879. $this->Controller->request->data = array(
  880. '_Token' => compact('key', 'fields', 'unlocked'),
  881. 'Test' => array('test' => '1')
  882. );
  883. $result = $this->Controller->Security->validatePost($this->Controller);
  884. $this->assertTrue($result);
  885. $this->Controller->request->data = array(
  886. '_Token' => compact('key', 'fields', 'unlocked'),
  887. 'Test' => array('test' => '2')
  888. );
  889. $result = $this->Controller->Security->validatePost($this->Controller);
  890. $this->assertTrue($result);
  891. }
  892. /**
  893. * test that a requestAction's controller will have the _Token appended to
  894. * the params.
  895. *
  896. * @return void
  897. * @see http://cakephp.lighthouseapp.com/projects/42648/tickets/68
  898. */
  899. public function testSettingTokenForRequestAction() {
  900. $this->Controller->Security->startup($this->Controller);
  901. $key = $this->Controller->request->params['_Token']['key'];
  902. $this->Controller->params['requested'] = 1;
  903. unset($this->Controller->request->params['_Token']);
  904. $this->Controller->Security->startup($this->Controller);
  905. $this->assertEqual($this->Controller->request->params['_Token']['key'], $key);
  906. }
  907. /**
  908. * test that blackhole doesn't delete the _Token session key so repeat data submissions
  909. * stay blackholed.
  910. *
  911. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/214
  912. * @return void
  913. */
  914. public function testBlackHoleNotDeletingSessionInformation() {
  915. $this->Controller->Security->startup($this->Controller);
  916. $this->Controller->Security->blackHole($this->Controller, 'auth');
  917. $this->assertTrue($this->Controller->Security->Session->check('_Token'), '_Token was deleted by blackHole %s');
  918. }
  919. /**
  920. * test setting
  921. *
  922. * @return void
  923. */
  924. public function testCsrfSettings() {
  925. $this->Security->validatePost = false;
  926. $this->Security->csrfCheck = true;
  927. $this->Security->csrfExpires = '+10 minutes';
  928. $this->Security->startup($this->Controller);
  929. $token = $this->Security->Session->read('_Token');
  930. $this->assertEquals(count($token['csrfTokens']), 1, 'Missing the csrf token.');
  931. $this->assertEquals(strtotime('+10 minutes'), current($token['csrfTokens']), 'Token expiry does not match');
  932. $this->assertEquals(array('key', 'unlockedFields'), array_keys($this->Controller->request->params['_Token']), 'Keys don not match');
  933. }
  934. /**
  935. * Test setting multiple nonces, when startup() is called more than once, (ie more than one request.)
  936. *
  937. * @return void
  938. */
  939. public function testCsrfSettingMultipleNonces() {
  940. $this->Security->validatePost = false;
  941. $this->Security->csrfCheck = true;
  942. $this->Security->csrfExpires = '+10 minutes';
  943. $this->Security->startup($this->Controller);
  944. $this->Security->startup($this->Controller);
  945. $token = $this->Security->Session->read('_Token');
  946. $this->assertEquals(count($token['csrfTokens']), 2, 'Missing the csrf token.');
  947. foreach ($token['csrfTokens'] as $key => $expires) {
  948. $this->assertEquals(strtotime('+10 minutes'), $expires, 'Token expiry does not match');
  949. }
  950. }
  951. /**
  952. * test that nonces are consumed by form submits.
  953. *
  954. * @return void
  955. */
  956. public function testCsrfNonceConsumption() {
  957. $this->Security->validatePost = false;
  958. $this->Security->csrfCheck = true;
  959. $this->Security->csrfExpires = '+10 minutes';
  960. $this->Security->Session->write('_Token.csrfTokens', array('nonce1' => strtotime('+10 minutes')));
  961. $this->Controller->request = $this->getMock('CakeRequest', array('is'));
  962. $this->Controller->request->expects($this->once())->method('is')
  963. ->with('post')
  964. ->will($this->returnValue(true));
  965. $this->Controller->request->params['action'] = 'index';
  966. $this->Controller->request->data = array(
  967. '_Token' => array(
  968. 'key' => 'nonce1'
  969. ),
  970. 'Post' => array(
  971. 'title' => 'Woot'
  972. )
  973. );
  974. $this->Security->startup($this->Controller);
  975. $token = $this->Security->Session->read('_Token');
  976. $this->assertFalse(isset($token['csrfTokens']['nonce1']), 'Token was not consumed');
  977. }
  978. /**
  979. * test that expired values in the csrfTokens are cleaned up.
  980. *
  981. * @return void
  982. */
  983. public function testCsrfNonceVacuum() {
  984. $this->Security->validatePost = false;
  985. $this->Security->csrfCheck = true;
  986. $this->Security->csrfExpires = '+10 minutes';
  987. $this->Security->Session->write('_Token.csrfTokens', array(
  988. 'valid' => strtotime('+30 minutes'),
  989. 'poof' => strtotime('-11 minutes'),
  990. 'dust' => strtotime('-20 minutes')
  991. ));
  992. $this->Security->startup($this->Controller);
  993. $tokens = $this->Security->Session->read('_Token.csrfTokens');
  994. $this->assertEquals(2, count($tokens), 'Too many tokens left behind');
  995. $this->assertNotEmpty('valid', $tokens, 'Valid token was removed.');
  996. }
  997. /**
  998. * test that when the key is missing the request is blackHoled
  999. *
  1000. * @return void
  1001. */
  1002. public function testCsrfBlackHoleOnKeyMismatch() {
  1003. $this->Security->validatePost = false;
  1004. $this->Security->csrfCheck = true;
  1005. $this->Security->csrfExpires = '+10 minutes';
  1006. $this->Security->Session->write('_Token.csrfTokens', array('nonce1' => strtotime('+10 minutes')));
  1007. $this->Controller->request = $this->getMock('CakeRequest', array('is'));
  1008. $this->Controller->request->expects($this->once())->method('is')
  1009. ->with('post')
  1010. ->will($this->returnValue(true));
  1011. $this->Controller->request->params['action'] = 'index';
  1012. $this->Controller->request->data = array(
  1013. '_Token' => array(
  1014. 'key' => 'not the right value'
  1015. ),
  1016. 'Post' => array(
  1017. 'title' => 'Woot'
  1018. )
  1019. );
  1020. $this->Security->startup($this->Controller);
  1021. $this->assertTrue($this->Controller->failed, 'fail() was not called.');
  1022. }
  1023. /**
  1024. * test that when the key is missing the request is blackHoled
  1025. *
  1026. * @return void
  1027. */
  1028. public function testCsrfBlackHoleOnExpiredKey() {
  1029. $this->Security->validatePost = false;
  1030. $this->Security->csrfCheck = true;
  1031. $this->Security->csrfExpires = '+10 minutes';
  1032. $this->Security->Session->write('_Token.csrfTokens', array('nonce1' => strtotime('-5 minutes')));
  1033. $this->Controller->request = $this->getMock('CakeRequest', array('is'));
  1034. $this->Controller->request->expects($this->once())->method('is')
  1035. ->with('post')
  1036. ->will($this->returnValue(true));
  1037. $this->Controller->request->params['action'] = 'index';
  1038. $this->Controller->request->data = array(
  1039. '_Token' => array(
  1040. 'key' => 'nonce1'
  1041. ),
  1042. 'Post' => array(
  1043. 'title' => 'Woot'
  1044. )
  1045. );
  1046. $this->Security->startup($this->Controller);
  1047. $this->assertTrue($this->Controller->failed, 'fail() was not called.');
  1048. }
  1049. /**
  1050. * test that csrfUseOnce = false works.
  1051. *
  1052. * @return void
  1053. */
  1054. public function testCsrfNotUseOnce() {
  1055. $this->Security->validatePost = false;
  1056. $this->Security->csrfCheck = true;
  1057. $this->Security->csrfUseOnce = false;
  1058. $this->Security->csrfExpires = '+10 minutes';
  1059. // Generate one token
  1060. $this->Security->startup($this->Controller);
  1061. $token = $this->Security->Session->read('_Token.csrfTokens');
  1062. $this->assertEquals(1, count($token), 'Should only be one token.');
  1063. $this->Security->startup($this->Controller);
  1064. $token2 = $this->Security->Session->read('_Token.csrfTokens');
  1065. $this->assertEquals(1, count($token2), 'Should only be one token.');
  1066. $this->assertEquals($token, $token2, 'Tokens should not be different.');
  1067. $key = $this->Controller->request->params['_Token']['key'];
  1068. $this->assertEquals(array($key), array_keys($token), '_Token.key and csrfToken do not match request will blackhole.');
  1069. }
  1070. /**
  1071. * ensure that longer session tokens are not consumed
  1072. *
  1073. * @return void
  1074. */
  1075. public function testCsrfNotUseOnceValidationLeavingToken() {
  1076. $this->Security->validatePost = false;
  1077. $this->Security->csrfCheck = true;
  1078. $this->Security->csrfUseOnce = false;
  1079. $this->Security->csrfExpires = '+10 minutes';
  1080. $this->Security->Session->write('_Token.csrfTokens', array('nonce1' => strtotime('+10 minutes')));
  1081. $this->Controller->request = $this->getMock('CakeRequest', array('is'));
  1082. $this->Controller->request->expects($this->once())->method('is')
  1083. ->with('post')
  1084. ->will($this->returnValue(true));
  1085. $this->Controller->request->params['action'] = 'index';
  1086. $this->Controller->request->data = array(
  1087. '_Token' => array(
  1088. 'key' => 'nonce1'
  1089. ),
  1090. 'Post' => array(
  1091. 'title' => 'Woot'
  1092. )
  1093. );
  1094. $this->Security->startup($this->Controller);
  1095. $token = $this->Security->Session->read('_Token');
  1096. $this->assertTrue(isset($token['csrfTokens']['nonce1']), 'Token was consumed');
  1097. }
  1098. }