From dd1a6a81c83f8eafa95f4b7f89c6de5de8c81517 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 11 May 2021 20:02:36 +0200 Subject: [PATCH] outposts/proxy: improve host header detection Signed-off-by: Jens Langhammer --- outpost/pkg/proxy/server.go | 2 +- outpost/pkg/proxy/utils.go | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/outpost/pkg/proxy/server.go b/outpost/pkg/proxy/server.go index 44a355b51..2e0cc48d3 100644 --- a/outpost/pkg/proxy/server.go +++ b/outpost/pkg/proxy/server.go @@ -57,7 +57,7 @@ func (s *Server) handler(w http.ResponseWriter, r *http.Request) { for k := range s.Handlers { hostKeys = append(hostKeys, k) } - s.logger.WithField("host", host).WithField("known-hosts", strings.Join(hostKeys, ", ")).Debug("Host header does not match any we know of") + s.logger.WithField("host", host).WithField("known-hosts", strings.Join(hostKeys, ",")).Debug("Host header does not match any we know of") w.WriteHeader(404) return } diff --git a/outpost/pkg/proxy/utils.go b/outpost/pkg/proxy/utils.go index d9e4602a9..32e26c905 100644 --- a/outpost/pkg/proxy/utils.go +++ b/outpost/pkg/proxy/utils.go @@ -1,12 +1,20 @@ package proxy -import "net/http" +import ( + "net" + "net/http" +) var xForwardedHost = http.CanonicalHeaderKey("X-Forwarded-Host") func getHost(req *http.Request) string { + host := req.Host if req.Header.Get(xForwardedHost) != "" { - return req.Header.Get(xForwardedHost) + host = req.Header.Get(xForwardedHost) } - return req.Host + hostOnly, _, err := net.SplitHostPort(host) + if err != nil { + return host + } + return hostOnly }