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

Add CORS parameters to Cornice services #48

Merged
merged 4 commits into from
Oct 28, 2015
Merged

Add CORS parameters to Cornice services #48

merged 4 commits into from
Oct 28, 2015

Conversation

asaunier
Copy link
Member

Replace #47

Based upon
https://blog.mozilla.org/services/2013/02/04/implementing-cross-origin-resource-sharing-cors-for-cornice/
https://cornice.readthedocs.org/en/latest/api.html?highlight=cors

I have tried to add the cors_policy to the DocumentRest class directly but it failed, most likely because I had omitted the path params in the @resource decorator. Anyway it's probably better to have all the cornice params explicitly set for each service.

@asaunier
Copy link
Member Author

I have made a rebase to fix some conflicts with master. In the process it seems that @tsauerwein following comment is now less visible: 68e20c3#commitcomment-13926722

@tsauerwein
Copy link
Member

I pushed a commit to your branch making the origins configurable. Could you please test with that?

@asaunier
Copy link
Member Author

I have juste made a rebase origin/master to take your #50 changes into account and fixed a conflict. The rebase is pushed. The API works as expected. I have not had the opportunity to test with the UI calls, it's currently broken in my instance...

@asaunier
Copy link
Member Author

Thanks for the improvements by the way!

@asaunier
Copy link
Member Author

Well, I think there's a problem:
http://c2corgv6-demo.gis.internal:6545/waypoints/1?l=fr returns attributes as expected but I get the following error when calling the URL in Angular:

GET http://c2corgv6-demo.gis.internal:6545/waypoints/1?l=fr
XMLHttpRequest cannot load http://c2corgv6-demo.gis.internal:6545/waypoints/1?l=fr. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://c2corgv6-demo.gis.internal:6555' is therefore not allowed access. The response had HTTP status code 400.

By the way: I agree that we should avoid that anyone may write using the API, but that's probably wanted that anyone may read the documents (at least non user-related docs). For instance if a hut website would like to show the outings starting from the hut (not sure we want to add websites in the config in that case).

@asaunier
Copy link
Member Author

In addition shouldn't we set the cors-origins values in the config/* files rather than directly in common.ini.in?

@asaunier
Copy link
Member Author

At least when setting cors.origins to * in https://github.com/c2corg/v6_api/blob/cornice_cors/common.ini.in#L12 it seems to work :P

@tsauerwein
Copy link
Member

By the way: I agree that we should avoid that anyone may write using the API, but that's probably wanted that anyone may read the documents (at least non user-related docs). For instance if a hut website would like to show the outings starting from the hut (not sure we want to add websites in the config in that case).

So, let's use "*" for uncritical GET requests and a specific origin for POST/PUT and critical GET requests (like for the user data)?

In addition shouldn't we set the cors-origins values in the config/* files rather than directly in common.ini.in?

Oh yes, I originally wanted to that, but then forgot.

@asaunier
Copy link
Member Author

So, let's use "*" for uncritical GET requests and a specific origin for POST/PUT and critical GET requests (like for the user data)?

Makes sense indeed

@gberaudo
Copy link
Contributor

We discussed with @asaunier and it appears we should have two policies:

  • one for public data, restricted to GET method and allowed for *;
  • the other for the rest (restricted data), only allowed for c2corg and requiring credentials (JWT).

@@ -12,6 +12,12 @@
import json


cors_policy = dict(
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be renamed to cors_public_policy.

@tsauerwein
Copy link
Member

Yes, that's what I also hand in mind.

@asaunier
Copy link
Member Author

I have rebased this branch to get the last changes in master and fixed the conflicts.

cors.origins = c2corgv6-demo.gis.internal does not work on my instance on the dev server (got cross origin errors). I had to set it to cors.origins = *.

What about merging this PR (with cors.origins = *) and creating an issue to keep track of the required fix/improvements still to apply? We need this PR to make the UI work.

@asaunier
Copy link
Member Author

With @gberaudo we have figured out that the cors.origins should contain the port number as well as the URL scheme (http://). We may use * to allow whatever port and scheme. For instance

foo,*c2corgv6-demo.gis.internal:*,fdsfsdfs

works.
I will suggest changes to get the cors.origins from the makefile.

@asaunier
Copy link
Member Author

What the use of "internalurl" set in https://github.com/c2corg/v6_api/blob/cornice_cors/config/default#L11 and https://github.com/c2corg/v6_api/blob/cornice_cors/config/dev#L6 ?

It seems to be used nowhere else.
Should I drop it?

@tsauerwein
Copy link
Member

Looks good!

Will you open tickets for #48 (comment) ?

asaunier added a commit that referenced this pull request Oct 28, 2015
Add CORS parameters to Cornice services
@asaunier asaunier merged commit 0cb9774 into master Oct 28, 2015
@asaunier asaunier deleted the cornice_cors branch October 28, 2015 11:43
@asaunier
Copy link
Member Author

Looks good!

Thanks. By the way I have dropped "internalurl", I hope you have seen it.

Will you open tickets for #48 (comment) ?

Yes, see #54

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.

3 participants