Skip to content

fix: catch errors in normalizeType instead of letting them throw#101

Open
einarpehrson wants to merge 1 commit into
jshttp:masterfrom
einarpehrson:fix-normalizeType-throw-on-empty-type
Open

fix: catch errors in normalizeType instead of letting them throw#101
einarpehrson wants to merge 1 commit into
jshttp:masterfrom
einarpehrson:fix-normalizeType-throw-on-empty-type

Conversation

@einarpehrson

Copy link
Copy Markdown

Fixes #100.

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 (#95, building on jshttp/content-type#58) 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 the way it does for a missing Content-Type header or other invalid media types.

This restores the try/catch so normalizeType degrades to null on any parse/test failure, matching the historical 1.x behavior, and adds a regression test.

Verified: the new test fails against current master with the exact TypeError: argument string is required from the linked issue, and the full suite (53 tests, was 52) passes with this fix applied. npm test and npm run lint both clean.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

normalizeType() throws instead of returning false for a malformed Content-Type header

1 participant