PageRenderTime 61ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/code/ryzom/tools/server/www/webtt/cake/tests/cases/libs/controller/components/security.test.php

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 1286 lines | 757 code | 125 blank | 404 comment | 5 complexity | a0e078cced16fab986340851850214d5 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * SecurityComponentTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing 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('Session', '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', serialize(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', serialize(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%3AModel.valid';
  530. $this->Controller->data = array(
  531. 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
  532. '_Token' => compact('key', 'fields')
  533. );
  534. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  535. }
  536. /**
  537. * test that validatePost fails if any of its required fields are missing.
  538. *
  539. * @return void
  540. */
  541. function testValidatePostFormHacking() {
  542. $this->Controller->Security->startup($this->Controller);
  543. $key = $this->Controller->params['_Token']['key'];
  544. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  545. $this->Controller->data = array(
  546. 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
  547. '_Token' => compact('key')
  548. );
  549. $result = $this->Controller->Security->validatePost($this->Controller);
  550. $this->assertFalse($result, 'validatePost passed when fields were missing. %s');
  551. $this->Controller->data = array(
  552. 'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
  553. '_Token' => compact('fields')
  554. );
  555. $result = $this->Controller->Security->validatePost($this->Controller);
  556. $this->assertFalse($result, 'validatePost passed when key was missing. %s');
  557. }
  558. /**
  559. * Test that objects can't be passed into the serialized string. This was a vector for RFI and LFI
  560. * attacks. Thanks to Felix Wilhelm
  561. *
  562. * @return void
  563. */
  564. function testValidatePostObjectDeserialize() {
  565. $this->Controller->Security->startup($this->Controller);
  566. $key = $this->Controller->params['_Token']['key'];
  567. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877';
  568. // a corrupted serialized object, so we can see if it ever gets to deserialize
  569. $attack = 'O:3:"App":1:{s:5:"__map";a:1:{s:3:"foo";s:7:"Hacked!";s:1:"fail"}}';
  570. $fields .= urlencode(':' . str_rot13($attack));
  571. $this->Controller->data = array(
  572. 'Model' => array('username' => 'mark', 'password' => 'foo', 'valid' => '0'),
  573. '_Token' => compact('key', 'fields')
  574. );
  575. $result = $this->Controller->Security->validatePost($this->Controller);
  576. $this->assertFalse($result, 'validatePost passed when key was missing. %s');
  577. }
  578. /**
  579. * Tests validation of checkbox arrays
  580. *
  581. * @access public
  582. * @return void
  583. */
  584. function testValidatePostArray() {
  585. $this->Controller->Security->startup($this->Controller);
  586. $key = $this->Controller->params['_Token']['key'];
  587. $fields = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  588. $this->Controller->data = array(
  589. 'Model' => array('multi_field' => array('1', '3')),
  590. '_Token' => compact('key', 'fields')
  591. );
  592. $this->assertTrue($this->Controller->Security->validatePost($this->Controller));
  593. }
  594. /**
  595. * testValidatePostNoModel method
  596. *
  597. * @access public
  598. * @return void
  599. */
  600. function testValidatePostNoModel() {
  601. $this->Controller->Security->startup($this->Controller);
  602. $key = $this->Controller->params['_Token']['key'];
  603. $fields = '540ac9c60d323c22bafe997b72c0790f39a8bdef%3A';
  604. $this->Controller->data = array(
  605. 'anything' => 'some_data',
  606. '_Token' => compact('key', 'fields')
  607. );
  608. $result = $this->Controller->Security->validatePost($this->Controller);
  609. $this->assertTrue($result);
  610. }
  611. /**
  612. * testValidatePostSimple method
  613. *
  614. * @access public
  615. * @return void
  616. */
  617. function testValidatePostSimple() {
  618. $this->Controller->Security->startup($this->Controller);
  619. $key = $this->Controller->params['_Token']['key'];
  620. $fields = '69f493434187b867ea14b901fdf58b55d27c935d%3A';
  621. $this->Controller->data = $data = array(
  622. 'Model' => array('username' => '', 'password' => ''),
  623. '_Token' => compact('key', 'fields')
  624. );
  625. $result = $this->Controller->Security->validatePost($this->Controller);
  626. $this->assertTrue($result);
  627. }
  628. /**
  629. * Tests hash validation for multiple records, including locked fields
  630. *
  631. * @access public
  632. * @return void
  633. */
  634. function testValidatePostComplex() {
  635. $this->Controller->Security->startup($this->Controller);
  636. $key = $this->Controller->params['_Token']['key'];
  637. $fields = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  638. $this->Controller->data = array(
  639. 'Addresses' => array(
  640. '0' => array(
  641. 'id' => '123456', 'title' => '', 'first_name' => '', 'last_name' => '',
  642. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  643. ),
  644. '1' => array(
  645. 'id' => '654321', 'title' => '', 'first_name' => '', 'last_name' => '',
  646. 'address' => '', 'city' => '', 'phone' => '', 'primary' => ''
  647. )
  648. ),
  649. '_Token' => compact('key', 'fields')
  650. );
  651. $result = $this->Controller->Security->validatePost($this->Controller);
  652. $this->assertTrue($result);
  653. }
  654. /**
  655. * test ValidatePost with multiple select elements.
  656. *
  657. * @return void
  658. */
  659. function testValidatePostMultipleSelect() {
  660. $this->Controller->Security->startup($this->Controller);
  661. $key = $this->Controller->params['_Token']['key'];
  662. $fields = '422cde416475abc171568be690a98cad20e66079%3A';
  663. $this->Controller->data = array(
  664. 'Tag' => array('Tag' => array(1, 2)),
  665. '_Token' => compact('key', 'fields'),
  666. );
  667. $result = $this->Controller->Security->validatePost($this->Controller);
  668. $this->assertTrue($result);
  669. $this->Controller->data = array(
  670. 'Tag' => array('Tag' => array(1, 2, 3)),
  671. '_Token' => compact('key', 'fields'),
  672. );
  673. $result = $this->Controller->Security->validatePost($this->Controller);
  674. $this->assertTrue($result);
  675. $this->Controller->data = array(
  676. 'Tag' => array('Tag' => array(1, 2, 3, 4)),
  677. '_Token' => compact('key', 'fields'),
  678. );
  679. $result = $this->Controller->Security->validatePost($this->Controller);
  680. $this->assertTrue($result);
  681. $fields = '19464422eafe977ee729c59222af07f983010c5f%3A';
  682. $this->Controller->data = array(
  683. 'User.password' => 'bar', 'User.name' => 'foo', 'User.is_valid' => '1',
  684. 'Tag' => array('Tag' => array(1)), '_Token' => compact('key', 'fields'),
  685. );
  686. $result = $this->Controller->Security->validatePost($this->Controller);
  687. $this->assertTrue($result);
  688. }
  689. /**
  690. * testValidatePostCheckbox method
  691. *
  692. * First block tests un-checked checkbox
  693. * Second block tests checked checkbox
  694. *
  695. * @access public
  696. * @return void
  697. */
  698. function testValidatePostCheckbox() {
  699. $this->Controller->Security->startup($this->Controller);
  700. $key = $this->Controller->params['_Token']['key'];
  701. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  702. $this->Controller->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. $fields = '874439ca69f89b4c4a5f50fb9c36ff56a28f5d42%3A';
  709. $this->Controller->data = array(
  710. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  711. '_Token' => compact('key', 'fields')
  712. );
  713. $result = $this->Controller->Security->validatePost($this->Controller);
  714. $this->assertTrue($result);
  715. $this->Controller->data = array();
  716. $this->Controller->Security->startup($this->Controller);
  717. $key = $this->Controller->params['_Token']['key'];
  718. $this->Controller->data = $data = array(
  719. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  720. '_Token' => compact('key', 'fields')
  721. );
  722. $result = $this->Controller->Security->validatePost($this->Controller);
  723. $this->assertTrue($result);
  724. }
  725. /**
  726. * testValidatePostHidden method
  727. *
  728. * @access public
  729. * @return void
  730. */
  731. function testValidatePostHidden() {
  732. $this->Controller->Security->startup($this->Controller);
  733. $key = $this->Controller->params['_Token']['key'];
  734. $fields = '51ccd8cb0997c7b3d4523ecde5a109318405ef8c%3AModel.hidden%7CModel.other_hidden';
  735. $fields .= '';
  736. $this->Controller->data = array(
  737. 'Model' => array(
  738. 'username' => '', 'password' => '', 'hidden' => '0',
  739. 'other_hidden' => 'some hidden value'
  740. ),
  741. '_Token' => compact('key', 'fields')
  742. );
  743. $result = $this->Controller->Security->validatePost($this->Controller);
  744. $this->assertTrue($result);
  745. }
  746. /**
  747. * testValidatePostWithDisabledFields method
  748. *
  749. * @access public
  750. * @return void
  751. */
  752. function testValidatePostWithDisabledFields() {
  753. $this->Controller->Security->disabledFields = array('Model.username', 'Model.password');
  754. $this->Controller->Security->startup($this->Controller);
  755. $key = $this->Controller->params['_Token']['key'];
  756. $fields = 'ef1082968c449397bcd849f963636864383278b1%3AModel.hidden';
  757. $this->Controller->data = array(
  758. 'Model' => array(
  759. 'username' => '', 'password' => '', 'hidden' => '0'
  760. ),
  761. '_Token' => compact('fields', 'key')
  762. );
  763. $result = $this->Controller->Security->validatePost($this->Controller);
  764. $this->assertTrue($result);
  765. }
  766. /**
  767. * testValidateHiddenMultipleModel method
  768. *
  769. * @access public
  770. * @return void
  771. */
  772. function testValidateHiddenMultipleModel() {
  773. $this->Controller->Security->startup($this->Controller);
  774. $key = $this->Controller->params['_Token']['key'];
  775. $fields = 'a2d01072dc4660eea9d15007025f35a7a5b58e18%3AModel.valid%7CModel2.valid%7CModel3.valid';
  776. $this->Controller->data = array(
  777. 'Model' => array('username' => '', 'password' => '', 'valid' => '0'),
  778. 'Model2' => array('valid' => '0'),
  779. 'Model3' => array('valid' => '0'),
  780. '_Token' => compact('key', 'fields')
  781. );
  782. $result = $this->Controller->Security->validatePost($this->Controller);
  783. $this->assertTrue($result);
  784. }
  785. /**
  786. * testLoginValidation method
  787. *
  788. * @access public
  789. * @return void
  790. */
  791. function testLoginValidation() {
  792. }
  793. /**
  794. * testValidateHasManyModel method
  795. *
  796. * @access public
  797. * @return void
  798. */
  799. function testValidateHasManyModel() {
  800. $this->Controller->Security->startup($this->Controller);
  801. $key = $this->Controller->params['_Token']['key'];
  802. $fields = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  803. $fields .= '%7CModel.1.hidden%7CModel.1.valid';
  804. $this->Controller->data = array(
  805. 'Model' => array(
  806. array(
  807. 'username' => 'username', 'password' => 'password',
  808. 'hidden' => 'value', 'valid' => '0'
  809. ),
  810. array(
  811. 'username' => 'username', 'password' => 'password',
  812. 'hidden' => 'value', 'valid' => '0'
  813. )
  814. ),
  815. '_Token' => compact('key', 'fields')
  816. );
  817. $result = $this->Controller->Security->validatePost($this->Controller);
  818. $this->assertTrue($result);
  819. }
  820. /**
  821. * testValidateHasManyRecordsPass method
  822. *
  823. * @access public
  824. * @return void
  825. */
  826. function testValidateHasManyRecordsPass() {
  827. $this->Controller->Security->startup($this->Controller);
  828. $key = $this->Controller->params['_Token']['key'];
  829. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
  830. $fields .= 'Address.1.id%7CAddress.1.primary';
  831. $this->Controller->data = array(
  832. 'Address' => array(
  833. 0 => array(
  834. 'id' => '123',
  835. 'title' => 'home',
  836. 'first_name' => 'Bilbo',
  837. 'last_name' => 'Baggins',
  838. 'address' => '23 Bag end way',
  839. 'city' => 'the shire',
  840. 'phone' => 'N/A',
  841. 'primary' => '1',
  842. ),
  843. 1 => array(
  844. 'id' => '124',
  845. 'title' => 'home',
  846. 'first_name' => 'Frodo',
  847. 'last_name' => 'Baggins',
  848. 'address' => '50 Bag end way',
  849. 'city' => 'the shire',
  850. 'phone' => 'N/A',
  851. 'primary' => '1'
  852. )
  853. ),
  854. '_Token' => compact('key', 'fields')
  855. );
  856. $result = $this->Controller->Security->validatePost($this->Controller);
  857. $this->assertTrue($result);
  858. }
  859. /**
  860. * testValidateHasManyRecords method
  861. *
  862. * validatePost should fail, hidden fields have been changed.
  863. *
  864. * @access public
  865. * @return void
  866. */
  867. function testValidateHasManyRecordsFail() {
  868. $this->Controller->Security->startup($this->Controller);
  869. $key = $this->Controller->params['_Token']['key'];
  870. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
  871. $fields .= 'Address.1.id%7CAddress.1.primary';
  872. $this->Controller->data = array(
  873. 'Address' => array(
  874. 0 => array(
  875. 'id' => '123',
  876. 'title' => 'home',
  877. 'first_name' => 'Bilbo',
  878. 'last_name' => 'Baggins',
  879. 'address' => '23 Bag end way',
  880. 'city' => 'the shire',
  881. 'phone' => 'N/A',
  882. 'primary' => '5',
  883. ),
  884. 1 => array(
  885. 'id' => '124',
  886. 'title' => 'home',
  887. 'first_name' => 'Frodo',
  888. 'last_name' => 'Baggins',
  889. 'address' => '50 Bag end way',
  890. 'city' => 'the shire',
  891. 'phone' => 'N/A',
  892. 'primary' => '1'
  893. )
  894. ),
  895. '_Token' => compact('key', 'fields')
  896. );
  897. $result = $this->Controller->Security->validatePost($this->Controller);
  898. $this->assertFalse($result);
  899. }
  900. /**
  901. * testLoginRequest method
  902. *
  903. * @access public
  904. * @return void
  905. */
  906. function testLoginRequest() {
  907. $this->Controller->Security->startup($this->Controller);
  908. $realm = 'cakephp.org';
  909. $options = array('realm' => $realm, 'type' => 'basic');
  910. $result = $this->Controller->Security->loginRequest($options);
  911. $expected = 'WWW-Authenticate: Basic realm="'.$realm.'"';
  912. $this->assertEqual($result, $expected);
  913. $this->Controller->Security->startup($this->Controller);
  914. $options = array('realm' => $realm, 'type' => 'digest');
  915. $result = $this->Controller->Security->loginRequest($options);
  916. $this->assertPattern('/realm="'.$realm.'"/', $result);
  917. $this->assertPattern('/qop="auth"/', $result);
  918. }
  919. /**
  920. * testGenerateDigestResponseHash method
  921. *
  922. * @access public
  923. * @return void
  924. */
  925. function testGenerateDigestResponseHash() {
  926. $this->Controller->Security->startup($this->Controller);
  927. $realm = 'cakephp.org';
  928. $loginData = array('realm' => $realm, 'users' => array('Willy Smith' => 'password'));
  929. $this->Controller->Security->requireLogin($loginData);
  930. $data = array(
  931. 'username' => 'Willy Smith',
  932. 'password' => 'password',
  933. 'nonce' => String::uuid(),
  934. 'nc' => 1,
  935. 'cnonce' => 1,
  936. 'realm' => $realm,
  937. 'uri' => 'path_to_identifier',
  938. 'qop' => 'testme'
  939. );
  940. $_SERVER['REQUEST_METHOD'] = 'POST';
  941. $result = $this->Controller->Security->generateDigestResponseHash($data);
  942. $expected = md5(
  943. md5($data['username'] . ':' . $loginData['realm'] . ':' . $data['password']) . ':' .
  944. $data['nonce'] . ':' . $data['nc'] . ':' . $data['cnonce'] . ':' . $data['qop'] . ':' .
  945. md5(env('REQUEST_METHOD') . ':' . $data['uri'])
  946. );
  947. $this->assertIdentical($result, $expected);
  948. }
  949. /**
  950. * testLoginCredentials method
  951. *
  952. * @access public
  953. * @return void
  954. */
  955. function testLoginCredentials() {
  956. $this->Controller->Security->startup($this->Controller);
  957. $_SERVER['PHP_AUTH_USER'] = $user = 'Willy Test';
  958. $_SERVER['PHP_AUTH_PW'] = $pw = 'some password for the nice test';
  959. $result = $this->Controller->Security->loginCredentials('basic');
  960. $expected = array('username' => $user, 'password' => $pw);
  961. $this->assertIdentical($result, $expected);
  962. if (version_compare(PHP_VERSION, '5.1') != -1) {
  963. $_SERVER['PHP_AUTH_DIGEST'] = $digest = <<<DIGEST
  964. Digest username="Mufasa",
  965. realm="testrealm@host.com",
  966. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  967. uri="/dir/index.html",
  968. qop=auth,
  969. nc=00000001,
  970. cnonce="0a4f113b",
  971. response="6629fae49393a05397450978507c4ef1",
  972. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  973. DIGEST;
  974. $expected = array(
  975. 'username' => 'Mufasa',
  976. 'realm' => 'testrealm@host.com',
  977. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  978. 'uri' => '/dir/index.html',
  979. 'qop' => 'auth',
  980. 'nc' => '00000001',
  981. 'cnonce' => '0a4f113b',
  982. 'response' => '6629fae49393a05397450978507c4ef1',
  983. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  984. );
  985. $result = $this->Controller->Security->loginCredentials('digest');
  986. $this->assertIdentical($result, $expected);
  987. }
  988. }
  989. /**
  990. * testParseDigestAuthData method
  991. *
  992. * @access public
  993. * @return void
  994. */
  995. function testParseDigestAuthData() {
  996. $this->Controller->Security->startup($this->Controller);
  997. $digest = <<<DIGEST
  998. Digest username="Mufasa",
  999. realm="testrealm@host.com",
  1000. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  1001. uri="/dir/index.html",
  1002. qop=auth,
  1003. nc=00000001,
  1004. cnonce="0a4f113b",
  1005. response="6629fae49393a05397450978507c4ef1",
  1006. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  1007. DIGEST;
  1008. $expected = array(
  1009. 'username' => 'Mufasa',
  1010. 'realm' => 'testrealm@host.com',
  1011. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  1012. 'uri' => '/dir/index.html',
  1013. 'qop' => 'auth',
  1014. 'nc' => '00000001',
  1015. 'cnonce' => '0a4f113b',
  1016. 'response' => '6629fae49393a05397450978507c4ef1',
  1017. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  1018. );
  1019. $result = $this->Controller->Security->parseDigestAuthData($digest);
  1020. $this->assertIdentical($result, $expected);
  1021. $result = $this->Controller->Security->parseDigestAuthData('');
  1022. $this->assertNull($result);
  1023. }
  1024. /**
  1025. * test parsing digest information with email addresses
  1026. *
  1027. * @return void
  1028. */
  1029. function testParseDigestAuthEmailAddress() {
  1030. $this->Controller->Security->startup($this->Controller);
  1031. $digest = <<<DIGEST
  1032. Digest username="mark@example.com",
  1033. realm="testrealm@host.com",
  1034. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  1035. uri="/dir/index.html",
  1036. qop=auth,
  1037. nc=00000001,
  1038. cnonce="0a4f113b",
  1039. response="6629fae49393a05397450978507c4ef1",
  1040. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  1041. DIGEST;
  1042. $expected = array(
  1043. 'username' => 'mark@example.com',
  1044. 'realm' => 'testrealm@host.com',
  1045. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  1046. 'uri' => '/dir/index.html',
  1047. 'qop' => 'auth',
  1048. 'nc' => '00000001',
  1049. 'cnonce' => '0a4f113b',
  1050. 'response' => '6629fae49393a05397450978507c4ef1',
  1051. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  1052. );
  1053. $result = $this->Controller->Security->parseDigestAuthData($digest);
  1054. $this->assertIdentical($result, $expected);
  1055. }
  1056. /**
  1057. * testFormDisabledFields method
  1058. *
  1059. * @access public
  1060. * @return void
  1061. */
  1062. function testFormDisabledFields() {
  1063. $this->Controller->Security->startup($this->Controller);
  1064. $key = $this->Controller->params['_Token']['key'];
  1065. $fields = '11842060341b9d0fc3808b90ba29fdea7054d6ad%3An%3A0%3A%7B%7D';
  1066. $this->Controller->data = array(
  1067. 'MyModel' => array('name' => 'some data'),
  1068. '_Token' => compact('key', 'fields')
  1069. );
  1070. $result = $this->Controller->Security->validatePost($this->Controller);
  1071. $this->assertFalse($result);
  1072. $this->Controller->Security->startup($this->Controller);
  1073. $this->Controller->Security->disabledFields = array('MyModel.name');
  1074. $key = $this->Controller->params['_Token']['key'];
  1075. $this->Controller->data = array(
  1076. 'MyModel' => array('name' => 'some data'),
  1077. '_Token' => compact('key', 'fields')
  1078. );
  1079. $result = $this->Controller->Security->validatePost($this->Controller);
  1080. $this->assertTrue($result);
  1081. }
  1082. /**
  1083. * testRadio method
  1084. *
  1085. * @access public
  1086. * @return void
  1087. */
  1088. function testRadio() {
  1089. $this->Controller->Security->startup($this->Controller);
  1090. $key = $this->Controller->params['_Token']['key'];
  1091. $fields = '575ef54ca4fc8cab468d6d898e9acd3a9671c17e%3An%3A0%3A%7B%7D';
  1092. $this->Controller->data = array(
  1093. '_Token' => compact('key', 'fields')
  1094. );
  1095. $result = $this->Controller->Security->validatePost($this->Controller);
  1096. $this->assertFalse($result);
  1097. $this->Controller->data = array(
  1098. '_Token' => compact('key', 'fields'),
  1099. 'Test' => array('test' => '')
  1100. );
  1101. $result = $this->Controller->Security->validatePost($this->Controller);
  1102. $this->assertTrue($result);
  1103. $this->Controller->data = array(
  1104. '_Token' => compact('key', 'fields'),
  1105. 'Test' => array('test' => '1')
  1106. );
  1107. $result = $this->Controller->Security->validatePost($this->Controller);
  1108. $this->assertTrue($result);
  1109. $this->Controller->data = array(
  1110. '_Token' => compact('key', 'fields'),
  1111. 'Test' => array('test' => '2')
  1112. );
  1113. $result = $this->Controller->Security->validatePost($this->Controller);
  1114. $this->assertTrue($result);
  1115. }
  1116. /**
  1117. * testInvalidAuthHeaders method
  1118. *
  1119. * @access public
  1120. * @return void
  1121. */
  1122. function testInvalidAuthHeaders() {
  1123. $this->Controller->Security->blackHoleCallback = null;
  1124. $_SERVER['PHP_AUTH_USER'] = 'admin';
  1125. $_SERVER['PHP_AUTH_PW'] = 'password';
  1126. $realm = 'cakephp.org';
  1127. $loginData = array('type' => 'basic', 'realm' => $realm);
  1128. $this->Controller->Security->requireLogin($loginData);
  1129. $this->Controller->Security->startup($this->Controller);
  1130. $expected = 'WWW-Authenticate: Basic realm="'.$realm.'"';
  1131. $this->assertEqual(count($this->Controller->testHeaders), 1);
  1132. $this->assertEqual(current($this->Controller->testHeaders), $expected);
  1133. }
  1134. /**
  1135. * test that a requestAction's controller will have the _Token appended to
  1136. * the params.
  1137. *
  1138. * @return void
  1139. * @see http://cakephp.lighthouseapp.com/projects/42648/tickets/68
  1140. */
  1141. function testSettingTokenForRequestAction() {
  1142. $this->Controller->Security->startup($this->Controller);
  1143. $key = $this->Controller->params['_Token']['key'];
  1144. $this->Controller->params['requested'] = 1;
  1145. unset($this->Controller->params['_Token']);
  1146. $this->Controller->Security->startup($this->Controller);
  1147. $this->assertEqual($this->Controller->params['_Token']['key'], $key);
  1148. }
  1149. /**
  1150. * test that blackhole doesn't delete the _Token session key so repeat data submissions
  1151. * stay blackholed.
  1152. *
  1153. * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/214
  1154. * @return void
  1155. */
  1156. function testBlackHoleNotDeletingSessionInformation() {
  1157. $this->Controller->Security->startup($this->Controller);
  1158. $this->Controller->Security->blackHole($this->Controller, 'auth');
  1159. $this->assertTrue($this->Controller->Security->Session->check('_Token'), '_Token was deleted by blackHole %s');
  1160. }
  1161. }