Skip to content

Commit 56d60a0

Browse files
efectngabycoderabbitai[bot]
authored
✨ v3 (feature): add CHIPS support to Cookie (#3047)
* ✨ v3 (feature): add CHIPS support to Cookie * update docs * Update docs/whats_new.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update docs/api/ctx.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: Juan Calderon-Perez <[email protected]> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent a18e8a8 commit 56d60a0

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

ctx.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,17 @@ type RangeSet struct {
175175

176176
// Cookie data for c.Cookie
177177
type Cookie struct {
178-
Name string `json:"name"`
179-
Value string `json:"value"`
180-
Path string `json:"path"`
181-
Domain string `json:"domain"`
182-
MaxAge int `json:"max_age"`
183-
Expires time.Time `json:"expires"`
184-
Secure bool `json:"secure"`
185-
HTTPOnly bool `json:"http_only"`
186-
SameSite string `json:"same_site"`
187-
SessionOnly bool `json:"session_only"`
178+
Name string `json:"name"` // The name of the cookie
179+
Value string `json:"value"` // The value of the cookie
180+
Path string `json:"path"` // Specifies a URL path which is allowed to receive the cookie
181+
Domain string `json:"domain"` // Specifies the domain which is allowed to receive the cookie
182+
MaxAge int `json:"max_age"` // The maximum age (in seconds) of the cookie
183+
Expires time.Time `json:"expires"` // The expiration date of the cookie
184+
Secure bool `json:"secure"` // Indicates that the cookie should only be transmitted over a secure HTTPS connection
185+
HTTPOnly bool `json:"http_only"` // Indicates that the cookie is accessible only through the HTTP protocol
186+
SameSite string `json:"same_site"` // Controls whether or not a cookie is sent with cross-site requests
187+
Partitioned bool `json:"partitioned"` // Indicates if the cookie is stored in a partitioned cookie jar
188+
SessionOnly bool `json:"session_only"` // Indicates if the cookie is a session-only cookie
188189
}
189190

190191
// Views is the interface that wraps the Render function.
@@ -437,6 +438,10 @@ func (c *DefaultCtx) Cookie(cookie *Cookie) {
437438
fcookie.SetSameSite(fasthttp.CookieSameSiteLaxMode)
438439
}
439440

441+
// CHIPS allows to partition cookie jar by top-level site.
442+
// refer: https://developers.google.com/privacy-sandbox/3pcd/chips
443+
fcookie.SetPartitioned(cookie.Partitioned)
444+
440445
c.fasthttp.Response.Header.SetCookie(fcookie)
441446
fasthttp.ReleaseCookie(fcookie)
442447
}

ctx_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,11 @@ func Test_Ctx_Cookie(t *testing.T) {
920920
cookie.MaxAge = 0
921921
c.Cookie(cookie)
922922
require.Equal(t, expect, string(c.Response().Header.Peek(HeaderSetCookie)))
923+
924+
expect = "username=john; path=/; secure; SameSite=None; Partitioned"
925+
cookie.Partitioned = true
926+
c.Cookie(cookie)
927+
require.Equal(t, expect, string(c.Response().Header.Peek(HeaderSetCookie)))
923928
}
924929

925930
// go test -v -run=^$ -bench=Benchmark_Ctx_Cookie -benchmem -count=4

docs/api/ctx.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -375,16 +375,17 @@ func (c Ctx) Cookie(cookie *Cookie)
375375

376376
```go
377377
type Cookie struct {
378-
Name string `json:"name"`
379-
Value string `json:"value"`
380-
Path string `json:"path"`
381-
Domain string `json:"domain"`
382-
MaxAge int `json:"max_age"`
383-
Expires time.Time `json:"expires"`
384-
Secure bool `json:"secure"`
385-
HTTPOnly bool `json:"http_only"`
386-
SameSite string `json:"same_site"`
387-
SessionOnly bool `json:"session_only"`
378+
Name string `json:"name"` // The name of the cookie
379+
Value string `json:"value"` // The value of the cookie
380+
Path string `json:"path"` // Specifies a URL path which is allowed to receive the cookie
381+
Domain string `json:"domain"` // Specifies the domain which is allowed to receive the cookie
382+
MaxAge int `json:"max_age"` // The maximum age (in seconds) of the cookie
383+
Expires time.Time `json:"expires"` // The expiration date of the cookie
384+
Secure bool `json:"secure"` // Indicates that the cookie should only be transmitted over a secure HTTPS connection
385+
HTTPOnly bool `json:"http_only"` // Indicates that the cookie is accessible only through the HTTP protocol
386+
SameSite string `json:"same_site"` // Controls whether or not a cookie is sent with cross-site requests
387+
Partitioned bool `json:"partitioned"` // Indicates if the cookie is stored in a partitioned cookie jar
388+
SessionOnly bool `json:"session_only"` // Indicates if the cookie is a session-only cookie
388389
}
389390
```
390391

@@ -402,6 +403,26 @@ app.Get("/", func(c fiber.Ctx) error {
402403
})
403404
```
404405

406+
:::info
407+
408+
Partitioned cookies allow partitioning the cookie jar by top-level site, enhancing user privacy by preventing cookies from being shared across different sites. This feature is particularly useful in scenarios where a user interacts with embedded third-party services that should not have access to the main site's cookies. You can check out [CHIPS](https://developers.google.com/privacy-sandbox/3pcd/chips) for more information.
409+
410+
:::
411+
412+
```go title="Example"
413+
app.Get("/", func(c fiber.Ctx) error {
414+
// Create a new partitioned cookie
415+
cookie := new(fiber.Cookie)
416+
cookie.Name = "user_session"
417+
cookie.Value = "abc123"
418+
cookie.Partitioned = true // This cookie will be stored in a separate jar when it's embeded into another website
419+
420+
// Set the cookie in the response
421+
c.Cookie(cookie)
422+
return c.SendString("Partitioned cookie set")
423+
})
424+
```
425+
405426
## Cookies
406427

407428
Get cookie value by key, you could pass an optional default value that will be returned if the cookie key does not exist.

docs/whats_new.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ To enable the routing changes above we had to slightly adjust the signature of t
186186
DRAFT section
187187
:::
188188

189+
### New Features
190+
191+
- Cookie now allows Partitioned cookies for [CHIPS](https://developers.google.com/privacy-sandbox/3pcd/chips) support. CHIPS (Cookies Having Independent Partitioned State) is a feature that improves privacy by allowing cookies to be partitioned by top-level site, mitigating cross-site tracking.
189192

190193
### new methods
191194

0 commit comments

Comments
 (0)