CsrfProtectionMiddlewareTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 delete cookie with invalid CSRF token');
  195. $this->assertTrue($cookie->isExpired(), 'Should 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 data works with the various http methods.
  224. *
  225. * @dataProvider httpMethodProvider
  226. * @return void
  227. */
  228. public function testInvalidTokenRequestData($method)
  229. {
  230. $request = new ServerRequest([
  231. 'environment' => [
  232. 'REQUEST_METHOD' => $method,
  233. ],
  234. 'post' => ['_csrfToken' => 'nope'],
  235. 'cookies' => ['csrfToken' => 'testing123'],
  236. ]);
  237. $middleware = new CsrfProtectionMiddleware();
  238. try {
  239. $middleware->process($request, $this->_getRequestHandler());
  240. $this->fail();
  241. } catch (InvalidCsrfTokenException $exception) {
  242. $responseHeaders = $exception->responseHeader();
  243. $this->assertArrayHasKey('Set-Cookie', $responseHeaders);
  244. $cookie = Cookie::createFromHeaderString($responseHeaders['Set-Cookie']);
  245. $this->assertSame('csrfToken', $cookie->getName(), 'Should delete cookie with invalid CSRF token');
  246. $this->assertTrue($cookie->isExpired(), 'Should delete cookie with invalid CSRF token');
  247. }
  248. }
  249. /**
  250. * Test that tokens cannot be simple matches and must pass our hmac.
  251. *
  252. * @return void
  253. */
  254. public function testInvalidTokenIncorrectOrigin()
  255. {
  256. $request = new ServerRequest([
  257. 'environment' => [
  258. 'REQUEST_METHOD' => 'POST',
  259. ],
  260. 'post' => ['_csrfToken' => 'this is a match'],
  261. 'cookies' => ['csrfToken' => 'this is a match'],
  262. ]);
  263. $middleware = new CsrfProtectionMiddleware();
  264. $this->expectException(InvalidCsrfTokenException::class);
  265. $middleware->process($request, $this->_getRequestHandler());
  266. }
  267. /**
  268. * Test that missing post field fails
  269. *
  270. * @return void
  271. */
  272. public function testInvalidTokenRequestDataMissing()
  273. {
  274. $request = new ServerRequest([
  275. 'environment' => [
  276. 'REQUEST_METHOD' => 'POST',
  277. ],
  278. 'post' => [],
  279. 'cookies' => ['csrfToken' => 'testing123'],
  280. ]);
  281. $middleware = new CsrfProtectionMiddleware();
  282. $this->expectException(InvalidCsrfTokenException::class);
  283. $middleware->process($request, $this->_getRequestHandler());
  284. }
  285. /**
  286. * Test that missing header and cookie fails
  287. *
  288. * @dataProvider httpMethodProvider
  289. * @return void
  290. */
  291. public function testInvalidTokenMissingCookie($method)
  292. {
  293. $request = new ServerRequest([
  294. 'environment' => [
  295. 'REQUEST_METHOD' => $method,
  296. ],
  297. 'post' => ['_csrfToken' => 'could-be-valid'],
  298. 'cookies' => [],
  299. ]);
  300. $middleware = new CsrfProtectionMiddleware();
  301. try {
  302. $middleware->process($request, $this->_getRequestHandler());
  303. $this->fail();
  304. } catch (InvalidCsrfTokenException $exception) {
  305. $responseHeaders = $exception->responseHeader();
  306. $this->assertEmpty($responseHeaders, 'Should not send any header');
  307. }
  308. }
  309. /**
  310. * Test that the configuration options work.
  311. *
  312. * @return void
  313. */
  314. public function testConfigurationCookieCreate()
  315. {
  316. $request = new ServerRequest([
  317. 'environment' => ['REQUEST_METHOD' => 'GET'],
  318. 'webroot' => '/dir/',
  319. ]);
  320. $middleware = new CsrfProtectionMiddleware([
  321. 'cookieName' => 'token',
  322. 'expiry' => '+1 hour',
  323. 'secure' => true,
  324. 'httpOnly' => true,
  325. ]);
  326. $response = $middleware->process($request, $this->_getRequestHandler());
  327. $this->assertEmpty($response->getCookie('csrfToken'));
  328. $cookie = $response->getCookie('token');
  329. $this->assertNotEmpty($cookie, 'Should set a token.');
  330. $this->assertRegExp('/^[a-f0-9]+$/', $cookie['value'], 'Should look like a hash.');
  331. $this->assertWithinRange(strtotime('+1 hour'), $cookie['expires'], 1, 'session duration.');
  332. $this->assertSame('/dir/', $cookie['path'], 'session path.');
  333. $this->assertTrue($cookie['secure'], 'cookie security flag missing');
  334. $this->assertTrue($cookie['httponly'], 'cookie httpOnly flag missing');
  335. }
  336. /**
  337. * Test that the configuration options work.
  338. *
  339. * There should be no exception thrown.
  340. *
  341. * @return void
  342. */
  343. public function testConfigurationValidate()
  344. {
  345. $middleware = new CsrfProtectionMiddleware([
  346. 'cookieName' => 'token',
  347. 'field' => 'token',
  348. 'expiry' => 90,
  349. ]);
  350. $token = $middleware->createToken();
  351. $request = new ServerRequest([
  352. 'environment' => ['REQUEST_METHOD' => 'POST'],
  353. 'cookies' => ['csrfToken' => 'nope', 'token' => $token],
  354. 'post' => ['_csrfToken' => 'no match', 'token' => $token],
  355. ]);
  356. $response = new Response();
  357. $response = $middleware->process($request, $this->_getRequestHandler());
  358. $this->assertInstanceOf(Response::class, $response);
  359. }
  360. /**
  361. * @return void
  362. */
  363. public function testSkippingTokenCheckUsingWhitelistCallback()
  364. {
  365. $request = new ServerRequest([
  366. 'post' => [
  367. '_csrfToken' => 'foo',
  368. ],
  369. 'environment' => [
  370. 'REQUEST_METHOD' => 'POST',
  371. ],
  372. ]);
  373. $response = new Response();
  374. $middleware = new CsrfProtectionMiddleware();
  375. $middleware->whitelistCallback(function (ServerRequestInterface $request) {
  376. $this->assertSame('POST', $request->getServerParams()['REQUEST_METHOD']);
  377. return true;
  378. });
  379. $handler = new TestRequestHandler(function ($request) {
  380. $this->assertEmpty($request->getParsedBody());
  381. return new Response();
  382. });
  383. $response = $middleware->process($request, $handler);
  384. $this->assertInstanceOf(Response::class, $response);
  385. }
  386. }