-
Notifications
You must be signed in to change notification settings - Fork 897
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package httpmw | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
const ( | ||
strictTransportSecurityHeader = "Strict-Transport-Security" | ||
strictTransportSecurityMaxAge = time.Hour * 24 * 365 // 1 year | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since these are just used in the one place, I don't think they should be constants. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consts are more self documenting (variable name) and cleaner code to read imo. I feel like I was told never to use magic numbers like day 1 of programming and have never looked back. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did it anyways to get this pr merged.... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can leave em' as consts that's just a minor nit. |
||
// 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. | ||
// Meaning: On first load of product.coder.com, they are redirected to https. | ||
// On all subsequent loads, the client's local browser forces https. This prevents man in the middle. | ||
// | ||
// This header only makes sense if the app is using tls. | ||
// Full header example | ||
// Strict-Transport-Security: max-age=63072000; | ||
// 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) { | ||
if enable { | ||
w.Header().Set(strictTransportSecurityHeader, fmt.Sprintf("max-age=%d", int64(strictTransportSecurityMaxAge.Seconds()))) | ||
} | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package httpmw_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/coderd/httpmw" | ||
) | ||
|
||
const ( | ||
strictTransportSecurityHeader = "Strict-Transport-Security" | ||
strictTransportSecurityMaxAge = time.Hour * 24 * 365 | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should probably be in the test function scope. If they aren't used globally between tests, it could be confusing. |
||
|
||
func TestStrictTransportSecurity(t *testing.T) { | ||
t.Parallel() | ||
|
||
setup := func(enable bool) *http.Response { | ||
rw := httptest.NewRecorder() | ||
r := httptest.NewRequest("GET", "/", nil) | ||
|
||
rtr := chi.NewRouter() | ||
rtr.Use(httpmw.StrictTransportSecurity(enable)) | ||
rtr.Get("/", func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte("hello!")) | ||
}) | ||
rtr.ServeHTTP(rw, r) | ||
return rw.Result() | ||
} | ||
|
||
t.Run("True", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
res := setup(true) | ||
defer res.Body.Close() | ||
require.Contains(t, res.Header.Get(strictTransportSecurityHeader), fmt.Sprintf("max-age=%d", int64(strictTransportSecurityMaxAge.Seconds()))) | ||
}) | ||
t.Run("False", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
res := setup(false) | ||
defer res.Body.Close() | ||
require.NotContains(t, res.Header.Get(strictTransportSecurityHeader), fmt.Sprintf("max-age=%d", int64(strictTransportSecurityMaxAge.Seconds()))) | ||
require.Equal(t, res.Header.Get(strictTransportSecurityHeader), "") | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed for static assets as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Emyrk ? I don't know but my guess is "no" because it will already be set on the browser after the first api call and also static assets would never have sensitive data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HSTS should be on static, but I think it might not really matter as you only need to hit 1 HSTS header for it "take effect". Every HSTS header after is redundant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably move it outside of there then, just to confirm properly.