Repository navigation
fix(http): stop malformed requests from crashing the server - #13
Merged
Merged
Conversation
The request URL was built from the client-supplied Host header, so a request such as "Host: a b" made new URL() throw inside a promise that nothing awaited. The unhandled rejection terminates Node, taking the server down with a single request. Route on the request path with a fixed base, answer 400 for an unparseable request target, and turn any other handler failure into a logged 500 instead of an unhandled rejection.
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.
Requested by mau
Summary
Before: the HTTP server built its request URL from the client-supplied
Hostheader inside a promise that nothing awaited. A single request likeHost: a b(or an absolute request target such asGET http://[) madenew URL()throw, the rejection went unhandled, and Node terminated the whole server.After: routing uses the request path with a fixed base. An unparseable request target gets a
400 {"error":"invalid_request_target"}, and any other handler failure is logged and answered with500 {"error":"internal_error"}. The server keeps serving in every case.How:
src/http.tsstops readingHostfor URL parsing, guards the parse, and attaches a.catchto the per-request promise. New tests send raw malformed requests over a socket and check that the server is still healthy afterwards. A mocked failing handler covers the 500 path. Both malformed-request tests fail onmainwith unhandled rejections.Verification
npm run verify