Skip to content

Commit ee33c6d

Browse files
committed
feat: improve defaults for new cookies
1 parent f1a8095 commit ee33c6d

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

packages/http/src/Cookie/CookieManager.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ public function get(string $key): ?Cookie
2929

3030
public function set(string $key, string $value, DateTimeInterface|int|null $expiresAt = null): Cookie
3131
{
32-
$cookie = $this->get($key) ?? new Cookie(key: $key);
32+
$cookie = $this->get($key) ?? new Cookie(
33+
key: $key,
34+
secure: true,
35+
httpOnly: true,
36+
sameSite: SameSite::LAX,
37+
);
3338

3439
$cookie->value = $value;
3540
$cookie->expiresAt = $expiresAt ?? $cookie->expiresAt;

packages/http/src/Cookie/SameSite.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,25 @@
44

55
namespace Tempest\Http\Cookie;
66

7+
/**
8+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Set-Cookie#samesitesamesite-value
9+
*/
710
enum SameSite: string
811
{
12+
/**
13+
* Send the cookie only for requests originating from the same site that set the cookie.
14+
*/
915
case STRICT = 'Strict';
16+
17+
/**
18+
* Send the cookie for requests originating from the same site that set the cookie, and for cross-site requests that meet both of the following criteria:
19+
* - The request is a top-level navigation: this essentially means that the request causes the URL shown in the browser's address bar to change.
20+
* - The request uses a safe method: in particular, this excludes `POST`, `PUT`, and `DELETE`.
21+
*/
1022
case LAX = 'Lax';
23+
24+
/**
25+
* Send the cookie with both cross-site and same-site requests. The `Secure` attribute must also be set when using this value.
26+
*/
1127
case NONE = 'None';
1228
}

tests/Integration/Http/CookieManagerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function test_creating_a_cookie(): void
3232
$this->http
3333
->get('/')
3434
->assertOk()
35-
->assertHeaderContains('set-cookie', 'new=value');
35+
->assertHeaderContains('set-cookie', 'new=value; Secure; HttpOnly; SameSite=Lax');
3636
}
3737

3838
public function test_removing_a_cookie(): void

0 commit comments

Comments
 (0)