Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pre-parsed headers #35

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
//
// You can configure it by passing an option struct to cors.New:
//
// c := cors.New(cors.Options{
// AllowedOrigins: []string{"foo.com"},
// AllowedMethods: []string{"GET", "POST", "DELETE"},
// AllowCredentials: true,
// })
// c := cors.New(cors.Options{
// AllowedOrigins: []string{"foo.com"},
// AllowedMethods: []string{"GET", "POST", "DELETE"},
// AllowCredentials: true,
// })
//
// Then insert the handler in the chain:
//
// handler = c.Handler(handler)
// handler = c.Handler(handler)
//
// See Options documentation for more options.
//
Expand Down Expand Up @@ -260,7 +260,23 @@ func (c *Cors) handlePreflight(w http.ResponseWriter, r *http.Request) {
c.logf("Preflight aborted: method '%s' not allowed", reqMethod)
return
}
reqHeaders := parseHeaderList(r.Header.Get("Access-Control-Request-Headers"))

// We need to call Values func instead of Get because the header might
// be parsed/split before it reaches the handler. Calling "Get" will only
// return the first value This guarantees that we get all values of
// the header and maintains the original functionality.
headerVals := r.Header.Values("Access-Control-Request-Headers")

c.logf("Preflight access control request headers: %v", headerVals)

var reqHeaders []string

// loop over the header values and parse them; this will allow us to handle
// the case where it's a single comma separated header or multiple headers
for _, headerVal := range headerVals {
parsed := parseHeaderList(headerVal)
reqHeaders = append(reqHeaders, parsed...)
}
if !c.areHeadersAllowed(reqHeaders) {
c.logf("Preflight aborted: headers '%v' not allowed", reqHeaders)
return
Expand Down
81 changes: 76 additions & 5 deletions cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func assertHeaders(t *testing.T, resHeaders http.Header, expHeaders map[string]s
}
}

