feat: add HSTS, CSP, and Permissions-Policy security headers (closes #91)
check / check (push) Failing after 0s
check / check (push) Failing after 0s
SecurityHeaders() now also sets Strict-Transport-Security (one year, includeSubDomains), a Content-Security-Policy (default-src self, frame-ancestors none) and a Permissions-Policy denying the browser features pixa does not use. X-Frame-Options stays as the legacy fallback. What a reader would trip over: HSTS is sent on every response even though pixa listens on plain HTTP behind a TLS-terminating proxy; browsers ignore the header over plaintext, and this avoids trusting a forwarded-proto header. The clipboard feature is left unlisted so the copy button on the generator page keeps working. Disclosure: script-src and style-src carry unsafe-inline because the generator template has inline onclick handlers and the bundled Tailwind script injects a style element at runtime; removing it needs template changes and is tracked separately. Model: opus-4-8 (implementation, review); fable-5-1 (landing message)
This commit was merged in pull request #121.
This commit is contained in:
@@ -21,6 +21,33 @@ import (
|
|||||||
// CORSMaxAgeSeconds is the max age for CORS preflight cache (24 hours).
|
// CORSMaxAgeSeconds is the max age for CORS preflight cache (24 hours).
|
||||||
const CORSMaxAgeSeconds = 86400
|
const CORSMaxAgeSeconds = 86400
|
||||||
|
|
||||||
|
// HSTSValue is the Strict-Transport-Security header value: one year with
|
||||||
|
// includeSubDomains. Emitted unconditionally even though pixa listens plain
|
||||||
|
// HTTP behind a TLS-terminating proxy; browsers ignore an HSTS header received
|
||||||
|
// over plaintext (RFC 6797 section 8.1), so it never lies about the connection,
|
||||||
|
// and emitting it here avoids trusting a forwarded-proto header.
|
||||||
|
const HSTSValue = "max-age=31536000; includeSubDomains"
|
||||||
|
|
||||||
|
// ContentSecurityPolicyValue is the Content-Security-Policy header value.
|
||||||
|
// default-src 'self' is the baseline and frame-ancestors 'none' is the primary
|
||||||
|
// clickjacking control. 'unsafe-inline' is required in script-src and style-src
|
||||||
|
// because the served templates carry inline onclick handlers (generator page)
|
||||||
|
// and the bundled Tailwind asset injects a runtime <style> element; dropping it
|
||||||
|
// needs template changes outside this issue's scope.
|
||||||
|
const ContentSecurityPolicyValue = "default-src 'self'; " +
|
||||||
|
"script-src 'self' 'unsafe-inline'; " +
|
||||||
|
"style-src 'self' 'unsafe-inline'; " +
|
||||||
|
"object-src 'none'; " +
|
||||||
|
"base-uri 'self'; " +
|
||||||
|
"form-action 'self'; " +
|
||||||
|
"frame-ancestors 'none'"
|
||||||
|
|
||||||
|
// PermissionsPolicyValue is the Permissions-Policy header value. Every listed
|
||||||
|
// feature is denied because pixa uses none of them.
|
||||||
|
const PermissionsPolicyValue = "accelerometer=(), autoplay=(), camera=(), " +
|
||||||
|
"display-capture=(), geolocation=(), gyroscope=(), magnetometer=(), " +
|
||||||
|
"microphone=(), payment=(), usb=()"
|
||||||
|
|
||||||
// Params defines dependencies for Middleware.
|
// Params defines dependencies for Middleware.
|
||||||
type Params struct {
|
type Params struct {
|
||||||
fx.In
|
fx.In
|
||||||
@@ -164,6 +191,16 @@ func (s *Middleware) SecurityHeaders() func(http.Handler) http.Handler {
|
|||||||
// Disable XSS filtering (modern browsers don't need it, can cause issues)
|
// Disable XSS filtering (modern browsers don't need it, can cause issues)
|
||||||
w.Header().Set("X-XSS-Protection", "0")
|
w.Header().Set("X-XSS-Protection", "0")
|
||||||
|
|
||||||
|
// Force HTTPS on future visits (ignored by browsers over plaintext)
|
||||||
|
w.Header().Set("Strict-Transport-Security", HSTSValue)
|
||||||
|
|
||||||
|
// Restrict content sources; frame-ancestors is the primary
|
||||||
|
// clickjacking control, X-Frame-Options the legacy fallback
|
||||||
|
w.Header().Set("Content-Security-Policy", ContentSecurityPolicyValue)
|
||||||
|
|
||||||
|
// Deny browser features pixa does not use
|
||||||
|
w.Header().Set("Permissions-Policy", PermissionsPolicyValue)
|
||||||
|
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,6 +56,61 @@ func TestSecurityHeaders(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSecurityHeaders_PolicyHeaders(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
cfg := &config.Config{}
|
||||||
|
mw := &Middleware{
|
||||||
|
log: slog.Default(),
|
||||||
|
config: cfg,
|
||||||
|
}
|
||||||
|
|
||||||
|
testHandler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
})
|
||||||
|
|
||||||
|
handler := mw.SecurityHeaders()(testHandler)
|
||||||
|
|
||||||
|
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/test", nil)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
|
||||||
|
handler.ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
header string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"Strict-Transport-Security", "max-age=31536000; includeSubDomains"},
|
||||||
|
{
|
||||||
|
"Content-Security-Policy",
|
||||||
|
"default-src 'self'; " +
|
||||||
|
"script-src 'self' 'unsafe-inline'; " +
|
||||||
|
"style-src 'self' 'unsafe-inline'; " +
|
||||||
|
"object-src 'none'; " +
|
||||||
|
"base-uri 'self'; " +
|
||||||
|
"form-action 'self'; " +
|
||||||
|
"frame-ancestors 'none'",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Permissions-Policy",
|
||||||
|
"accelerometer=(), autoplay=(), camera=(), " +
|
||||||
|
"display-capture=(), geolocation=(), gyroscope=(), " +
|
||||||
|
"magnetometer=(), microphone=(), payment=(), usb=()",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.header, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
got := rec.Header().Get(tt.header)
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("%s = %q, want %q", tt.header, got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSecurityHeaders_PreservesExistingHeaders(t *testing.T) {
|
func TestSecurityHeaders_PreservesExistingHeaders(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user