PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/controller/components/security.test.php

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