Skip to content

Commit 5198391

Browse files
authored
Merge pull request #650 from guyu233/fix/console-security-headers
fix(console): send browser hardening headers on the operator console
2 parents 075f542 + c1f616a commit 5198391

4 files changed

Lines changed: 58 additions & 1 deletion

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### English
2+
3+
- 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.
4+
5+
### 中文
6+
7+
- 为运维控制台及其静态资源增加浏览器加固响应头:`X-Content-Type-Options: nosniff`、`X-Frame-Options: DENY`、`Referrer-Policy: no-referrer`。对外的 OpenAI / Anthropic 网关端点保持原有协议响应面不变。

‎internal/server/middleware.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ func setOpenAICORSHeaders(w http.ResponseWriter, r *http.Request) {
4848
header.Set("Access-Control-Max-Age", "600")
4949
}
5050

51+
// setUISecurityHeaders applies browser-facing hardening to the authenticated
52+
// operator console and its static assets only. These headers are deliberately
53+
// NOT set on the OpenAI/Anthropic gateway endpoints, where client SDKs parse
54+
// the protocol body and the public surface is the API itself.
55+
func setUISecurityHeaders(w http.ResponseWriter) {
56+
h := w.Header()
57+
h.Set("X-Content-Type-Options", "nosniff")
58+
h.Set("X-Frame-Options", "DENY")
59+
h.Set("Referrer-Policy", "no-referrer")
60+
}
61+
62+
// withUISecurityHeaders wraps a console UI handler so every HTML/asset response
63+
// carries the same browser hardening headers.
64+
func withUISecurityHeaders(next http.Handler) http.Handler {
65+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
66+
setUISecurityHeaders(w)
67+
next.ServeHTTP(w, r)
68+
})
69+
}
70+
5171
func (s *Server) withAPIKey(next http.HandlerFunc) http.HandlerFunc {
5272
return func(w http.ResponseWriter, r *http.Request) {
5373
identity, ok := s.Auth.Authenticate(r.Context(), r)

‎internal/server/router.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (s *Server) routes() {
5050
s.mux.HandleFunc(endpoint.MessagesPath, s.withAPIKey(s.gatewayHandler().HandleAnthropicMessages))
5151
s.mux.HandleFunc(endpoint.ResponsesPath, s.withAPIKey(s.gatewayHandler().HandleResponses))
5252

53-
ui := webui.Handler()
53+
ui := withUISecurityHeaders(webui.Handler())
5454
s.mux.Handle("/assets/", ui)
5555
s.mux.Handle("/favicon.svg", ui)
5656
s.mux.Handle("/favicon-dark.svg", ui)
@@ -77,6 +77,7 @@ func (s *Server) routes() {
7777
http.Error(w, err.Error(), http.StatusInternalServerError)
7878
return
7979
}
80+
setUISecurityHeaders(w)
8081
w.Header().Set("Content-Type", "text/html; charset=utf-8")
8182
_, _ = w.Write(data)
8283
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package server
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
)
8+
9+
// The authenticated operator console and its SPA fallback must carry the
10+
// standard browser hardening headers; the public gateway endpoints keep their
11+
// protocol-only response surface.
12+
func TestConsoleUISendsSecurityHeaders(t *testing.T) {
13+
s := New(Server{})
14+
for _, path := range []string{"/", "/login", "/assets/"} {
15+
rec := httptest.NewRecorder()
16+
req := httptest.NewRequest(http.MethodGet, path, nil)
17+
s.Handler().ServeHTTP(rec, req)
18+
19+
if got := rec.Header().Get("X-Content-Type-Options"); got != "nosniff" {
20+
t.Errorf("%s: X-Content-Type-Options = %q", path, got)
21+
}
22+
if got := rec.Header().Get("X-Frame-Options"); got != "DENY" {
23+
t.Errorf("%s: X-Frame-Options = %q", path, got)
24+
}
25+
if got := rec.Header().Get("Referrer-Policy"); got != "no-referrer" {
26+
t.Errorf("%s: Referrer-Policy = %q", path, got)
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)