|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Integration\Http; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | +use ReflectionClass; |
| 9 | +use Tempest\Cryptography\Encryption\Encrypter; |
| 10 | +use Tempest\Http\Cookie\Cookie; |
| 11 | +use Tempest\Http\Cookie\CookieConfig; |
| 12 | +use Tempest\Http\Request; |
| 13 | +use Tempest\Http\Responses\Ok; |
| 14 | +use Tempest\Reflection\MethodReflector; |
| 15 | +use Tempest\Router\Get; |
| 16 | +use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
| 17 | + |
| 18 | +final class CookieHandlingTest extends FrameworkIntegrationTestCase |
| 19 | +{ |
| 20 | + #[Test] |
| 21 | + public function encrypted_cookies_are_kept_when_default(): void |
| 22 | + { |
| 23 | + try { |
| 24 | + $encrypter = $this->container->get(Encrypter::class); |
| 25 | + $_COOKIE['Cookie_name'] = $encrypter->encrypt('myCookieValue')->serialize(); |
| 26 | + |
| 27 | + $responseHelper = $this->http |
| 28 | + ->registerRoute($this->returnCookieValueController()) |
| 29 | + ->get('/get_cookie_value') |
| 30 | + ->assertOk() |
| 31 | + ->assertSee('myCookieValue'); |
| 32 | + |
| 33 | + foreach ($responseHelper->headers as $header) { |
| 34 | + if ($header->name !== 'set-cookie') { |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + foreach ($header->values as $value) { |
| 39 | + $this->assertNotEquals( |
| 40 | + $value, |
| 41 | + 'Cookie_name=; Expires=Wed, 31-Dec-1969 23:59:59 GMT; Max-Age=0; Path=/; Secure; SameSite=Lax', |
| 42 | + ); |
| 43 | + } |
| 44 | + } |
| 45 | + } finally { |
| 46 | + unset($_COOKIE['Cookie_name']); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + #[Test] |
| 51 | + public function unencrypted_cookies_are_discarded_when_default(): void |
| 52 | + { |
| 53 | + try { |
| 54 | + $_COOKIE['Cookie_name'] = 'myCookieValue'; |
| 55 | + |
| 56 | + $this->http |
| 57 | + ->registerRoute($this->returnCookieValueController()) |
| 58 | + ->get('/get_cookie_value') |
| 59 | + ->assertOk() |
| 60 | + ->assertHeaderMatches('set-cookie', 'Cookie_name=; Expires=Wed, 31-Dec-1969 23:59:59 GMT; Max-Age=0; Path=/; Secure; SameSite=Lax') |
| 61 | + ->assertNotSee('myCookieValue'); |
| 62 | + } finally { |
| 63 | + unset($_COOKIE['Cookie_name']); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + #[Test] |
| 68 | + public function unencrypted_cookies_are_kept_when_discard_false(): void |
| 69 | + { |
| 70 | + $this->container->config(new CookieConfig(discardUnencryptedCookies: false)); |
| 71 | + |
| 72 | + try { |
| 73 | + $_COOKIE['Cookie_name'] = 'myCookieValue'; |
| 74 | + |
| 75 | + $responseHelper = $this->http |
| 76 | + ->registerRoute($this->returnCookieValueController()) |
| 77 | + ->get('/get_cookie_value') |
| 78 | + ->assertOk() |
| 79 | + ->assertNotSee('myCookieValue'); // cookies are not discarded but not whitelisted so not available |
| 80 | + |
| 81 | + foreach ($responseHelper->headers as $header) { |
| 82 | + if ($header->name !== 'set-cookie') { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + foreach ($header->values as $value) { |
| 87 | + $this->assertNotEquals( |
| 88 | + $value, |
| 89 | + 'Cookie_name=; Expires=Wed, 31-Dec-1969 23:59:59 GMT; Max-Age=0; Path=/; Secure; SameSite=Lax', |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | + } finally { |
| 94 | + unset($_COOKIE['Cookie_name']); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + #[Test] |
| 99 | + public function unencrypted_cookies_are_discarded_when_discard_true(): void |
| 100 | + { |
| 101 | + $this->container->config(new CookieConfig(discardUnencryptedCookies: true)); |
| 102 | + |
| 103 | + try { |
| 104 | + $_COOKIE['Cookie_name'] = 'myCookieValue'; |
| 105 | + |
| 106 | + $this->http |
| 107 | + ->registerRoute($this->returnCookieValueController()) |
| 108 | + ->get('/get_cookie_value') |
| 109 | + ->assertOk() |
| 110 | + ->assertHeaderMatches('set-cookie', 'Cookie_name=; Expires=Wed, 31-Dec-1969 23:59:59 GMT; Max-Age=0; Path=/; Secure; SameSite=Lax') |
| 111 | + ->assertNotSee('myCookieValue'); |
| 112 | + } finally { |
| 113 | + unset($_COOKIE['Cookie_name']); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + #[Test] |
| 118 | + public function whitelisted_plaintext_cookies_are_kept(): void |
| 119 | + { |
| 120 | + $this->container->config(new CookieConfig( |
| 121 | + discardUnencryptedCookies: true, |
| 122 | + plaintextCookies: ['Cookie_name'], |
| 123 | + )); |
| 124 | + |
| 125 | + try { |
| 126 | + $_COOKIE['Cookie_name'] = 'myCookieValue'; |
| 127 | + |
| 128 | + $responseHelper = $this->http |
| 129 | + ->registerRoute($this->returnCookieValueController()) |
| 130 | + ->get('/get_cookie_value') |
| 131 | + ->assertOk() |
| 132 | + ->assertSee('myCookieValue'); |
| 133 | + |
| 134 | + foreach ($responseHelper->headers as $header) { |
| 135 | + if ($header->name !== 'set-cookie') { |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + foreach ($header->values as $value) { |
| 140 | + $this->assertNotEquals( |
| 141 | + $value, |
| 142 | + 'Cookie_name=; Expires=Wed, 31-Dec-1969 23:59:59 GMT; Max-Age=0; Path=/; Secure; SameSite=Lax', |
| 143 | + ); |
| 144 | + } |
| 145 | + } |
| 146 | + } finally { |
| 147 | + unset($_COOKIE['Cookie_name']); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + #[Test] |
| 152 | + public function whitelisted_plaintext_cookies_are_send_in_plain(): void |
| 153 | + { |
| 154 | + $this->container->config(new CookieConfig( |
| 155 | + plaintextCookies: ['Cookie_name'], |
| 156 | + )); |
| 157 | + |
| 158 | + $controller = new class { |
| 159 | + #[Get('/test_whitelisted_unencrypted_cookies_are_send_in_plain')] |
| 160 | + public function __invoke(): Ok |
| 161 | + { |
| 162 | + return new Ok()->addCookie( |
| 163 | + new Cookie( |
| 164 | + key: 'Cookie_name', |
| 165 | + value: 'value', |
| 166 | + ), |
| 167 | + ); |
| 168 | + } |
| 169 | + }; |
| 170 | + |
| 171 | + $reflection = new ReflectionClass($controller); |
| 172 | + $method = $reflection->getMethod('__invoke'); |
| 173 | + |
| 174 | + $this->http |
| 175 | + ->registerRoute(new MethodReflector($method)) |
| 176 | + ->get('/test_whitelisted_unencrypted_cookies_are_send_in_plain') |
| 177 | + ->assertOk() |
| 178 | + ->assertHeaderMatches('set-cookie', 'Cookie_name=value; Path=/; Secure; SameSite=Lax'); |
| 179 | + } |
| 180 | + |
| 181 | + private function returnCookieValueController(): MethodReflector |
| 182 | + { |
| 183 | + $controller = new class() { |
| 184 | + #[Get('/get_cookie_value')] |
| 185 | + public function __invoke(Request $request): Ok |
| 186 | + { |
| 187 | + return new Ok( |
| 188 | + $request->getCookie('Cookie_name')->value ?? '', |
| 189 | + ); |
| 190 | + } |
| 191 | + }; |
| 192 | + |
| 193 | + $reflection = new ReflectionClass($controller); |
| 194 | + $method = $reflection->getMethod('__invoke'); |
| 195 | + |
| 196 | + return new MethodReflector($method); |
| 197 | + } |
| 198 | +} |
0 commit comments