Skip to content

Commit b6bd878

Browse files
committed
Don't set the cookie domain to the host by default, as it breaks Cookie Prefixes
The Cookie Prefixes spec disallows the use of the `domain` attribute in cookies if the `__Host-` prefix is used (https://tools.ietf.org/html/draft-ietf-httpbis-cookie-prefixes-00#section-3.2). There's no need to set it to the host by default, so make it optional. If it is set to a non-empty value, still output a warning if it is not a suffix of the host, as that's likely not wanted. Fixes bitly#352.
1 parent b90a234 commit b6bd878

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Usage of oauth2_proxy:
164164
-client-id string: the OAuth Client ID: ie: "123456.apps.googleusercontent.com"
165165
-client-secret string: the OAuth Client Secret
166166
-config string: path to config file
167-
-cookie-domain string: an optional cookie domain to force cookies to (ie: .yourcompany.com)*
167+
-cookie-domain string: an optional cookie domain to force cookies to (ie: .yourcompany.com)
168168
-cookie-expire duration: expire timeframe for cookie (default 168h0m0s)
169169
-cookie-httponly: set HttpOnly cookie flag (default true)
170170
-cookie-name string: the name of the cookie that the oauth_proxy creates (default "_oauth2_proxy")

oauthproxy.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,12 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
155155
redirectURL.Path = fmt.Sprintf("%s/callback", opts.ProxyPrefix)
156156

157157
log.Printf("OAuthProxy configured for %s Client ID: %s", opts.provider.Data().ProviderName, opts.ClientID)
158-
domain := opts.CookieDomain
159-
if domain == "" {
160-
domain = "<default>"
161-
}
162158
refresh := "disabled"
163159
if opts.CookieRefresh != time.Duration(0) {
164160
refresh = fmt.Sprintf("after %s", opts.CookieRefresh)
165161
}
166162

167-
log.Printf("Cookie settings: name:%s secure(https):%v httponly:%v expiry:%s domain:%s refresh:%s", opts.CookieName, opts.CookieSecure, opts.CookieHttpOnly, opts.CookieExpire, domain, refresh)
163+
log.Printf("Cookie settings: name:%s secure(https):%v httponly:%v expiry:%s domain:%s refresh:%s", opts.CookieName, opts.CookieSecure, opts.CookieHttpOnly, opts.CookieExpire, opts.CookieDomain, refresh)
168164

169165
var cipher *cookie.Cipher
170166
if opts.PassAccessToken || (opts.CookieRefresh != time.Duration(0)) {
@@ -267,22 +263,21 @@ func (p *OAuthProxy) MakeCSRFCookie(req *http.Request, value string, expiration
267263
}
268264

269265
func (p *OAuthProxy) makeCookie(req *http.Request, name string, value string, expiration time.Duration, now time.Time) *http.Cookie {
270-
domain := req.Host
271-
if h, _, err := net.SplitHostPort(domain); err == nil {
272-
domain = h
273-
}
274266
if p.CookieDomain != "" {
267+
domain := req.Host
268+
if h, _, err := net.SplitHostPort(domain); err == nil {
269+
domain = h
270+
}
275271
if !strings.HasSuffix(domain, p.CookieDomain) {
276272
log.Printf("Warning: request host is %q but using configured cookie domain of %q", domain, p.CookieDomain)
277273
}
278-
domain = p.CookieDomain
279274
}
280275

281276
return &http.Cookie{
282277
Name: name,
283278
Value: value,
284279
Path: "/",
285-
Domain: domain,
280+
Domain: p.CookieDomain,
286281
HttpOnly: p.CookieHttpOnly,
287282
Secure: p.CookieSecure,
288283
Expires: now.Add(expiration),

0 commit comments

Comments
 (0)