@@ -9,13 +9,14 @@ import (
99
1010 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
1111 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
12+ "github.com/oauth2-proxy/oauth2-proxy/pkg/util"
1213)
1314
1415// MakeCookie constructs a cookie from the given parameters,
1516// discovering the domain from the request if not specified.
1617func MakeCookie(req *http.Request, name string, value string, path string, domain string, httpOnly bool, secure bool, expiration time.Duration, now time.Time, sameSite http.SameSite) *http.Cookie {
1718 if domain != "" {
18− host := req.Host
19+ host := util.GetRequestHost(req)
1920 if h, _, err := net.SplitHostPort(host); err == nil {
2021 host = h
2122 }
@@ -47,7 +48,7 @@ func MakeCookieFromOptions(req *http.Request, name string, value string, cookieO
4748 // If nothing matches, create the cookie with the shortest domain
4849 defaultDomain := ""
4950 if len(cookieOpts.Domains) > 0 {
50− logger.Errorf("Warning: request host %q did not match any of the specific cookie domains of %q", GetRequestHost(req), strings.Join(cookieOpts.Domains, ","))
51+ logger.Errorf("Warning: request host %q did not match any of the specific cookie domains of %q", util.GetRequestHost(req), strings.Join(cookieOpts.Domains, ","))
5152 defaultDomain = cookieOpts.Domains[len(cookieOpts.Domains)-1]
5253 }
5354 return MakeCookie(req, name, value, cookieOpts.Path, defaultDomain, cookieOpts.HTTPOnly, cookieOpts.Secure, expiration, now, ParseSameSite(cookieOpts.SameSite))
@@ -56,7 +57,7 @@ func MakeCookieFromOptions(req *http.Request, name string, value string, cookieO
5657// GetCookieDomain returns the correct cookie domain given a list of domains
5758// by checking the X-Fowarded-Host and host header of an an http request
5859func GetCookieDomain(req *http.Request, cookieDomains []string) string {
59− host := GetRequestHost(req)
60+ host := util.GetRequestHost(req)
6061 for _, domain := range cookieDomains {
6162 if strings.HasSuffix(host, domain) {
6263 return domain
@@ -65,15 +66,6 @@ func GetCookieDomain(req *http.Request, cookieDomains []string) string {
6566 return ""
6667}
6768
68−// GetRequestHost return the request host header or X-Forwarded-Host if present
69−func GetRequestHost(req *http.Request) string {
70− host := req.Header.Get("X-Forwarded-Host")
71− if host == "" {
72− host = req.Host
73− }
74− return host
75−}
76−
7769// Parse a valid http.SameSite value from a user supplied string for use of making cookies.
7870func ParseSameSite(v string) http.SameSite {
7971 switch v {