-
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package httpmw | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// 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(rw http.ResponseWriter, r *http.Request) { | ||
if enable { | ||
age := time.Hour * 24 * 365 // 1 year | ||
rw.Header().Set("Strict-Transport-Security", fmt.Sprintf("max-age=%d", int64(age.Seconds()))) | ||
} | ||
|
||
next.ServeHTTP(rw, r) | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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" | ||
) | ||
|
||
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) | ||
|
||
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), "") | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.