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