jstedfast / MimeKit

A .NET MIME creation and parser library with support for S/MIME, PGP, DKIM, TNEF and Unix mbox spools.
http://www.mimekit.net
MIT License
1.84k stars 373 forks source link

MailKit crashes from headers in Korean #1064

Closed SviatoslavRazmyslov closed 3 months ago

SviatoslavRazmyslov commented 3 months ago

Describe the bug

I received an email from Korean student and saw a MailKit exception in my service.

Platform:

Exception

System.FormatException: Failed to parse headers.
   at MimeKit.MimeParser.ParseHeaders(Byte* inbuf, CancellationToken cancellationToken)
   at MimeKit.MimeParser.ParseHeaders(CancellationToken cancellationToken)
   at MimeKit.HeaderList.Load(ParserOptions options, Stream stream, CancellationToken cancellationToken)
   at MimeKit.HeaderList.Load(Stream stream, CancellationToken cancellationToken)
   at MimeKit.MessageDispositionNotification.get_Fields()

To Reproduce Steps to reproduce the behavior:

I guess the reason is this:

From: "=?UTF-8?B?7ZmN7KeE6re8?=" <jkhong@bu.ac.kr>

or that:

Reporting-UA: mail.bu.ac.kr; 백석대학교

Expected behavior

A mail is loaded without exceptions.

jstedfast commented 3 months ago

It's neither of those. Any chance you can send me the message privately? I need to see the full content of the message/disposition-notification MIME part.

The content is malformed somehow.

jstedfast commented 3 months ago

Moving this to MimeKit since, if it is a bug, then it's a bug in my MimeKit library and not MailKit.

SviatoslavRazmyslov commented 3 months ago

Hi @jstedfast,

I've attached the original .eml file of the Korean mail.

Read- PVS-Studio for educational purposes.txt

jstedfast commented 3 months ago

Okay, so, looking at this email, the reason that it failed wasn't because it was in Korean but was because the Content-Transfer-Encoding is set to base64, therefore, when it goes to parse the content (another set of "headers"), it fails because the parser is dealing with the result of whatever the base64 decoder gives back (which will be garbage because it's not really base64 encoded).

I'm not sure what can really be done by MimeKit, here. This is really a bug in the sending client.

SviatoslavRazmyslov commented 3 months ago

@jstedfast , thank you for the feedback!