Skip to content
Merged
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
17 changes: 15 additions & 2 deletions pkg/server/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,15 +490,28 @@ func (h *HTTPServer) pollHandler(w http.ResponseWriter, req *http.Request) {

func (h *HTTPServer) corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
allowOrigin := h.options.OriginURL
// When the allowed origin is the wildcard, reflect the request's Origin
// instead of returning "*". Browsers reject responses that combine
// "Access-Control-Allow-Origin: *" with "Access-Control-Allow-Credentials: true",
// so reflecting the origin keeps the default permissive while remaining valid.
if h.options.OriginURL == "*" {
if origin := req.Header.Get("Origin"); origin != "" {
allowOrigin = origin
}
// Response varies by Origin, so caches must key on it.
w.Header().Add("Vary", "Origin")
}

// Set CORS headers for the preflight request
if req.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Origin", h.options.OriginURL)
w.Header().Set("Access-Control-Allow-Origin", allowOrigin)
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
w.WriteHeader(http.StatusNoContent)
return
}
w.Header().Set("Access-Control-Allow-Origin", h.options.OriginURL)
w.Header().Set("Access-Control-Allow-Origin", allowOrigin)
w.Header().Set("Access-Control-Allow-Credentials", "true")
Comment on lines +498 to 515

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Add Vary: Origin when Access-Control-Allow-Origin is dynamic.

When OriginURL == "*", Access-Control-Allow-Origin varies per request origin, but the response currently does not advertise that variance. Shared caches/CDNs can serve a response with the wrong CORS origin to another client.

Suggested patch
 		if h.options.OriginURL == "*" {
 			acao_url = req.Header.Get("Origin")
+			w.Header().Add("Vary", "Origin")
 		} else {
 			acao_url = h.options.OriginURL
 		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if h.options.OriginURL == "*" {
acao_url = req.Header.Get("Origin")
} else {
acao_url = h.options.OriginURL
}
// Set CORS headers for the preflight request
if req.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Origin", h.options.OriginURL)
w.Header().Set("Access-Control-Allow-Origin", acao_url)
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
w.WriteHeader(http.StatusNoContent)
return
}
w.Header().Set("Access-Control-Allow-Origin", h.options.OriginURL)
w.Header().Set("Access-Control-Allow-Origin", acao_url)
w.Header().Set("Access-Control-Allow-Credentials", "true")
if h.options.OriginURL == "*" {
acao_url = req.Header.Get("Origin")
w.Header().Add("Vary", "Origin")
} else {
acao_url = h.options.OriginURL
}
// Set CORS headers for the preflight request
if req.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Origin", acao_url)
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
w.WriteHeader(http.StatusNoContent)
return
}
w.Header().Set("Access-Control-Allow-Origin", acao_url)
w.Header().Set("Access-Control-Allow-Credentials", "true")
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/server/http_server.go` around lines 492 - 507, The CORS handling sets
Access-Control-Allow-Origin dynamically when h.options.OriginURL == "*"
(computed into acao_url from req.Header.Get("Origin")) but does not set the Vary
header; add w.Header().Add("Vary", "Origin") whenever
Access-Control-Allow-Origin is determined from the request (i.e., in the branch
where h.options.OriginURL == "*") so both the preflight (req.Method ==
http.MethodOptions) and normal response paths include Vary: Origin to prevent
incorrect cached responses.

w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
next.ServeHTTP(w, req)
Expand Down
Loading