Skip to content

Commit

Permalink
Make CORS safer by not reflecting origin for *
Browse files Browse the repository at this point in the history
This seems like it's small and nit-picky, but I think it
is pretty unexpected for someone using this to set a policy
of ACAO * and have the Origin reflected.
  • Loading branch information
Evan Johnson committed May 7, 2017
1 parent a090f6d commit cc1cf75
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion caddy/corsPlugin.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package caddy

import (
"github.com/captncraig/cors"
"net/http"
"strconv"
"strings"

"github.com/captncraig/cors"

"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
Expand Down
6 changes: 5 additions & 1 deletion cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ func (c *Config) HandleRequest(w http.ResponseWriter, r *http.Request) {
//check origin against allowed origins
for _, ao := range c.AllowedOrigins {
if ao == "*" || ao == requestOrigin {
w.Header().Set(allowOriginKey, requestOrigin)
responseOrigin := "*"
if ao != "*" {
responseOrigin = requestOrigin
}
w.Header().Set(allowOriginKey, responseOrigin)
w.Header().Add(varyKey, originKey)
break
}
Expand Down
4 changes: 2 additions & 2 deletions cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func TestDefault_Origin_Supplied(t *testing.T) {
w, r := getReq("OPTIONS")
r.Header.Set(originKey, "http://bar.com")
Default().HandleRequest(w, r)
if w.Header().Get(allowOriginKey) != "http://bar.com" {
t.Fatalf("Expect origin of \"http://bar.com\". Got \"%s\".", w.Header().Get(allowOriginKey))
if w.Header().Get(allowOriginKey) != "*" {
t.Fatalf("Expect origin of \"*\". Got \"%s\".", w.Header().Get(allowOriginKey))
}
if w.Header().Get(varyKey) != "Origin" {
t.Fatal("Must include Vary:Origin if allow origin header set to specific domain.")
Expand Down

0 comments on commit cc1cf75

Please sign in to comment.