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

Generate escaped regexes for cors config #16204

Merged
merged 1 commit into from Sep 15, 2017
Merged

Generate escaped regexes for cors config #16204

merged 1 commit into from Sep 15, 2017

Conversation

liggitt
Copy link
Contributor

@liggitt liggitt commented Sep 7, 2017

this ensures the generated config matches hosts exactly instead of treating . like a "match any character" rule

@openshift-ci-robot openshift-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Sep 7, 2017
@openshift-merge-robot openshift-merge-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 7, 2017
@liggitt liggitt assigned spadgett, enj and simo5 and unassigned soltysh and csrwng Sep 7, 2017
@liggitt
Copy link
Contributor Author

liggitt commented Sep 7, 2017

cc @openshift/sig-security
cc @spadgett

Copy link
Contributor

@enj enj left a comment

Choose a reason for hiding this comment

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

Comment about func. Otherwise LGTM.

@@ -149,7 +150,15 @@ func (args MasterArgs) BuildSerializeableMasterConfig() (*configapi.MasterConfig
// always include localhost as an allowed CORS origin
// always include master public address as an allowed CORS origin
corsAllowedOrigins := sets.NewString(args.CORSAllowedOrigins...)
corsAllowedOrigins.Insert(assetPublicAddr.Host, masterPublicAddr.Host, "localhost", "127.0.0.1")
matchLiteralHost := func(s string) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why an inline function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no reason, fixed

@spadgett
Copy link
Member

spadgett commented Sep 8, 2017

How is this list used by the API server?

It looks to me like the Access-Control-Allow-Origin header itself doesn't allow regular expression. It's either a list of domains or a single wildcard *

https://www.w3.org/TR/cors/#access-control-allow-origin-response-header

@liggitt
Copy link
Contributor Author

liggitt commented Sep 8, 2017

How is this list used by the API server?

It tries to match the incoming Origin header against one of these regexes, and if one matches, echoes the Origin header back as Access-Control-Allow-Origin:

origin := req.Header.Get("Origin")
if origin != "" {
allowed := false
for _, re := range allowedOriginPatternsREs {
if allowed = re.MatchString(origin); allowed {
break
}
}
if allowed {
w.Header().Set("Access-Control-Allow-Origin", origin)

makeExactMatchRegex(assetPublicAddr.Host),
makeExactMatchRegex(masterPublicAddr.Host),
makeExactMatchRegex("localhost"),
makeExactMatchRegex("127.0.0.1"),
Copy link
Member

Choose a reason for hiding this comment

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

It looks like this will now block origins like https://localhost:9000? If so, I wonder if it's still worth settings these as defaults.

Copy link
Contributor

Choose a reason for hiding this comment

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

If we trust localhost, I assume we trust it on any port and protocol?

Copy link
Member

Choose a reason for hiding this comment

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

Do assetPublicAddres.Host and masterPublicAddres.Host include scheme and port as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm, yeah... need to rethink the scheme/port bit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, pinned to the beginning of the host with // and the end of the host with (:|$) or $, depending on whether host already contained a port.

This results in output like this:

corsAllowedOrigins:
- //127\.0\.0\.1(:|$)
- //192\.168\.1\.101:8443$
- //localhost(:|$)

@spadgett, are you able to check if that works across browsers for the cross-domain case?

@liggitt
Copy link
Contributor Author

liggitt commented Sep 12, 2017

/retest

@spadgett
Copy link
Member

@liggitt Built and tested the cross-domain case with Chrome, Firefox, Safari, and IE 11. It works in all browsers. I also checked using curl that substituting another character for . does not add the CORS headers.

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Sep 13, 2017
@openshift-merge-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: liggitt, spadgett

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

4 similar comments
@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@soltysh
Copy link
Member

soltysh commented Sep 15, 2017

/retest

@openshift-merge-robot
Copy link
Contributor

Automatic merge from submit-queue

@openshift-merge-robot openshift-merge-robot merged commit 169bd66 into openshift:master Sep 15, 2017
@liggitt liggitt deleted the cors-escaping branch September 21, 2017 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants