Skip to content

Commit

Permalink
Support SameSite value "None" cookie attribute (#581)
Browse files Browse the repository at this point in the history
* Support SameSite value "None" cookie attribute

* Fix typo in CookieSameSiteNoneMode comment

* fix comment for SameSite None
  • Loading branch information
RemiSirdata authored and erikdubbelboer committed Jun 14, 2019
1 parent 9494955 commit 9ba4cef
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const (
CookieSameSiteLaxMode
// CookieSameSiteStrictMode sets the SameSite flag with the "Strict" parameter
CookieSameSiteStrictMode
// CookieSameSiteNoneMode sets the SameSite flag with the "None" parameter
// see https://tools.ietf.org/html/draft-west-cookie-incrementalism-00
CookieSameSiteNoneMode
)

// AcquireCookie returns an empty Cookie object from the pool.
Expand Down Expand Up @@ -119,8 +122,12 @@ func (c *Cookie) SameSite() CookieSameSite {
}

// SetSameSite sets the cookie's SameSite flag to the given value.
// set value CookieSameSiteNoneMode will set Secure to true also to avoid browser rejection
func (c *Cookie) SetSameSite(mode CookieSameSite) {
c.sameSite = mode
if mode == CookieSameSiteNoneMode {
c.SetSecure(true)
}
}

// Path returns cookie path.
Expand Down Expand Up @@ -288,6 +295,11 @@ func (c *Cookie) AppendBytes(dst []byte) []byte {
dst = append(dst, strCookieSameSite...)
dst = append(dst, '=')
dst = append(dst, strCookieSameSiteStrict...)
case CookieSameSiteNoneMode:
dst = append(dst, ';', ' ')
dst = append(dst, strCookieSameSite...)
dst = append(dst, '=')
dst = append(dst, strCookieSameSiteNone...)
}
return dst
}
Expand Down Expand Up @@ -386,6 +398,10 @@ func (c *Cookie) ParseBytes(src []byte) error {
if caseInsensitiveCompare(strCookieSameSiteStrict, kv.value) {
c.sameSite = CookieSameSiteStrictMode
}
case 'n': // "none"
if caseInsensitiveCompare(strCookieSameSiteNone, kv.value) {
c.sameSite = CookieSameSiteNoneMode
}
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,29 @@ func TestCookieSameSite(t *testing.T) {
t.Fatalf("missing SameSite flag in cookie %q", s)
}

if err := c.Parse("foo=bar; samesite=none"); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if c.SameSite() != CookieSameSiteNoneMode {
t.Fatalf("SameSite None Mode must be set")
}
s = c.String()
if !strings.Contains(s, "; SameSite=None") {
t.Fatalf("missing SameSite flag in cookie %q", s)
}

if err := c.Parse("foo=bar"); err != nil {
t.Fatalf("unexpected error: %s", err)
}
c.SetSameSite(CookieSameSiteNoneMode)
s = c.String()
if !strings.Contains(s, "; SameSite=None") {
t.Fatalf("missing SameSite flag in cookie %q", s)
}
if !strings.Contains(s, "; secure") {
t.Fatalf("missing Secure flag in cookie %q", s)
}

if err := c.Parse("foo=bar"); err != nil {
t.Fatalf("unexpected error: %s", err)
}
Expand Down
1 change: 1 addition & 0 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ var (
strCookieSameSite = []byte("SameSite")
strCookieSameSiteLax = []byte("Lax")
strCookieSameSiteStrict = []byte("Strict")
strCookieSameSiteNone = []byte("None")

strClose = []byte("close")
strGzip = []byte("gzip")
Expand Down

0 comments on commit 9ba4cef

Please sign in to comment.