CsrfProtectionMiddlewareTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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\Cookie\CookieInterface;
  19. use Cake\Http\Exception\InvalidCsrfTokenException;
  20. use Cake\Http\Middleware\CsrfProtectionMiddleware;
  21. use Cake\Http\Response;
  22. use Cake\Http\ServerRequest;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Security;
  25. use Laminas\Diactoros\Response as DiactorosResponse;
  26. use Laminas\Diactoros\Response\RedirectResponse;
  27. use Psr\Http\Message\ServerRequestInterface;
  28. use RuntimeException;
  29. use TestApp\Http\TestRequestHandler;
  30. /**
  31. * Test for CsrfProtection
  32. */
  33. class CsrfProtectionMiddlewareTest extends TestCase
  34. {
  35. protected function createOldToken(): string
  36. {
  37. // Create an old style token. These tokens are hexadecimal with an hmac.
  38. $random = Security::randomString(CsrfProtectionMiddleware::TOKEN_VALUE_LENGTH);
  39. return $random . hash_hmac('sha1', $random, Security::getSalt());
  40. }
  41. /**
  42. * Data provider for HTTP method tests.
  43. *
  44. * HEAD and GET do not populate $_POST or request->data.
  45. *
  46. * @return array
  47. */
  48. public static function safeHttpMethodProvider()
  49. {
  50. return [
  51. ['GET'],
  52. ['HEAD'],
  53. ];
  54. }
  55. /**
  56. * Data provider for HTTP methods that can contain request bodies.
  57. *
  58. * @return array
  59. */
  60. public static function httpMethodProvider()
  61. {
  62. return [
  63. ['OPTIONS'], ['PATCH'], ['PUT'], ['POST'], ['DELETE'], ['PURGE'], ['INVALIDMETHOD'],
  64. ];
  65. }
  66. /**
  67. * Provides the request handler
  68. *
  69. * @return \Psr\Http\Server\RequestHandlerInterface
  70. */
  71. protected function _getRequestHandler()
  72. {
  73. return new TestRequestHandler(function () {
  74. return new Response();
  75. });
  76. }
  77. /**
  78. * Test setting the cookie value
  79. *
  80. * @return void
  81. */
  82. public function testSettingCookie()
  83. {
  84. $request = new ServerRequest([
  85. 'environment' => ['REQUEST_METHOD' => 'GET'],
  86. 'webroot' => '/dir/',
  87. ]);
  88. /** @var \Cake\Http\ServerRequest $updatedRequest */
  89. $updatedRequest = null;
  90. $handler = new TestRequestHandler(function ($request) use (&$updatedRequest) {
  91. $updatedRequest = $request;
  92. return new Response();
  93. });
  94. $middleware = new CsrfProtectionMiddleware();
  95. $response = $middleware->process($request, $handler);
  96. $cookie = $response->getCookie('csrfToken');
  97. $this->assertNotEmpty($cookie, 'Should set a token.');
  98. $this->assertMatchesRegularExpression('/^[a-z0-9\/+]+={0,2}$/i', $cookie['value'], 'Should look like base64.');
  99. $this->assertSame(0, $cookie['expires'], 'session duration.');
  100. $this->assertSame('/dir/', $cookie['path'], 'session path.');
  101. $requestAttr = $updatedRequest->getAttribute('csrfToken');
  102. $this->assertNotEquals($cookie['value'], $requestAttr);
  103. $this->assertEquals(strlen($cookie['value']) * 2, strlen($requestAttr));
  104. $this->assertMatchesRegularExpression('/^[A-Z0-9\/+]+=*$/i', $requestAttr);
  105. }
  106. /**
  107. * Test setting request attribute based on old cookie value.
  108. *
  109. * @return void
  110. */
  111. public function testRequestAttributeCompatWithOldToken()
  112. {
  113. $middleware = new CsrfProtectionMiddleware();
  114. $oldToken = $this->createOldToken();
  115. $request = new ServerRequest([
  116. 'environment' => [
  117. 'REQUEST_METHOD' => 'GET',
  118. ],
  119. 'cookies' => ['csrfToken' => $oldToken],
  120. ]);
  121. /** @var \Cake\Http\ServerRequest $updatedRequest */
  122. $updatedRequest = null;
  123. $handler = new TestRequestHandler(function ($request) use (&$updatedRequest) {
  124. $updatedRequest = $request;
  125. return new Response();
  126. });
  127. $response = $middleware->process($request, $handler);
  128. $this->assertInstanceOf(Response::class, $response);
  129. $requestAttr = $updatedRequest->getAttribute('csrfToken');
  130. $this->assertSame($requestAttr, $oldToken, 'Request attribute should match the token.');
  131. }
  132. /**
  133. * Test that the CSRF tokens are not required for idempotent operations
  134. *
  135. * @dataProvider safeHttpMethodProvider
  136. * @return void
  137. */
  138. public function testSafeMethodNoCsrfRequired($method)
  139. {
  140. $request = new ServerRequest([
  141. 'environment' => [
  142. 'REQUEST_METHOD' => $method,
  143. 'HTTP_X_CSRF_TOKEN' => 'nope',
  144. ],
  145. 'cookies' => ['csrfToken' => 'testing123'],
  146. ]);
  147. // No exception means the test is valid
  148. $middleware = new CsrfProtectionMiddleware();
  149. $response = $middleware->process($request, $this->_getRequestHandler());
  150. $this->assertInstanceOf(Response::class, $response);
  151. }
  152. /**
  153. * Test that the CSRF tokens are regenerated when token is not valid
  154. *
  155. * @return void
  156. */
  157. public function testRegenerateTokenOnGetWithInvalidData()
  158. {
  159. $request = new ServerRequest([
  160. 'environment' => [
  161. 'REQUEST_METHOD' => 'GET',
  162. ],
  163. 'cookies' => ['csrfToken' => "\x20\x26"],
  164. ]);
  165. $middleware = new CsrfProtectionMiddleware();
  166. /** @var \Cake\Http\Response $response */
  167. $response = $middleware->process($request, $this->_getRequestHandler());
  168. $this->assertInstanceOf(Response::class, $response);
  169. $this->assertGreaterThan(32, strlen($response->getCookie('csrfToken')['value']));
  170. }
  171. /**
  172. * Test that the CSRF tokens are set for redirect responses
  173. *
  174. * @return void
  175. */
  176. public function testRedirectResponseCookies()
  177. {
  178. $request = new ServerRequest([
  179. 'environment' => ['REQUEST_METHOD' => 'GET'],
  180. ]);
  181. $handler = new TestRequestHandler(function () {
  182. return new RedirectResponse('/');
  183. });
  184. $middleware = new CsrfProtectionMiddleware();
  185. $response = $middleware->process($request, $handler);
  186. $this->assertStringContainsString('csrfToken=', $response->getHeaderLine('Set-Cookie'));
  187. }
  188. /**
  189. * Test that double applying CSRF causes a failure.
  190. *
  191. * @return void
  192. */
  193. public function testDoubleApplicationFailure()
  194. {
  195. $request = new ServerRequest([
  196. 'environment' => ['REQUEST_METHOD' => 'GET'],
  197. ]);
  198. $request = $request->withAttribute('csrfToken', 'not-good');
  199. $handler = new TestRequestHandler(function () {
  200. return new RedirectResponse('/');
  201. });
  202. $middleware = new CsrfProtectionMiddleware();
  203. $this->expectException(RuntimeException::class);
  204. $middleware->process($request, $handler);
  205. }
  206. /**
  207. * Test that the CSRF tokens are set for diactoros responses
  208. *
  209. * @return void
  210. */
  211. public function testDiactorosResponseCookies()
  212. {
  213. $request = new ServerRequest([
  214. 'environment' => ['REQUEST_METHOD' => 'GET'],
  215. ]);
  216. $handler = new TestRequestHandler(function () {
  217. return new DiactorosResponse();
  218. });
  219. $middleware = new CsrfProtectionMiddleware();
  220. $response = $middleware->process($request, $handler);
  221. $this->assertStringContainsString('csrfToken=', $response->getHeaderLine('Set-Cookie'));
  222. }
  223. /**
  224. * Test that the X-CSRF-Token works with the various http methods.
  225. *
  226. * @dataProvider httpMethodProvider
  227. * @return void
  228. */
  229. public function testValidTokenInHeaderCompat($method)
  230. {
  231. $middleware = new CsrfProtectionMiddleware();
  232. $token = $middleware->createToken();
  233. $request = new ServerRequest([
  234. 'environment' => [
  235. 'REQUEST_METHOD' => $method,
  236. 'HTTP_X_CSRF_TOKEN' => $token,
  237. ],
  238. 'post' => ['a' => 'b'],
  239. 'cookies' => ['csrfToken' => $token],
  240. ]);
  241. $response = new Response();
  242. // No exception means the test is valid
  243. $response = $middleware->process($request, $this->_getRequestHandler());
  244. $this->assertInstanceOf(Response::class, $response);
  245. }
  246. /**
  247. * Test that the X-CSRF-Token works with the various http methods.
  248. *
  249. * @dataProvider httpMethodProvider
  250. * @return void
  251. */
  252. public function testValidTokenInHeader($method)
  253. {
  254. $middleware = new CsrfProtectionMiddleware();
  255. $token = $middleware->createToken();
  256. $salted = $middleware->saltToken($token);
  257. $request = new ServerRequest([
  258. 'environment' => [
  259. 'REQUEST_METHOD' => $method,
  260. 'HTTP_X_CSRF_TOKEN' => $salted,
  261. ],
  262. 'post' => ['a' => 'b'],
  263. 'cookies' => ['csrfToken' => $token],
  264. ]);
  265. $response = new Response();
  266. // No exception means the test is valid
  267. $response = $middleware->process($request, $this->_getRequestHandler());
  268. $this->assertInstanceOf(Response::class, $response);
  269. }
  270. /**
  271. * Test that the X-CSRF-Token works with the various http methods.
  272. *
  273. * @dataProvider httpMethodProvider
  274. * @return void
  275. */
  276. public function testInvalidTokenInHeader($method)
  277. {
  278. $request = new ServerRequest([
  279. 'environment' => [
  280. 'REQUEST_METHOD' => $method,
  281. 'HTTP_X_CSRF_TOKEN' => 'nope',
  282. ],
  283. 'post' => ['a' => 'b'],
  284. 'cookies' => ['csrfToken' => 'testing123'],
  285. ]);
  286. $middleware = new CsrfProtectionMiddleware();
  287. try {
  288. $middleware->process($request, $this->_getRequestHandler());
  289. $this->fail();
  290. } catch (InvalidCsrfTokenException $exception) {
  291. $responseHeaders = $exception->getHeaders();
  292. $this->assertArrayHasKey('Set-Cookie', $responseHeaders);
  293. $cookie = Cookie::createFromHeaderString($responseHeaders['Set-Cookie']);
  294. $this->assertSame('csrfToken', $cookie->getName(), 'Should automatically delete cookie with invalid CSRF token');
  295. $this->assertTrue($cookie->isExpired(), 'Should automatically delete cookie with invalid CSRF token');
  296. }
  297. }
  298. /**
  299. * Test that request data works with the various http methods.
  300. *
  301. * @dataProvider httpMethodProvider
  302. * @return void
  303. */
  304. public function testValidTokenRequestDataCompat($method)
  305. {
  306. $middleware = new CsrfProtectionMiddleware();
  307. $token = $this->createOldToken();
  308. $request = new ServerRequest([
  309. 'environment' => [
  310. 'REQUEST_METHOD' => $method,
  311. ],
  312. 'post' => ['_csrfToken' => $token],
  313. 'cookies' => ['csrfToken' => $token],
  314. ]);
  315. $handler = new TestRequestHandler(function ($request) {
  316. $this->assertNull($request->getData('_csrfToken'));
  317. return new Response();
  318. });
  319. // No exception means everything is OK
  320. $middleware->process($request, $handler);
  321. }
  322. /**
  323. * Test that request data works with the various http methods.
  324. *
  325. * @dataProvider httpMethodProvider
  326. * @return void
  327. */
  328. public function testValidTokenRequestDataSalted($method)
  329. {
  330. $middleware = new CsrfProtectionMiddleware();
  331. $token = $middleware->createToken();
  332. $salted = $middleware->saltToken($token);
  333. $request = new ServerRequest([
  334. 'environment' => [
  335. 'REQUEST_METHOD' => $method,
  336. ],
  337. 'post' => ['_csrfToken' => $salted],
  338. 'cookies' => ['csrfToken' => $token],
  339. ]);
  340. $handler = new TestRequestHandler(function ($request) {
  341. $this->assertNull($request->getData('_csrfToken'));
  342. return new Response();
  343. });
  344. // No exception means everything is OK
  345. $middleware->process($request, $handler);
  346. }
  347. /**
  348. * Test that invalid string cookies are rejected.
  349. *
  350. * @return void
  351. */
  352. public function testInvalidTokenStringCookies()
  353. {
  354. $this->expectException(InvalidCsrfTokenException::class);
  355. $request = new ServerRequest([
  356. 'environment' => [
  357. 'REQUEST_METHOD' => 'POST',
  358. ],
  359. 'post' => ['_csrfToken' => ["\x20\x26"]],
  360. 'cookies' => ['csrfToken' => ["\x20\x26"]],
  361. ]);
  362. $middleware = new CsrfProtectionMiddleware();
  363. $middleware->process($request, $this->_getRequestHandler());
  364. }
  365. /**
  366. * Test that request non string cookies are ignored.
  367. *
  368. * @return void
  369. */
  370. public function testInvalidTokenNonStringCookies()
  371. {
  372. $this->expectException(InvalidCsrfTokenException::class);
  373. $request = new ServerRequest([
  374. 'environment' => [
  375. 'REQUEST_METHOD' => 'POST',
  376. ],
  377. 'post' => ['_csrfToken' => ['nope']],
  378. 'cookies' => ['csrfToken' => ['nope']],
  379. ]);
  380. $middleware = new CsrfProtectionMiddleware();
  381. $middleware->process($request, $this->_getRequestHandler());
  382. }
  383. /**
  384. * Test that request data works with the various http methods.
  385. *
  386. * @dataProvider httpMethodProvider
  387. * @return void
  388. */
  389. public function testInvalidTokenRequestData($method)
  390. {
  391. $request = new ServerRequest([
  392. 'environment' => [
  393. 'REQUEST_METHOD' => $method,
  394. ],
  395. 'post' => ['_csrfToken' => 'nope'],
  396. 'cookies' => ['csrfToken' => 'testing123'],
  397. ]);
  398. $middleware = new CsrfProtectionMiddleware();
  399. try {
  400. $middleware->process($request, $this->_getRequestHandler());
  401. $this->fail();
  402. } catch (InvalidCsrfTokenException $exception) {
  403. $responseHeaders = $exception->getHeaders();
  404. $this->assertArrayHasKey('Set-Cookie', $responseHeaders);
  405. $cookie = Cookie::createFromHeaderString($responseHeaders['Set-Cookie']);
  406. $this->assertSame('csrfToken', $cookie->getName(), 'Should automatically delete cookie with invalid CSRF token');
  407. $this->assertTrue($cookie->isExpired(), 'Should automatically delete cookie with invalid CSRF token');
  408. }
  409. }
  410. /**
  411. * Test that tokens cannot be simple matches and must pass our hmac.
  412. *
  413. * @return void
  414. */
  415. public function testInvalidTokenIncorrectOrigin()
  416. {
  417. $request = new ServerRequest([
  418. 'environment' => [
  419. 'REQUEST_METHOD' => 'POST',
  420. ],
  421. 'post' => ['_csrfToken' => 'this is a match'],
  422. 'cookies' => ['csrfToken' => 'this is a match'],
  423. ]);
  424. $middleware = new CsrfProtectionMiddleware();
  425. $this->expectException(InvalidCsrfTokenException::class);
  426. $middleware->process($request, $this->_getRequestHandler());
  427. }
  428. /**
  429. * Test that missing post field fails
  430. *
  431. * @return void
  432. */
  433. public function testInvalidTokenRequestDataMissing()
  434. {
  435. $request = new ServerRequest([
  436. 'environment' => [
  437. 'REQUEST_METHOD' => 'POST',
  438. ],
  439. 'post' => [],
  440. 'cookies' => ['csrfToken' => 'testing123'],
  441. ]);
  442. $middleware = new CsrfProtectionMiddleware();
  443. $this->expectException(InvalidCsrfTokenException::class);
  444. $middleware->process($request, $this->_getRequestHandler());
  445. }
  446. /**
  447. * Test that missing header and cookie fails
  448. *
  449. * @dataProvider httpMethodProvider
  450. * @return void
  451. */
  452. public function testInvalidTokenMissingCookie($method)
  453. {
  454. $request = new ServerRequest([
  455. 'environment' => [
  456. 'REQUEST_METHOD' => $method,
  457. ],
  458. 'post' => ['_csrfToken' => 'could-be-valid'],
  459. 'cookies' => [],
  460. ]);
  461. $middleware = new CsrfProtectionMiddleware();
  462. try {
  463. $middleware->process($request, $this->_getRequestHandler());
  464. $this->fail();
  465. } catch (InvalidCsrfTokenException $exception) {
  466. $responseHeaders = $exception->getHeaders();
  467. $this->assertEmpty($responseHeaders, 'Should not send any header');
  468. }
  469. }
  470. /**
  471. * Test that the configuration options work.
  472. *
  473. * @return void
  474. */
  475. public function testConfigurationCookieCreate()
  476. {
  477. $request = new ServerRequest([
  478. 'environment' => ['REQUEST_METHOD' => 'GET'],
  479. 'webroot' => '/dir/',
  480. ]);
  481. $middleware = new CsrfProtectionMiddleware([
  482. 'cookieName' => 'token',
  483. 'expiry' => '+1 hour',
  484. 'secure' => true,
  485. 'httponly' => true,
  486. 'samesite' => CookieInterface::SAMESITE_STRICT,
  487. ]);
  488. $response = $middleware->process($request, $this->_getRequestHandler());
  489. $this->assertEmpty($response->getCookie('csrfToken'));
  490. $cookie = $response->getCookie('token');
  491. $this->assertNotEmpty($cookie, 'Should set a token.');
  492. $this->assertMatchesRegularExpression('/^[a-z0-9\/+]+={0,2}$/i', $cookie['value'], 'Should look like base64.');
  493. $this->assertWithinRange(strtotime('+1 hour'), $cookie['expires'], 1, 'session duration.');
  494. $this->assertSame('/dir/', $cookie['path'], 'session path.');
  495. $this->assertTrue($cookie['secure'], 'cookie security flag missing');
  496. $this->assertTrue($cookie['httponly'], 'cookie httponly flag missing');
  497. $this->assertSame(CookieInterface::SAMESITE_STRICT, $cookie['samesite'], 'samesite attribute missing');
  498. }
  499. public function testUsingDeprecatedConfigKey()
  500. {
  501. $this->deprecated(function () {
  502. $request = new ServerRequest([
  503. 'environment' => ['REQUEST_METHOD' => 'GET'],
  504. 'webroot' => '/dir/',
  505. ]);
  506. $middleware = new CsrfProtectionMiddleware([
  507. 'cookieName' => 'token',
  508. 'expiry' => '+1 hour',
  509. 'secure' => true,
  510. 'httpOnly' => true,
  511. 'samesite' => CookieInterface::SAMESITE_STRICT,
  512. ]);
  513. $response = $middleware->process($request, $this->_getRequestHandler());
  514. $cookie = $response->getCookie('token');
  515. $this->assertTrue($cookie['httponly'], 'cookie httponly flag missing');
  516. });
  517. }
  518. /**
  519. * Test that the configuration options work.
  520. *
  521. * There should be no exception thrown.
  522. *
  523. * @return void
  524. */
  525. public function testConfigurationValidate()
  526. {
  527. $middleware = new CsrfProtectionMiddleware([
  528. 'cookieName' => 'token',
  529. 'field' => 'token',
  530. 'expiry' => 90,
  531. ]);
  532. $token = $middleware->createToken();
  533. $request = new ServerRequest([
  534. 'environment' => ['REQUEST_METHOD' => 'POST'],
  535. 'cookies' => ['csrfToken' => 'nope', 'token' => $token],
  536. 'post' => ['_csrfToken' => 'no match', 'token' => $token],
  537. ]);
  538. $response = new Response();
  539. $response = $middleware->process($request, $this->_getRequestHandler());
  540. $this->assertInstanceOf(Response::class, $response);
  541. }
  542. /**
  543. * @return void
  544. */
  545. public function testSkippingTokenCheckUsingWhitelistCallback()
  546. {
  547. $this->deprecated(function () {
  548. $request = new ServerRequest([
  549. 'post' => [
  550. '_csrfToken' => 'foo',
  551. ],
  552. 'environment' => [
  553. 'REQUEST_METHOD' => 'POST',
  554. ],
  555. ]);
  556. $response = new Response();
  557. $middleware = new CsrfProtectionMiddleware();
  558. $middleware->whitelistCallback(function (ServerRequestInterface $request) {
  559. $this->assertSame('POST', $request->getServerParams()['REQUEST_METHOD']);
  560. return true;
  561. });
  562. $handler = new TestRequestHandler(function ($request) {
  563. $this->assertEmpty($request->getParsedBody());
  564. return new Response();
  565. });
  566. $response = $middleware->process($request, $handler);
  567. $this->assertInstanceOf(Response::class, $response);
  568. });
  569. }
  570. /**
  571. * @return void
  572. */
  573. public function testSkippingTokenCheckUsingSkipCheckCallback()
  574. {
  575. $request = new ServerRequest([
  576. 'post' => [
  577. '_csrfToken' => 'foo',
  578. ],
  579. 'environment' => [
  580. 'REQUEST_METHOD' => 'POST',
  581. ],
  582. ]);
  583. $response = new Response();
  584. $middleware = new CsrfProtectionMiddleware();
  585. $middleware->skipCheckCallback(function (ServerRequestInterface $request) {
  586. $this->assertSame('POST', $request->getServerParams()['REQUEST_METHOD']);
  587. return true;
  588. });
  589. $handler = new TestRequestHandler(function ($request) {
  590. $this->assertEmpty($request->getParsedBody());
  591. return new Response();
  592. });
  593. $response = $middleware->process($request, $handler);
  594. $this->assertInstanceOf(Response::class, $response);
  595. }
  596. /**
  597. * Ensure salting is not consistent
  598. *
  599. * @return void
  600. */
  601. public function testSaltToken()
  602. {
  603. $middleware = new CsrfProtectionMiddleware();
  604. $token = $middleware->createToken();
  605. $results = [];
  606. for ($i = 0; $i < 10; $i++) {
  607. $results[] = $middleware->saltToken($token);
  608. }
  609. $this->assertCount(10, array_unique($results));
  610. }
  611. }