EncryptedCookieMiddlewareTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.3.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\CookieCollection;
  19. use Cake\Http\Middleware\EncryptedCookieMiddleware;
  20. use Cake\Http\Response;
  21. use Cake\Http\ServerRequest;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\Utility\CookieCryptTrait;
  24. use TestApp\Http\TestRequestHandler;
  25. /**
  26. * Test for EncryptedCookieMiddleware
  27. */
  28. class EncryptedCookieMiddlewareTest extends TestCase
  29. {
  30. use CookieCryptTrait;
  31. protected $middleware;
  32. protected function _getCookieEncryptionKey()
  33. {
  34. return 'super secret key that no one can guess';
  35. }
  36. /**
  37. * Setup
  38. */
  39. public function setUp(): void
  40. {
  41. $this->middleware = new EncryptedCookieMiddleware(
  42. ['secret', 'ninja'],
  43. $this->_getCookieEncryptionKey(),
  44. 'aes'
  45. );
  46. }
  47. /**
  48. * Test decoding request cookies
  49. *
  50. * @return void
  51. */
  52. public function testDecodeRequestCookies()
  53. {
  54. $request = new ServerRequest(['url' => '/cookies/nom']);
  55. $request = $request->withCookieParams([
  56. 'plain' => 'always plain',
  57. 'secret' => $this->_encrypt('decoded', 'aes'),
  58. ]);
  59. $this->assertNotEquals('decoded', $request->getCookie('decoded'));
  60. $handler = new TestRequestHandler(function ($req) {
  61. $this->assertSame('decoded', $req->getCookie('secret'));
  62. $this->assertSame('always plain', $req->getCookie('plain'));
  63. return (new Response())->withHeader('called', 'yes');
  64. });
  65. $response = $this->middleware->process($request, $handler);
  66. $this->assertSame('yes', $response->getHeaderLine('called'), 'Inner middleware not invoked');
  67. }
  68. /**
  69. * Test decoding malformed cookies
  70. *
  71. * @dataProvider malformedCookies
  72. * @param string $cookie
  73. * @return void
  74. */
  75. public function testDecodeMalformedCookies($cookie)
  76. {
  77. $request = new ServerRequest(['url' => '/cookies/nom']);
  78. $request = $request->withCookieParams(['secret' => $cookie]);
  79. $handler = new TestRequestHandler(function ($req) {
  80. $this->assertSame('', $req->getCookie('secret'));
  81. return new Response();
  82. });
  83. $middleware = new EncryptedCookieMiddleware(
  84. ['secret'],
  85. $this->_getCookieEncryptionKey(),
  86. 'aes'
  87. );
  88. $middleware->process($request, $handler);
  89. }
  90. /**
  91. * Data provider for malformed cookies.
  92. *
  93. * @return array
  94. */
  95. public function malformedCookies()
  96. {
  97. $encrypted = $this->_encrypt('secret data', 'aes');
  98. return [
  99. 'empty' => [''],
  100. 'wrong prefix' => [substr_replace($encrypted, 'foo', 0, 3)],
  101. 'altered' => [str_replace('M', 'A', $encrypted)],
  102. 'invalid chars' => [str_replace('M', 'M#', $encrypted)],
  103. ];
  104. }
  105. /**
  106. * Test encoding cookies in the set-cookie header.
  107. *
  108. * @return void
  109. */
  110. public function testEncodeResponseSetCookieHeader()
  111. {
  112. $request = new ServerRequest(['url' => '/cookies/nom']);
  113. $handler = new TestRequestHandler(function ($req) {
  114. return (new Response())->withAddedHeader('Set-Cookie', 'secret=be%20quiet')
  115. ->withAddedHeader('Set-Cookie', 'plain=in%20clear')
  116. ->withAddedHeader('Set-Cookie', 'ninja=shuriken');
  117. });
  118. $response = $this->middleware->process($request, $handler);
  119. $this->assertStringNotContainsString('ninja=shuriken', $response->getHeaderLine('Set-Cookie'));
  120. $this->assertStringContainsString('plain=in%20clear', $response->getHeaderLine('Set-Cookie'));
  121. $cookies = CookieCollection::createFromHeader($response->getHeader('Set-Cookie'));
  122. $this->assertTrue($cookies->has('ninja'));
  123. $this->assertEquals(
  124. 'shuriken',
  125. $this->_decrypt($cookies->get('ninja')->getValue(), 'aes')
  126. );
  127. }
  128. /**
  129. * Test encoding cookies in the cookie collection.
  130. *
  131. * @return void
  132. */
  133. public function testEncodeResponseCookieData()
  134. {
  135. $request = new ServerRequest(['url' => '/cookies/nom']);
  136. $handler = new TestRequestHandler(function ($req) {
  137. return (new Response())->withCookie(new Cookie('secret', 'be quiet'))
  138. ->withCookie(new Cookie('plain', 'in clear'))
  139. ->withCookie(new Cookie('ninja', 'shuriken'));
  140. });
  141. $response = $this->middleware->process($request, $handler);
  142. $this->assertNotSame('shuriken', $response->getCookie('ninja'));
  143. $this->assertEquals(
  144. 'shuriken',
  145. $this->_decrypt($response->getCookie('ninja')['value'], 'aes')
  146. );
  147. }
  148. }