Skip to content

Commit

Permalink
Prevent sending response headers if already sent in default error han…
Browse files Browse the repository at this point in the history
…dler

This change addresses a bug where the defaultProcessEventErrorHandler
could attempt to send HTTP response headers after they've already been
sent, leading to a server error. The fix adds a check for the
response.headersSent property before attempting to write headers or
end the response.

The update ensures that the error handler behaves correctly even if
ack() has been called previously during request processing.
  • Loading branch information
suhailgupta03 committed Dec 1, 2023
1 parent 3142f9a commit bc2a530
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/receivers/HTTPModuleFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ export class HTTPModuleFunctions {
args: ReceiverProcessEventErrorHandlerArgs,
): Promise<boolean> {
const { error, response, logger, storedResponse } = args;

// Check if the response headers have already been sent
if (response.headersSent) {
logger.error('Headers already sent, cannot send another response');
return false;

Check warning on line 197 in src/receivers/HTTPModuleFunctions.ts

View check run for this annotation

Codecov / codecov/patch

src/receivers/HTTPModuleFunctions.ts#L196-L197

Added lines #L196 - L197 were not covered by tests
}

if ('code' in error) {
// CodedError has code: string
const errorCode = (error as CodedError).code;
Expand Down

0 comments on commit bc2a530

Please # to comment.