FormProtectorTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 4.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Form;
  17. use Cake\Core\Configure;
  18. use Cake\Form\FormProtector;
  19. use Cake\TestSuite\TestCase;
  20. use Cake\Utility\Security;
  21. /**
  22. * FormProtectorTest class
  23. */
  24. class FormProtectorTest extends TestCase
  25. {
  26. /**
  27. * @var string
  28. */
  29. protected $url = '/articles/index';
  30. /**
  31. * @var string
  32. */
  33. protected $sessionId = 'cli';
  34. public function setUp(): void
  35. {
  36. parent::setUp();
  37. Security::setSalt('foo!');
  38. // $this->protector = new FormProtector('http://localhost/articles/index', 'cli');
  39. }
  40. /**
  41. * Helper function for validation.
  42. *
  43. * @param array $data
  44. * @param string|null $errorMessage
  45. * @return void
  46. */
  47. public function validate($data, $errorMessage = null)
  48. {
  49. $protector = new FormProtector();
  50. $result = $protector->validate($data, $this->url, $this->sessionId);
  51. if ($errorMessage === null) {
  52. $this->assertTrue($result);
  53. } else {
  54. $this->assertFalse($result);
  55. $this->assertSame($errorMessage, $protector->getError());
  56. }
  57. }
  58. /**
  59. * testValidate method
  60. *
  61. * Simple hash validation test
  62. *
  63. * @return void
  64. */
  65. public function testValidate(): void
  66. {
  67. $fields = '4697b45f7f430ff3ab73018c20f315eecb0ba5a6%3AModel.valid';
  68. $unlocked = '';
  69. $debug = '';
  70. $data = [
  71. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  72. '_Token' => compact('fields', 'unlocked', 'debug'),
  73. ];
  74. $this->validate($data);
  75. }
  76. /**
  77. * testValidateNoUnlockedInRequestData method
  78. *
  79. * Test that validate fails if you are missing unlocked in request data.
  80. *
  81. * @return void
  82. */
  83. public function testValidateNoUnlockedInRequestData(): void
  84. {
  85. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  86. $data = [
  87. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  88. '_Token' => compact('fields'),
  89. ];
  90. $this->validate($data, '\'_Token.unlocked\' was not found in request data.');
  91. }
  92. /**
  93. * testValidateFormHacking method
  94. *
  95. * Test that validate fails if any of its required fields are missing.
  96. *
  97. * @return void
  98. */
  99. public function testValidateFormHacking(): void
  100. {
  101. $unlocked = '';
  102. $data = [
  103. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  104. '_Token' => compact('unlocked'),
  105. ];
  106. $this->validate($data, '\'_Token.fields\' was not found in request data.');
  107. }
  108. /**
  109. * testValidateEmptyForm method
  110. *
  111. * Test that validate fails if empty form is submitted.
  112. *
  113. * @return void
  114. */
  115. public function testValidateEmptyForm(): void
  116. {
  117. $this->validate([], '\'_Token\' was not found in request data.');
  118. }
  119. /**
  120. * testValidateObjectDeserialize
  121. *
  122. * Test that objects can't be passed into the serialized string. This was a vector for RFI and LFI
  123. * attacks. Thanks to Felix Wilhelm
  124. *
  125. * @return void
  126. */
  127. public function testValidateObjectDeserialize(): void
  128. {
  129. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877';
  130. $unlocked = '';
  131. $debug = urlencode(json_encode([
  132. '/articles/index',
  133. ['Model.password', 'Model.username', 'Model.valid'],
  134. [],
  135. ]));
  136. // a corrupted serialized object, so we can see if it ever gets to deserialize
  137. $attack = 'O:3:"App":1:{s:5:"__map";a:1:{s:3:"foo";s:7:"Hacked!";s:1:"fail"}}';
  138. $fields .= urlencode(':' . str_rot13($attack));
  139. $data = [
  140. 'Model' => ['username' => 'mark', 'password' => 'foo', 'valid' => '0'],
  141. '_Token' => compact('fields', 'unlocked', 'debug'),
  142. ];
  143. $protector = new FormProtector();
  144. $result = $protector->validate($data, $this->url, $this->sessionId);
  145. $this->assertFalse($result);
  146. }
  147. /**
  148. * testValidateArray method
  149. *
  150. * Tests validation of checkbox arrays.
  151. *
  152. * @return void
  153. */
  154. public function testValidateArray(): void
  155. {
  156. $fields = 'f95b472a63f1d883b9eaacaf8a8e36e325e3fe82%3A';
  157. $unlocked = '';
  158. $debug = urlencode(json_encode([
  159. 'some-action',
  160. [],
  161. [],
  162. ]));
  163. $data = [
  164. 'Model' => ['multi_field' => ['1', '3']],
  165. '_Token' => compact('fields', 'unlocked', 'debug'),
  166. ];
  167. $this->validate($data);
  168. $data = [
  169. 'Model' => ['multi_field' => [12 => '1', 20 => '3']],
  170. '_Token' => compact('fields', 'unlocked', 'debug'),
  171. ];
  172. $this->validate($data);
  173. }
  174. /**
  175. * testValidateIntFieldName method
  176. *
  177. * Tests validation of integer field names.
  178. *
  179. * @return void
  180. */
  181. public function testValidateIntFieldName(): void
  182. {
  183. $fields = '11f87a5962db9ac26405e460cd3063bb6ff76cf8%3A';
  184. $unlocked = '';
  185. $debug = urlencode(json_encode([
  186. 'some-action',
  187. [],
  188. [],
  189. ]));
  190. $data = [
  191. 1 => 'value,',
  192. '_Token' => compact('fields', 'unlocked', 'debug'),
  193. ];
  194. $this->validate($data);
  195. }
  196. /**
  197. * testValidateNoModel method
  198. *
  199. * @return void
  200. */
  201. public function testValidateNoModel(): void
  202. {
  203. $fields = 'a2a942f587deb20e90241c51b59d901d8a7f796b%3A';
  204. $unlocked = '';
  205. $debug = 'not used';
  206. $data = [
  207. 'anything' => 'some_data',
  208. '_Token' => compact('fields', 'unlocked', 'debug'),
  209. ];
  210. $this->validate($data);
  211. }
  212. /**
  213. * test validate uses full URL
  214. *
  215. * @return void
  216. */
  217. public function testValidateSubdirectory(): void
  218. {
  219. $this->url = '/subdir' . $this->url;
  220. $fields = 'cc9b6af3f33147235ae8f8037b0a71399a2425f2%3A';
  221. $unlocked = '';
  222. $debug = '';
  223. $data = [
  224. 'Model' => ['username' => '', 'password' => ''],
  225. '_Token' => compact('fields', 'unlocked', 'debug'),
  226. ];
  227. $this->validate($data);
  228. }
  229. /**
  230. * testValidateComplex method
  231. *
  232. * Tests hash validation for multiple records, including locked fields.
  233. *
  234. * @return void
  235. */
  236. public function testValidateComplex(): void
  237. {
  238. $fields = 'b00b7e5c2e3bf8bc474fb7cfde6f9c2aa06ab9bc%3AAddresses.0.id%7CAddresses.1.id';
  239. $unlocked = '';
  240. $debug = 'not used';
  241. $data = [
  242. 'Addresses' => [
  243. '0' => [
  244. 'id' => '123456', 'title' => '', 'first_name' => '', 'last_name' => '',
  245. 'address' => '', 'city' => '', 'phone' => '', 'primary' => '',
  246. ],
  247. '1' => [
  248. 'id' => '654321', 'title' => '', 'first_name' => '', 'last_name' => '',
  249. 'address' => '', 'city' => '', 'phone' => '', 'primary' => '',
  250. ],
  251. ],
  252. '_Token' => compact('fields', 'unlocked', 'debug'),
  253. ];
  254. $this->validate($data);
  255. }
  256. /**
  257. * testValidateMultipleSelect method
  258. *
  259. * Test ValidatePost with multiple select elements.
  260. *
  261. * @return void
  262. */
  263. public function testValidateMultipleSelect(): void
  264. {
  265. $fields = '28dd05f0af314050784b18b3366857e8e8c78e73%3A';
  266. $unlocked = '';
  267. $debug = 'not used';
  268. $data = [
  269. 'Tag' => ['Tag' => [1, 2]],
  270. '_Token' => compact('fields', 'unlocked', 'debug'),
  271. ];
  272. $this->validate($data);
  273. $data = [
  274. 'Tag' => ['Tag' => [1, 2, 3]],
  275. '_Token' => compact('fields', 'unlocked', 'debug'),
  276. ];
  277. $this->validate($data);
  278. $data = [
  279. 'Tag' => ['Tag' => [1, 2, 3, 4]],
  280. '_Token' => compact('fields', 'unlocked', 'debug'),
  281. ];
  282. $this->validate($data);
  283. $fields = '1e4c9269b64756e9b141d364497c5f037b428a37%3A';
  284. $data = [
  285. 'User.password' => 'bar', 'User.name' => 'foo', 'User.is_valid' => '1',
  286. 'Tag' => ['Tag' => [1]],
  287. '_Token' => compact('fields', 'unlocked', 'debug'),
  288. ];
  289. $this->validate($data);
  290. }
  291. /**
  292. * testValidateCheckbox method
  293. *
  294. * First block tests un-checked checkbox
  295. * Second block tests checked checkbox
  296. *
  297. * @return void
  298. */
  299. public function testValidateCheckbox(): void
  300. {
  301. $fields = '4697b45f7f430ff3ab73018c20f315eecb0ba5a6%3AModel.valid';
  302. $unlocked = '';
  303. $debug = 'not used';
  304. $data = [
  305. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  306. '_Token' => compact('fields', 'unlocked', 'debug'),
  307. ];
  308. $this->validate($data);
  309. $fields = '3f368401f9a8610bcace7746039651066cdcdc38%3A';
  310. $data = [
  311. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  312. '_Token' => compact('fields', 'unlocked', 'debug'),
  313. ];
  314. $this->validate($data);
  315. $data = [
  316. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  317. '_Token' => compact('fields', 'unlocked', 'debug'),
  318. ];
  319. $this->validate($data);
  320. }
  321. /**
  322. * testValidateHidden method
  323. *
  324. * @return void
  325. */
  326. public function testValidateHidden(): void
  327. {
  328. $fields = '96e61bded2b62b0c420116a0eb06a3b3acddb8f1%3AModel.hidden%7CModel.other_hidden';
  329. $unlocked = '';
  330. $debug = 'not used';
  331. $data = [
  332. 'Model' => [
  333. 'username' => '', 'password' => '', 'hidden' => '0',
  334. 'other_hidden' => 'some hidden value',
  335. ],
  336. '_Token' => compact('fields', 'unlocked', 'debug'),
  337. ];
  338. $this->validate($data);
  339. }
  340. /**
  341. * testValidateDisabledFieldsInData method
  342. *
  343. * Test validating post data with posted unlocked fields.
  344. *
  345. * @return void
  346. */
  347. public function testValidateDisabledFieldsInData(): void
  348. {
  349. $unlocked = 'Model.username';
  350. $fields = ['Model.hidden', 'Model.password'];
  351. $fields = urlencode(
  352. hash_hmac('sha1', '/articles/index' . serialize($fields) . $unlocked . 'cli', Security::getSalt())
  353. );
  354. $debug = 'not used';
  355. $data = [
  356. 'Model' => [
  357. 'username' => 'mark',
  358. 'password' => 'sekret',
  359. 'hidden' => '0',
  360. ],
  361. '_Token' => compact('fields', 'unlocked', 'debug'),
  362. ];
  363. $this->validate($data);
  364. }
  365. /**
  366. * testValidateFailNoDisabled method
  367. *
  368. * Test that missing 'unlocked' input causes failure.
  369. *
  370. * @return void
  371. */
  372. public function testValidateFailNoDisabled(): void
  373. {
  374. $fields = ['Model.hidden', 'Model.password', 'Model.username'];
  375. $fields = urlencode(Security::hash(serialize($fields) . Security::getSalt()));
  376. $data = [
  377. 'Model' => [
  378. 'username' => 'mark',
  379. 'password' => 'sekret',
  380. 'hidden' => '0',
  381. ],
  382. '_Token' => compact('fields'),
  383. ];
  384. $this->validate($data, '\'_Token.unlocked\' was not found in request data.');
  385. }
  386. /**
  387. * testValidateFailNoDebug method
  388. *
  389. * Test that missing 'debug' input causes failure.
  390. *
  391. * @return void
  392. */
  393. public function testValidateFailNoDebug(): void
  394. {
  395. $fields = ['Model.hidden', 'Model.password', 'Model.username'];
  396. $fields = urlencode(Security::hash(serialize($fields) . Security::getSalt()));
  397. $unlocked = '';
  398. $data = [
  399. 'Model' => [
  400. 'username' => 'mark',
  401. 'password' => 'sekret',
  402. 'hidden' => '0',
  403. ],
  404. '_Token' => compact('fields', 'unlocked'),
  405. ];
  406. $this->validate($data, '\'_Token.debug\' was not found in request data.');
  407. }
  408. /**
  409. * testValidateFailNoDebugMode method
  410. *
  411. * Test that missing 'debug' input is not the problem when debug mode disabled.
  412. *
  413. * @return void
  414. */
  415. public function testValidateFailNoDebugMode(): void
  416. {
  417. $fields = ['Model.hidden', 'Model.password', 'Model.username'];
  418. $fields = urlencode(Security::hash(serialize($fields) . Security::getSalt()));
  419. $unlocked = '';
  420. $data = [
  421. 'Model' => [
  422. 'username' => 'mark',
  423. 'password' => 'sekret',
  424. 'hidden' => '0',
  425. ],
  426. '_Token' => compact('fields', 'unlocked'),
  427. ];
  428. Configure::write('debug', false);
  429. $protector = new FormProtector();
  430. $result = $protector->validate($data, $this->url, $this->sessionId);
  431. $this->assertFalse($result);
  432. }
  433. /**
  434. * testValidateFailDisabledFieldTampering method
  435. *
  436. * Test that validate fails when unlocked fields are changed.
  437. *
  438. * @return void
  439. */
  440. public function testValidateFailDisabledFieldTampering(): void
  441. {
  442. $unlocked = 'Model.username';
  443. $fields = ['Model.hidden', 'Model.password'];
  444. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::getSalt()));
  445. $debug = urlencode(json_encode([
  446. '/articles/index',
  447. ['Model.hidden', 'Model.password'],
  448. ['Model.username'],
  449. ]));
  450. // Tamper the values.
  451. $unlocked = 'Model.username|Model.password';
  452. $data = [
  453. 'Model' => [
  454. 'username' => 'mark',
  455. 'password' => 'sekret',
  456. 'hidden' => '0',
  457. ],
  458. '_Token' => compact('fields', 'unlocked', 'debug'),
  459. ];
  460. $this->validate($data, 'Missing field \'Model.password\' in POST data, Unexpected unlocked field \'Model.password\' in POST data');
  461. }
  462. /**
  463. * testValidateHiddenMultipleModel method
  464. *
  465. * @return void
  466. */
  467. public function testValidateHiddenMultipleModel(): void
  468. {
  469. $fields = '642b7a6db3b848fab88952b86ea36c572f93df40%3AModel.valid%7CModel2.valid%7CModel3.valid';
  470. $unlocked = '';
  471. $debug = 'not used';
  472. $data = [
  473. 'Model' => ['username' => '', 'password' => '', 'valid' => '0'],
  474. 'Model2' => ['valid' => '0'],
  475. 'Model3' => ['valid' => '0'],
  476. '_Token' => compact('fields', 'unlocked', 'debug'),
  477. ];
  478. $this->validate($data);
  479. }
  480. /**
  481. * testValidateHasManyModel method
  482. *
  483. * @return void
  484. */
  485. public function testValidateHasManyModel(): void
  486. {
  487. $fields = '792324c8a374772ad82acfb28f0e77e70f8ed3af%3AModel.0.hidden%7CModel.0.valid';
  488. $fields .= '%7CModel.1.hidden%7CModel.1.valid';
  489. $unlocked = '';
  490. $debug = 'not used';
  491. $data = [
  492. 'Model' => [
  493. [
  494. 'username' => 'username', 'password' => 'password',
  495. 'hidden' => 'value', 'valid' => '0',
  496. ],
  497. [
  498. 'username' => 'username', 'password' => 'password',
  499. 'hidden' => 'value', 'valid' => '0',
  500. ],
  501. ],
  502. '_Token' => compact('fields', 'unlocked', 'debug'),
  503. ];
  504. $this->validate($data);
  505. }
  506. /**
  507. * testValidateHasManyRecordsPass method
  508. *
  509. * @return void
  510. */
  511. public function testValidateHasManyRecordsPass(): void
  512. {
  513. $fields = '7f4bff67558e25ebeea44c84ea4befa8d50b080c%3AAddress.0.id%7CAddress.0.primary%7C';
  514. $fields .= 'Address.1.id%7CAddress.1.primary';
  515. $unlocked = '';
  516. $debug = 'not used';
  517. $data = [
  518. 'Address' => [
  519. 0 => [
  520. 'id' => '123',
  521. 'title' => 'home',
  522. 'first_name' => 'Bilbo',
  523. 'last_name' => 'Baggins',
  524. 'address' => '23 Bag end way',
  525. 'city' => 'the shire',
  526. 'phone' => 'N/A',
  527. 'primary' => '1',
  528. ],
  529. 1 => [
  530. 'id' => '124',
  531. 'title' => 'home',
  532. 'first_name' => 'Frodo',
  533. 'last_name' => 'Baggins',
  534. 'address' => '50 Bag end way',
  535. 'city' => 'the shire',
  536. 'phone' => 'N/A',
  537. 'primary' => '1',
  538. ],
  539. ],
  540. '_Token' => compact('fields', 'unlocked', 'debug'),
  541. ];
  542. $this->validate($data);
  543. }
  544. /**
  545. * testValidateHasManyRecords method
  546. *
  547. * validate should fail, hidden fields have been changed.
  548. *
  549. * @return void
  550. */
  551. public function testValidateHasManyRecordsFail(): void
  552. {
  553. $fields = '7a203edb3d345bbf38fe0dccae960da8842e11d7%3AAddress.0.id%7CAddress.0.primary%7C';
  554. $fields .= 'Address.1.id%7CAddress.1.primary';
  555. $unlocked = '';
  556. $debug = urlencode(json_encode([
  557. '/articles/index',
  558. [
  559. 'Address.0.address',
  560. 'Address.0.city',
  561. 'Address.0.first_name',
  562. 'Address.0.last_name',
  563. 'Address.0.phone',
  564. 'Address.0.title',
  565. 'Address.1.address',
  566. 'Address.1.city',
  567. 'Address.1.first_name',
  568. 'Address.1.last_name',
  569. 'Address.1.phone',
  570. 'Address.1.title',
  571. 'Address.0.id' => '123',
  572. 'Address.0.primary' => '5',
  573. 'Address.1.id' => '124',
  574. 'Address.1.primary' => '1',
  575. ],
  576. [],
  577. ]));
  578. $data = [
  579. 'Address' => [
  580. 0 => [
  581. 'id' => '123',
  582. 'title' => 'home',
  583. 'first_name' => 'Bilbo',
  584. 'last_name' => 'Baggins',
  585. 'address' => '23 Bag end way',
  586. 'city' => 'the shire',
  587. 'phone' => 'N/A',
  588. 'primary' => '5',
  589. ],
  590. 1 => [
  591. 'id' => '124',
  592. 'title' => 'home',
  593. 'first_name' => 'Frodo',
  594. 'last_name' => 'Baggins',
  595. 'address' => '50 Bag end way',
  596. 'city' => 'the shire',
  597. 'phone' => 'N/A',
  598. 'primary' => '1',
  599. ],
  600. ],
  601. '_Token' => compact('fields', 'unlocked', 'debug'),
  602. ];
  603. $protector = new FormProtector();
  604. $result = $protector->validate($data, $this->url, $this->sessionId);
  605. $this->assertFalse($result);
  606. }
  607. /**
  608. * testValidateRadio method
  609. *
  610. * Test validate with radio buttons.
  611. *
  612. * @return void
  613. * @triggers Controller.startup $this->Controller
  614. */
  615. public function testValidateRadio(): void
  616. {
  617. $fields = 'a709dfdee0a0cce52c4c964a1b8a56159bb081b4%3An%3A0%3A%7B%7D';
  618. $unlocked = '';
  619. $debug = urlencode(json_encode([
  620. '/articles/index',
  621. [],
  622. [],
  623. ]));
  624. $data = [
  625. '_Token' => compact('fields', 'unlocked', 'debug'),
  626. ];
  627. $protector = new FormProtector();
  628. $result = $protector->validate($data, $this->url, $this->sessionId);
  629. $this->assertFalse($result);
  630. $data = [
  631. '_Token' => compact('fields', 'unlocked', 'debug'),
  632. 'Test' => ['test' => ''],
  633. ];
  634. $this->validate($data);
  635. $data = [
  636. '_Token' => compact('fields', 'unlocked', 'debug'),
  637. 'Test' => ['test' => '1'],
  638. ];
  639. $this->validate($data);
  640. $data = [
  641. '_Token' => compact('fields', 'unlocked', 'debug'),
  642. 'Test' => ['test' => '2'],
  643. ];
  644. $this->validate($data);
  645. }
  646. /**
  647. * testValidateUrlAsHashInput method
  648. *
  649. * Test validate uses here() as a hash input.
  650. *
  651. * @return void
  652. */
  653. public function testValidateUrlAsHashInput(): void
  654. {
  655. $fields = 'de2ca3670dd06c29558dd98482c8739e86da2c7c%3A';
  656. $unlocked = '';
  657. $debug = urlencode(json_encode([
  658. 'another-url',
  659. ['Model.username', 'Model.password'],
  660. [],
  661. ]));
  662. $data = [
  663. 'Model' => ['username' => '', 'password' => ''],
  664. '_Token' => compact('fields', 'unlocked', 'debug'),
  665. ];
  666. $this->validate($data);
  667. $this->url = '/posts/index?page=1';
  668. $this->validate(
  669. $data,
  670. 'URL mismatch in POST data (expected \'another-url\' but found \'/posts/index?page=1\')'
  671. );
  672. $this->url = '/posts/edit/1';
  673. $this->validate(
  674. $data,
  675. 'URL mismatch in POST data (expected \'another-url\' but found \'/posts/edit/1\')'
  676. );
  677. }
  678. /**
  679. * testValidateDebugFormat method
  680. *
  681. * Test that debug token format is right.
  682. *
  683. * @return void
  684. */
  685. public function testValidateDebugFormat(): void
  686. {
  687. $unlocked = 'Model.username';
  688. $fields = ['Model.hidden', 'Model.password'];
  689. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::getSalt()));
  690. $debug = urlencode(json_encode([
  691. '/articles/index',
  692. ['Model.hidden', 'Model.password'],
  693. ['Model.username'],
  694. ['not expected'],
  695. ]));
  696. $data = [
  697. 'Model' => [
  698. 'username' => 'mark',
  699. 'password' => 'sekret',
  700. 'hidden' => '0',
  701. ],
  702. '_Token' => compact('fields', 'unlocked', 'debug'),
  703. ];
  704. $this->validate($data, 'Invalid form protection debug token.');
  705. $debug = urlencode(json_encode('not an array'));
  706. $this->validate($data, 'Invalid form protection debug token.');
  707. }
  708. /**
  709. * testValidateFailTampering method
  710. *
  711. * Test that validate fails with tampered fields and explanation.
  712. *
  713. * @return void
  714. */
  715. public function testValidateFailTampering(): void
  716. {
  717. $unlocked = '';
  718. $fields = ['Model.hidden' => 'value', 'Model.id' => '1'];
  719. $debug = urlencode(json_encode([
  720. '/articles/index',
  721. $fields,
  722. [],
  723. ]));
  724. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::getSalt()));
  725. $fields .= urlencode(':Model.hidden|Model.id');
  726. $data = [
  727. 'Model' => [
  728. 'hidden' => 'tampered',
  729. 'id' => '1',
  730. ],
  731. '_Token' => compact('fields', 'unlocked', 'debug'),
  732. ];
  733. $this->validate($data, 'Tampered field \'Model.hidden\' in POST data (expected value \'value\' but found \'tampered\')');
  734. }
  735. /**
  736. * testValidateFailTamperingMutatedIntoArray method
  737. *
  738. * Test that validate fails with tampered fields and explanation.
  739. *
  740. * @return void
  741. */
  742. public function testValidateFailTamperingMutatedIntoArray(): void
  743. {
  744. $unlocked = '';
  745. $fields = ['Model.hidden' => 'value', 'Model.id' => '1'];
  746. $debug = urlencode(json_encode([
  747. '/articles/index',
  748. $fields,
  749. [],
  750. ]));
  751. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::getSalt()));
  752. $fields .= urlencode(':Model.hidden|Model.id');
  753. $data = [
  754. 'Model' => [
  755. 'hidden' => ['some-key' => 'some-value'],
  756. 'id' => '1',
  757. ],
  758. '_Token' => compact('fields', 'unlocked', 'debug'),
  759. ];
  760. $this->validate(
  761. $data,
  762. 'Unexpected field \'Model.hidden.some-key\' in POST data, Missing field \'Model.hidden\' in POST data'
  763. );
  764. }
  765. /**
  766. * testValidateUnexpectedDebugToken method
  767. *
  768. * Test that debug token should not be sent if debug is disabled.
  769. *
  770. * @return void
  771. */
  772. public function testValidateUnexpectedDebugToken(): void
  773. {
  774. $unlocked = '';
  775. $fields = ['Model.hidden' => 'value', 'Model.id' => '1'];
  776. $debug = urlencode(json_encode([
  777. '/articles/index',
  778. $fields,
  779. [],
  780. ]));
  781. $fields = urlencode(Security::hash(serialize($fields) . $unlocked . Security::getSalt()));
  782. $fields .= urlencode(':Model.hidden|Model.id');
  783. $data = [
  784. 'Model' => [
  785. 'hidden' => ['some-key' => 'some-value'],
  786. 'id' => '1',
  787. ],
  788. '_Token' => compact('fields', 'unlocked', 'debug'),
  789. ];
  790. Configure::write('debug', false);
  791. $this->validate($data, 'Unexpected \'_Token.debug\' found in request data');
  792. }
  793. }