Skip to content

Commit 2bf686d

Browse files
islishudeneild
authored andcommitted
net/http: add partitioned attribute to cookie type
Fixes #62490 Change-Id: Ibe7df96f50275c9321462e994a962031cb1f3018 GitHub-Last-Rev: 7df8738 GitHub-Pull-Request: #62499 Reviewed-on: https://go-review.googlesource.com/c/go/+/526435 Reviewed-by: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Théo Dury <[email protected]>
1 parent 614f228 commit 2bf686d

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

api/next/62490.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg net/http, type Cookie struct, Partitioned bool #62490
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The new [Cookie.Partitioned] field identifies cookies with the Partitioned attribute.

src/net/http/cookie.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ type Cookie struct {
3333
// MaxAge=0 means no 'Max-Age' attribute specified.
3434
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
3535
// MaxAge>0 means Max-Age attribute present and given in seconds
36-
MaxAge int
37-
Secure bool
38-
HttpOnly bool
39-
SameSite SameSite
40-
Raw string
41-
Unparsed []string // Raw text of unparsed attribute-value pairs
36+
MaxAge int
37+
Secure bool
38+
HttpOnly bool
39+
SameSite SameSite
40+
Partitioned bool
41+
Raw string
42+
Unparsed []string // Raw text of unparsed attribute-value pairs
4243
}
4344

4445
// SameSite allows a server to define a cookie attribute making it impossible for
@@ -185,6 +186,9 @@ func ParseSetCookie(line string) (*Cookie, error) {
185186
case "path":
186187
c.Path = val
187188
continue
189+
case "partitioned":
190+
c.Partitioned = true
191+
continue
188192
}
189193
c.Unparsed = append(c.Unparsed, parts[i])
190194
}
@@ -280,6 +284,9 @@ func (c *Cookie) String() string {
280284
case SameSiteStrictMode:
281285
b.WriteString("; SameSite=Strict")
282286
}
287+
if c.Partitioned {
288+
b.WriteString("; Partitioned")
289+
}
283290
return b.String()
284291
}
285292

@@ -311,6 +318,11 @@ func (c *Cookie) Valid() error {
311318
return errors.New("http: invalid Cookie.Domain")
312319
}
313320
}
321+
if c.Partitioned {
322+
if !c.Secure {
323+
return errors.New("http: partitioned cookies must be set with Secure")
324+
}
325+
}
314326
return nil
315327
}
316328

src/net/http/cookie_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ var writeSetCookiesTests = []struct {
8181
&Cookie{Name: "cookie-15", Value: "samesite-none", SameSite: SameSiteNoneMode},
8282
"cookie-15=samesite-none; SameSite=None",
8383
},
84+
{
85+
&Cookie{Name: "cookie-16", Value: "partitioned", SameSite: SameSiteNoneMode, Secure: true, Path: "/", Partitioned: true},
86+
"cookie-16=partitioned; Path=/; Secure; SameSite=None; Partitioned",
87+
},
8488
// The "special" cookies have values containing commas or spaces which
8589
// are disallowed by RFC 6265 but are common in the wild.
8690
{
@@ -570,12 +574,14 @@ func TestCookieValid(t *testing.T) {
570574
{&Cookie{Name: ""}, false},
571575
{&Cookie{Name: "invalid-value", Value: "foo\"bar"}, false},
572576
{&Cookie{Name: "invalid-path", Path: "/foo;bar/"}, false},
577+
{&Cookie{Name: "invalid-secure-for-partitioned", Value: "foo", Path: "/", Secure: false, Partitioned: true}, false},
573578
{&Cookie{Name: "invalid-domain", Domain: "example.com:80"}, false},
574579
{&Cookie{Name: "invalid-expiry", Value: "", Expires: time.Date(1600, 1, 1, 1, 1, 1, 1, time.UTC)}, false},
575580
{&Cookie{Name: "valid-empty"}, true},
576581
{&Cookie{Name: "valid-expires", Value: "foo", Path: "/bar", Domain: "example.com", Expires: time.Unix(0, 0)}, true},
577582
{&Cookie{Name: "valid-max-age", Value: "foo", Path: "/bar", Domain: "example.com", MaxAge: 60}, true},
578583
{&Cookie{Name: "valid-all-fields", Value: "foo", Path: "/bar", Domain: "example.com", Expires: time.Unix(0, 0), MaxAge: 0}, true},
584+
{&Cookie{Name: "valid-partitioned", Value: "foo", Path: "/", Secure: true, Partitioned: true}, true},
579585
}
580586

581587
for _, tt := range tests {

0 commit comments

Comments
 (0)