Skip to content

System.Net.Mail Validation improvements - #128981

Merged
mrek-msft merged 56 commits into
mainfrom
copilot/better-contenttype-name-validation
Oct 22, 2026
Merged

mrek-msft merged 56 commits into
mainfrom
copilot/better-contenttype-name-validation

Conversation

Copilot AI commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

ContentType.Name (and ContentDisposition.FileName) only checked the prefix of the value via MimeBasePart.DecodeEncoding. A value that starts with a valid RFC 2047 encoded-word but contains trailing junk — including unescaped " — was passed through unquoted, allowing injection of additional MIME parameters into the serialized header.

var ct = new ContentType("application/octet-stream");
ct.Name = "=?utf-8?Q?foo?=?evil\"; filename=\"foo.txt";
// Before: application/octet-stream; name="=?utf-8?Q?foo?=?evil"; filename="foo.txt"
// (the attacker-controlled `filename` parameter has been injected)

Changes

  • MimeBasePart.DecodeEncoding: tightened the recognizer so it only returns a non-null encoding when the input is a well-formed RFC 2047 encoded-word (or a sequence of them joined by folding whitespace). Values containing characters that could break out of a quoted parameter (e.g. ", ;) no longer take the pass-through path.
  • ContentType.EncodeToBuffer / ContentDisposition.EncodeToBuffer: the "manually encoded, pass through" branch is now reached only for genuinely valid encoded-words, so the raw-append-inside-quotes is safe. Anything else falls through to GetTokenOrQuotedString, which properly escapes embedded quotes.
  • Tests: added coverage in ContentTypeTest (and equivalent for ContentDisposition.FileName) for the injection payloads and for legitimate encoded-word round-tripping to ensure no regression.

Copilot AI self-assigned this Jun 4, 2026
Copilot AI review requested due to automatic review settings June 4, 2026 11:51
Copilot AI removed the request for review from Copilot June 4, 2026 11:51
Copilot AI linked an issue Jun 4, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix ContentType.Name validation on composed values Validate ContentType.Name and ContentDisposition.FileName against header injection Jun 4, 2026
Copilot AI requested a review from MihaZupan June 4, 2026 12:01
@MihaZupan MihaZupan assigned mrek-msft and unassigned MihaZupan Jun 4, 2026
@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.

@mrek-msft

Copy link
Copy Markdown
Member

@copilot, I do not see any files changed. Can you proceed with it?

Co-authored-by: mrek-msft <188900745+mrek-msft@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 18, 2026 09:56

Copilot AI commented Jun 18, 2026

Copy link
Copy Markdown
Contributor Author

@copilot, I do not see any files changed. Can you proceed with it?

Sorry about that — my previous session ended after only committing the plan. The actual fix is now pushed: MimeBasePart.DecodeEncoding is tightened to require the entire value to be a well-formed RFC 2047 encoded-word (or folded sequence), and tests covering both injection payloads and legitimate encoded-word round-trips are added in ContentTypeTest / ContentDispositionTest. Locally the full System.Net.Mail.Functional.Tests suite (456 tests) passes with 0 failures.

Note

This comment was generated by an AI agent (GitHub Copilot).

@rzikm rzikm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread src/libraries/System.Net.Mail/src/System/Net/Mime/MimeBasePart.cs Outdated
@rzikm

rzikm commented Aug 14, 2026

Copy link
Copy Markdown
Member

@copilot please address review comments

@mrek-msft mrek-msft reopened this Oct 15, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@mrek-msft mrek-msft changed the title Validate ContentType.Name and ContentDisposition.FileName against header injection System.Net.Mail Validation improvements Oct 15, 2026
@mrek-msft
mrek-msft requested a balanced review from Copilot October 15, 2026 08:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new ContentType theory incorrectly expects raw encoded text from a getter that decodes it.

1 open finding

🧠 Review effort: Balanced

Comment thread src/libraries/System.Net.Mail/tests/Functional/ContentTypeTest.cs Outdated

@MihaZupan MihaZupan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also handle other similar properties like the ones called out in #128637 (comment)

Comment thread src/libraries/System.Net.Mail/src/System/Net/Mail/Attachment.cs Outdated
Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
@mrek-msft

Copy link
Copy Markdown
Member

/ba-g Unrelated timeout

Comment thread src/libraries/System.Net.Mail/src/Resources/Strings.resx Outdated
Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
@mrek-msft
mrek-msft merged commit 513a3ef into main Oct 22, 2026
88 checks passed
@mrek-msft
mrek-msft deleted the copilot/better-contenttype-name-validation branch October 22, 2026 11:28
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 23, 2026
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.

Better ContentType.Name validation on composed values

5 participants