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

use exact matching of allowed domain entries, issue #489 #493

Merged
merged 5 commits into from Jun 6, 2022
Merged

Conversation

emicklei
Copy link
Owner

No description provided.

@codecov-commenter
Copy link

codecov-commenter commented Apr 30, 2022

Codecov Report

Merging #493 (9e49191) into v3 (2326c8e) will decrease coverage by 0.43%.
The diff coverage is 54.87%.

@@            Coverage Diff             @@
##               v3     #493      +/-   ##
==========================================
- Coverage   70.07%   69.63%   -0.44%     
==========================================
  Files          26       27       +1     
  Lines        1223     1581     +358     
==========================================
+ Hits          857     1101     +244     
- Misses        293      409     +116     
+ Partials       73       71       -2     
Impacted Files Coverage Δ
extensions.go 0.00% <0.00%> (ø)
filter.go 54.54% <ø> (-1.02%) ⬇️
route.go 83.58% <0.00%> (+0.24%) ⬆️
parameter.go 43.52% <25.00%> (-15.10%) ⬇️
container.go 75.43% <87.50%> (+2.42%) ⬆️
compress.go 77.96% <100.00%> (-1.21%) ⬇️
cors_filter.go 63.44% <100.00%> (+7.88%) ⬆️
route_builder.go 71.25% <100.00%> (-0.29%) ⬇️
web_service.go 68.06% <100.00%> (-0.99%) ⬇️
... and 25 more

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@crenshaw-dev
Copy link

The docs should be updated to indicate that each allowed domain isn't a potential regexp pattern, it is a regexp pattern. So ^some.example.com$ matches somexexample.com. Even after the PR, ^some\.example\.com matches some.example.com.org.

@emicklei
Copy link
Owner Author

@JamieSlome is your hunt issue resolved by this PR?

cors_filter.go Outdated
AllowedDomains []string // list of allowed values for Http Origin. An allowed value can be a regular expression to support subdomain matching. If empty all are allowed.
// AllowedDomains list of allowed values for Http Origin.
// An allowed value can be a regular expression to support subdomain matching.
// Non-regular expression values will be changed into an exact match: ^yourdomain.com$

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure ^yourdomain.com$ is an intuitive "exact match". The . is still a wildcard. You'd have to do fmt.Sprintf("^%s$", regexp.QuoteMeta(each)) to get an "exact match".

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. ^www.amazon.com$ still matches wwwqamazon.com and www.amazonqcom.

cors_filter.go Outdated
AllowedDomains []string // list of allowed values for Http Origin. An allowed value can be a regular expression to support subdomain matching. If empty all are allowed.
// AllowedDomains list of allowed values for Http Origin.
// An allowed value can be a regular expression to support subdomain matching.
// Non-regular expression values will be changed into an exact match: ^yourdomain.com$

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me from this what qualifies as "non-regular expression". example.com and .* and example\.com are all regular expressions, even though they don't start with ^ or end with $.

@JamieSlome
Copy link

@emicklei:

Just (cc) @sickcodes and @ktg9 to see if they think this addresses the reported issue 👍

@steven-collins-omega
Copy link

@emicklei if I'm reading the code correctly, this just simply isn't going to work in practice in most cases. isOriginAllowed() is called on the literal value of the Origin header, which looks something like https://some.example.com:8080. Technically the documentation does say that AllowedDomains is a "list of allowed values for Http Origin", which is what it is. However, given the name AllowedDomains and the cors_filter test code, a library user would be likely to interpret it as a list of allowed domains. Such a user will find, after this proposed change, that nothing is allowed anymore. ^some.example.com$ doesn't match https://some.example.com:8080.

@emicklei
Copy link
Owner Author

@steven-collins-omega thank you for commenting on this. I agree that it is not correct and confusing. I will give it second time to come up with a solution

@emicklei
Copy link
Owner Author

emicklei commented May 25, 2022

hi all, I am thinking about changing the behaviour of CrossOriginResourceSharing:

  • AllowedDomains will have entries that must include the origin value from the request by string comparing.
  • add the AllowedDomainFunc field which can be set with a function takes takes the origin and returns a bool for a match. This allows the developer to define his/her own logic.

cors_filter.go Outdated
if domain == origin {
allowed = true
break
if domain == ".*" || domain == origin {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should domain and origin be lowercased before comparison?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i think this should happen. RFC4343: Domain Name System (DNS) names are "case insensitive".

@emicklei emicklei merged commit fd3c327 into v3 Jun 6, 2022
L3n41c pushed a commit to L3n41c/go-restful that referenced this pull request Jul 11, 2022
…cklei#493)

* use exact matching of allowed domain entries, issue emicklei#489

* update doc, add testcases from PR conversation

* introduce AllowedDomainFunc emicklei#489

* more tests, fix doc

* lowercase origin before checking cors
emicklei added a commit that referenced this pull request Jul 11, 2022
* use exact matching of allowed domain entries, issue #489

* update doc, add testcases from PR conversation

* introduce AllowedDomainFunc #489

* more tests, fix doc

* lowercase origin before checking cors

Co-authored-by: Ernest Micklei <ernest.micklei@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants