Skip to content

Commit 8780463

Browse files
committed
fix: 修复了存在的XSS跨站攻击
1 parent 9f69b14 commit 8780463

File tree

3 files changed

+4
-11
lines changed

3 files changed

+4
-11
lines changed

backend/internal/app/chat.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,7 @@ func (s *Server) handleChat(w http.ResponseWriter, r *http.Request) {
9999
}
100100

101101
if stream {
102-
contentType := response.Header.Get("Content-Type")
103-
if contentType == "" {
104-
contentType = "text/event-stream; charset=utf-8"
105-
}
106-
w.Header().Set("Content-Type", contentType)
102+
w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
107103
w.Header().Set("Cache-Control", "no-cache, no-transform")
108104
w.Header().Set("Connection", "keep-alive")
109105
w.Header().Set("X-Accel-Buffering", "no")
@@ -117,11 +113,7 @@ func (s *Server) handleChat(w http.ResponseWriter, r *http.Request) {
117113
return
118114
}
119115

120-
contentType := response.Header.Get("Content-Type")
121-
if contentType == "" {
122-
contentType = "application/json; charset=utf-8"
123-
}
124-
w.Header().Set("Content-Type", contentType)
116+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
125117
w.WriteHeader(http.StatusOK)
126118
_, _ = io.Copy(w, response.Body)
127119
}

backend/internal/app/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func (s *Server) withLogging(route string, next http.HandlerFunc) http.HandlerFu
5757
r = ensureRequestContext(r)
5858
requestID := requestIDFromContext(r.Context())
5959
w.Header().Set("X-Request-Id", requestID)
60+
w.Header().Set("X-Content-Type-Options", "nosniff")
6061

6162
startedAt := time.Now()
6263
recorder := &responseRecorder{

backend/internal/app/server_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestHandleChat_ProxiesSSE(t *testing.T) {
6969
t.Fatalf("unexpected status: %d", response.StatusCode)
7070
}
7171
assertHeader(t, response.Header, "Access-Control-Allow-Origin", "http://frontend.test")
72-
assertHeader(t, response.Header, "Content-Type", "text/event-stream")
72+
assertHeader(t, response.Header, "Content-Type", "text/event-stream; charset=utf-8")
7373
assertHeader(t, response.Header, "Cache-Control", "no-cache, no-transform")
7474
assertHeader(t, response.Header, "X-Accel-Buffering", "no")
7575

0 commit comments

Comments
 (0)