fix(res): remove magic from res.set() Content-Type - #7430
Open
vaibhavmashal wants to merge 1 commit into
Open
Conversation
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.
Problem
Currently,
res.set()contains unexpected magic when setting theContent-Typeheader. If a given content-type string does not contain a/(e.g., shorthand like'json'or invalid input),res.set()attempts a MIME type lookup viamime.contentType(). This mutates user input unexpectedly and causes a known bug (Issue #7034), where unrecognized types returnfalsefrommime.contentType()and the header gets coercively set to the literal string"false".Solution
This PR removes the MIME type lookup logic from
res.set()completely, keepingres.set()strictly for assigning header values as provided, which resolves unexpected mutations. The intended way to set a content type with MIME type expansion remainsres.type()(which already exists for this exact purpose).By migrating internal dependencies (
res.jsonandres.jsonp) to useres.type()instead ofres.set(), we maintain backward compatibility for built-in response types (such as automatically appendingcharset=utf-8to JSON responses) while fulfilling the goal of makingres.set()predictable.Changes Made
lib/response.js:res.set(): Removed themime.contentType()assignment block and the associated obsolete JSDoc comments. Kept the validation that preventsContent-Typefrom being set to an Array.res.json(): Refactoredthis.set('Content-Type', ...)tothis.type('json')to preservecharset=utf-8expansion.res.jsonp(): Refactoredthis.set('Content-Type', ...)tothis.type('json')andthis.type('text/javascript')to maintain expansion behavior for JSON/JSONP responses.test/res.send.js:res.set('Content-Type', 'text/plain')was used, expecting exactly'text/plain'rather than'text/plain; charset=utf-8'.Testing
res.set('Content-Type', 'some-custom-type');Content-Type: false.Content-Type: some-custom-type.res.type('json')andres.json({})still properly resolve toapplication/json; charset=utf-8.npm test(all 1260 tests passed locally).Fixes #7145
Fixes #7034