Skip to content

Commit e642412

Browse files
vsekharbradfitz
authored andcommitted
net/http: add support for SameSite=None
Section 4.2 of the Internet-Draft for SameSite includes the possible SameSite value of "None". https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00 Change-Id: I44f246024429ec175db13ff6b36bee465f3d233d GitHub-Last-Rev: 170d24a GitHub-Pull-Request: #31842 Reviewed-on: https://go-review.googlesource.com/c/go/+/175337 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
1 parent b98cecf commit e642412

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

api/next.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ pkg net, type ListenConfig struct, KeepAlive time.Duration
180180
pkg net/http, const StatusEarlyHints = 103
181181
pkg net/http, const StatusEarlyHints ideal-int
182182
pkg net/http, method (Header) Clone() Header
183+
pkg net/http, const SameSiteNoneMode = 4
184+
pkg net/http, const SameSiteNoneMode SameSite
183185
pkg net/http, type Server struct, BaseContext func(net.Listener) context.Context
184186
pkg net/http, type Server struct, ConnContext func(context.Context, net.Conn) context.Context
185187
pkg net/http, type Transport struct, ForceAttemptHTTP2 bool

src/net/http/cookie.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const (
4848
SameSiteDefaultMode SameSite = iota + 1
4949
SameSiteLaxMode
5050
SameSiteStrictMode
51+
SameSiteNoneMode
5152
)
5253

5354
// readSetCookies parses all "Set-Cookie" values from
@@ -105,6 +106,8 @@ func readSetCookies(h Header) []*Cookie {
105106
c.SameSite = SameSiteLaxMode
106107
case "strict":
107108
c.SameSite = SameSiteStrictMode
109+
case "none":
110+
c.SameSite = SameSiteNoneMode
108111
default:
109112
c.SameSite = SameSiteDefaultMode
110113
}
@@ -217,6 +220,8 @@ func (c *Cookie) String() string {
217220
switch c.SameSite {
218221
case SameSiteDefaultMode:
219222
b.WriteString("; SameSite")
223+
case SameSiteNoneMode:
224+
b.WriteString("; SameSite=None")
220225
case SameSiteLaxMode:
221226
b.WriteString("; SameSite=Lax")
222227
case SameSiteStrictMode:

src/net/http/cookie_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ var writeSetCookiesTests = []struct {
7777
&Cookie{Name: "cookie-14", Value: "samesite-strict", SameSite: SameSiteStrictMode},
7878
"cookie-14=samesite-strict; SameSite=Strict",
7979
},
80+
{
81+
&Cookie{Name: "cookie-15", Value: "samesite-none", SameSite: SameSiteNoneMode},
82+
"cookie-15=samesite-none; SameSite=None",
83+
},
8084
// The "special" cookies have values containing commas or spaces which
8185
// are disallowed by RFC 6265 but are common in the wild.
8286
{
@@ -296,6 +300,15 @@ var readSetCookiesTests = []struct {
296300
Raw: "samesitestrict=foo; SameSite=Strict",
297301
}},
298302
},
303+
{
304+
Header{"Set-Cookie": {"samesitenone=foo; SameSite=None"}},
305+
[]*Cookie{{
306+
Name: "samesitenone",
307+
Value: "foo",
308+
SameSite: SameSiteNoneMode,
309+
Raw: "samesitenone=foo; SameSite=None",
310+
}},
311+
},
299312
// Make sure we can properly read back the Set-Cookie headers we create
300313
// for values containing spaces or commas:
301314
{

0 commit comments

Comments
 (0)