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

Setting lambda handler is broken for API Gateway V2 Websocket lambda payloads since 12.8.1 #2857

Closed
mark-siggins-genesys opened this issue Jan 3, 2025 · 5 comments · Fixed by #2863
Assignees
Labels

Comments

@mark-siggins-genesys
Copy link

Instrumenting lambda functions which process API Gateway V2 Websocket Authorizer calls causes invocation failures since 12.8.1
This was working prior to that.

Description

Using the newrelic.setLambdaHandler on the handler function for an APIgatewayV2 websocket Authorizer lambda function with an event of Type APIGatewayRequestAuthorizerEvent causes the following runtime error

{
  "errorType": "TypeError",
  "errorMessage": "Cannot read properties of undefined (reading 'pathname')",
  "stack": [
    "TypeError: Cannot read properties of undefined (reading 'pathname')",
    "    at Object.scrub (/var/task/node_modules/newrelic/lib/util/urltils.js:94:27)",
    "    at setWebRequest (/var/task/node_modules/newrelic/lib/serverless/aws-lambda.js:328:29)",
    "    at Runtime.wrappedHandler (/var/task/node_modules/newrelic/lib/serverless/aws-lambda.js:153:9)",
    "    at Runtime.runInContextCb (/var/task/node_modules/newrelic/lib/shim/shim.js:1183:20)",
    "    at AsyncLocalStorage.run (node:async_hooks:346:14)",
    "    at AsyncLocalContextManager.runInContext (/var/task/node_modules/newrelic/lib/context-manager/async-local-context-manager.js:62:38)",
    "    at Runtime.wrapped (/var/task/node_modules/newrelic/lib/transaction/tracer/index.js:208:37)",
    "    at TransactionShim.applySegment (/var/task/node_modules/newrelic/lib/shim/shim.js:1186:66)",
    "    at Runtime.transactionWrapper [as handler] (/var/task/node_modules/newrelic/lib/shim/transaction-shim.js:414:17)",
    "    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)"
  ]
}

The payload shape for this use case can be seen here:
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-lambda-auth.html

Expected Behavior

setting the lambda handler does not throw errors

Steps to Reproduce

Setup a websocket APIGateway, add a lambda as an authorizer and attempt to connect to the wss address. The lambda invocation will fail with the error seen above.
You can see tests fail if you provide the payload shape from https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-lambda-auth.html and use them in the tests identifying APIGateway events - eg
https://github.com/newrelic/node-newrelic/blob/main/test/unit/serverless/utils.test.js#L34

Your Environment

AWS APIGatewayV2 Websocket Authorizer nodejs lambda

Additional context

I think this is related to this ticket - #2780
The event payload seems to be identified as isGatewayV1Event but because there is no event.path this causes problems further on when the url pathname is expected to be present.

@workato-integration
Copy link

@newrelic-node-agent-team newrelic-node-agent-team moved this to Triage Needed: Unprioritized Features in Node.js Engineering Board Jan 3, 2025
@mrickard mrickard self-assigned this Jan 3, 2025
@mrickard mrickard moved this from Triage Needed: Unprioritized Features to Prioritized in Node.js Engineering Board Jan 3, 2025
@mrickard
Copy link
Member

mrickard commented Jan 3, 2025

@mark-siggins-genesys Sorry to hear you're having trouble with this. Is this a REQUEST input format? Would you be able to log out a sanitized example of the event parameter to your function?

@mark-siggins-genesys
Copy link
Author

Sure, it is of type REQUEST. This is from a PoC stack with a websocket apigateway and very simple authorizer setup to take the Auth query param and forward it to a lambda, this is the event payload the lambda receives.

{
  "headers": {
    "Connection": "upgrade",
    "content-length": "0",
    "Host": "[redacted-apiid].execute-api.us-east-1.amazonaws.com",
    "Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits",
    "Sec-WebSocket-Key": "[redacted]",
    "Sec-WebSocket-Version": "13",
    "Upgrade": "websocket",
    "X-Amzn-Trace-Id": "Root=[redacted-traceid]",
    "X-Forwarded-For": "[redacted-ipaddress]",
    "X-Forwarded-Port": "443",
    "X-Forwarded-Proto": "https"
  },
  "type": "REQUEST",
  "methodArn": "arn:aws:execute-api:us-east-1:[redacted-accountid]:[redacted-apiid]/[redacted-stage]/$connect",
  "multiValueHeaders": {
    "Connection": [
      "upgrade"
    ],
    "content-length": [
      "0"
    ],
    "Host": [
      "[redacted-apiid].execute-api.us-east-1.amazonaws.com"
    ],
    "Sec-WebSocket-Extensions": [
      "permessage-deflate; client_max_window_bits"
    ],
    "Sec-WebSocket-Key": [
      "[redacted]"
    ],
    "Sec-WebSocket-Version": [
      "13"
    ],
    "Upgrade": [
      "websocket"
    ],
    "X-Amzn-Trace-Id": [
      "Root=[redacted]"
    ],
    "X-Forwarded-For": [
      "[redacted-ipaddress]"
    ],
    "X-Forwarded-Port": [
      "443"
    ],
    "X-Forwarded-Proto": [
      "https"
    ]
  },
  "queryStringParameters": {
    "Auth": "blaa"
  },
  "multiValueQueryStringParameters": {
    "Auth": [
      "blaa"
    ]
  },
  "stageVariables": {},
  "requestContext": {
    "routeKey": "$connect",
    "eventType": "CONNECT",
    "extendedRequestId": "[redacted]",
    "requestTime": "02/Jan/2025:17:23:00 +0000",
    "messageDirection": "IN",
    "stage": "[redacted-stage]",
    "connectedAt": 1735838580271,
    "requestTimeEpoch": 1735838580272,
    "identity": {
      "sourceIp": "[redacted-ipaddress]"
    },
    "requestId": "[redacted]",
    "domainName": "[redacted-apiid].execute-api.us-east-1.amazonaws.com",
    "connectionId": "DxS6OdL1IAMCJqw=",
    "apiId": "[redacted-apiid]"
  }
}

@mrickard mrickard moved this from Prioritized to In progress: Issues being worked on in Node.js Engineering Board Jan 3, 2025
@mrickard mrickard moved this from In progress: Issues being worked on to Needs PR Review in Node.js Engineering Board Jan 7, 2025
@github-project-automation github-project-automation bot moved this from Needs PR Review to Done: Issues recently completed in Node.js Engineering Board Jan 9, 2025
@bizob2828
Copy link
Member

@mark-siggins-genesys this has been fixed in v12.10.0. please upgrade and confirm this fixes your issue

@mark-siggins-genesys
Copy link
Author

Thanks @bizob2828 - v12.10.0 has fixed this issue. Much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants