Skip to content

Commit e84ea22

Browse files
ejcxrs
authored andcommitted
Fix * behavior to be standards compliant. (#57)
* Fix * behavior to be standards compliant. In section 6.1 of the CORS standard is talks about this exact situation. Even though you have built in a guard-rail in to the library which will print a nice warning, it is preferred to rely on the security already built in to the standard. When ACAO: * and ACAC: true are both specified the browser will refuse to make the request. Refer to the standard: https://www.w3.org/TR/cors/ * Update test to handle new behavior
1 parent 694cf2a commit e84ea22

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

cors.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,6 @@ func New(options Options) *Cors {
159159
c.allowedMethods = convert(options.AllowedMethods, strings.ToUpper)
160160
}
161161

162-
if c.allowedOriginsAll && c.allowCredentials {
163-
// See https://github.com/rs/cors/issues/55
164-
log.Print("[cors] WARNING: unsafe configuration: AllowOrigin * and AllowCredientials true combined")
165-
}
166-
167162
return c
168163
}
169164

@@ -274,7 +269,7 @@ func (c *Cors) handlePreflight(w http.ResponseWriter, r *http.Request) {
274269
c.logf(" Preflight aborted: headers '%v' not allowed", reqHeaders)
275270
return
276271
}
277-
if c.allowedOriginsAll && !c.allowCredentials {
272+
if c.allowedOriginsAll {
278273
headers.Set("Access-Control-Allow-Origin", "*")
279274
} else {
280275
headers.Set("Access-Control-Allow-Origin", origin)
@@ -326,7 +321,7 @@ func (c *Cors) handleActualRequest(w http.ResponseWriter, r *http.Request) {
326321

327322
return
328323
}
329-
if c.allowedOriginsAll && !c.allowCredentials {
324+
if c.allowedOriginsAll {
330325
headers.Set("Access-Control-Allow-Origin", "*")
331326
} else {
332327
headers.Set("Access-Control-Allow-Origin", origin)

cors_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestSpec(t *testing.T) {
8383
},
8484
map[string]string{
8585
"Vary": "Origin",
86-
"Access-Control-Allow-Origin": "http://foobar.com",
86+
"Access-Control-Allow-Origin": "*",
8787
"Access-Control-Allow-Credentials": "true",
8888
},
8989
},

0 commit comments

Comments
 (0)