Skip to content

Commit 3a53812

Browse files
authored
Fix Origin header check (#38)
* Check for Origin header - it's mandatory * Allow null/empty origin header
1 parent f8fbaee commit 3a53812

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

cors.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
//
44
// You can configure it by passing an option struct to cors.New:
55
//
6-
// c := cors.New(cors.Options{
7-
// AllowedOrigins: []string{"foo.com"},
8-
// AllowedMethods: []string{"GET", "POST", "DELETE"},
9-
// AllowCredentials: true,
10-
// })
6+
// c := cors.New(cors.Options{
7+
// AllowedOrigins: []string{"foo.com"},
8+
// AllowedMethods: []string{"GET", "POST", "DELETE"},
9+
// AllowCredentials: true,
10+
// })
1111
//
1212
// Then insert the handler in the chain:
1313
//
14-
// handler = c.Handler(handler)
14+
// handler = c.Handler(handler)
1515
//
1616
// See Options documentation for more options.
1717
//
@@ -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") != "" {
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
@@ -291,14 +290,17 @@ func (c *Cors) handlePreflight(w http.ResponseWriter, r *http.Request) {
291290
// handleActualRequest handles simple cross-origin requests, actual request or redirects
292291
func (c *Cors) handleActualRequest(w http.ResponseWriter, r *http.Request) {
293292
headers := w.Header()
294-
origin := r.Header.Get("Origin")
293+
// null Origin header value is acceptable and it is considered having that header
294+
_, hasOriginHeader := r.Header["Origin"]
295295

296296
// Always set Vary, see https://github.com/rs/cors/issues/10
297297
headers.Add("Vary", "Origin")
298-
if origin == "" {
298+
299+
if !hasOriginHeader {
299300
c.logf("Actual request no headers added: missing origin")
300301
return
301302
}
303+
origin := r.Header.Get("Origin")
302304
if !c.isOriginAllowed(r, origin) {
303305
c.logf("Actual request no headers added: origin '%s' not allowed", origin)
304306
return

0 commit comments

Comments
 (0)