FormProtectionComponentTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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\Controller\Component;
  17. use Cake\Controller\Component\FormProtectionComponent;
  18. use Cake\Controller\Controller;
  19. use Cake\Event\Event;
  20. use Cake\Http\Exception\BadRequestException;
  21. use Cake\Http\Exception\NotFoundException;
  22. use Cake\Http\Response;
  23. use Cake\Http\ServerRequest;
  24. use Cake\Http\Session;
  25. use Cake\Routing\Router;
  26. use Cake\TestSuite\TestCase;
  27. use Cake\Utility\Security;
  28. /**
  29. * FormProtectionComponentTest class
  30. */
  31. class FormProtectionComponentTest extends TestCase
  32. {
  33. /**
  34. * @var \Cake\Controller\Controller
  35. */
  36. protected $Controller;
  37. /**
  38. * @var \Cake\Controller\Component\FormProtectionComponent
  39. */
  40. protected $FormProtection;
  41. /**
  42. * setUp method
  43. *
  44. * Initializes environment state.
  45. */
  46. public function setUp(): void
  47. {
  48. parent::setUp();
  49. $session = new Session();
  50. $session->id('cli');
  51. $request = new ServerRequest([
  52. 'url' => '/articles/index',
  53. 'session' => $session,
  54. 'params' => ['controller' => 'Articles', 'action' => 'index'],
  55. ]);
  56. $this->Controller = new Controller($request);
  57. $this->Controller->loadComponent('FormProtection');
  58. $this->FormProtection = $this->Controller->FormProtection;
  59. Security::setSalt('foo!');
  60. }
  61. public function testConstructorSettingProperties(): void
  62. {
  63. $settings = [
  64. 'requireSecure' => ['update_account'],
  65. 'validatePost' => false,
  66. ];
  67. $FormProtection = new FormProtectionComponent($this->Controller->components(), $settings);
  68. $this->assertEquals($FormProtection->validatePost, $settings['validatePost']);
  69. }
  70. public function testValidation(): void
  71. {
  72. $fields = '4697b45f7f430ff3ab73018c20f315eecb0ba5a6%3AModel.valid';
  73. $unlocked = '';
  74. $debug = '';
  75. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  76. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  77. '_Token' => compact('fields', 'unlocked', 'debug'),
  78. ]));
  79. $event = new Event('Controller.startup', $this->Controller);
  80. $this->assertNull($this->FormProtection->startup($event));
  81. }
  82. public function testValidationWithBaseUrl(): void
  83. {
  84. $session = new Session();
  85. $session->id('cli');
  86. $request = new ServerRequest([
  87. 'url' => '/articles/index',
  88. 'base' => '/subfolder',
  89. 'webroot' => '/subfolder/',
  90. 'session' => $session,
  91. 'params' => ['controller' => 'Articles', 'action' => 'index'],
  92. ]);
  93. Router::setRequest($request);
  94. $this->Controller->setRequest($request);
  95. $unlocked = '';
  96. $fields = ['id' => '1'];
  97. $debug = urlencode(json_encode([
  98. '/subfolder/articles/index',
  99. $fields,
  100. [],
  101. ]));
  102. $fields = hash_hmac(
  103. 'sha1',
  104. '/subfolder/articles/index' . serialize($fields) . $unlocked . 'cli',
  105. Security::getSalt()
  106. );
  107. $fields .= urlencode(':id');
  108. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  109. 'id' => '1',
  110. '_Token' => compact('fields', 'unlocked', 'debug'),
  111. ]));
  112. $event = new Event('Controller.startup', $this->Controller);
  113. $this->assertNull($this->FormProtection->startup($event));
  114. }
  115. public function testValidationOnGetWithData(): void
  116. {
  117. $fields = 'an-invalid-token';
  118. $unlocked = '';
  119. $debug = urlencode(json_encode([
  120. 'some-action',
  121. [],
  122. [],
  123. ]));
  124. $this->Controller->setRequest($this->Controller->getRequest()
  125. ->withEnv('REQUEST_METHOD', 'GET')
  126. ->withData('Model', ['username' => 'nate', 'password' => 'foo', 'valid' => '0'])
  127. ->withData('_Token', compact('fields', 'unlocked', 'debug')));
  128. $event = new Event('Controller.startup', $this->Controller);
  129. $this->expectException(BadRequestException::class);
  130. $this->FormProtection->startup($event);
  131. }
  132. public function testValidationNoSession(): void
  133. {
  134. $unlocked = '';
  135. $debug = urlencode(json_encode([
  136. '/articles/index',
  137. [],
  138. [],
  139. ]));
  140. $fields = 'a5475372b40f6e3ccbf9f8af191f20e1642fd877%3AModel.valid';
  141. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  142. 'Model' => ['username' => 'nate', 'password' => 'foo', 'valid' => '0'],
  143. '_Token' => compact('fields', 'unlocked', 'debug'),
  144. ]));
  145. $event = new Event('Controller.startup', $this->Controller);
  146. $this->expectException(BadRequestException::class);
  147. $this->expectExceptionMessage('Unexpected field `Model.password` in POST data, Unexpected field `Model.username` in POST data');
  148. $this->FormProtection->startup($event);
  149. }
  150. public function testValidationEmptyForm(): void
  151. {
  152. $this->Controller->setRequest($this->Controller->getRequest()
  153. ->withEnv('REQUEST_METHOD', 'POST')
  154. ->withParsedBody([]));
  155. $event = new Event('Controller.startup', $this->Controller);
  156. $this->expectException(BadRequestException::class);
  157. $this->expectExceptionMessage('`_Token` was not found in request data.');
  158. $this->FormProtection->startup($event);
  159. }
  160. public function testValidationFailTampering(): void
  161. {
  162. $unlocked = '';
  163. $fields = ['Model.hidden' => 'value', 'Model.id' => '1'];
  164. $debug = urlencode(json_encode([
  165. '/articles/index',
  166. $fields,
  167. [],
  168. ]));
  169. $fields = hash_hmac('sha1', '/articles/index' . serialize($fields) . $unlocked . 'cli', Security::getSalt());
  170. $fields .= urlencode(':Model.hidden|Model.id');
  171. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  172. 'Model' => [
  173. 'hidden' => 'tampered',
  174. 'id' => '1',
  175. ],
  176. '_Token' => compact('fields', 'unlocked', 'debug'),
  177. ]));
  178. $this->expectException(BadRequestException::class);
  179. $this->expectExceptionMessage('Tampered field `Model.hidden` in POST data (expected value `value` but found `tampered`)');
  180. $event = new Event('Controller.startup', $this->Controller);
  181. $this->FormProtection->startup($event);
  182. }
  183. public function testValidationUnlockedFieldsMismatch(): void
  184. {
  185. // Unlocked is empty when the token is created.
  186. $unlocked = '';
  187. $fields = ['open', 'title'];
  188. $debug = urlencode(json_encode([
  189. '/articles/index',
  190. $fields,
  191. [''],
  192. ]));
  193. $fields = hash_hmac('sha1', '/articles/index' . serialize($fields) . $unlocked . 'cli', Security::getSalt());
  194. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  195. 'open' => 'yes',
  196. 'title' => 'yay',
  197. '_Token' => compact('fields', 'unlocked', 'debug'),
  198. ]));
  199. $this->expectException(BadRequestException::class);
  200. $this->expectExceptionMessage('Missing unlocked field');
  201. $event = new Event('Controller.startup', $this->Controller);
  202. $this->FormProtection->setConfig('unlockedFields', ['open']);
  203. $this->FormProtection->startup($event);
  204. }
  205. public function testValidationUnlockedFieldsSuccess(): void
  206. {
  207. $unlocked = 'open';
  208. $fields = ['title'];
  209. $debug = urlencode(json_encode([
  210. '/articles/index',
  211. $fields,
  212. ['open'],
  213. ]));
  214. $fields = hash_hmac('sha1', '/articles/index' . serialize($fields) . $unlocked . 'cli', Security::getSalt());
  215. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody([
  216. 'title' => 'yay',
  217. 'open' => 'yes',
  218. '_Token' => compact('fields', 'unlocked', 'debug'),
  219. ]));
  220. $event = new Event('Controller.startup', $this->Controller);
  221. $this->FormProtection->setConfig('unlockedFields', ['open']);
  222. $result = $this->FormProtection->startup($event);
  223. $this->assertNull($result);
  224. }
  225. public function testCallbackReturnResponse(): void
  226. {
  227. $this->FormProtection->setConfig('validationFailureCallback', function (BadRequestException $exception) {
  228. return new Response(['body' => 'from callback']);
  229. });
  230. $this->Controller->setRequest($this->Controller->getRequest()
  231. ->withEnv('REQUEST_METHOD', 'POST')
  232. ->withParsedBody([]));
  233. $event = new Event('Controller.startup', $this->Controller);
  234. $result = $this->FormProtection->startup($event);
  235. $this->assertInstanceOf(Response::class, $result);
  236. $this->assertSame('from callback', (string)$result->getBody());
  237. }
  238. public function testUnlockedActions(): void
  239. {
  240. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody(['data']));
  241. $this->FormProtection->setConfig('unlockedActions', ['index']);
  242. $event = new Event('Controller.startup', $this->Controller);
  243. $result = $this->Controller->FormProtection->startup($event);
  244. $this->assertNull($result);
  245. }
  246. public function testCallbackThrowsException(): void
  247. {
  248. $this->expectException(NotFoundException::class);
  249. $this->expectExceptionMessage('error description');
  250. $this->FormProtection->setConfig('validationFailureCallback', function (BadRequestException $exception): void {
  251. throw new NotFoundException('error description');
  252. });
  253. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody(['data']));
  254. $event = new Event('Controller.startup', $this->Controller);
  255. $this->Controller->FormProtection->startup($event);
  256. }
  257. public function testSettingTokenDataAsRequestAttribute(): void
  258. {
  259. $event = new Event('Controller.startup', $this->Controller);
  260. $this->Controller->FormProtection->startup($event);
  261. $securityToken = $this->Controller->getRequest()->getAttribute('formTokenData');
  262. $this->assertNotEmpty($securityToken);
  263. $this->assertSame([], $securityToken['unlockedFields']);
  264. }
  265. public function testClearingOfTokenFromRequestData(): void
  266. {
  267. $this->Controller->setRequest($this->Controller->getRequest()->withParsedBody(['_Token' => 'data']));
  268. $this->FormProtection->setConfig('validate', false);
  269. $event = new Event('Controller.startup', $this->Controller);
  270. $this->Controller->FormProtection->startup($event);
  271. $this->assertSame([], $this->Controller->getRequest()->getParsedBody());
  272. }
  273. }