Skip to content

Bug: APIGatewayAuthorizerResponse cannot be used for Websocket $connect #5800

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

Closed
deuscapturus opened this issue Dec 27, 2024 · 5 comments · Fixed by #6058
Closed

Bug: APIGatewayAuthorizerResponse cannot be used for Websocket $connect #5800

deuscapturus opened this issue Dec 27, 2024 · 5 comments · Fixed by #6058
Assignees
Labels
bug Something isn't working event_sources Event Source Data Class utility

Comments

@deuscapturus
Copy link

deuscapturus commented Dec 27, 2024

Expected Behaviour

the allow_route() and allow_all_routes() methods in APIGatewayAuthorizerResponse do not create a valid policy statement for a Websocket connection. These methods should work with websocket connections or some other Response class should be created for websocket responses.

Current Behaviour

Using this class to authorize a websocket connection with allow methods will always result in 403 Unauthorized

Code snippet

@event_source(data_class=APIGatewayAuthorizerRequestEvent)
def handler(event: APIGatewayAuthorizerRequestEvent, context: LambdaContext) -> dict:

    response = APIGatewayAuthorizerResponse(
        aws_account_id=event.parsed_arn.aws_account_id,
        api_id=event.parsed_arn.api_id,
        stage=event.parsed_arn.stage,
        region=event.parsed_arn.region,
        principal_id='ws-tut-user',
        context={"principalId": "ws-tut-user"},
        partition=event.parsed_arn.partition,
    )
   response.allow_all_routes()
   return response.asdict()

Results in resource ['arn:aws:execute-api:us-east-1:1234567891:111aaa222b/prod/*/*'] which doesn't match arn:aws:execute-api:us-east-1:1234567891:111aaa222b/prod/$connect and results in a 403 error.

Possible Solution

WORKAROUND

response._allow_routes.append({"resourceArn": event.method_arn})

Steps to Reproduce

_

Powertools for AWS Lambda (Python) version

3.4.0

AWS Lambda function runtime

3.12

Packaging format used

PyPi

Debugging logs

@deuscapturus deuscapturus added bug Something isn't working triage Pending triage from maintainers labels Dec 27, 2024
Copy link

boring-cyborg bot commented Dec 27, 2024

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@leandrodamascena
Copy link
Contributor

Hi @deuscapturus! Thanks for opening this issue.

Let me check the code to see what's going on, but based on your investigation, I see that the /prod/*/* route will not match /prod/$connect and so it will fail.

@leandrodamascena leandrodamascena moved this from Triage to Working on it in Powertools for AWS Lambda (Python) Dec 28, 2024
@leandrodamascena leandrodamascena self-assigned this Dec 28, 2024
@leandrodamascena leandrodamascena added event_sources Event Source Data Class utility and removed triage Pending triage from maintainers labels Dec 28, 2024
@leandrodamascena leandrodamascena moved this from Working on it to Backlog in Powertools for AWS Lambda (Python) Feb 2, 2025
@leandrodamascena leandrodamascena moved this from Backlog to Working on it in Powertools for AWS Lambda (Python) Feb 4, 2025
@leandrodamascena
Copy link
Contributor

Hi @deuscapturus! We are working on this PR #6058 to add the new class APIGatewayAuthorizerResponseWebSocket to handle specifics of WebSocket events and solve the issue of when authorizing or denying routes.

I hope to have this available in the next release, which is expected on 11/02/2025.

Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Feb 10, 2025
Copy link
Contributor

This is now released under 3.6.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Feb 11, 2025
@leandrodamascena leandrodamascena moved this from Coming soon to Shipped in Powertools for AWS Lambda (Python) Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working event_sources Event Source Data Class utility
Projects
Status: Shipped
2 participants