Skip to content

Commit 624f918

Browse files
committed
Fix Location response header http to https when SSL
1 parent 48ade6b commit 624f918

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

secure.go

+7
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,13 @@ func (s *Secure) isSSL(r *http.Request) bool {
439439
// Used by http.ReverseProxy.
440440
func (s *Secure) ModifyResponseHeaders(res *http.Response) error {
441441
if res != nil && res.Request != nil {
442+
// Fix Location response header http to https when SSL is enabled.
443+
location := res.Header.Get("Location")
444+
if s.isSSL(res.Request) && strings.Contains(location, "http:") {
445+
location = strings.Replace(location, "http:", "https:", 1)
446+
res.Header.Set("Location", location)
447+
}
448+
442449
responseHeader := res.Request.Context().Value(ctxSecureHeaderKey)
443450
if responseHeader != nil {
444451
for header, values := range responseHeader.(http.Header) {

secure_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,43 @@ func TestSSLForceHostTemporaryRedirect(t *testing.T) {
12371237
expect(t, res.Code, http.StatusTemporaryRedirect)
12381238
}
12391239

1240+
func TestModifyResponseHeadersNoSSL(t *testing.T) {
1241+
s := New(Options{
1242+
SSLRedirect: false,
1243+
})
1244+
1245+
res := &http.Response{}
1246+
res.Header = http.Header{"Location": []string{"http://example.com"}}
1247+
1248+
err := s.ModifyResponseHeaders(res)
1249+
expect(t, err, nil)
1250+
1251+
expect(t, res.Header.Get("Location"), "http://example.com")
1252+
}
1253+
1254+
func TestModifyResponseHeadersWithSSL(t *testing.T) {
1255+
s := New(Options{
1256+
SSLRedirect: true,
1257+
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
1258+
})
1259+
1260+
req, _ := http.NewRequest("GET", "/foo", nil)
1261+
req.Host = "www.example.com"
1262+
req.URL.Scheme = "http"
1263+
req.Header.Add("X-Forwarded-Proto", "https")
1264+
1265+
res := &http.Response{}
1266+
res.Header = http.Header{"Location": []string{"http://example.com"}}
1267+
res.Request = req
1268+
1269+
expect(t, res.Header.Get("Location"), "http://example.com")
1270+
1271+
err := s.ModifyResponseHeaders(res)
1272+
expect(t, err, nil)
1273+
1274+
expect(t, res.Header.Get("Location"), "https://example.com")
1275+
}
1276+
12401277
/* Test Helpers */
12411278
func expect(t *testing.T, a interface{}, b interface{}) {
12421279
if a != b {

0 commit comments

Comments
 (0)