-
Notifications
You must be signed in to change notification settings - Fork 164
Description
I found an interesting issue introduced with PR #123:
In this PR, the type SameSiteMode was added that mirrors type SameSite from go 1.13.
This seems to be a bad idea, since when considering the context, it advertises support for SameSiteNoneMode for go versions 1.11 - 1.12.
However the option SameSiteNoneMode has been introduced in go 1.13 and was never backported to the older versions (see the commit introducing SameSiteNoneMode at the go project).
Since this package relies on http.Cookie and http.SetCookie(), the code for actually writing out the SameSite=None option into the cookie is simply not present in go versions 1.11 - 1.12.
As a quick repro, try adding this test case to store_test.go and have it run with go versions 1.11 or 1.12 -- where it will fail:
// TestSameSiteNoneModeSet tests that setting SameSite Option to SameSiteNoneMode sets the SameSite
// attribute on the cookie in post go1.11 systems.
func TestSameSiteNoneModeSet(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, SameSite(SameSiteNoneMode))(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)
}
if rr.Header().Get("Set-Cookie") == "" {
t.Fatalf("cookie not set: got %q", rr.Header().Get("Set-Cookie"))
}
cookie := rr.Header().Get("Set-Cookie")
if !strings.Contains(cookie, "SameSite=None") {
t.Fatalf("cookie incorrectly does not have the SameSite attribute set: got %q", cookie)
}
}To prevent users from running into this issue, I think it would be best to remove the mirrored type SameSiteMode and to use http.SameSite, instead.
Alternatively, we could also close the loop by mirroring http.Cookie.String() and http.SetCookie() from go1.13, thus adding support for SameSiteNoneMode for the go versions where it's missing. This is something I wished would exist for gorilla/sessions. Of course, that would mean this needs to be maintained in the future.
Would be happy to implement a PR -- just let me know what you think.