CsrfProtectionMiddlewareTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  12. * @link http://cakephp.org CakePHP(tm) Project
  13. * @since 3.5.0
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Http\Middleware;
  17. use Cake\Http\Cookie\Cookie;
  18. use Cake\Http\Exception\InvalidCsrfTokenException;
  19. use Cake\Http\Middleware\CsrfProtectionMiddleware;
  20. use Cake\Http\Response;
  21. use Cake\Http\ServerRequest;
  22. use Cake\TestSuite\TestCase;
  23. use Laminas\Diactoros\Response as DiactorosResponse;
  24. use Laminas\Diactoros\Response\RedirectResponse;
  25. use Psr\Http\Message\ServerRequestInterface;
  26. use TestApp\Http\TestRequestHandler;
  27. /**
  28. * Test for CsrfProtection
  29. */
  30. class CsrfProtectionMiddlewareTest extends TestCase
  31. {
  32. /**
  33. * Data provider for HTTP method tests.
  34. *
  35. * HEAD and GET do not populate $_POST or request->data.
  36. *
  37. * @return array
  38. */
  39. public static function safeHttpMethodProvider()
  40. {
  41. return [
  42. ['GET'],
  43. ['HEAD'],
  44. ];
  45. }
  46. /**
  47. * Data provider for HTTP methods that can contain request bodies.
  48. *
  49. * @return array
  50. */
  51. public static function httpMethodProvider()
  52. {
  53. return [
  54. ['OPTIONS'], ['PATCH'], ['PUT'], ['POST'], ['DELETE'], ['PURGE'], ['INVALIDMETHOD'],
  55. ];
  56. }
  57. /**
  58. * Provides the request handler
  59. *
  60. * @return \Psr\Http\Server\RequestHandlerInterface
  61. */
  62. protected function _getRequestHandler()
  63. {
  64. return new TestRequestHandler(function () {
  65. return new Response();
  66. });
  67. }
  68. /**
  69. * Test setting the cookie value
  70. *
  71. * @return void
  72. */
  73. public function testSettingCookie()
  74. {
  75. $request = new ServerRequest([
  76. 'environment' => ['REQUEST_METHOD' => 'GET'],
  77. 'webroot' => '/dir/',
  78. ]);
  79. $updatedRequest = null;
  80. $handler = new TestRequestHandler(function ($request) use (&$updatedRequest) {
  81. $updatedRequest = $request;
  82. return new Response();
  83. });
  84. $middleware = new CsrfProtectionMiddleware();
  85. $response = $middleware->process($request, $handler);
  86. $cookie = $response->getCookie('csrfToken');
  87. $this->assertNotEmpty($cookie, 'Should set a token.');
  88. $this->assertRegExp('/^[a-f0-9]+$/', $cookie['value'], 'Should look like a hash.');
  89. $this->assertSame(0, $cookie['expires'], 'session duration.');
  90. $this->assertSame('/dir/', $cookie['path'], 'session path.');
  91. $this->assertEquals($cookie['value'], $updatedRequest->getAttribute('csrfToken'));
  92. }
  93. /**
  94. * Test that the CSRF tokens are not required for idempotent operations
  95. *
  96. * @dataProvider safeHttpMethodProvider
  97. * @return void
  98. */
  99. public function testSafeMethodNoCsrfRequired($method)
  100. {
  101. $request = new ServerRequest([
  102. 'environment' => [
  103. 'REQUEST_METHOD' => $method,
  104. 'HTTP_X_CSRF_TOKEN' => 'nope',
  105. ],
  106. 'cookies' => ['csrfToken' => 'testing123'],
  107. ]);
  108. // No exception means the test is valid
  109. $middleware = new CsrfProtectionMiddleware();
  110. $response = $middleware->process($request, $this->_getRequestHandler());
  111. $this->assertInstanceOf(Response::class, $response);
  112. }
  113. /**
  114. * Test that the CSRF tokens are set for redirect responses
  115. *
  116. * @return void
  117. */
  118. public function testRedirectResponseCookies()
  119. {
  120. $request = new ServerRequest([
  121. 'environment' => ['REQUEST_METHOD' => 'GET'],
  122. ]);
  123. $handler = new TestRequestHandler(function () {
  124. return new RedirectResponse('/');
  125. });
  126. $middleware = new CsrfProtectionMiddleware();
  127. $response = $middleware->process($request, $handler);
  128. $this->assertStringContainsString('csrfToken=', $response->getHeaderLine('Set-Cookie'));
  129. }
  130. /**
  131. * Test that the CSRF tokens are set for diactoros responses
  132. *
  133. * @return void
  134. */
  135. public function testDiactorosResponseCookies()
  136. {
  137. $request = new ServerRequest([
  138. 'environment' => ['REQUEST_METHOD' => 'GET'],
  139. ]);
  140. $handler = new TestRequestHandler(function () {
  141. return new DiactorosResponse();
  142. });
  143. $middleware = new CsrfProtectionMiddleware();
  144. $response = $middleware->process($request, $handler);
  145. $this->assertStringContainsString('csrfToken=', $response->getHeaderLine('Set-Cookie'));
  146. }
  147. /**
  148. * Test that the X-CSRF-Token works with the various http methods.
  149. *
  150. * @dataProvider httpMethodProvider
  151. * @return void
  152. */
  153. public function testValidTokenInHeader($method)
  154. {
  155. $middleware = new CsrfProtectionMiddleware();
  156. $token = $middleware->createToken();
  157. $request = new ServerRequest([
  158. 'environment' => [
  159. 'REQUEST_METHOD' => $method,
  160. 'HTTP_X_CSRF_TOKEN' => $token,
  161. ],
  162. 'post' => ['a' => 'b'],
  163. 'cookies' => ['csrfToken' => $token],
  164. ]);
  165. $response = new Response();
  166. // No exception means the test is valid
  167. $response = $middleware->process($request, $this->_getRequestHandler());
  168. $this->assertInstanceOf(Response::class, $response);
  169. }
  170. /**
  171. * Test that the X-CSRF-Token works with the various http methods.
  172. *
  173. * @dataProvider httpMethodProvider
  174. * @return void
  175. */
  176. public function testInvalidTokenInHeader($method)
  177. {
  178. $request = new ServerRequest([
  179. 'environment' => [
  180. 'REQUEST_METHOD' => $method,
  181. 'HTTP_X_CSRF_TOKEN' => 'nope',
  182. ],
  183. 'post' => ['a' => 'b'],
  184. 'cookies' => ['csrfToken' => 'testing123'],
  185. ]);
  186. $middleware = new CsrfProtectionMiddleware();
  187. try {
  188. $middleware->process($request, $this->_getRequestHandler());
  189. $this->fail();
  190. } catch (InvalidCsrfTokenException $exception) {
  191. $responseHeaders = $exception->responseHeader();
  192. $this->assertArrayHasKey('Set-Cookie', $responseHeaders);
  193. $cookie = Cookie::createFromHeaderString($responseHeaders['Set-Cookie']);
  194. $this->assertSame('csrfToken', $cookie->getName(), 'Should automatically delete cookie with invalid CSRF token');
  195. $this->assertTrue($cookie->isExpired(), 'Should automatically delete cookie with invalid CSRF token');
  196. }
  197. }
  198. /**
  199. * Test that request data works with the various http methods.
  200. *
  201. * @dataProvider httpMethodProvider
  202. * @return void
  203. */
  204. public function testValidTokenRequestData($method)
  205. {
  206. $middleware = new CsrfProtectionMiddleware();
  207. $token = $middleware->createToken();
  208. $request = new ServerRequest([
  209. 'environment' => [
  210. 'REQUEST_METHOD' => $method,
  211. ],
  212. 'post' => ['_csrfToken' => $token],
  213. 'cookies' => ['csrfToken' => $token],
  214. ]);
  215. $handler = new TestRequestHandler(function ($request) {
  216. $this->assertNull($request->getData('_csrfToken'));
  217. return new Response();
  218. });
  219. // No exception means everything is OK
  220. $middleware->process($request, $handler);
  221. }
  222. /**
  223. * Test that request non string cookies are ignored.
  224. *
  225. * @return void
  226. */
  227. public function testInvalidTokenNonStringCookies()
  228. {
  229. $this->expectException(\Cake\Http\Exception\InvalidCsrfTokenException::class);
  230. $request = new ServerRequest([
  231. 'environment' => [
  232. 'REQUEST_METHOD' => 'POST',
  233. ],
  234. 'post' => ['_csrfToken' => ['nope']],
  235. 'cookies' => ['csrfToken' => ['nope']],
  236. ]);
  237. $middleware = new CsrfProtectionMiddleware();
  238. $middleware->process($request, $this->_getRequestHandler());
  239. }
  240. /**
  241. * Test that request data works with the various http methods.
  242. *
  243. * @dataProvider httpMethodProvider
  244. * @return void
  245. */
  246. public function testInvalidTokenRequestData($method)
  247. {
  248. $request = new ServerRequest([
  249. 'environment' => [
  250. 'REQUEST_METHOD' => $method,
  251. ],
  252. 'post' => ['_csrfToken' => 'nope'],
  253. 'cookies' => ['csrfToken' => 'testing123'],
  254. ]);
  255. $middleware = new CsrfProtectionMiddleware();
  256. try {
  257. $middleware->process($request, $this->_getRequestHandler());
  258. $this->fail();
  259. } catch (InvalidCsrfTokenException $exception) {
  260. $responseHeaders = $exception->responseHeader();
  261. $this->assertArrayHasKey('Set-Cookie', $responseHeaders);
  262. $cookie = Cookie::createFromHeaderString($responseHeaders['Set-Cookie']);
  263. $this->assertSame('csrfToken', $cookie->getName(), 'Should automatically delete cookie with invalid CSRF token');
  264. $this->assertTrue($cookie->isExpired(), 'Should automatically delete cookie with invalid CSRF token');
  265. }
  266. }
  267. /**
  268. * Test that tokens cannot be simple matches and must pass our hmac.
  269. *
  270. * @return void
  271. */
  272. public function testInvalidTokenIncorrectOrigin()
  273. {
  274. $request = new ServerRequest([
  275. 'environment' => [
  276. 'REQUEST_METHOD' => 'POST',
  277. ],
  278. 'post' => ['_csrfToken' => 'this is a match'],
  279. 'cookies' => ['csrfToken' => 'this is a match'],
  280. ]);
  281. $middleware = new CsrfProtectionMiddleware();
  282. $this->expectException(InvalidCsrfTokenException::class);
  283. $middleware->process($request, $this->_getRequestHandler());
  284. }
  285. /**
  286. * Test that missing post field fails
  287. *
  288. * @return void
  289. */
  290. public function testInvalidTokenRequestDataMissing()
  291. {
  292. $request = new ServerRequest([
  293. 'environment' => [
  294. 'REQUEST_METHOD' => 'POST',
  295. ],
  296. 'post' => [],
  297. 'cookies' => ['csrfToken' => 'testing123'],
  298. ]);
  299. $middleware = new CsrfProtectionMiddleware();
  300. $this->expectException(InvalidCsrfTokenException::class);
  301. $middleware->process($request, $this->_getRequestHandler());
  302. }
  303. /**
  304. * Test that missing header and cookie fails
  305. *
  306. * @dataProvider httpMethodProvider
  307. * @return void
  308. */
  309. public function testInvalidTokenMissingCookie($method)
  310. {
  311. $request = new ServerRequest([
  312. 'environment' => [
  313. 'REQUEST_METHOD' => $method,
  314. ],
  315. 'post' => ['_csrfToken' => 'could-be-valid'],
  316. 'cookies' => [],
  317. ]);
  318. $middleware = new CsrfProtectionMiddleware();
  319. try {
  320. $middleware->process($request, $this->_getRequestHandler());
  321. $this->fail();
  322. } catch (InvalidCsrfTokenException $exception) {
  323. $responseHeaders = $exception->responseHeader();
  324. $this->assertEmpty($responseHeaders, 'Should not send any header');
  325. }
  326. }
  327. /**
  328. * Test that the configuration options work.
  329. *
  330. * @return void
  331. */
  332. public function testConfigurationCookieCreate()
  333. {
  334. $request = new ServerRequest([
  335. 'environment' => ['REQUEST_METHOD' => 'GET'],
  336. 'webroot' => '/dir/',
  337. ]);
  338. $middleware = new CsrfProtectionMiddleware([
  339. 'cookieName' => 'token',
  340. 'expiry' => '+1 hour',
  341. 'secure' => true,
  342. 'httpOnly' => true,
  343. ]);
  344. $response = $middleware->process($request, $this->_getRequestHandler());
  345. $this->assertEmpty($response->getCookie('csrfToken'));
  346. $cookie = $response->getCookie('token');
  347. $this->assertNotEmpty($cookie, 'Should set a token.');
  348. $this->assertRegExp('/^[a-f0-9]+$/', $cookie['value'], 'Should look like a hash.');
  349. $this->assertWithinRange(strtotime('+1 hour'), $cookie['expires'], 1, 'session duration.');
  350. $this->assertSame('/dir/', $cookie['path'], 'session path.');
  351. $this->assertTrue($cookie['secure'], 'cookie security flag missing');
  352. $this->assertTrue($cookie['httponly'], 'cookie httpOnly flag missing');
  353. }
  354. /**
  355. * Test that the configuration options work.
  356. *
  357. * There should be no exception thrown.
  358. *
  359. * @return void
  360. */
  361. public function testConfigurationValidate()
  362. {
  363. $middleware = new CsrfProtectionMiddleware([
  364. 'cookieName' => 'token',
  365. 'field' => 'token',
  366. 'expiry' => 90,
  367. ]);
  368. $token = $middleware->createToken();
  369. $request = new ServerRequest([
  370. 'environment' => ['REQUEST_METHOD' => 'POST'],
  371. 'cookies' => ['csrfToken' => 'nope', 'token' => $token],
  372. 'post' => ['_csrfToken' => 'no match', 'token' => $token],
  373. ]);
  374. $response = new Response();
  375. $response = $middleware->process($request, $this->_getRequestHandler());
  376. $this->assertInstanceOf(Response::class, $response);
  377. }
  378. /**
  379. * @return void
  380. */
  381. public function testSkippingTokenCheckUsingWhitelistCallback()
  382. {
  383. $request = new ServerRequest([
  384. 'post' => [
  385. '_csrfToken' => 'foo',
  386. ],
  387. 'environment' => [
  388. 'REQUEST_METHOD' => 'POST',
  389. ],
  390. ]);
  391. $response = new Response();
  392. $middleware = new CsrfProtectionMiddleware();
  393. $middleware->whitelistCallback(function (ServerRequestInterface $request) {
  394. $this->assertSame('POST', $request->getServerParams()['REQUEST_METHOD']);
  395. return true;
  396. });
  397. $handler = new TestRequestHandler(function ($request) {
  398. $this->assertEmpty($request->getParsedBody());
  399. return new Response();
  400. });
  401. $response = $middleware->process($request, $handler);
  402. $this->assertInstanceOf(Response::class, $response);
  403. }
  404. }