DataDog/datadog-lambda-js

null `headers` from API Gateway causes handler to return null

jstephenson opened this issue · 2 comments

Expected Behavior

When a Lambda is invoked with an API Gateway REST proxy input payload with null headers it should return the Lambda's response. null headers is the basic behaviour when using the API Gateway test client where no headers are specified. Without the DD wrapper the response is passed through as expected.

{
    "event": {
        ...
        "headers": null,
        ...
    }
}

Actual Behavior

https://github.com/DataDog/datadog-lambda-js/blob/main/src/trace/context.ts#L260 is not guarded properly (see the caller) because typeof null === 'object' in JS which causes an exception during

try {
await traceListener.onStartInvocation(event, context);
await metricsListener.onStartInvocation(event);
if (finalConfig.enhancedMetrics) {
incrementInvocationsMetric(metricsListener, context);
}
} catch (err) {
logDebug(`Failed to start listeners with error ${err}`);
}

For a reason I can't immediately identify this scenario leads to null returning from the Lambda (which the runtime will return if the handler returned undefined which I suspect is happening).

Steps to Reproduce the Problem

  1. Lambda wrapped in DD (can statically return 200 or whatever)
  2. Invoke with API Gateway payload including "headers": null
  3. Observe null response
  4. Change to "headers": {}
  5. Observe proper response

Specifications

  • Datadog Lambda Layer version: 50
  • Node version: 12.x

Hi @jstephenson, thanks for the bug report. I've pushed a release with a fix for this issue, (Layer Version 51, npm release 3.51.0).

Thanks :). Do you have any sense of why this condition would have yielded a null response from the handler? A read of the code would suggest it should have continued on normally.