aws / chalice

Python Serverless Microframework for AWS
Apache License 2.0
10.67k stars 1.01k forks source link

Middleware short circuit does not use app.api.cors setting #2059

Open nineclicks opened 1 year ago

nineclicks commented 1 year ago

chalice 1.29.0 Python 3.11.4 Windows 10.0.19045 Build 19045

from chalice import Chalice, Response

app = Chalice(app_name='chalice-test')
app.api.cors = True

@app.middleware('http')
def middle(event, get_response):
    return Response({'test': 123})

@app.route('/test')
def index():
    return {'hello': 'world'}

Without the middleware, /test returns headers

With the middleware function, these headers are not set. Expected behavior is that CORS headers would be set app-wide when using app.api.cors. I cannot find anything in the docs indicating that CORS should not be set by middleware.

kelvan commented 5 months ago

I have the same problem, using a middleware to validate the request and throwing BadRequestError if invalid. If the error is thrown cors header is not set leading to problems on client side.