feat(server): add browser protections for the operator console

This commit is contained in:
anqorithm
2026-11-18 22:13:26 +01:00
parent d89cf781b7
commit 23f95611c1
2 changed files with 47 additions and 1 deletions
+8 -1
View File
@@ -1046,7 +1046,14 @@ func (s *Server) Handler() http.Handler {
api := cors(s.requireAuth(mux))
root := http.NewServeMux()
root.Handle("/api/", api)
root.Handle("/", s.webuiHandler())
webui := s.webuiHandler()
root.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Keep the operator console out of cross-origin frames and referrers.
w.Header().Set("Content-Security-Policy", "frame-ancestors 'self'")
w.Header().Set("X-Frame-Options", "SAMEORIGIN")
w.Header().Set("Referrer-Policy", "same-origin")
webui.ServeHTTP(w, r)
})
return limitRequestBody(root)
}
+39
View File
@@ -0,0 +1,39 @@
package server
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestConsoleBrowserProtections(t *testing.T) {
handler := (&Server{}).Handler()
headers := map[string]string{
"Content-Security-Policy": "frame-ancestors 'self'",
"X-Frame-Options": "SAMEORIGIN",
"Referrer-Policy": "same-origin",
}
for _, path := range []string{"/", "/settings/"} {
res := httptest.NewRecorder()
handler.ServeHTTP(res, httptest.NewRequest(http.MethodGet, path, nil))
sent := res.Result().Header
for name, want := range headers {
if got := sent.Get(name); got != want {
t.Errorf("%s: %s = %q, want %q", path, name, got, want)
}
}
}
res := httptest.NewRecorder()
handler.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/api/health", nil))
if res.Code != http.StatusOK {
t.Fatalf("health: status = %d, want %d", res.Code, http.StatusOK)
}
sent := res.Result().Header
for name := range headers {
if got := sent.Get(name); got != "" {
t.Errorf("health: unexpected %s = %q", name, got)
}
}
}