Skip to content

feat: Add strict transport security and secure cookie options #741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
pr comments
  • Loading branch information
f0ssel committed Mar 31, 2022
commit fccf4bba440e537e4c71394b15c94e251e144c01
12 changes: 4 additions & 8 deletions coderd/httpmw/stricttransportsecurity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import (
"time"
)

const (
strictTransportSecurityHeader = "Strict-Transport-Security"
strictTransportSecurityMaxAge = time.Hour * 24 * 365 // 1 year
)

// StrictTransportSecurity will add the strict-transport-security header if enabled.
// This header forces a browser to always use https for the domain after it loads https
// once.
Expand All @@ -23,12 +18,13 @@ const (
// nolint:revive
func StrictTransportSecurity(enable bool) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if enable {
w.Header().Set(strictTransportSecurityHeader, fmt.Sprintf("max-age=%d", int64(strictTransportSecurityMaxAge.Seconds())))
age := time.Hour * 24 * 365 // 1 year
rw.Header().Set("Strict-Transport-Security", fmt.Sprintf("max-age=%d", int64(age.Seconds())))
}

next.ServeHTTP(w, r)
next.ServeHTTP(rw, r)
})
}
}
8 changes: 3 additions & 5 deletions coderd/httpmw/stricttransportsecurity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ import (
"github.com/coder/coder/coderd/httpmw"
)

const (
strictTransportSecurityHeader = "Strict-Transport-Security"
strictTransportSecurityMaxAge = time.Hour * 24 * 365
)

func TestStrictTransportSecurity(t *testing.T) {
t.Parallel()

strictTransportSecurityHeader := "Strict-Transport-Security"
strictTransportSecurityMaxAge := time.Hour * 24 * 365

setup := func(enable bool) *http.Response {
rw := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
Expand Down
2 changes: 1 addition & 1 deletion coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func (api *api) postLogin(rw http.ResponseWriter, r *http.Request) {
Path: "/",
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
Secure: api.SecureCookie,
Secure: api.SecureAuthCookie,
})

render.Status(r, http.StatusCreated)
Expand Down