This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add proper CORS handling by the HTTP API
A flag can be set by the user to specify the allowed origins to access the HTTP API. A wrapper handler sets the proper CORS headers if the flag is enabled.
- Loading branch information
Showing
14 changed files
with
226 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"github.com/timescale/timescale-prometheus/pkg/log" | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestCORSWrapper(t *testing.T) { | ||
_ = log.Init("debug") | ||
acceptSpecific, _ := regexp.Compile("^(?:" + "http://some-site.com" + ")$") | ||
acceptAny, _ := regexp.Compile("^(?:" + ".*" + ")$") | ||
|
||
testCases := []struct { | ||
name string | ||
requestOrigin string | ||
acceptedOrigin *regexp.Regexp | ||
expectHeaders map[string][]string | ||
}{ | ||
{ | ||
name: "No origin", | ||
requestOrigin: "", | ||
acceptedOrigin: acceptSpecific, | ||
expectHeaders: map[string][]string{}, | ||
}, { | ||
name: "Origin doesn't match accepted", | ||
requestOrigin: "http://some-unknown-site.com", | ||
acceptedOrigin: acceptSpecific, | ||
expectHeaders: map[string][]string{ | ||
"Access-Control-Allow-Headers": {"Accept, Authorization, Content-Type, Origin"}, | ||
"Access-Control-Allow-Methods": {"GET, POST, OPTIONS"}, | ||
"Access-Control-Expose-Headers": {"Date"}, | ||
"Vary": {"Origin"}, | ||
}, | ||
}, | ||
{ | ||
name: "Origin matches accepted", | ||
requestOrigin: "http://some-site.com", | ||
acceptedOrigin: acceptSpecific, | ||
expectHeaders: map[string][]string{ | ||
"Access-Control-Allow-Headers": {"Accept, Authorization, Content-Type, Origin"}, | ||
"Access-Control-Allow-Methods": {"GET, POST, OPTIONS"}, | ||
"Access-Control-Expose-Headers": {"Date"}, | ||
"Access-Control-Allow-Origin": {"http://some-site.com"}, | ||
"Vary": {"Origin"}, | ||
}, | ||
}, { | ||
name: "Wildcard allowed origin", | ||
requestOrigin: "http://any-site.com", | ||
acceptedOrigin: acceptAny, | ||
expectHeaders: map[string][]string{ | ||
"Access-Control-Allow-Headers": {"Accept, Authorization, Content-Type, Origin"}, | ||
"Access-Control-Allow-Methods": {"GET, POST, OPTIONS"}, | ||
"Access-Control-Expose-Headers": {"Date"}, | ||
"Access-Control-Allow-Origin": {"*"}, | ||
"Vary": {"Origin"}, | ||
}, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
conf := &Config{} | ||
if tc.acceptedOrigin != nil { | ||
conf.AllowedOrigin = tc.acceptedOrigin | ||
} else { | ||
tc.acceptedOrigin = ®exp.Regexp{} | ||
} | ||
internalHandlerCalled := false | ||
handler := corsWrapper(conf, func(http.ResponseWriter, *http.Request) { | ||
internalHandlerCalled = true | ||
}) | ||
w := doCORSWrapperRequest(t, handler, "http://localhost/", tc.requestOrigin) | ||
if !internalHandlerCalled { | ||
t.Fatalf("internal handler not called by CORS wrapper") | ||
return | ||
} | ||
returnedHeaders := w.Header() | ||
if len(returnedHeaders) != len(tc.expectHeaders) { | ||
t.Fatalf("expected %d headers, got %d", len(tc.expectHeaders), len(returnedHeaders)) | ||
return | ||
} | ||
for hName, hValues := range tc.expectHeaders { | ||
returnedValues := returnedHeaders[hName] | ||
if !reflect.DeepEqual(hValues, returnedValues) { | ||
t.Errorf("expected header %s with value %v; got %v", hName, hValues, returnedValues) | ||
} | ||
} | ||
}) | ||
|
||
} | ||
|
||
} | ||
|
||
func doCORSWrapperRequest(t *testing.T, queryHandler http.Handler, url, origin string) *httptest.ResponseRecorder { | ||
req, err := http.NewRequestWithContext(context.Background(), "GET", url, nil) | ||
if err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
|
||
req.Header.Set("Origin", origin) | ||
w := httptest.NewRecorder() | ||
queryHandler.ServeHTTP(w, req) | ||
return w | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.