Skip to content
Myr

feat: do not modify the Content-Type twice when sending strings (#6991)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix: improve content-type handling in res.send method * fix: ensure content-type is a string before setting charset in res.send * fix: refactor content-type handling in res.send to use const and improve clarity * Apply suggestion from @bjohansebas * docs: update History.md

authored by Sebastian Beltran · committed by GitHub · parent 5a4568a · Browse files · 2 files+11 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+11 added−14 removed

  • other:+11 added−14 removed

2 files+11 added−14 removed

modified:lib/response.js+6 added-13 removed
124 unmodified lines
125res.send = function send(body) {126 var chunk = body;127 var encoding;128 var req = this.req;129 var type;129130 // settings131 var app = this.app;132133 switch (typeof chunk) {134 // string defaulting to html135 case 'string':137 if (!this.get('Content-Type')) {136 encoding = 'utf8';137 const type = this.get('Content-Type');138139 if (typeof type === 'string') {140 this.set('Content-Type', setCharset(type, 'utf-8'));141 } else {142 this.type('html');143 }144 break;145 case 'boolean':
10 unmodified lines
156 }157 break;158 }159156 // write strings in utf-8157 if (typeof chunk === 'string') {158 encoding = 'utf8';159 type = this.get('Content-Type');160161 // reflect this in content-type162 if (typeof type === 'string') {163 this.set('Content-Type', setCharset(type, 'utf-8'));164 }165 }166160 // determine if ETag should be generated161 var etagFn = app.get('etag fn')162 var generateETag = !this.get('ETag') && typeof etagFn === 'function'163
884 unmodified lines
modified:History.md+5 added-1 removed
1# Unreleased Changes23## 🚀 Improvements45- Improve HTML structure in `res.redirect()` responses when HTML format is accepted by adding `<!DOCTYPE html>`, `<title>`, and `<body>` tags for better browser compatibility - by [@Bernice55231](https://github.com/Bernice55231) in [#5167](https://github.com/expressjs/express/pull/5167)5* Improve HTML structure in `res.redirect()` responses when HTML format is accepted by adding `<!DOCTYPE html>`, `<title>`, and `<body>` tags for better browser compatibility - by [@Bernice55231](https://github.com/Bernice55231) in [#5167](https://github.com/expressjs/express/pull/5167)67## ⚡ Performance89* Avoid duplicate Content-Type header processing in `res.send()` when sending string responses without an explicit Content-Type header - by [@bjohansebas](https://github.com/bjohansebas) in [#6991](https://github.com/expressjs/express/pull/6991)10115.2.1 / 2025-12-0112=======================1314* Revert security fix for [CVE-2024-51999](https://www.cve.org/CVERecord?id=CVE-2024-51999) ([GHSA-pj86-cfqh-vqx6](https://github.com/expressjs/express/security/advisories/GHSA-pj86-cfqh-vqx6))
3867 unmodified lines