Make the HSTS max-age configurable with HSTS_MAX_AGE (closes #271)
check / check (push) Waiting to run
check / check (push) Waiting to run
HSTS_MAX_AGE, a Go duration, sets the max-age of the Strict-Transport-Security header. Unset, it is ten years (87600h); 0 sends no header at all. A negative value, or one that is not a whole number of seconds, stops startup like an unparseable one. includeSubDomains and preload stay in the header. The value is shown on /settings and in the startup log, and the README's HSTS section now says what each part of the header commits the operator to. Model: opus-5-5
This commit was merged in pull request #515.
This commit is contained in:
@@ -379,6 +379,83 @@ func testSessionIdleTimeoutSuccess(
|
||||
assert.Equal(t, expected, cfg.SessionIdleTimeout)
|
||||
}
|
||||
|
||||
func TestHSTSMaxAge(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
set bool
|
||||
value string
|
||||
expectError bool
|
||||
// sentinel, when set, must be wrapped by the startup
|
||||
// error; every error case must additionally name the
|
||||
// variable in its message.
|
||||
sentinel error
|
||||
expected time.Duration
|
||||
}{
|
||||
{
|
||||
name: caseUnsetUsesDefault,
|
||||
set: false,
|
||||
expected: 87600 * time.Hour,
|
||||
},
|
||||
{
|
||||
name: caseValidValueParsed,
|
||||
set: true,
|
||||
value: "720h",
|
||||
expected: 720 * time.Hour,
|
||||
},
|
||||
{
|
||||
// Zero turns the header off; it is not an error.
|
||||
name: "zero is accepted",
|
||||
set: true,
|
||||
value: "0",
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
name: caseUnparseableFails,
|
||||
set: true,
|
||||
value: "10 years",
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
// A whole number of seconds, so only the sign is wrong.
|
||||
name: "negative whole seconds fail startup",
|
||||
set: true,
|
||||
value: "-1s",
|
||||
expectError: true,
|
||||
sentinel: config.ErrInvalidHSTSMaxAge,
|
||||
},
|
||||
{
|
||||
name: "fraction of a second fails startup",
|
||||
set: true,
|
||||
value: "1500ms",
|
||||
expectError: true,
|
||||
sentinel: config.ErrInvalidHSTSMaxAge,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Cannot use t.Parallel() here because t.Setenv
|
||||
// is incompatible with parallel subtests.
|
||||
configtest.ClearEnv(t)
|
||||
t.Setenv("WEBHOOKER_ENVIRONMENT", "dev")
|
||||
|
||||
if tt.set {
|
||||
t.Setenv("HSTS_MAX_AGE", tt.value)
|
||||
}
|
||||
|
||||
if tt.expectError {
|
||||
expectStartupErrorFor(t, "HSTS_MAX_AGE", tt.sentinel)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
cfg, err := buildConfig(t)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.expected, cfg.HSTSMaxAge)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultDataDir(t *testing.T) {
|
||||
for _, env := range []string{"", "dev", "prod"} {
|
||||
name := env
|
||||
|
||||
Reference in New Issue
Block a user