From f7777e79a52f475f51f894af5610a199396836f3 Mon Sep 17 00:00:00 2001 From: Apoorv Darshan Date: Wed, 18 Feb 2026 21:36:12 +0530 Subject: [PATCH] fix: prevent res.set('Content-Type') from setting header to 'false' When mime.contentType() cannot resolve a shorthand value, it returns false, which was silently set as the Content-Type header. Fall back to the original value instead. Fixes #7034 --- lib/response.js | 2 +- test/res.set.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/response.js b/lib/response.js index f965e539dd2..bc2bc6fb438 100644 --- a/lib/response.js +++ b/lib/response.js @@ -673,7 +673,7 @@ res.header = function header(field, val) { if (Array.isArray(value)) { throw new TypeError('Content-Type cannot be set to an Array'); } - value = mime.contentType(value) + value = mime.contentType(value) || value } this.setHeader(field, value); diff --git a/test/res.set.js b/test/res.set.js index 04511c1c95f..de9cbff00dc 100644 --- a/test/res.set.js +++ b/test/res.set.js @@ -31,6 +31,34 @@ describe('res', function(){ .expect('X-Number', '123') .expect(200, 'string', done); }) + + it('should preserve unknown Content-Type shorthand', function (done) { + var app = express(); + + app.use(function (req, res) { + res.set('Content-Type', 'custom-type'); + res.end(); + }); + + request(app) + .get('/') + .expect('Content-Type', 'custom-type') + .expect(200, done); + }) + + it('should set Content-Type with valid full MIME type', function (done) { + var app = express(); + + app.use(function (req, res) { + res.set('Content-Type', 'application/x-custom'); + res.end(); + }); + + request(app) + .get('/') + .expect('Content-Type', 'application/x-custom') + .expect(200, done); + }) }) describe('.set(field, values)', function(){