8000 chore(http): add a warning if multiple CORS headers are detected · symfony-cli/symfony-cli@ac66e13 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac66e13

Browse files
committed
chore(http): add a warning if multiple CORS headers are detected
1 parent 577e097 commit ac66e13

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

local/http/cors.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,26 @@ package http
2121

2222
import (
2323
"net/http"
24+
25+
"github.com/rs/zerolog"
2426
)
2527

26-
func corsWrapper(h http.Handler) http.Handler {
28+
func corsWrapper(h http.Handler, logger zerolog.Logger) http.Handler {
29+
var corsHeaders = []string{"Access-Control-Allow-Origin", "Access-Control-Allow-Methods", "Access-Control-Allow-Headers"}
30+
2731
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
28-
w.Header().Set("Access-Control-Allow-Origin", "*")
29-
w.Header().Set("Access-Control-Allow-Methods", "*")
30-
w.Header().Set("Access-Control-Allow-Headers", "*")
32+
for _, corsHeader := range corsHeaders {
33+
w.Header().Set(corsHeader, "*")
34+
}
3135

3236
h.ServeHTTP(w, r)
37+
38+
for _, corsHeader := range corsHeaders {
39+
if headers, exists := w.Header()[corsHeader]; !exists || len(headers) < 2 {
40+
continue
41+
}
42+
43+
logger.Warn().Msgf(`Multiple entries detected for header "%s". Only one should be set: you should enable CORS handling in the CLI only if the application does not handle them.`, corsHeader)
44+
}
3345
})
3446
}

local/http/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (s *Server) Start(errChan chan error) (int, error) {
100100
}
101101

102102
if s.AllowCORS {
103-
proxyHandler = corsWrapper(proxyHandler)
103+
proxyHandler = corsWrapper(proxyHandler, s.Logger)
104104
}
105105

106106
s.httpserver = &http.Server{

0 commit comments

Comments
 (0)
0