Skip to content

Commit

Permalink
Reject requests over TLS if it has been disabled
Browse files Browse the repository at this point in the history
If we were running TLS on a site and then disable it, any active
connections that were made over TLS will still be valid until closed.
This could be confusing if you disable TLS then refresh your browser and
find that it's still working.

Instead, we can reject the request with an error so that the change
applies to active connections as well.
  • Loading branch information
kevinmcconnell committed Sep 12, 2024
1 parent eaf506e commit 8065f92
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ func (s *Service) serviceRequestWithTarget(w http.ResponseWriter, r *http.Reques
return
}

if !s.options.RequireTLS() && r.TLS != nil {
SetErrorResponse(w, r, http.StatusServiceUnavailable, nil)
return
}

if s.handlePausedAndStoppedRequests(w, r) {
return
}
Expand Down
18 changes: 18 additions & 0 deletions internal/server/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ func TestService_RedirectToHTTPWhenTLSRequired(t *testing.T) {
require.Equal(t, http.StatusOK, w.Result().StatusCode)
}

func TestService_RejectTLSRequestsWhenNotConfigured(t *testing.T) {
service := testCreateService(t, defaultServiceOptions, defaultTargetOptions)

require.False(t, service.options.RequireTLS())

req := httptest.NewRequest(http.MethodGet, "http://example.com/", nil)
w := httptest.NewRecorder()
service.ServeHTTP(w, req)

require.Equal(t, http.StatusOK, w.Result().StatusCode)

req = httptest.NewRequest(http.MethodGet, "https://example.com", nil)
w = httptest.NewRecorder()
service.ServeHTTP(w, req)

require.Equal(t, http.StatusServiceUnavailable, w.Result().StatusCode)
}

func TestService_ReturnSuccessfulHealthCheckWhilePausedOrStopped(t *testing.T) {
service := testCreateService(t, defaultServiceOptions, defaultTargetOptions)

Expand Down

0 comments on commit 8065f92

Please sign in to comment.