Skip to content

fix(res.set): remove implicit mime lookup and charset injection for Content-Type #7146

Open
Pandey-Krishnaa wants to merge 1 commit intoexpressjs:masterfrom
Pandey-Krishnaa:fix/res-set-no-mime-lookup
Open

fix(res.set): remove implicit mime lookup and charset injection for Content-Type #7146
Pandey-Krishnaa wants to merge 1 commit intoexpressjs:masterfrom
Pandey-Krishnaa:fix/res-set-no-mime-lookup

Conversation

@Pandey-Krishnaa
Copy link
Copy Markdown

Closes #7034

res.set('Content-Type', value) was silently calling mime.contentType() on the value, which would:

  • Perform a mime-type lookup if the value had no / (e.g. 'html''text/html; charset=utf-8')
  • Append a charset if none was present (e.g. 'text/plain''text/plain; charset=utf-8')

This hidden mutation is unexpected — res.set is a generic header setter and should not transform user input.

Solution

Remove the Content-Type special-casing from res.set/res.header. Users who want mime lookup + charset behaviour
should use res.type(), which already exists for exactly this purpose.

Changes

  • Remove the mime.contentType() block from res.set
  • res.json now explicitly sets 'application/json; charset=utf-8' so its behaviour is unchanged
  • Update tests to reflect the new pass-through semantics of res.set

Breaking Change

res.set('Content-Type', 'text/plain') will no longer silently become text/plain; charset=utf-8. Use
res.type('text/plain') if charset injection is desired.

…ontent-Type

Previously, res.set('Content-Type', value) silently called mime.contentType()
on the value, which would:
  - perform a mime-type lookup if the value contained no '/' (e.g. 'html' → 'text/html; charset=utf-8')
  - append a charset if none was present (e.g. 'text/plain' → 'text/plain; charset=utf-8')

This hidden mutation was unexpected: res.set is a generic header setter and
should not transform user input. res.type() already exists as the dedicated
API for mime-lookup + charset behaviour, and callers that want it should use
that method explicitly.

Changes:
- Remove the Content-Type special-case block from res.set/res.header
- res.json now explicitly sets 'application/json; charset=utf-8' so its
  behaviour is unchanged without relying on the removed magic
- Update tests to reflect the new pass-through semantics of res.set

Fixes: expressjs#7034
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.

res.set('Content-Type') silently sets header to literal string 'false' for unknown types

1 participant