Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions secure.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ func (s *Secure) processRequest(w http.ResponseWriter, r *http.Request) (http.He

// SSL check.
if s.opt.SSLRedirect && !ssl && !s.opt.IsDevelopment {
url := r.URL
// Copy the URL. Writing through r.URL would change the caller's request.
url := *r.URL
url.Scheme = "https"
url.Host = host

Expand Down Expand Up @@ -381,7 +382,8 @@ func (s *Secure) processRequest(w http.ResponseWriter, r *http.Request) (http.He
}

if tempSSLHost != host {
url := r.URL
// Copy the URL. Writing through r.URL would change the caller's request.
url := *r.URL
url.Scheme = "https"
url.Host = tempSSLHost

Expand Down
39 changes: 39 additions & 0 deletions secure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,45 @@ func TestBasicSSL(t *testing.T) {
expect(t, res.Header().Get("Location"), "https://www.example.com/foo")
}

func TestSSLRedirectLeavesRequestURL(t *testing.T) {
s := New(Options{
SSLRedirect: true,
})

res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/foo?x=1", nil)
req.Host = exampleHost
req.URL.Scheme = httpSchema

s.Handler(myHandler).ServeHTTP(res, req)

expect(t, res.Code, http.StatusMovedPermanently)
expect(t, res.Header().Get("Location"), "https://www.example.com/foo?x=1")
expect(t, req.URL.Scheme, httpSchema)
expect(t, req.URL.Host, "")
expect(t, req.URL.RawQuery, "x=1")
}

func TestSSLForceHostLeavesRequestURL(t *testing.T) {
s := New(Options{
SSLHost: "secure.example.com",
SSLForceHost: true,
})

res := httptest.NewRecorder()
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/foo?x=1", nil)
req.Host = exampleHost
req.URL.Scheme = httpsSchema

s.Handler(myHandler).ServeHTTP(res, req)

expect(t, res.Code, http.StatusMovedPermanently)
expect(t, res.Header().Get("Location"), "https://secure.example.com/foo?x=1")
expect(t, req.URL.Scheme, httpsSchema)
expect(t, req.URL.Host, "")
expect(t, req.URL.RawQuery, "x=1")
}

func TestBasicSSLWithHost(t *testing.T) {
s := New(Options{
SSLRedirect: true,
Expand Down