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

Overwriting read-only Lambda@Edge headers #262

Open
UnderSampled opened this issue May 5, 2022 · 3 comments
Open

Overwriting read-only Lambda@Edge headers #262

UnderSampled opened this issue May 5, 2022 · 3 comments
Labels
help wanted Extra attention is needed improvement Improve an existing feature

Comments

@UnderSampled
Copy link

Hooking up a Mangum lambda to CloudFront as EventType: origin-request returns a 502 response: "The Lambda function result failed validation: The function tried to add, delete, or change a read-only header."

According to the documentation, the Content-Length Header is one of the read-only headers for origin-request events. Not quite sure why. But it's certainly one of the headers returned when calling the lambda. I use the lambda to handle API requests, so it needs IncludeBody, which is only available with origin-request.

I was able to get around this by hijacking the response:

def handler(event, context):
    response = Mangum(app, lifespan="off")(event, context)
    if 'headers' in response:
        response['headers'].pop('content-length')
    return response
@jordaneremieff jordaneremieff added help wanted Extra attention is needed improvement Improve an existing feature labels May 10, 2022
@jordaneremieff
Copy link
Collaborator

Thanks @UnderSampled for the report and example workaround. I won't be able to look into it myself at this time, but I would be willing to review and merge any PRs to address these rules in the handler itself.

@khamaileon
Copy link
Contributor

Duplicate of: #143

@eidorb
Copy link

eidorb commented Apr 11, 2023

Including Content-Length in the exclude_headers parameter fixes this:

handler = Mangum(
    app,
    # Content-Length header is not allowed in Lambda@Edge responses.
    exclude_headers=["Content-Length"],
)

exclude_headers was introduced in #280.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed improvement Improve an existing feature
Projects
None yet
Development

No branches or pull requests

4 participants