diff --git a/CHANGELOG.md b/CHANGELOG.md index bca3684..8ba2989 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ## Changes since v6.0.0 +- [#668](https://github.com/oauth2-proxy/oauth2-proxy/pull/668) Use req.Host in --force-https when req.URL.Host is empty (@zucaritask) - [#660](https://github.com/oauth2-proxy/oauth2-proxy/pull/660) Use builder pattern to simplify requests to external endpoints (@JoelSpeed) - [#591](https://github.com/oauth2-proxy/oauth2-proxy/pull/591) Introduce upstream package with new reverse proxy implementation (@JoelSpeed) - [#576](https://github.com/oauth2-proxy/oauth2-proxy/pull/576) Separate Cookie validation out of main options validation (@JoelSpeed) diff --git a/pkg/middleware/redirect_to_https.go b/pkg/middleware/redirect_to_https.go index 141d076..2738456 100644 --- a/pkg/middleware/redirect_to_https.go +++ b/pkg/middleware/redirect_to_https.go @@ -38,6 +38,11 @@ func redirectToHTTPS(httpsPort string, next http.Handler) http.Handler { // Set the scheme to HTTPS targetURL.Scheme = httpsScheme + // Set the req.Host when the targetURL still does not have one + if targetURL.Host == "" { + targetURL.Host = req.Host + } + // Overwrite the port if the original request was to a non-standard port if targetURL.Port() != "" { // If Port was not empty, this should be fine to ignore the error diff --git a/pkg/middleware/redirect_to_https_test.go b/pkg/middleware/redirect_to_https_test.go index c4b631b..238c3b8 100644 --- a/pkg/middleware/redirect_to_https_test.go +++ b/pkg/middleware/redirect_to_https_test.go @@ -154,5 +154,15 @@ var _ = Describe("RedirectToHTTPS suite", func() { expectedStatus: 200, expectedBody: "test", }), + // By using newRequest from httptest we get example.com as a Host + // when the target is just a path. + // For details: https://golang.org/pkg/net/http/httptest/#NewRequest + Entry("without TLS with a path as request", &requestTableInput{ + requestString: "/", + useTLS: false, + expectedStatus: 308, + expectedBody: permanentRedirectBody("https://example.com/"), + expectedLocation: "https://example.com/", + }), ) })