Skip to content

Commit 79c60d0

Browse files
authored
fix: Set SameSite=Lax by default (#136)
* change: set SameSite=Lax by default * deps: update errors to v0.9.1 * build: add go 1.13, go 1.14 * docs: update SameSiteDefaultMode godoc
1 parent dbfab4e commit 79c60d0

File tree

6 files changed

+30
-10
lines changed

6 files changed

+30
-10
lines changed

.circleci/config.yml

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ jobs:
2323
LATEST: "true"
2424
GO111MODULE: "on"
2525

26+
"1.14":
27+
<<: *test
28+
docker:
29+
- image: circleci/golang:1.14
30+
31+
"1.13":
32+
<<: *test
33+
docker:
34+
- image: circleci/golang:1.13
35+
2636
"1.12":
2737
<<: *test
2838
docker:
@@ -58,6 +68,8 @@ workflows:
5868
build:
5969
jobs:
6070
- "latest"
71+
- "1.14"
72+
- "1.13"
6173
- "1.12"
6274
- "1.11"
6375
- "1.10"

csrf.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ type SameSiteMode int
6262

6363
// SameSite options
6464
const (
65-
// SameSiteDefaultMode sets an invalid SameSite header which defaults to
66-
// 'Lax' in most browsers, but may cause some browsers to ignore the cookie
67-
// entirely.
65+
// SameSiteDefaultMode sets the `SameSite` cookie attribute, which is
66+
// invalid in some older browsers due to changes in the SameSite spec. These
67+
// browsers will not send the cookie to the server.
68+
// csrf uses SameSiteLaxMode (SameSite=Lax) as the default as of v1.7.0+
6869
SameSiteDefaultMode SameSiteMode = iota + 1
6970
SameSiteLaxMode
7071
SameSiteStrictMode

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/gorilla/csrf
22

33
require (
44
github.com/gorilla/securecookie v1.1.1
5-
github.com/pkg/errors v0.8.0
5+
github.com/pkg/errors v0.9.1
66
)
77

88
go 1.13

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyC
22
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
33
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
44
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
5+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
6+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

options.go

+4
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ func parseOptions(h http.Handler, opts ...Option) *csrf {
152152
cs.opts.Secure = true
153153
cs.opts.HttpOnly = true
154154

155+
// Set SameSite=Lax by default, allowing the CSRF cookie to only be sent on
156+
// top-level navigations.
157+
cs.opts.SameSite = SameSiteLaxMode
158+
155159
// Default; only override this if the package user explicitly calls MaxAge(0)
156160
cs.opts.MaxAge = defaultAge
157161

store_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ func TestSameSizeSet(t *testing.T) {
160160
}
161161
}
162162

163-
// TestSamesiteBackwardsCompat tests that the default set of options do not set
164-
// any SameSite attribute.
165-
func TestSamesiteBackwardsCompat(t *testing.T) {
163+
// TestSameSiteDefault tests that the default set of options
164+
// set SameSite=Lax on the CSRF cookie.
165+
func TestSameSiteDefaultLaxMode(t *testing.T) {
166166
s := http.NewServeMux()
167167
s.HandleFunc("/", testHandler)
168168

@@ -182,10 +182,11 @@ func TestSamesiteBackwardsCompat(t *testing.T) {
182182

183183
cookie := rr.Header().Get("Set-Cookie")
184184
if cookie == "" {
185-
t.Fatalf("cookie not get set-cookie header: got headers %v", rr.Header())
185+
t.Fatalf("cookie not get Set-Cookie header: got headers %v", rr.Header())
186186
}
187187

188-
if strings.Contains(cookie, "SameSite") {
189-
t.Fatalf("cookie should not contain the substring 'SameSite' by default, but did: %q", cookie)
188+
sameSiteLax := "SameSite=Lax"
189+
if !strings.Contains(cookie, sameSiteLax) {
190+
t.Fatalf("cookie should contain %q by default: got %s", sameSiteLax, cookie)
190191
}
191192
}

0 commit comments

Comments
 (0)