Skip to content

Commit cfac76d

Browse files
authored
Merge pull request #3976 from darwin808/fix/expire-cookie-path-domain
2 parents 3df74e3 + 338dad1 commit cfac76d

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

ctx_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ func Test_Ctx_Cookie_Invalid(t *testing.T) {
14041404
{Name: "i", Value: "b", Domain: "2001:db8::1"}, // ipv6 not allowed
14051405
{Name: "p", Value: "b", Path: "\x00"}, // invalid path byte
14061406
{Name: "e", Value: "b", Expires: time.Date(1500, 1, 1, 0, 0, 0, 0, time.UTC)}, // invalid expires
1407-
{Name: "s", Value: "b", Partitioned: true}, // partitioned but not secure
1407+
// Note: Partitioned without Secure is auto-fixed (Secure=true set automatically per CHIPS spec)
14081408
}
14091409

14101410
for _, invalid := range cases {

docs/api/ctx.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,8 +2088,7 @@ app.Get("/set", func(c fiber.Ctx) error {
20882088
app.Get("/delete", func(c fiber.Ctx) error {
20892089
c.Cookie(&fiber.Cookie{
20902090
Name: "token",
2091-
// Set expiry date to the past
2092-
Expires: time.Now().Add(-(time.Hour * 2)),
2091+
Expires: fasthttp.CookieExpireDelete, // Use fasthttp's built-in constant
20932092
HTTPOnly: true,
20942093
SameSite: "Lax",
20952094
})
@@ -2098,6 +2097,22 @@ app.Get("/delete", func(c fiber.Ctx) error {
20982097
})
20992098
```
21002099

2100+
You can also use `c.Cookie()` to expire cookies with specific `Path` or `Domain` attributes:
2101+
2102+
```go title="Example"
2103+
app.Get("/logout", func(c fiber.Ctx) error {
2104+
// Expire a cookie with path and domain
2105+
c.Cookie(&fiber.Cookie{
2106+
Name: "token",
2107+
Path: "/api",
2108+
Domain: "example.com",
2109+
Expires: fasthttp.CookieExpireDelete,
2110+
})
2111+
2112+
return c.SendStatus(fiber.StatusOK)
2113+
})
2114+
```
2115+
21012116
### Cookie
21022117

21032118
Sets a cookie.

res.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ func (r *DefaultRes) Cookie(cookie *Cookie) {
249249
sameSite = http.SameSiteLaxMode
250250
}
251251

252+
// Partitioned requires Secure=true per CHIPS spec
253+
if cookie.Partitioned {
254+
cookie.Secure = true
255+
}
256+
252257
// create/validate cookie using net/http
253258
hc := &http.Cookie{
254259
Name: cookie.Name,

0 commit comments

Comments
 (0)