mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2026-10-21 10:17:22 +02:00
fix: strip the port from the request host when matching cookie domains (#3546)
* Strip the port from the request host when matching cookie domains GetCookieDomain suffix-matches the request host against each configured cookie domain. When a reverse proxy rewrites the Host header to the upstream's address, that host arrives as "host:port" and never matches, so the request falls through to the "did not match any of the specific cookie domains" warning and the shortest configured domain is used. One way to hit this is Traefik's Errors middleware: it fetches a page from a Service on port 443 with passHostHeader disabled, and forwards the Host as "<host>:443". The request reaches oauth2-proxy correctly and the resulting cookie is still usable, but every such request logs an error that suggests a misconfiguration when none exists. warnInvalidDomain, a few lines below in the same file, already calls net.SplitHostPort on the host before comparing it to the cookie domain. This makes GetCookieDomain consistent with it. Tests cover a Host header with a port, an X-Forwarded-Host header with a port, a non-standard port, and a host with a port that matches no configured domain. Signed-off-by: Kiril Jurbinsky <kiril@instaffo.com> * chore: add changelog entry for #3546 Signed-off-by: Kiril Jurbinsky <kiril@instaffo.com> * test: keep the new port entries at the end of the table The port cases were inserted between the Host and X-Forwarded-Host variants of the existing suffix-match case, which split a pair that reads as one. Move them after the last existing entry instead. Signed-off-by: Kiril Jurbinsky <kiril@instaffo.com> * test: pin that a port part can no longer match a cookie domain SplitHostPort does not require the port to be numeric, so a host such as "evil.com:.cookies.test" used to suffix-match a configured cookie domain through its port part, and the cookie was then set for that domain. Matching now runs against the host alone, so it does not. Covers both the Host header and X-Forwarded-Host, since GetRequestHost returns one or the other into the same comparison. A Host of this shape cannot be expressed through the request URL, because http.NewRequest rejects it as an invalid port, so the table gains a rawHost field that assigns req.Host after the request is built. That is how a server populates it from the wire, and pkg/upstream and pkg/middleware already set req.Host the same way in their tests. Thanks to @Lrifton92 for spotting this. Signed-off-by: Kiril Jurbinsky <kiril@instaffo.com> --------- Signed-off-by: Kiril Jurbinsky <kiril@instaffo.com>
This commit is contained in:
@@ -60,6 +60,14 @@ func MakeCookieFromOptions(req *http.Request, opts *CookieOptions) *http.Cookie
|
||||
// by checking the X-Fowarded-Host and host header of an an http request
|
||||
func GetCookieDomain(req *http.Request, cookieDomains []string) string {
|
||||
host := requestutil.GetRequestHost(req)
|
||||
// Strip any port before matching. A reverse proxy that rewrites the Host
|
||||
// header to the upstream's address (for example Traefik's Errors
|
||||
// middleware pointing at a Service on port 443) sends "host:port", which
|
||||
// would never suffix-match a configured domain. warnInvalidDomain below
|
||||
// already normalises the host this way.
|
||||
if h, _, err := net.SplitHostPort(host); err == nil {
|
||||
host = h
|
||||
}
|
||||
for _, domain := range cookieDomains {
|
||||
if strings.HasSuffix(host, domain) {
|
||||
return domain
|
||||
|
||||
@@ -14,7 +14,10 @@ import (
|
||||
var _ = Describe("Cookie Tests", func() {
|
||||
Context("GetCookieDomain", func() {
|
||||
type getCookieDomainTableInput struct {
|
||||
host string
|
||||
host string
|
||||
// rawHost sets req.Host after the request is built, for values a
|
||||
// URL cannot carry, such as a host whose port part is not numeric.
|
||||
rawHost string
|
||||
xForwardedHost string
|
||||
cookieDomains []string
|
||||
expectedOutput string
|
||||
@@ -29,6 +32,10 @@ var _ = Describe("Cookie Tests", func() {
|
||||
)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
if in.rawHost != "" {
|
||||
req.Host = in.rawHost
|
||||
}
|
||||
|
||||
if in.xForwardedHost != "" {
|
||||
req.Header.Add("X-Forwarded-Host", in.xForwardedHost)
|
||||
req.RemoteAddr = "127.0.0.1:4180"
|
||||
@@ -80,6 +87,51 @@ var _ = Describe("Cookie Tests", func() {
|
||||
cookieDomains: []string{".cookies.wrong", ".cookies.false"},
|
||||
expectedOutput: "",
|
||||
}),
|
||||
Entry("a Host header carrying a port", getCookieDomainTableInput{
|
||||
host: "www.cookies.test:443",
|
||||
cookieDomains: []string{".cookies.test"},
|
||||
expectedOutput: ".cookies.test",
|
||||
}),
|
||||
Entry("an X-Forwarded-Host header carrying a port", getCookieDomainTableInput{
|
||||
host: "backend.cookies.internal",
|
||||
xForwardedHost: "www.cookies.test:443",
|
||||
cookieDomains: []string{".cookies.test"},
|
||||
expectedOutput: ".cookies.test",
|
||||
}),
|
||||
Entry("a non-standard port", getCookieDomainTableInput{
|
||||
host: "www.cookies.test:8443",
|
||||
cookieDomains: []string{".cookies.test"},
|
||||
expectedOutput: ".cookies.test",
|
||||
}),
|
||||
Entry("a host with a port that matches no domain", getCookieDomainTableInput{
|
||||
host: "www.example.com:443",
|
||||
cookieDomains: []string{".cookies.test"},
|
||||
expectedOutput: "",
|
||||
}),
|
||||
Entry("a Host header whose port part looks like a cookie domain", getCookieDomainTableInput{
|
||||
host: "backend.cookies.internal",
|
||||
rawHost: "evil.com:.cookies.test",
|
||||
cookieDomains: []string{".cookies.test"},
|
||||
expectedOutput: "",
|
||||
}),
|
||||
Entry("a Host header whose port part ends in a cookie domain", getCookieDomainTableInput{
|
||||
host: "backend.cookies.internal",
|
||||
rawHost: "evil.com:443.cookies.test",
|
||||
cookieDomains: []string{".cookies.test"},
|
||||
expectedOutput: "",
|
||||
}),
|
||||
Entry("an X-Forwarded-Host header whose port part looks like a cookie domain", getCookieDomainTableInput{
|
||||
host: "backend.cookies.internal",
|
||||
xForwardedHost: "evil.com:.cookies.test",
|
||||
cookieDomains: []string{".cookies.test"},
|
||||
expectedOutput: "",
|
||||
}),
|
||||
Entry("an X-Forwarded-Host header whose port part ends in a cookie domain", getCookieDomainTableInput{
|
||||
host: "backend.cookies.internal",
|
||||
xForwardedHost: "evil.com:443.cookies.test",
|
||||
cookieDomains: []string{".cookies.test"},
|
||||
expectedOutput: "",
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user