Add optional Secure flag for session cookies (WGUI_SESSION_SECURE_COOKIE)

This commit is contained in:
Ioannis Dressos 2026-07-08 12:48:57 +03:00
parent 6048a3f83d
commit 21862d7156
No known key found for this signature in database
5 changed files with 11 additions and 0 deletions

View File

@ -130,6 +130,7 @@ func Login(db store.IStore) echo.HandlerFunc {
Path: cookiePath,
MaxAge: ageMax,
HttpOnly: true,
Secure: util.SecureCookie,
SameSite: http.SameSiteLaxMode,
}
@ -152,6 +153,7 @@ func Login(db store.IStore) echo.HandlerFunc {
cookie.Value = tokenUID
cookie.MaxAge = ageMax
cookie.HttpOnly = true
cookie.Secure = util.SecureCookie
cookie.SameSite = http.SameSiteLaxMode
c.SetCookie(cookie)

View File

@ -113,6 +113,7 @@ func doRefreshSession(c echo.Context) {
Path: cookiePath,
MaxAge: maxAge,
HttpOnly: true,
Secure: util.SecureCookie,
SameSite: http.SameSiteLaxMode,
}
sess.Save(c.Request(), c.Response())
@ -123,6 +124,7 @@ func doRefreshSession(c echo.Context) {
cookie.Value = oldCookie.Value
cookie.MaxAge = maxAge
cookie.HttpOnly = true
cookie.Secure = util.SecureCookie
cookie.SameSite = http.SameSiteLaxMode
c.SetCookie(cookie)
}
@ -244,6 +246,7 @@ func clearSession(c echo.Context) {
cookie.Path = cookiePath
cookie.MaxAge = -1
cookie.HttpOnly = true
cookie.Secure = util.SecureCookie
cookie.SameSite = http.SameSiteLaxMode
c.SetCookie(cookie)
}

View File

@ -51,6 +51,7 @@ var (
flagTelegramFloodWait = 60
flagSessionSecret = util.RandomString(32)
flagSessionMaxDuration = 90
flagSecureCookie = false
flagWgConfTemplate string
flagBasePath string
flagSubnetRanges string
@ -100,6 +101,7 @@ func init() {
flag.StringVar(&flagBasePath, "base-path", util.LookupEnvOrString("BASE_PATH", flagBasePath), "The base path of the URL")
flag.StringVar(&flagSubnetRanges, "subnet-ranges", util.LookupEnvOrString("SUBNET_RANGES", flagSubnetRanges), "IP ranges to choose from when assigning an IP for a client.")
flag.IntVar(&flagSessionMaxDuration, "session-max-duration", util.LookupEnvOrInt("SESSION_MAX_DURATION", flagSessionMaxDuration), "Max time in days a remembered session is refreshed and valid.")
flag.BoolVar(&flagSecureCookie, "secure-cookie", util.LookupEnvOrBool(util.SecureCookieEnvVar, flagSecureCookie), "Set the Secure flag on session cookies. Enable when serving over HTTPS (e.g. behind a TLS reverse proxy).")
flag.StringVar(&flagBrandText, "brand-text", util.LookupEnvOrString("WGUI_BRAND_TEXT", flagBrandText), "The UI brand text or name")
flag.StringVar(&flagAccentColor, "accent-color", util.LookupEnvOrString("WGUI_ACCENT_COLOR", flagAccentColor), "The UI accent color")
flag.StringVar(&flagPageTitlePrefix, "page-title-prefix", util.LookupEnvOrString("WGUI_PAGE_TITLE_PREFIX", flagPageTitlePrefix), "The prefix of the page title")
@ -150,6 +152,7 @@ func init() {
util.EmailFromName = flagEmailFromName
util.SessionSecret = sha512.Sum512([]byte(flagSessionSecret))
util.SessionMaxDuration = int64(flagSessionMaxDuration) * 86_400 // Store in seconds
util.SecureCookie = flagSecureCookie
util.WgConfTemplate = flagWgConfTemplate
util.BasePath = util.ParseBasePath(flagBasePath)
util.SubnetRanges = util.ParseSubnetRanges(flagSubnetRanges)

View File

@ -60,6 +60,7 @@ func New(tmplDir fs.FS, extraData map[string]interface{}, secret [64]byte) *echo
cookieStore := sessions.NewCookieStore(secret[:32], secret[32:])
cookieStore.Options.Path = cookiePath
cookieStore.Options.HttpOnly = true
cookieStore.Options.Secure = util.SecureCookie
cookieStore.MaxAge(86400 * 7)
e.Use(session.Middleware(cookieStore))

View File

@ -24,6 +24,7 @@ var (
EmailFromName string
SessionSecret [64]byte
SessionMaxDuration int64
SecureCookie bool
WgConfTemplate string
BasePath string
SubnetRanges map[string]([]*net.IPNet)
@ -77,6 +78,7 @@ const (
LogoFilePathEnvVar = "WGUI_LOGO_FILE_PATH"
MaxmindLicenseKeyEnvVar = "WGUI_MAXMIND_LICENSE_KEY"
GeoLite2DBPathEnvVar = "WGUI_GEOLITE_DB_PATH"
SecureCookieEnvVar = "WGUI_SESSION_SECURE_COOKIE"
)
func ParseBasePath(basePath string) string {