From 4d91c2c333571f2b922551cf0cb1085f78f2292f Mon Sep 17 00:00:00 2001 From: guyu233 Date: Fri, 4 Dec 2026 15:13:55 +0800 Subject: [PATCH] fix(console): send browser hardening headers on the operator console Add X-Content-Type-Options: nosniff, X-Frame-Options: DENY, and Referrer-Policy: no-referrer to console HTML, the SPA fallback, and embedded assets. The public OpenAI/Anthropic gateway endpoints keep their protocol-only response surface. --- changelog/unreleased/ui-security-headers.md | 7 +++++ internal/server/middleware.go | 20 ++++++++++++++ internal/server/router.go | 3 ++- internal/server/ui_security_headers_test.go | 29 +++++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/ui-security-headers.md create mode 100644 internal/server/ui_security_headers_test.go diff --git a/changelog/unreleased/ui-security-headers.md b/changelog/unreleased/ui-security-headers.md new file mode 100644 index 000000000..5df154265 --- /dev/null +++ b/changelog/unreleased/ui-security-headers.md @@ -0,0 +1,7 @@ +### English + +- Add browser hardening headers to the operator console and its static assets: `X-Content-Type-Options: nosniff`, `X-Frame-Options: DENY`, and `Referrer-Policy: no-referrer`. The public OpenAI/Anthropic gateway endpoints keep their protocol-only response surface unchanged. + +### 中文 + +- 为运维控制台及其静态资源增加浏览器加固响应头:`X-Content-Type-Options: nosniff`、`X-Frame-Options: DENY`、`Referrer-Policy: no-referrer`。对外的 OpenAI / Anthropic 网关端点保持原有协议响应面不变。 diff --git a/internal/server/middleware.go b/internal/server/middleware.go index f17379e2e..c57946b9d 100644 --- a/internal/server/middleware.go +++ b/internal/server/middleware.go @@ -48,6 +48,26 @@ func setOpenAICORSHeaders(w http.ResponseWriter, r *http.Request) { header.Set("Access-Control-Max-Age", "600") } +// setUISecurityHeaders applies browser-facing hardening to the authenticated +// operator console and its static assets only. These headers are deliberately +// NOT set on the OpenAI/Anthropic gateway endpoints, where client SDKs parse +// the protocol body and the public surface is the API itself. +func setUISecurityHeaders(w http.ResponseWriter) { + h := w.Header() + h.Set("X-Content-Type-Options", "nosniff") + h.Set("X-Frame-Options", "DENY") + h.Set("Referrer-Policy", "no-referrer") +} + +// withUISecurityHeaders wraps a console UI handler so every HTML/asset response +// carries the same browser hardening headers. +func withUISecurityHeaders(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + setUISecurityHeaders(w) + next.ServeHTTP(w, r) + }) +} + func (s *Server) withAPIKey(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { identity, ok := s.Auth.Authenticate(r.Context(), r) diff --git a/internal/server/router.go b/internal/server/router.go index 21fc40e4f..845541ba7 100644 --- a/internal/server/router.go +++ b/internal/server/router.go @@ -50,7 +50,7 @@ func (s *Server) routes() { s.mux.HandleFunc(endpoint.MessagesPath, s.withAPIKey(s.gatewayHandler().HandleAnthropicMessages)) s.mux.HandleFunc(endpoint.ResponsesPath, s.withAPIKey(s.gatewayHandler().HandleResponses)) - ui := webui.Handler() + ui := withUISecurityHeaders(webui.Handler()) s.mux.Handle("/assets/", ui) s.mux.Handle("/favicon.svg", ui) s.mux.Handle("/favicon-dark.svg", ui) @@ -77,6 +77,7 @@ func (s *Server) routes() { http.Error(w, err.Error(), http.StatusInternalServerError) return } + setUISecurityHeaders(w) w.Header().Set("Content-Type", "text/html; charset=utf-8") _, _ = w.Write(data) }) diff --git a/internal/server/ui_security_headers_test.go b/internal/server/ui_security_headers_test.go new file mode 100644 index 000000000..77a9cd57d --- /dev/null +++ b/internal/server/ui_security_headers_test.go @@ -0,0 +1,29 @@ +package server + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +// The authenticated operator console and its SPA fallback must carry the +// standard browser hardening headers; the public gateway endpoints keep their +// protocol-only response surface. +func TestConsoleUISendsSecurityHeaders(t *testing.T) { + s := New(Server{}) + for _, path := range []string{"/", "/login", "/assets/"} { + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, path, nil) + s.Handler().ServeHTTP(rec, req) + + if got := rec.Header().Get("X-Content-Type-Options"); got != "nosniff" { + t.Errorf("%s: X-Content-Type-Options = %q", path, got) + } + if got := rec.Header().Get("X-Frame-Options"); got != "DENY" { + t.Errorf("%s: X-Frame-Options = %q", path, got) + } + if got := rec.Header().Get("Referrer-Policy"); got != "no-referrer" { + t.Errorf("%s: Referrer-Policy = %q", path, got) + } + } +}