-
-
Notifications
You must be signed in to change notification settings - Fork 378
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
(http-cors): Wrong Access-Control-Allow-Origin is returned with multiple origins on subsequent calls when returned headers are defined outside from handler #1263
Comments
In you above use cases your Origin is |
Sorry, its a typo, I corrected it 🙈 |
I added in this unit test, and it passed.
|
Thank you for taking the time to look into this. |
Can you share the entire event you're seeing in your e2e test? Put that into the unit test. The only thing coming to mind is there is something else in the event having an effect, or something in the build process that's causing the issue. |
Input a) and b) are the minimal events I used in the lambda test console with the sample code from above (originally the lambda is attached to an API Gateway but I wanted to exclude as many factors as possible). If you want I can build a sample cdk project with the e2e test. Would that help? Alternatively I can provide you a lambda zip with the bundled code if you prefer that.
Unlikely, initially I made the screenshot with the diff on the resulting javascript, the diff looked the same. EDIT: I just saw that in the response something odd is happening additionally. For each invocation {
"statusCode": 200,
"body": "{\"hi\":\"there\"}",
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "https://example.org",
"Vary": "Origin, Origin, Origin, Origin, Origin, Origin, Origin, Origin"
}
} EDIT2: I've set up a cdk demo project here: https://github.com/WtfJoke/middy_cors_1263 |
Describe the bug
It sounds really odd and silly and I hope I dont oversee anything huge here....
However when we return headers (from type
Record<string, string>
) in our lambdaHandler, somehow middy seems to cache the cors headers origin (Access-Control-Allow-Origin) and on subsequent calls it returns always the same Access-Control-Allow-Origin header and doesn't respect the request Origin Header.Examples:
Assuming we have two allowed origins configured: http://localhost:3000 and https://example.org.
Make sure the lambda is cold when changing/testing use cases.
Use case 1
Origin
= https://example.org --> The lambda responds withAccess-Control-Allow-Origin
https://example.org 🥳Origin
= http://localhost:3000 --> The lambda responds withAccess-Control-Allow-Origin
https://example.org 😢Origin
= http://localhost:3000 --> The lambda responds withAccess-Control-Allow-Origin
https://example.org 😢Use case 2
Origin
= http://localhost:3000 --> The lambda responds withAccess-Control-Allow-Origin
http://localhost:3000 🥳Origin
= https://example.org --> The lambda responds withAccess-Control-Allow-Origin
http://localhost:3000 😢Origin
= https://example.org --> The lambda responds withAccess-Control-Allow-Origin
http://localhost:3000 😢To Reproduce
Input a)
or
Input b)
Expected behaviour
When we have two allowed origins "http://localhost:3000" and "https://example.org"
I would expect always the correct
Access-Control-Allow-Origin
is returned when requesting it with one of the origins.Environment (please complete the following information):
Additional context
The error is not reproducable when we inline the
CONTENT_TYPE_JSON_HEADER
variable (or define the variable inside of the lambdaHandler method).E.g returning
will NOT result in a wrong behaviour.
Left is working, right is not working
The text was updated successfully, but these errors were encountered: