fix: catch errors in normalizeType instead of letting them throw#101
Open
einarpehrson wants to merge 1 commit into
Open
fix: catch errors in normalizeType instead of letting them throw#101einarpehrson wants to merge 1 commit into
einarpehrson wants to merge 1 commit into
Conversation
normalizeType() no longer catches errors from content-type.parse() or media-typer.test(), unlike 1.x's tryNormalizeType. Since content-type@2 became more lenient and can return an empty type string for a malformed Content-Type header (e.g. ";" or an all-whitespace value), that empty string reaches media-typer.test(), which throws on falsy input rather than returning false. This means a request with a malformed but present Content-Type header (combined with a Content-Length or Transfer-Encoding header) crashes typeis()/typeofrequest() with an uncaught TypeError, instead of returning false/null like it does for a missing Content-Type header or other invalid media types. Restore the try/catch so normalizeType degrades to null on any parse/test failure, matching the historical behavior.
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.
Fixes #100.
normalizeType()no longer catches errors fromcontent-type.parse()ormedia-typer.test(), unlike1.x'stryNormalizeType. Sincecontent-type@2became more lenient (#95, building on jshttp/content-type#58) and can return an emptytypestring for a malformedContent-Typeheader (e.g.;or an all-whitespace value), that empty string reachesmedia-typer.test(), which throws on falsy input rather than returningfalse.This means a request with a malformed-but-present
Content-Typeheader (combined with aContent-LengthorTransfer-Encodingheader) crashestypeis()/typeofrequest()with an uncaughtTypeError, instead of returningfalse/nullthe way it does for a missingContent-Typeheader or other invalid media types.This restores the
try/catchsonormalizeTypedegrades tonullon any parse/test failure, matching the historical1.xbehavior, and adds a regression test.Verified: the new test fails against current
masterwith the exactTypeError: argument string is requiredfrom the linked issue, and the full suite (53 tests, was 52) passes with this fix applied.npm testandnpm run lintboth clean.