Skip to content

Respect event["cookies"] #32

Description

@calebsyring

I had to override ActionHandler.on_event in one of our projects:

@classmethod
    def on_event(cls, event, context):
        """
        Normalize API Gateway HTTP API v2 cookie handling before awsgi2 builds the WSGI environ.

        HTTP API v2 may deliver browser cookies in event["cookies"] instead of headers["cookie"].
        awsgi2/mu expects a Cookie header when constructing the Flask request environ; without it,
        Flask sees an empty request.cookies on POSTs even though the browser sent the session
        cookie. That breaks session-backed CSRF validation because the form token is posted but the
        corresponding session token is missing. Reconstructing the Cookie header here keeps Flask
        session handling and CSRF checks working behind API Gateway.
        """
        if isinstance(event, dict) and 'rawPath' in event:
            headers = event.setdefault('headers', {})
            if event.get('cookies') and 'cookie' not in {k.lower() for k in headers}:
                headers['cookie'] = '; '.join(event['cookies'])
        return super().on_event(event, context)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions