fix #84: security headers dropped because they were set post-flush; set pre-handler
This commit is contained in:
@@ -3,7 +3,6 @@ package web
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -28,29 +27,20 @@ func TestSecurityHeaders(t *testing.T) {
|
||||
t.Errorf("X-Content-Type-Options = %q, want nosniff", got)
|
||||
}
|
||||
|
||||
// JSON response: no security headers.
|
||||
// JSON/raw responses: headers are now set unconditionally BEFORE the handler
|
||||
// runs. The previous post-handler approach was silently dropped once a page
|
||||
// handler flushed its template output (headers must be set before WriteHeader).
|
||||
// CSP/nosniff/referrer on non-HTML bodies is harmless and desirable.
|
||||
jsonh := SecurityHeaders(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"ok":true}`))
|
||||
}))
|
||||
rec = httptest.NewRecorder()
|
||||
jsonh.ServeHTTP(rec, httptest.NewRequest("GET", "/api/x", nil))
|
||||
if got := rec.Header().Get("Content-Security-Policy"); got != "" {
|
||||
t.Errorf("unexpected CSP %q on JSON response", got)
|
||||
if got := rec.Header().Get("Content-Security-Policy"); got != wantCSP {
|
||||
t.Errorf("CSP missing on JSON response: got %q", got)
|
||||
}
|
||||
if got := rec.Header().Get("Referrer-Policy"); got != "" {
|
||||
t.Errorf("unexpected Referrer-Policy %q on JSON response", got)
|
||||
}
|
||||
|
||||
// Content type set after the first Write (as the inline can page does) is
|
||||
// still picked up because headers are inspected post-handler.
|
||||
lateh := SecurityHeaders(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("<html></html>"))
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
}))
|
||||
rec = httptest.NewRecorder()
|
||||
lateh.ServeHTTP(rec, httptest.NewRequest("GET", "/", nil))
|
||||
if got := rec.Header().Get("Content-Security-Policy"); !strings.Contains(got, "frame-ancestors 'none'") {
|
||||
t.Errorf("CSP = %q, want frame-ancestors 'none'", got)
|
||||
if got := rec.Header().Get("Referrer-Policy"); got != "no-referrer" {
|
||||
t.Errorf("Referrer-Policy missing on JSON response: got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user