How to accept requests from a specific domain in API Gateway WebSocket?

0

Hi! I'm trying to protect my API Gateway WebSocket and specifically my lambda functions that are connected to that API Gateway, my idea of protection is that only specific domains can access my lambda functions connected to my API Gateway, because we want to avoid unwanted invocations or attacks.

I read about the authorizers, but I don't know if the key to provide in the header (Sec-WebSocket-Key) I have to generate it or is generated automatically. So my question is, exists another way to protect my API Gateway and lambda functions to only accept requests from specific domains? Or well, is correct to use an authorizer to protect the connection to the WebSocket?

已提問 7 個月前檢視次數 437 次
1 個回答
0

To protect your Lambda Function thats accessed through API Gateway you can control/limit who can access your Websocket API Gateway. In order to accept specific domains on API Gateway you would have to enable cors and set the 'Access-Control-Allow-Origin' to accept requests from specific domains, but the problem with this approach is that the is no option to allow CORS for WebSocket protocol.

API Gateway supports the following authentication mechanisms, for more details you can refer to [1]:

  • Standard AWS IAM roles and policies
  • IAM tags
  • Lambda authorizers

With the Lambda authorizer you have more control of authenticating requests by creating your own custom Lambda function that implements the logic to authorize, for more details on Lambda Authorizers [2] and depending on how you handle authenticating requests. eg, token username/password these values will have to be provided when invoking your Websocket API by the requester through incoming headers and API Gateway will pass the values to your Lambda Authorizer Function which handles the authentication and when a request is successfully authenticated by your function the requests will then be successfully passed to your integration Lambda Function and this will happen on all invocations.

NB: You can only use a Lambda authorizer function for the $connect route.

References:

[1]. Controlling and managing access to a WebSocket API in API Gateway - https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-control-access.html

[2]. Use API Gateway Lambda authorizers - https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html#api-gateway-lambda-authorizer-lambda-function-create

AWS
已回答 7 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南