Skip to content

Commit

Permalink
bugfix: Don't set a default samesite for backwards compatibility (#132)
Browse files Browse the repository at this point in the history
Also add a comment over SameSiteDefaultMode discouraging its use.
  • Loading branch information
euank authored Apr 26, 2020
1 parent 4b50158 commit dbfab4e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
3 changes: 3 additions & 0 deletions csrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ type SameSiteMode int

// SameSite options
const (
// SameSiteDefaultMode sets an invalid SameSite header which defaults to
// 'Lax' in most browsers, but may cause some browsers to ignore the cookie
// entirely.
SameSiteDefaultMode SameSiteMode = iota + 1
SameSiteLaxMode
SameSiteStrictMode
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ require (
github.com/gorilla/securecookie v1.1.1
github.com/pkg/errors v0.8.0
)

go 1.13
3 changes: 0 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,6 @@ func parseOptions(h http.Handler, opts ...Option) *csrf {
cs.opts.Secure = true
cs.opts.HttpOnly = true

// Default to blank to maintain backwards compatibility
cs.opts.SameSite = SameSiteDefaultMode

// Default; only override this if the package user explicitly calls MaxAge(0)
cs.opts.MaxAge = defaultAge

Expand Down
30 changes: 30 additions & 0 deletions store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,33 @@ func TestSameSizeSet(t *testing.T) {
t.Fatalf("cookie incorrectly does not have the SameSite attribute set: got %q", cookie)
}
}

// TestSamesiteBackwardsCompat tests that the default set of options do not set
// any SameSite attribute.
func TestSamesiteBackwardsCompat(t *testing.T) {
s := http.NewServeMux()
s.HandleFunc("/", testHandler)

r, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
p := Protect(testKey)(s)
p.ServeHTTP(rr, r)

if rr.Code != http.StatusOK {
t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
rr.Code, http.StatusOK)
}

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

if strings.Contains(cookie, "SameSite") {
t.Fatalf("cookie should not contain the substring 'SameSite' by default, but did: %q", cookie)
}
}

0 comments on commit dbfab4e

Please sign in to comment.