1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2024-11-28 09:08:44 +02:00

Remove duplication in proxy directors

This commit is contained in:
Joel Speed 2021-03-21 18:14:10 +00:00
parent 025ef3eca4
commit 92ae5d9d24
No known key found for this signature in database
GPG Key ID: 6E80578D6751DEFB
2 changed files with 45 additions and 17 deletions

View File

@ -116,11 +116,11 @@ func newReverseProxy(target *url.URL, upstream options.Upstream, errorHandler Pr
}
}
// Set the request director based on the PassHostHeader option
// Ensure we always pass the original request path
setProxyDirector(proxy)
if upstream.PassHostHeader != nil && !*upstream.PassHostHeader {
setProxyUpstreamHostHeader(proxy, target)
} else {
setProxyDirector(proxy)
}
// Set the error handler so that upstream connection failures render the
@ -137,10 +137,7 @@ func setProxyUpstreamHostHeader(proxy *httputil.ReverseProxy, target *url.URL) {
director := proxy.Director
proxy.Director = func(req *http.Request) {
director(req)
// use RequestURI so that we aren't unescaping encoded slashes in the request path
req.Host = target.Host
req.URL.Opaque = req.RequestURI
req.URL.RawQuery = ""
}
}

View File

@ -30,16 +30,17 @@ var _ = Describe("HTTP Upstream Suite", func() {
falsum := false
type httpUpstreamTableInput struct {
id string
serverAddr *string
target string
method string
body []byte
signatureData *options.SignatureData
existingHeaders map[string]string
expectedResponse testHTTPResponse
expectedUpstream string
errorHandler ProxyErrorHandler
id string
serverAddr *string
target string
method string
body []byte
passUpstreamHostHeader bool
signatureData *options.SignatureData
existingHeaders map[string]string
expectedResponse testHTTPResponse
expectedUpstream string
errorHandler ProxyErrorHandler
}
DescribeTable("HTTP Upstream ServeHTTP",
@ -52,6 +53,9 @@ var _ = Describe("HTTP Upstream Suite", func() {
for key, value := range in.existingHeaders {
req.Header.Add(key, value)
}
if host := req.Header.Get("Host"); host != "" {
req.Host = host
}
req = middlewareapi.AddRequestScope(req, &middlewareapi.RequestScope{})
rw := httptest.NewRecorder()
@ -60,7 +64,7 @@ var _ = Describe("HTTP Upstream Suite", func() {
upstream := options.Upstream{
ID: in.id,
PassHostHeader: &truth,
PassHostHeader: &in.passUpstreamHostHeader,
ProxyWebSockets: &falsum,
InsecureSkipTLSVerify: false,
FlushInterval: &flush,
@ -280,6 +284,33 @@ var _ = Describe("HTTP Upstream Suite", func() {
},
expectedUpstream: "existingHeaders",
}),
Entry("when passing the existing host header", &httpUpstreamTableInput{
id: "passExistingHostHeader",
serverAddr: &serverAddr,
target: "/existingHostHeader",
method: "GET",
body: []byte{},
errorHandler: nil,
passUpstreamHostHeader: true,
existingHeaders: map[string]string{
"Host": "existing-host",
},
expectedResponse: testHTTPResponse{
code: 200,
header: map[string][]string{
contentType: {applicationJSON},
},
request: testHTTPRequest{
Method: "GET",
URL: "/existingHostHeader",
Header: map[string][]string{},
Body: []byte{},
Host: "existing-host",
RequestURI: "/existingHostHeader",
},
},
expectedUpstream: "passExistingHostHeader",
}),
)
It("ServeHTTP, when not passing a host header", func() {