Repository navigation
fix: return 400 for malformed Content-Type header - #497
Merged
Merged
Conversation
buffer(), text() and json() parsed the Content-Type charset outside the error handling of getRawBody. A malformed header such as 'application/json;' threw a raw TypeError, which callers answered with a 500. It now throws a 400 HttpError 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().buffer()parses the charset withcontent-typewhile it builds thegetRawBody()options, so the parse runs before the.catch()that maps body errors. 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 header🤺 How to test?
New tests in
test/suite/index.ts: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.Without the fix, the 5 invalid-header tests fail with
Internal Server Error.🔬 Notes to reviewers
prettier-checkinpackages/microfails on the generatedtypes/*.d.tsfiles, also onmainwithout this change.🔗 Related PRs
9.x, for services that still usemicro@9.3.5-canary.3.