Skip to content

fix(mime-node): escape backslash and quote in Content-Type name parameter - #1837

Merged
andris9 merged 1 commit into
nodemailer:masterfrom
webdevelopersrinu:fix/content-type-name-quoted-string-escaping
Aug 4, 2026
Merged

andris9 merged 1 commit into
nodemailer:masterfrom
webdevelopersrinu:fix/content-type-name-quoted-string-escaping

Conversation

@webdevelopersrinu

Copy link
Copy Markdown
Contributor

Summary

The name parameter of the Content-Type header is wrapped in a quoted-string without escaping \ or ", unlike the Content-Disposition filename parameter built on the same node (which goes through buildHeaderValue → JSON.stringify and is escaped correctly).

An attachment whose filename ends in a backslash produces an unterminated quoted-string, because the trailing backslash escapes the closing quote:

Content-Type: application/octet-stream; name="foo\"      ← broken (unterminated)
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="foo\\"        ← correct

A lenient MIME parser continues consuming past the line boundary until it finds the next ", merging or hiding the following headers (Content-Transfer-Encoding, Content-Disposition) — i.e. header/parameter smuggling. A backslash elsewhere in the name (e.g. a\b.txt) is also read as a quoted-pair by a conforming parser, silently corrupting the received filename.

Reproduction

const MailComposer = require('nodemailer/lib/mail-composer');

new MailComposer({
  from: '[email protected]',
  to: '[email protected]',
  text: 'hi',
  attachments: [{ filename: 'foo\\', content: 'x' }]
})
  .compile()
  .build((err, msg) => console.log(msg.toString()));

// Content-Type: application/octet-stream; name="foo\"   <-- unterminated quoted-string

Fix

Escape \ and " as quoted-pairs before wrapping the name value in quotes, matching what the Content-Disposition path already does:

param = '"' + param.replace(/["\\]/g, '\\$&') + '"';

The change is a no-op for the existing non-ASCII (RFC 2047 encoded-word) path, since encoded words contain no \ or ".

Tests

Adds two regression tests in test/mime-node/mime-node-test.js asserting the name= parameter is emitted with the same escaping as filename=. Both fail without the fix and pass with it.

  • npm run lint — clean
  • npm run format:check — changed files clean
  • npm test — full suite passing (615 tests)

…eter

The `name` parameter of the Content-Type header was wrapped in a
quoted-string without escaping backslash or double-quote characters,
unlike the Content-Disposition `filename` parameter on the same node
(which is built via buildHeaderValue). An attachment filename ending
in a backslash produced `name="foo\"` -- an unterminated quoted-string,
since the trailing backslash escapes the closing quote. A lenient MIME
parser can consume past the line boundary and merge or hide the
following headers (Content-Transfer-Encoding, Content-Disposition),
enabling header/parameter smuggling. Backslashes elsewhere in the name
also silently corrupted the received filename.

Escape `\` and `"` as quoted-pairs before wrapping, matching the
Content-Disposition path. Adds regression tests.
@andris9
andris9 merged commit adcfc4f into nodemailer:master Aug 4, 2026
2 checks passed
andris9 added a commit that referenced this pull request Aug 4, 2026
…stringify

Follow-up to #1837, no behaviour change. The escaping added there is exactly
what buildHeaderValue already does for filename=, so use the same call rather
than a second hand-rolled escaper that can drift from it.

The tests that came with the fix asserted with indexOf, and filename= ends in
name=, so both assertions matched inside the Content-Disposition line instead.
One of the two passed against the unpatched code. Pin the full header line
with anchored regexes like the neighbouring filename tests do, and add a case
for a quote in the filename, which never reaches the quoting step because
encodeWords turns the whole value into a mime word first.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013bYRfSN3MWR2St9WLuPeXA
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.

2 participants