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

Set arbitrary headers for APIG (will enable CORS) #70

Closed
amir-mehler opened this issue Jul 26, 2016 · 18 comments
Closed

Set arbitrary headers for APIG (will enable CORS) #70

amir-mehler opened this issue Jul 26, 2016 · 18 comments

Comments

@amir-mehler
Copy link

amir-mehler commented Jul 26, 2016

Currently I can define a resource like this:

@app.route('/scalars', methods=['GET', 'OPTIONS'])
def scalars():
return { 'mau': 27048, 'wau: 7003 }'

The OPTIONS will help me with enabling CORS in APIG But I'm still missing the 'Access-Control-Allow-Origin' header, so I enable it manually in the console after each deploy.

One approach would be to configure headers in the method.
Another would be to call the "Enable CORS" magic button in APIG.

WDYT?

@jamesls
Copy link
Member

jamesls commented Jul 28, 2016

I think having something similar to what the console has via "Enable CORS" would be great to add. Marking as a feature request.

@zaga1
Copy link

zaga1 commented Aug 1, 2016

Even if I go into AWS API Gateway console and select 'Access-Control-Allow-Origin' it still gives the error in the browser of not having the CORS header?

@beaucronin
Copy link

👍 very important for me as well to be able to enable CORS from chalice

@mlapida
Copy link

mlapida commented Aug 8, 2016

+1 - having the ability to enable CORS on deploy would be fantastic.

@zaga1 you'll need to enable CORS for each resource then deploy the API from the console.

@howardroark
Copy link

Is everyone enabling CORS in the console right now? Or is no one really building APIs meant for the browser with this? Would really like to try this, but don't have any server to server use cases.

@chrisgilmerproj
Copy link

Yes, this is how I'm doing this. It's a bit more manual than I'd like but it will do for now.

@jamesls
Copy link
Member

jamesls commented Sep 21, 2016

Taking a look now.

@jamesls
Copy link
Member

jamesls commented Sep 22, 2016

I've been playing around with this feature and I think there's a few use cases I can envision. Would like to hear what others think about this:

1. Enable CORS for a single route:

@app.route('/corsenabled', enable_cors=True)
def yescors():
    return {'hello': 'cors'}

This would mimic the behavior of the "Enable CORS" in the console. Would default to '*' for the allow origin header.

2. Enable CORs for the entire app. If you just want cors enabled for every view function, you can specify this when creating your app:

from chalice import Chalice

# enable_cors=True when creating the `Chalice` object.
app = Chalice(app_name='foo', enable_cors=True)

# Every route is enabled with CORs by default.
@app.route('/')
def yescors():
    return {'hello': 'cors'}


@app.route('/foo')
def foo():
    return {'hello': 'cors again'}

# Can still turn it off if you want:
@app.route('/no_cors', enable_cors=False)
def foo():
    return {'hello': 'cors again'}

3. Customized CORS configuration

from chalice import Chalice, CORSConfig

app = Chalice(app_name='foo')

cors = CORSConfig(allow_origin='http://foo', allow_headers=['list', 'of', 'headers'],
                  expose_headers=..., max_age=..., allow_credentials=...)

@app.route('/', cors_config=cors)
def foo():
  ...

Optionally, I could just use a single args, cors, which could be one of True|False|CORSConfig.

Would those three cases cover most people's usage?

@chrisgilmerproj
Copy link

I like the above. One thing that I'd want is to ensure that the CORS headers are on all the response codes, not just 200. Otherwise I'll still have to go in and update 400, 403, 404, etc.

@jamesls
Copy link
Member

jamesls commented Sep 22, 2016

Good point, I'll be sure to add this to all status codes.

@howardroark
Copy link

@jamesls Sounds like a good plan!

@DeviaVir
Copy link

@jamesls definitely looking good! I ran into this issue a while ago and was thinking about building this in. Any way I can help you, perhaps?

@jackrk
Copy link

jackrk commented Apr 20, 2017

The fact that CORS support here just automatically gives you '*' for allowed origins is a real problem. In the API Gateway console, there is a warning explaining what that means and it encourages the developer to use their own specific domain.

The above warning is not mentioned in the Chalice docs... do we trust every consumer to understand how CORS works and what Access-Control-Allow-Origin: '*' means?

CORS is just a best-intention spec anyway (you have to trust the browser), but I don't think CORS should have shipped without something like the cors_config suggested by @jamesls.

@jamesls
Copy link
Member

jamesls commented Apr 26, 2017

@jackrk thanks for the feedback. We're working on adding the CORSConfig option.

cc @stealthycoin

@stealthycoin
Copy link
Contributor

Implemented in #311

@dmulter
Copy link
Contributor

dmulter commented Jan 5, 2018

The CORS support is nice, but I see that the cors=True wasn't made part of Chalice instantiation. I have lots of routes and it would be nicer if I could just turn it on for everything. Not sure what others think.

@russelldavies
Copy link

@dmulter I would like to turn it on for everything too via passing an option during instantiation of the app.

@rahulakash
Copy link

@jamesls @stealthycoin: We see that the support is now added for 3. Customized CORS configuration. Is there a plan to add support for 2. Enable CORs for the entire app? It would be really helpful to enable cors for all the endpoints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests