Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/unreleased/ui-security-headers.md
Original file line number Diff line number Diff line change
@@ -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 网关端点保持原有协议响应面不变。
20 changes: 20 additions & 0 deletions internal/server/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
})
Expand Down
29 changes: 29 additions & 0 deletions internal/server/ui_security_headers_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Loading