Skip to content

Commit 5f89c00

Browse files
committed
Allow null/empty origin header
1 parent 58f953d commit 5f89c00

1 file changed

Lines changed: 5 additions & 9 deletions

File tree

cors.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ func AllowAll() *Cors {
210210
// as necessary.
211211
func (c *Cors) Handler(next http.Handler) http.Handler {
212212
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
213-
if r.Method == http.MethodOptions && r.Header.Get("Access-Control-Request-Method") != "" && r.Header.Get("Origin") != "" {
213+
// null or empty Origin header value is acceptable and it is considered having that header
214+
_, hasOriginHeader := r.Header["Origin"]
215+
216+
if r.Method == http.MethodOptions && r.Header.Get("Access-Control-Request-Method") != "" && hasOriginHeader {
214217
c.logf("Handler: Preflight request")
215218
c.handlePreflight(w, r)
216219
// Preflight requests are standalone and should stop the chain as some other
@@ -246,10 +249,6 @@ func (c *Cors) handlePreflight(w http.ResponseWriter, r *http.Request) {
246249
headers.Add("Vary", "Access-Control-Request-Method")
247250
headers.Add("Vary", "Access-Control-Request-Headers")
248251

249-
if origin == "" {
250-
c.logf("Preflight aborted: empty origin")
251-
return
252-
}
253252
if !c.isOriginAllowed(r, origin) {
254253
c.logf("Preflight aborted: origin '%s' not allowed", origin)
255254
return
@@ -295,10 +294,7 @@ func (c *Cors) handleActualRequest(w http.ResponseWriter, r *http.Request) {
295294

296295
// Always set Vary, see https://github.com/rs/cors/issues/10
297296
headers.Add("Vary", "Origin")
298-
if origin == "" {
299-
c.logf("Actual request no headers added: missing origin")
300-
return
301-
}
297+
302298
if !c.isOriginAllowed(r, origin) {
303299
c.logf("Actual request no headers added: origin '%s' not allowed", origin)
304300
return

0 commit comments

Comments
 (0)