-
Notifications
You must be signed in to change notification settings - Fork 108
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
feat(configx): allow exceptions in immutables #713
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,53 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package corsx | ||
|
||
import "strings" | ||
|
||
// CheckOrigin is a function that can be used well with cors.Options.AllowOriginRequestFunc. | ||
// It checks whether the origin is allowed following the same behavior as github.com/rs/cors. | ||
// | ||
// Recommended usage for hot-reloadable origins: | ||
// | ||
// func (p *Config) cors(ctx context.Context, prefix string) (cors.Options, bool) { | ||
// opts, enabled := p.GetProvider(ctx).CORS(prefix, cors.Options{ | ||
// AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE"}, | ||
// AllowedHeaders: []string{"Authorization", "Content-Type", "Cookie"}, | ||
// ExposedHeaders: []string{"Content-Type", "Set-Cookie"}, | ||
// AllowCredentials: true, | ||
// }) | ||
// opts.AllowOriginRequestFunc = func(r *http.Request, origin string) bool { | ||
// // load the origins from the config on every request to allow hot-reloading | ||
// allowedOrigins := p.GetProvider(r.Context()).Strings(prefix + ".cors.allowed_origins") | ||
// return corsx.CheckOrigin(allowedOrigins, origin) | ||
// } | ||
// return opts, enabled | ||
// } | ||
func CheckOrigin(allowedOrigins []string, origin string) bool { | ||
if len(allowedOrigins) == 0 { | ||
return true | ||
} | ||
for _, o := range allowedOrigins { | ||
if o == "*" { | ||
// allow all origins | ||
return true | ||
} | ||
// Note: for origins and methods matching, the spec requires a case-sensitive matching. | ||
// As it may be error-prone, we chose to ignore the spec here. | ||
// https://github.com/rs/cors/blob/066574eebbd0f5f1b6cd1154a160cc292ac1835e/cors.go#L132-L133 | ||
prefix, suffix, found := strings.Cut(strings.ToLower(o), "*") | ||
if !found { | ||
// not a pattern, check for equality | ||
if o == origin { | ||
return true | ||
} | ||
continue | ||
} | ||
// inspired by https://github.com/rs/cors/blob/066574eebbd0f5f1b6cd1154a160cc292ac1835e/utils.go#L15 | ||
if len(origin) >= len(prefix)+len(suffix) && strings.HasPrefix(origin, prefix) && strings.HasSuffix(origin, suffix) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,111 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package corsx | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/rs/cors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCheckOrigin(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
allowedOrigins []string | ||
expect, expectOther bool | ||
}{ | ||
{ | ||
name: "empty", | ||
allowedOrigins: []string{}, | ||
expect: true, | ||
expectOther: true, | ||
}, | ||
{ | ||
name: "wildcard", | ||
allowedOrigins: []string{"https://example.com", "*"}, | ||
expect: true, | ||
expectOther: true, | ||
}, | ||
{ | ||
name: "exact", | ||
allowedOrigins: []string{"https://www.ory.sh"}, | ||
expect: true, | ||
}, | ||
{ | ||
name: "wildcard in the beginning", | ||
allowedOrigins: []string{"*.ory.sh"}, | ||
expect: true, | ||
}, | ||
{ | ||
name: "wildcard in the middle", | ||
allowedOrigins: []string{"https://*.ory.sh"}, | ||
expect: true, | ||
}, | ||
{ | ||
name: "wildcard in the end", | ||
allowedOrigins: []string{"https://www.ory.*"}, | ||
expect: true, | ||
}, | ||
{ | ||
name: "second wildcard is ignored", | ||
allowedOrigins: []string{"https://*.ory.*"}, | ||
expect: false, | ||
}, | ||
{ | ||
name: "multiple exact", | ||
allowedOrigins: []string{"https://example.com", "https://www.ory.sh"}, | ||
expect: true, | ||
}, | ||
{ | ||
name: "multiple wildcard", | ||
allowedOrigins: []string{"https://*.example.com", "https://*.ory.sh"}, | ||
expect: true, | ||
}, | ||
{ | ||
name: "wildcard and exact origins 1", | ||
allowedOrigins: []string{"https://*.example.com", "https://www.ory.sh"}, | ||
expect: true, | ||
}, | ||
{ | ||
name: "wildcard and exact origins 2", | ||
allowedOrigins: []string{"https://example.com", "https://*.ory.sh"}, | ||
expect: true, | ||
}, | ||
{ | ||
name: "multiple unrelated exact", | ||
allowedOrigins: []string{"https://example.com", "https://example.org"}, | ||
expect: false, | ||
}, | ||
{ | ||
name: "multiple unrelated with wildcard", | ||
allowedOrigins: []string{"https://*.example.com", "https://*.example.org"}, | ||
expect: false, | ||
}, | ||
{ | ||
name: "uppercase exact", | ||
allowedOrigins: []string{"https://www.ORY.sh"}, | ||
expect: true, | ||
}, | ||
{ | ||
name: "uppercase wildcard", | ||
allowedOrigins: []string{"https://*.ORY.sh"}, | ||
expect: true, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
assert.Equal(t, tc.expect, CheckOrigin(tc.allowedOrigins, "https://www.ory.sh")) | ||
|
||
assert.Equal(t, tc.expectOther, CheckOrigin(tc.allowedOrigins, "https://google.com")) | ||
|
||
// check for consistency with rs/cors | ||
assert.Equal(t, tc.expect, cors.New(cors.Options{AllowedOrigins: tc.allowedOrigins}). | ||
OriginAllowed(&http.Request{Header: http.Header{"Origin": []string{"https://www.ory.sh"}}})) | ||
|
||
assert.Equal(t, tc.expectOther, cors.New(cors.Options{AllowedOrigins: tc.allowedOrigins}). | ||
OriginAllowed(&http.Request{Header: http.Header{"Origin": []string{"https://google.com"}}})) | ||
}) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with this was that the config has to come from the config provider, but where you initialize the middleware you have no access to the config.