-
Notifications
You must be signed in to change notification settings - Fork 899
feat: Add option to enable hsts header #6147
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 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
643f42a
feat: Add option to enable hsts header
Emyrk bc89288
Import sorting
Emyrk f216054
Make gen
Emyrk 5e0a232
Actually use the right handler
Emyrk 6389727
Add parallel unit test
Emyrk 8221ab0
make gen
Emyrk ac1fd5c
Verify hsts options earlier
Emyrk 8f15025
Clarity of what the value is
Emyrk 602aa8f
Fix syntax
Emyrk 895fece
Make gen to update msg
Emyrk c3c822c
Update golden files
Emyrk 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,72 @@ | ||
package httpmw | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
||
const ( | ||
hstsHeader = "Strict-Transport-Security" | ||
) | ||
|
||
type HSTSConfig struct { | ||
// HeaderValue is an empty string if hsts header is disabled. | ||
HeaderValue string | ||
} | ||
|
||
func HSTSConfigOptions(maxAge int, options []string) (HSTSConfig, error) { | ||
if maxAge <= 0 { | ||
// No header, so no need to build the header string. | ||
return HSTSConfig{}, nil | ||
} | ||
|
||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security | ||
var str strings.Builder | ||
_, err := str.WriteString(fmt.Sprintf("max-age=%d", maxAge)) | ||
if err != nil { | ||
return HSTSConfig{}, xerrors.Errorf("hsts: write max-age: %w", err) | ||
} | ||
|
||
for _, option := range options { | ||
switch { | ||
// Only allow valid options and fix any casing mistakes | ||
case strings.EqualFold(option, "includeSubDomains"): | ||
option = "includeSubDomains" | ||
case strings.EqualFold(option, "preload"): | ||
option = "preload" | ||
default: | ||
return HSTSConfig{}, xerrors.Errorf("hsts: invalid option: %q. Must be 'preload' and/or 'includeSubDomains'", option) | ||
} | ||
_, err = str.WriteString("; " + option) | ||
if err != nil { | ||
return HSTSConfig{}, xerrors.Errorf("hsts: write option: %w", err) | ||
} | ||
} | ||
return HSTSConfig{ | ||
HeaderValue: str.String(), | ||
}, nil | ||
} | ||
|
||
// HSTS 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; includeSubDomains; preload | ||
func HSTS(next http.Handler, cfg HSTSConfig) http.Handler { | ||
if cfg.HeaderValue == "" { | ||
// No header, so no need to wrap the handler. | ||
return next | ||
} | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set(hstsHeader, cfg.HeaderValue) | ||
next.ServeHTTP(w, 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,103 @@ | ||
package httpmw_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/coderd/httpmw" | ||
) | ||
|
||
func TestHSTS(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
Name string | ||
MaxAge int | ||
Options []string | ||
|
||
wantErr bool | ||
expectHeader string | ||
}{ | ||
{ | ||
Name: "Empty", | ||
MaxAge: 0, | ||
Options: nil, | ||
}, | ||
{ | ||
Name: "NoAge", | ||
MaxAge: 0, | ||
Options: []string{"includeSubDomains"}, | ||
}, | ||
{ | ||
Name: "NegativeAge", | ||
MaxAge: -100, | ||
Options: []string{"includeSubDomains"}, | ||
}, | ||
{ | ||
Name: "Age", | ||
MaxAge: 1000, | ||
Options: []string{}, | ||
expectHeader: "max-age=1000", | ||
}, | ||
{ | ||
Name: "AgeSubDomains", | ||
MaxAge: 1000, | ||
// Mess with casing | ||
Options: []string{"INCLUDESUBDOMAINS"}, | ||
expectHeader: "max-age=1000; includeSubDomains", | ||
}, | ||
{ | ||
Name: "AgePreload", | ||
MaxAge: 1000, | ||
Options: []string{"Preload"}, | ||
expectHeader: "max-age=1000; preload", | ||
}, | ||
{ | ||
Name: "AllOptions", | ||
MaxAge: 1000, | ||
Options: []string{"preload", "includeSubDomains"}, | ||
expectHeader: "max-age=1000; preload; includeSubDomains", | ||
}, | ||
|
||
// Error values | ||
{ | ||
Name: "BadOption", | ||
MaxAge: 100, | ||
Options: []string{"not-valid"}, | ||
wantErr: true, | ||
}, | ||
{ | ||
Name: "BadOptions", | ||
MaxAge: 100, | ||
Options: []string{"includeSubDomains", "not-valid", "still-not-valid"}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.Name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
|
||
cfg, err := httpmw.HSTSConfigOptions(tt.MaxAge, tt.Options) | ||
if tt.wantErr { | ||
require.Error(t, err, "Expect error, HSTS(%v, %v)", tt.MaxAge, tt.Options) | ||
return | ||
} | ||
require.NoError(t, err, "Expect no error, HSTS(%v, %v)", tt.MaxAge, tt.Options) | ||
|
||
got := httpmw.HSTS(handler, cfg) | ||
req := httptest.NewRequest("GET", "/", nil) | ||
res := httptest.NewRecorder() | ||
got.ServeHTTP(res, req) | ||
|
||
require.Equal(t, tt.expectHeader, res.Header().Get("Strict-Transport-Security"), "expected header value") | ||
}) | ||
} | ||
} |
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
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
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.
Nice solution! 💪🏻