Skip to content

Prevent corrupted SMTP headers when MailAddress.DisplayName contains CR/LF#128980

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-corrupted-email-headers
Draft

Prevent corrupted SMTP headers when MailAddress.DisplayName contains CR/LF#128980
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-corrupted-email-headers

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 4, 2026

MailAddress accepted display names containing \r/\n and SmtpClient embedded them verbatim into From:/To: headers, breaking header structure and SMTP framing. Typical source: a trailing newline left over from parsing upstream input.

Changes in MailAddress.TryParse (src/libraries/System.Net.Mail/src/System/Net/Mail/MailAddress.cs):

  • Caller-supplied display name: reject CR/LF by throwing FormatException, matching the existing convention for MailMessage.Subject (MailBnfHelper.HasCROrLFMailSubjectInvalidFormat). TryCreate returns false.
  • Display name parsed out of the address string: unfold per RFC 5322 §3.2.4 by stripping CR/LF from the stored value. This preserves the previously-accepted folded input "display \r\n name" <"folding"@domain.com> while ensuring re-encoding produces a clean header.
  • New StripCRLF helper (guarded by an HasCROrLF pre-check, so allocation only occurs when needed).
// Now throws FormatException instead of producing a broken From: header
new MailAddress("user@example.com", "Display Name\r\n");

Test coverage added in MailAddressDisplayNameTest.MailAddress_Ctor_DisplayNameContainsCRLF_Throws for CR, LF, and CRLF in leading/middle/trailing positions and inside bounding quotes, against both the constructor and TryCreate.

Copilot AI requested review from Copilot and removed request for Copilot June 4, 2026 11:51
Copilot AI linked an issue Jun 4, 2026 that may be closed by this pull request
@MihaZupan MihaZupan assigned mrek-msft and unassigned MihaZupan Jun 4, 2026
…TP headers

Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 4, 2026 12:21
Copilot AI changed the title [WIP] Fix corrupted email headers due to new line in DisplayName Prevent corrupted SMTP headers when MailAddress.DisplayName contains CR/LF Jun 4, 2026
Copilot AI requested a review from MihaZupan June 4, 2026 12:22
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Corrupted email headers when DisplayName contains new line

3 participants