git.iko.soy/typeable/oauth2-proxy
on master help sign in
Merge pull request #1115 from oauth2-proxy/remove-force-query

Fix upstream proxy appending `?` to requests

commitb82182763e14cb5d13cee9517ee9ded11f9326a9 authorJoel Speed <Joel.speed@hotmail.co.uk> dateMon 22 Mar 2021, 13:12 (6 years ago) parentf0963b392ae5d9
3 files changed+70−17
CHANGELOG.md+1 pkg/upstream/http.go+4−6 pkg/upstream/http_test.go+65−11
CHANGELOG.md+1
@@ -16,6 +16,7 @@
1616
1717## Changes since v7.0.1
1818
19+- [#1115](https://github.com/oauth2-proxy/oauth2-proxy/pull/1115) Fix upstream proxy appending ? to requests (@JoelSpeed)
1920- [#1117](https://github.com/oauth2-proxy/oauth2-proxy/pull/1117) Deprecate GCP HealthCheck option (@JoelSpeed)
2021- [#1104](https://github.com/oauth2-proxy/oauth2-proxy/pull/1104) Allow custom robots text pages (@JoelSpeed)
2122- [#1045](https://github.com/oauth2-proxy/oauth2-proxy/pull/1045) Ensure redirect URI always has a scheme (@JoelSpeed)
pkg/upstream/http.go+4−6
@@ -116,11 +116,11 @@ func newReverseProxy(target *url.URL, upstream options.Upstream, errorHandler Pr
116116 }
117117 }
118118
119− // Set the request director based on the PassHostHeader option
119+ // Ensure we always pass the original request path
120+ setProxyDirector(proxy)
121+
120122 if upstream.PassHostHeader != nil && !*upstream.PassHostHeader {
121123 setProxyUpstreamHostHeader(proxy, target)
122− } else {
123− setProxyDirector(proxy)
124124 }
125125
126126 // Set the error handler so that upstream connection failures render the
@@ -137,10 +137,7 @@ func setProxyUpstreamHostHeader(proxy *httputil.ReverseProxy, target *url.URL) {
137137 director := proxy.Director
138138 proxy.Director = func(req *http.Request) {
139139 director(req)
140− // use RequestURI so that we aren't unescaping encoded slashes in the request path
141140 req.Host = target.Host
142− req.URL.Opaque = req.RequestURI
143− req.URL.RawQuery = ""
144141 }
145142}
146143
@@ -153,6 +150,7 @@ func setProxyDirector(proxy *httputil.ReverseProxy) {
153150 // use RequestURI so that we aren't unescaping encoded slashes in the request path
154151 req.URL.Opaque = req.RequestURI
155152 req.URL.RawQuery = ""
153+ req.URL.ForceQuery = false
156154 }
157155}
158156
pkg/upstream/http_test.go+65−11
@@ -30,16 +30,17 @@ var _ = Describe("HTTP Upstream Suite", func() {
3030 falsum := false
3131
3232 type httpUpstreamTableInput struct {
33− id string
34− serverAddr *string
35− target string
36− method string
37− body []byte
38− signatureData *options.SignatureData
39− existingHeaders map[string]string
40− expectedResponse testHTTPResponse
41− expectedUpstream string
42− errorHandler ProxyErrorHandler
33+ id string
34+ serverAddr *string
35+ target string
36+ method string
37+ body []byte
38+ passUpstreamHostHeader bool
39+ signatureData *options.SignatureData
40+ existingHeaders map[string]string
41+ expectedResponse testHTTPResponse
42+ expectedUpstream string
43+ errorHandler ProxyErrorHandler
4344 }
4445
4546 DescribeTable("HTTP Upstream ServeHTTP",
@@ -52,6 +53,9 @@ var _ = Describe("HTTP Upstream Suite", func() {
5253 for key, value := range in.existingHeaders {
5354 req.Header.Add(key, value)
5455 }
56+ if host := req.Header.Get("Host"); host != "" {
57+ req.Host = host
58+ }
5559
5660 req = middlewareapi.AddRequestScope(req, &middlewareapi.RequestScope{})
5761 rw := httptest.NewRecorder()
@@ -60,7 +64,7 @@ var _ = Describe("HTTP Upstream Suite", func() {
6064
6165 upstream := options.Upstream{
6266 ID: in.id,
63− PassHostHeader: &truth,
67+ PassHostHeader: &in.passUpstreamHostHeader,
6468 ProxyWebSockets: &falsum,
6569 InsecureSkipTLSVerify: false,
6670 FlushInterval: &flush,
@@ -140,6 +144,29 @@ var _ = Describe("HTTP Upstream Suite", func() {
140144 },
141145 expectedUpstream: "encodedSlashes",
142146 }),
147+ Entry("request a path with an empty query string", &httpUpstreamTableInput{
148+ id: "default",
149+ serverAddr: &serverAddr,
150+ target: "http://example.localhost/foo?",
151+ method: "GET",
152+ body: []byte{},
153+ errorHandler: nil,
154+ expectedResponse: testHTTPResponse{
155+ code: 200,
156+ header: map[string][]string{
157+ contentType: {applicationJSON},
158+ },
159+ request: testHTTPRequest{
160+ Method: "GET",
161+ URL: "http://example.localhost/foo?",
162+ Header: map[string][]string{},
163+ Body: []byte{},
164+ Host: "example.localhost",
165+ RequestURI: "http://example.localhost/foo?",
166+ },
167+ },
168+ expectedUpstream: "default",
169+ }),
143170 Entry("when the request has a body", &httpUpstreamTableInput{
144171 id: "requestWithBody",
145172 serverAddr: &serverAddr,
@@ -257,6 +284,33 @@ var _ = Describe("HTTP Upstream Suite", func() {
257284 },
258285 expectedUpstream: "existingHeaders",
259286 }),
287+ Entry("when passing the existing host header", &httpUpstreamTableInput{
288+ id: "passExistingHostHeader",
289+ serverAddr: &serverAddr,
290+ target: "/existingHostHeader",
291+ method: "GET",
292+ body: []byte{},
293+ errorHandler: nil,
294+ passUpstreamHostHeader: true,
295+ existingHeaders: map[string]string{
296+ "Host": "existing-host",
297+ },
298+ expectedResponse: testHTTPResponse{
299+ code: 200,
300+ header: map[string][]string{
301+ contentType: {applicationJSON},
302+ },
303+ request: testHTTPRequest{
304+ Method: "GET",
305+ URL: "/existingHostHeader",
306+ Header: map[string][]string{},
307+ Body: []byte{},
308+ Host: "existing-host",
309+ RequestURI: "/existingHostHeader",
310+ },
311+ },
312+ expectedUpstream: "passExistingHostHeader",
313+ }),
260314 )
261315
262316 It("ServeHTTP, when not passing a host header", func() {