Skip to content

fix: preserve original value when Content-Type is unrecognized#7067

Closed
Nicknamess96 wants to merge 1 commit intoexpressjs:masterfrom
Nicknamess96:fix/content-type-false-literal
Closed

fix: preserve original value when Content-Type is unrecognized#7067
Nicknamess96 wants to merge 1 commit intoexpressjs:masterfrom
Nicknamess96:fix/content-type-false-literal

Conversation

@Nicknamess96
Copy link

When res.set('Content-Type', value) is called with a value that mime.contentType() cannot resolve (returns false), the Content-Type header is currently set to the literal string "false".

This happens because mime.contentType(value) returns false for unrecognized types, and that false value is passed to setHeader(), which coerces it to the string "false".

The fix preserves the original user-supplied value when mime.contentType() returns false, so res.set('Content-Type', 'some-custom-type') will set the header to "some-custom-type" instead of "false".

This is consistent with how res.type() handles unknown types (it falls back to "application/octet-stream" rather than propagating false).

Reproduction:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.set('Content-Type', 'some-custom-type');
  res.send('hello');
});
// Response has Content-Type: false

Changes:

  • lib/response.js: Only assign the result of mime.contentType() when it returns a truthy value
  • test/res.set.js: Add 5 test cases covering known types, shorthand types, unknown types, the false regression, and full MIME types with slashes

Fixes #7034

When res.set('Content-Type', value) is called with a value that
mime.contentType() cannot resolve (returns false), preserve the
original user-supplied value instead of setting the header to the
literal string "false".

Previously, mime.contentType(value) returning false caused
value = false, which was coerced to the string "false" by
setHeader(). This is inconsistent with res.type() which falls
back to "application/octet-stream" for unknown types.

Fixes expressjs#7034
@krzysdz
Copy link
Contributor

krzysdz commented Feb 25, 2026

Duplicate of #7035

@krzysdz krzysdz marked this as a duplicate of #7035 Feb 25, 2026
@krzysdz krzysdz closed this Feb 25, 2026
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

2 participants