fix: prevent server crash on unauthenticated requests (ERR_HTTP_HEADERS_SENT) - #47
Open
XciD wants to merge 3 commits into
Open
fix: prevent server crash on unauthenticated requests (ERR_HTTP_HEADERS_SENT)#47XciD wants to merge 3 commits into
XciD wants to merge 3 commits into
Conversation
XciD
marked this pull request as ready for review
August 1, 2026 08:28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A single
POST /v1/responseswithout anAuthorizationheader crashes the whole server. In production (responses-js-prod), this shows up as recurring pod restarts with:The chain:
innerRunStreamanswered the missing-key case itself withres.status(401).json(...)and then returned normally. The parent generatorrunCreateResponseStreamstill yielded aresponse.completedevent, sopostCreateResponsecalledres.json()a second time, which throwsERR_HTTP_HEADERS_SENT. Since Express 4 does not catch errors from async handlers, the throw becomes an unhandled rejection and kills the Node process. Any unauthenticated probe (scanner, health check, curl without a token) takes the service down.Fix
postCreateResponsebefore starting the event stream, and pass it down explicitly (runCreateResponseStream/innerRunStreamno longer touchres).try/catch: an unexpected error now returns a 500 (or ends the stream if headers were already sent) instead of crashing the process.Verification
Reproduced the crash on
main(one unauthenticated request kills the server), then with the fix:401, server stays upresponse.failedobjectunauthenticated request gets a 401 and does not crash the server)pnpm run checkandpnpm run lint:checkpass