//lint:ignore U1000 This is a test helper function.
func assertResponse(t *testing.T, res *httptest.ResponseRecorder, responseCode int) {
if responseCode != res.Code {
t.Errorf("assertResponse: expected response code to be %d but got %d. ", responseCode, res.Code)
Expand All @@ -40,11 +41,12 @@ func assertResponse(t *testing.T, res *httptest.ResponseRecorder, responseCode i

func TestSpec(t *testing.T) {
cases := []struct {
name string
options Options
method string
reqHeaders map[string]string
resHeaders map[string]string
name string
options Options
method string
reqHeaders map[string]string
resHeaders map[string]string
preSplitHeaders bool
}{
{
"NoConfig",
Expand All @@ -56,6 +58,7 @@ func TestSpec(t *testing.T) {
map[string]string{
"Vary": "Origin",
},
false,
},
{
"MatchAllOrigin",
Expand All @@ -70,6 +73,7 @@ func TestSpec(t *testing.T) {
"Vary": "Origin",
"Access-Control-Allow-Origin": "*",
},
false,
},
{
"MatchAllOriginWithCredentials",
Expand All @@ -86,6 +90,7 @@ func TestSpec(t *testing.T) {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
},
false,
},
{
"AllowedOrigin",
Expand All @@ -100,6 +105,7 @@ func TestSpec(t *testing.T) {
"Vary": "Origin",
"Access-Control-Allow-Origin": "http://foobar.com",
},
false,
},
{
"WildcardOrigin",
Expand All @@ -114,6 +120,7 @@ func TestSpec(t *testing.T) {
"Vary": "Origin",
"Access-Control-Allow-Origin": "http://foo.bar.com",
},
false,
},
{
"DisallowedOrigin",
Expand All @@ -127,6 +134,7 @@ func TestSpec(t *testing.T) {
map[string]string{
"Vary": "Origin",
},
false,
},
{
"DisallowedWildcardOrigin",
Expand All @@ -140,6 +148,7 @@ func TestSpec(t *testing.T) {
map[string]string{
"Vary": "Origin",
},
false,
},
{
"AllowedOriginFuncMatch",
Expand All @@ -157,6 +166,7 @@ func TestSpec(t *testing.T) {
"Vary": "Origin",
"Access-Control-Allow-Origin": "http://foobar.com",
},
false,
},
{
"AllowOriginFuncNotMatch",
Expand All @@ -173,6 +183,7 @@ func TestSpec(t *testing.T) {
map[string]string{
"Vary": "Origin",
},
false,
},
{
"MaxAge",
Expand All @@ -192,6 +203,7 @@ func TestSpec(t *testing.T) {
"Access-Control-Allow-Methods": "GET",
"Access-Control-Max-Age": "10",
},
false,
},
{
"AllowedMethod",
Expand All @@ -209,6 +221,7 @@ func TestSpec(t *testing.T) {
"Access-Control-Allow-Origin": "http://foobar.com",
"Access-Control-Allow-Methods": "PUT",
},
false,
},
{
"DisallowedMethod",
Expand All @@ -224,6 +237,7 @@ func TestSpec(t *testing.T) {
map[string]string{
"Vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers",
},
false,
},
{
"AllowedHeaders",
Expand All @@ -243,6 +257,47 @@ func TestSpec(t *testing.T) {
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "X-Header-2, X-Header-1",
},
false,
},
{
"AllowedHeadersNoSpacesLowercase",
Options{
AllowedOrigins: []string{"http://foobar.com"},
AllowedHeaders: []string{"X-Header-1", "x-header-2"},
},
"OPTIONS",
map[string]string{
"Origin": "http://foobar.com",
"Access-Control-Request-Method": "GET",
"Access-Control-Request-Headers": "x-header-2,x-header-1",
},
map[string]string{
"Vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers",
"Access-Control-Allow-Origin": "http://foobar.com",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "X-Header-2, X-Header-1",
},
false,
},
{
"AllowedHeadersNoSpacesLowercasePreParsed",
Options{
AllowedOrigins: []string{"http://foobar.com"},
AllowedHeaders: []string{"X-Header-1", "x-header-2"},
},
"OPTIONS",
map[string]string{
"Origin": "http://foobar.com",
"Access-Control-Request-Method": "GET",
"Access-Control-Request-Headers": "x-header-2,x-header-1",
},
map[string]string{
"Vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers",
"Access-Control-Allow-Origin": "http://foobar.com",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "X-Header-2, X-Header-1",
},
true,
},
{
"DefaultAllowedHeaders",
Expand All @@ -262,6 +317,7 @@ func TestSpec(t *testing.T) {
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
},
false,
},
{
"AllowedWildcardHeader",
Expand All @@ -281,6 +337,7 @@ func TestSpec(t *testing.T) {
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "X-Header-2, X-Header-1",
},
false,
},
{
"DisallowedHeader",
Expand All @@ -297,6 +354,7 @@ func TestSpec(t *testing.T) {
map[string]string{
"Vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers",
},
false,
},
{
"OriginHeader",
Expand All @@ -315,6 +373,7 @@ func TestSpec(t *testing.T) {
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Origin",
},
false,
},
{
"ExposedHeader",
Expand All @@ -331,6 +390,7 @@ func TestSpec(t *testing.T) {
"Access-Control-Allow-Origin": "http://foobar.com",
"Access-Control-Expose-Headers": "X-Header-1, X-Header-2",
},
false,
},
{
"AllowedCredentials",
Expand All @@ -349,6 +409,7 @@ func TestSpec(t *testing.T) {
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Credentials": "true",
},
false,
},
{
"OptionPassthrough",
Expand All @@ -365,6 +426,7 @@ func TestSpec(t *testing.T) {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
},
false,
},
{
"NonPreflightOptions",
Expand All @@ -379,6 +441,7 @@ func TestSpec(t *testing.T) {
"Vary": "Origin",
"Access-Control-Allow-Origin": "http://foobar.com",
},
false,
},
}
for i := range cases {
Expand All @@ -388,6 +451,14 @@ func TestSpec(t *testing.T) {

req, _ := http.NewRequest(tc.method, "http://example.com/foo", nil)
for name, value := range tc.reqHeaders {
if strings.EqualFold(name, "Access-Control-Request-Headers") && tc.preSplitHeaders {
parsed := strings.Split(value, ",")
for _, v := range parsed {
req.Header.Add(strings.TrimSpace(name), v)
}
continue
}

req.Header.Add(name, value)
}

Expand Down