Repository navigation
fix: return 400 for malformed Content-Type header (9.x) - #496
Merged
Merged
Conversation
buffer(), text() and json() parsed the Content-Type charset without error handling. A malformed header such as 'application/json;' threw a raw TypeError, which callers answered with a 500. It now throws a 400 error with the message 'Invalid Content-Type header'.
AndyBitz
approved these changes
Nov 5, 2026
Merged
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.
🖖 What's in there?
Fixes a 500 on malformed
Content-Typeheaders inbuffer(),text()andjson().These functions parse the charset with
content-typeoutside of any error handling. A header likeContent-Type: application/json;(trailing semicolon) throws a rawTypeError: invalid parameter format, somicroanswers with a 500. The fix wraps the parse and throwscreateError(400, 'Invalid Content-Type header', err), like the existingInvalid JSONandInvalid bodyerrors.500 Internal Server Error400 Invalid Content-Type headerThis PR targets
9.x, which is new and starts atv9.3.5-canary.3. Services that still usemicro@9.3.5-canary.3can get the fix from a new canary release without moving to 10.x.🤺 How to test?
New tests in
test/index.js:json()returns 400 forapplication/json;,application/json; charset,application/json;; charset=utf-8andapplication.text()returns 400 fortext/plain;.buffer()with an explicitencodingstill skips the parse.json()still parses a body sent withapplication/json; charset=utf-8.Local run on Node 12 with
yarn test: lint passes, 66 tests pass. Without the fix, the 5 invalid-header tests fail.🔬 Notes to reviewers
9.xhas no CI: the 2019 CircleCI config is not active. I ran the tests locally.Unknownfor all files on this branch, also without this change.main(10.x).