Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Add middleware for setting HSTS headers #387

Merged
merged 1 commit into from
Aug 26, 2020
Merged
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: 6 additions & 0 deletions cmd/adminapi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ func realMain(ctx context.Context) error {
}
rateLimit := httplimiter.Handle

// Install HSTS headers in production
if !config.DevMode {
addHSTS := middleware.AddHSTS(ctx)
r.Use(addHSTS)
}

// Create the renderer
h, err := render.New(ctx, "", config.DevMode)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/apiserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ func realMain(ctx context.Context) error {
}
rateLimit := httplimiter.Handle

// Install HSTS headers in production
if !config.DevMode {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guessing this won't matter as non-browsers will ignore...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but also no harm

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't want to set it in dev because localhost w/ hsts doesn't work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Mike was saying that no one will call the API in the browser so it probably doesn't matter

addHSTS := middleware.AddHSTS(ctx)
r.Use(addHSTS)
}

// Create the renderer
h, err := render.New(ctx, "", config.DevMode)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ func realMain(ctx context.Context) error {
return fmt.Errorf("failed to create limiter middleware: %w", err)
}

// Install HSTS headers in production
if !config.DevMode {
addHSTS := middleware.AddHSTS(ctx)
r.Use(addHSTS)
}

// Install the CSRF protection middleware.
configureCSRF := middleware.ConfigureCSRF(ctx, config, h)
r.Use(configureCSRF)
Expand Down
53 changes: 53 additions & 0 deletions pkg/controller/middleware/hsts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package middleware

import (
"context"
"net/http"

"github.com/google/exposure-notifications-server/pkg/logging"

"github.com/gorilla/mux"
)

// AddHSTS adds the required HSTS headers to the response. You should only
// enable this middlware in production systems.
func AddHSTS(ctx context.Context) mux.MiddlewareFunc {
logger := logging.FromContext(ctx).Named("middleware.AddHSTS")

writeHeader := func(w http.ResponseWriter) {
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains; preload")
}

return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Add the HSTS header before sending the first byte
bfbw := &beforeFirstByteWriter{
w: w,
before: func() error {
writeHeader(w)
return nil
},
logger: logger,
}

next.ServeHTTP(bfbw, r)

// Ensure the header is written on empty responses
writeHeader(w)
})
}
}