mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Breaking change: If disabled the cookie samesite cookie attribute will not be set, but if none the attribute will be set and is a breaking change compared to before where none did not render the attribute. This was due to a known issue in Safari. Co-Authored-By: Arve Knudsen <arve.knudsen@gmail.com> Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> Fixes #19847
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
)
|
|
|
|
type CookieOptions struct {
|
|
Path string
|
|
Secure bool
|
|
SameSiteDisabled bool
|
|
SameSiteMode http.SameSite
|
|
}
|
|
|
|
func newCookieOptions() CookieOptions {
|
|
return CookieOptions{
|
|
Path: setting.AppSubUrl + "/",
|
|
Secure: setting.CookieSecure,
|
|
SameSiteDisabled: setting.CookieSameSiteDisabled,
|
|
SameSiteMode: setting.CookieSameSiteMode,
|
|
}
|
|
}
|
|
|
|
type GetCookieOptionsFunc func() CookieOptions
|
|
|
|
func DeleteCookie(w http.ResponseWriter, name string, getCookieOptionsFunc GetCookieOptionsFunc) {
|
|
WriteCookie(w, name, "", -1, getCookieOptionsFunc)
|
|
}
|
|
|
|
func WriteCookie(w http.ResponseWriter, name string, value string, maxAge int, getCookieOptionsFunc GetCookieOptionsFunc) {
|
|
options := getCookieOptionsFunc()
|
|
cookie := http.Cookie{
|
|
Name: name,
|
|
MaxAge: maxAge,
|
|
Value: value,
|
|
HttpOnly: true,
|
|
Path: options.Path,
|
|
Secure: options.Secure,
|
|
}
|
|
if !options.SameSiteDisabled {
|
|
cookie.SameSite = options.SameSiteMode
|
|
}
|
|
http.SetCookie(w, &cookie)
|
|
}
|