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

fix: backport fix for client specific CORS (#1754) #3163

Merged
merged 1 commit into from
Jun 20, 2022
Merged
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
6 changes: 6 additions & 0 deletions x/oauth2cors/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ func Middleware(reg interface {
}
}

// pre-flight requests do not contain credentials (cookies, HTTP authorization)
// so we return true in all cases here.
if r.Method == http.MethodOptions {
return true
}

username, _, ok := r.BasicAuth()
if !ok || username == "" {
token := fosite.AccessTokenFromRequest(r)
Expand Down
18 changes: 17 additions & 1 deletion x/oauth2cors/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestOAuth2AwareCORSMiddleware(t *testing.T) {
code int
header http.Header
expectHeader http.Header
method string
}{
{
d: "should ignore when disabled",
Expand Down Expand Up @@ -160,6 +161,17 @@ func TestOAuth2AwareCORSMiddleware(t *testing.T) {
header: http.Header{"Origin": {"http://foobar.com"}, "Authorization": {fmt.Sprintf("Basic %s", x.BasicAuth("foo-7", "bar"))}},
expectHeader: http.Header{"Access-Control-Allow-Credentials": []string{"true"}, "Access-Control-Allow-Origin": []string{"http://foobar.com"}, "Access-Control-Expose-Headers": []string{"Content-Type"}, "Vary": []string{"Origin"}},
},
{
d: "should succeed on pre-flight request when token introspection fails",
prep: func(t *testing.T, r driver.Registry) {
r.Config().MustSet("serve.public.cors.enabled", true)
r.Config().MustSet("serve.public.cors.allowed_origins", []string{"http://not-test-domain.com"})
},
code: http.StatusNotImplemented,
header: http.Header{"Origin": {"http://foobar.com"}, "Authorization": {"Bearer 1234"}},
expectHeader: http.Header{"Access-Control-Allow-Credentials": []string{"true"}, "Access-Control-Allow-Origin": []string{"http://foobar.com"}, "Access-Control-Expose-Headers": []string{"Content-Type"}, "Vary": []string{"Origin"}},
method: "OPTIONS",
},
{
d: "should fail when token introspection fails",
prep: func(t *testing.T, r driver.Registry) {
Expand Down Expand Up @@ -237,7 +249,11 @@ func TestOAuth2AwareCORSMiddleware(t *testing.T) {
tc.prep(t, r)
}

req, err := http.NewRequest("GET", "http://foobar.com/", nil)
method := "GET"
if tc.method != "" {
method = tc.method
}
req, err := http.NewRequest(method, "http://foobar.com/", nil)
require.NoError(t, err)
for k := range tc.header {
req.Header.Set(k, tc.header.Get(k))
Expand Down