Files
Agent-System/internal/server/cors.go
T
weichen 171959710d fix(security): add CORS allowlist and enforce principal on chat handlers
Replace wildcard CORS with configured origins and bind chat/task user
identity to the authenticated principal instead of request body fields.
2026-09-13 02:50:22 +00:00

28 lines
603 B
Go

package server
import (
"net/http"
"strings"
)
func applyCORSHeaders(w http.ResponseWriter, r *http.Request, allowedOrigins []string) {
origin := strings.TrimSpace(r.Header.Get("Origin"))
if origin == "" || len(allowedOrigins) == 0 {
return
}
for _, allowed := range allowedOrigins {
if strings.TrimSpace(allowed) == origin {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Vary", "Origin")
return
}
}
}
func (s *Server) writeCORS(w http.ResponseWriter, r *http.Request) {
if s.cfg == nil {
return
}
applyCORSHeaders(w, r, s.cfg.CORSAllowedOrigins)
}