FormProtectorTest.php 26 KB

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