Skip to content

Commit

Permalink
fix no default samesite
Browse files Browse the repository at this point in the history
  • Loading branch information
bharat-rajani committed Apr 21, 2024
1 parent e308bfd commit a5d8b51
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
coverage.coverprofile

# IDE Metadata
.idea
.vscode
9 changes: 9 additions & 0 deletions sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/gob"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

Expand Down Expand Up @@ -39,6 +40,10 @@ func TestFlashes(t *testing.T) {

store := NewCookieStore([]byte("secret-key"))

if store.Options.SameSite != http.SameSiteLaxMode {
t.Fatalf("cookie store error: default same site is not set to Lax")
}

// Round 1 ----------------------------------------------------------------

req, _ = http.NewRequest("GET", "http://localhost:8080/", nil)
Expand Down Expand Up @@ -67,6 +72,10 @@ func TestFlashes(t *testing.T) {
t.Fatal("No cookies. Header:", hdr)
}

if !strings.Contains(cookies[0], "SameSite=Lax") {
t.Fatal("Set-Cookie does not contains SameSite=Lax, cookie string:", cookies[0])
}

if _, err = store.Get(req, "session:key"); err.Error() != "sessions: invalid character in cookie name: session:key" {
t.Fatalf("Expected error due to invalid cookie name")
}
Expand Down
5 changes: 3 additions & 2 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ func NewCookieStore(keyPairs ...[]byte) *CookieStore {
cs := &CookieStore{
Codecs: securecookie.CodecsFromPairs(keyPairs...),
Options: &Options{
Path: "/",
MaxAge: 86400 * 30,
Path: "/",
MaxAge: 86400 * 30,
SameSite: http.SameSiteLaxMode,
},
}

Expand Down

0 comments on commit a5d8b51

Please sign in to comment.