Skip to content

Commit

Permalink
Merge pull request #1498 from aacapella/feature/same-site-cookies
Browse files Browse the repository at this point in the history
SameSite cookie support
  • Loading branch information
notzippy committed Jul 10, 2020
2 parents ff43c73 + c6c4c35 commit ff2da7e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions flash.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func FlashFilter(c *Controller, fc []Filter) {
Value: url.QueryEscape(flashValue),
HttpOnly: true,
Secure: CookieSecure,
SameSite: CookieSameSite,
Path: "/",
})
}
Expand Down
16 changes: 15 additions & 1 deletion revel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package revel

import (
"go/build"
"net/http"
"path/filepath"
"strings"

Expand Down Expand Up @@ -73,7 +74,8 @@ var (
// Cookie domain
CookieDomain string
// Cookie flags
CookieSecure bool
CookieSecure bool
CookieSameSite http.SameSite

// Revel request access log, not exposed from package.
// However output settings can be controlled from app.conf
Expand Down Expand Up @@ -174,6 +176,18 @@ func Init(inputmode, importPath, srcPath string) {
CookiePrefix = Config.StringDefault("cookie.prefix", "REVEL")
CookieDomain = Config.StringDefault("cookie.domain", "")
CookieSecure = Config.BoolDefault("cookie.secure", HTTPSsl)

switch Config.StringDefault("cookie.samesite", "") {
case "lax":
CookieSameSite = http.SameSiteLaxMode
case "strict":
CookieSameSite = http.SameSiteStrictMode
case "none":
CookieSameSite = http.SameSiteNoneMode
default:
CookieSameSite = http.SameSiteDefaultMode
}

if secretStr := Config.StringDefault("app.secret", ""); secretStr != "" {
SetSecretKey([]byte(secretStr))
}
Expand Down
1 change: 1 addition & 0 deletions session_adapter_cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (cse *SessionCookieEngine) GetCookie(s session.Session) *http.Cookie {
Path: "/",
HttpOnly: true,
Secure: CookieSecure,
SameSite: CookieSameSite,
Expires: ts.UTC(),
MaxAge: int(cse.ExpireAfterDuration.Seconds()),
}
Expand Down
2 changes: 2 additions & 0 deletions validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ func ValidationFilter(c *Controller, fc []Filter) {
Path: "/",
HttpOnly: true,
Secure: CookieSecure,
SameSite: CookieSameSite,
})
} else if hasCookie {
c.SetCookie(&http.Cookie{
Expand All @@ -304,6 +305,7 @@ func ValidationFilter(c *Controller, fc []Filter) {
Path: "/",
HttpOnly: true,
Secure: CookieSecure,
SameSite: CookieSameSite,
})
}
}
Expand Down

0 comments on commit ff2da7e

Please sign in to comment.