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

refactor: cleanup the code for CORS handling #1959

Merged
merged 3 commits into from
Aug 2, 2020
Merged

Conversation

harsimranmaan
Copy link
Contributor

@harsimranmaan harsimranmaan commented Jul 21, 2020

Related issue

#1754

Proposed changes

This change was originally intended to fix the issue described in the bug. However, I am not able to repro it as shown in the newly added test.

Checklist

  • I have read the contributing guidelines.
  • I have read the security policy.
  • I confirm that this pull request does not address a security
    vulnerability. If this pull request addresses a security. vulnerability, I
    confirm that I got green light (please contact
    security@ory.sh) from the maintainers to push
    the changes.
  • I have added tests that prove my fix is effective or that my feature
    works.
  • [N/A] I have added or changed the documentation.

Further comments

I removed many extra loops for validating the origin against the CORS allowlist. The viper config was shared across test cases causing dependency on the order of the test cases. It also hid some assumptions about the setup.

@harsimranmaan harsimranmaan changed the title Cleanup the code for CORS handling refactor: cleanup the code for CORS handling Jul 21, 2020
Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

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

Thank you, and sorry for the super late review - this got lost in my inbox. The changes look good to me, I think I also identified a test case which should reproduce #1754. Would you be open to continue working on that? I promise that my next review will be much faster ;)

code: http.StatusNotImplemented,
header: http.Header{"Origin": {"http://client-app.example.com"}, "Authorization": {"Basic Zm9vLTU6YmFy"}},
expectHeader: http.Header{"Access-Control-Allow-Credentials": []string{"true"}, "Access-Control-Allow-Origin": []string{"http://client-app.example.com"}, "Access-Control-Expose-Headers": []string{"Content-Type"}, "Vary": []string{"Origin"}},
},
Copy link
Member

Choose a reason for hiding this comment

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

I think the bug that was reported would be reproduced like this (checking against the origin domain, not the server one):


 		{
 			d: "should accept when basic auth client exists and origin is set at the client as well as the server",
 			prep: func() {
 				viper.Set("serve.public.cors.enabled", true)
 				viper.Set("serve.public.cors.allowed_origins", []string{"http://**.example.com"})
 				r.ClientManager().CreateClient(context.Background(), &client.Client{ID: "foo8", Secret: "bar", AllowedCORSOrigins: []string{"http://myapp.example.biz"}})
 			},
 			code:         http.StatusNotImplemented,
 			header:       http.Header{"Origin": {"http://myapp.example.biz"}, "Authorization": {"Basic Zm9vLTU6YmFy"}},
 			expectHeader: http.Header{"Access-Control-Allow-Credentials": []string{"true"}, "Access-Control-Allow-Origin": []string{"http://client-app.example.com"}, "Access-Control-Expose-Headers": []string{"Content-Type"}, "Vary": []string{"Origin"}},
 		},

Copy link
Contributor Author

@harsimranmaan harsimranmaan Jul 31, 2020

Choose a reason for hiding this comment

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

Lemme give it a try. I expect the Allow-Origin to be set to http://myapp.example.biz.

Copy link
Contributor Author

@harsimranmaan harsimranmaan Jul 31, 2020

Choose a reason for hiding this comment

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

@aeneasr I could not repro the defect. Added the new test-case. Also refactored auth headers(in test cases) for readability.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, in that case I don't think there's any more to do! And you are correct, the expected value was a copy/paste mistake on my end!

@harsimranmaan harsimranmaan force-pushed the cors branch 2 times, most recently from 764a7bd to 7afe15d Compare July 31, 2020 17:59
@aeneasr
Copy link
Member

aeneasr commented Aug 2, 2020

Awesome 🎉

Thank you for your contribution!

@aeneasr aeneasr merged commit 5a53d28 into ory:master Aug 2, 2020
@harsimranmaan harsimranmaan deleted the cors branch August 8, 2020 00:42
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.

2 participants