FormProtectorTest.php 25 KB

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