jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.
http://www.mimekit.net
MIT License
6.15k stars 818 forks source link

Illegal character in header field #806

Closed Sicos1977 closed 5 years ago

Sicos1977 commented 5 years ago

Describe the bug A clear and concise description of what the bug is. Illegal character in header field

To Reproduce Steps to reproduce the behavior: Add more then 10 headers fields to fetch, this will throw the exception

Expected behavior Get all the requested header fields

Screenshots If applicable, add screenshots to help explain your problem. image

Desktop (please complete the following information):

Additional context The exception is only thrown in mailkit 2.1.0.3 and higher, everything below works fine.

jstedfast commented 5 years ago

Looks like the problem is that the header field name ends with a \r\n sequence in it (which is illegal) instead of :.

Would it be possible to get the protocol log for the FETCH response for the message that has X-ES2-EMAIL_KEYWORDS\r\n?

I'm wondering what other context of the headers might exist so that I can see if I can work-around this issue in the parser.

Sicos1977 commented 5 years ago

Connected to imaps://....:993/ S: OK The Microsoft Exchange IMAP4 service is ready. C: A00000000 CAPABILITY S: CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN UIDPLUS MOVE ID CHILDREN IDLE NAMESPACE LITERAL+ S: A00000000 OK CAPABILITY completed. ... ... C: A00000002 CAPABILITY S: CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN UIDPLUS MOVE ID CHILDREN IDLE NAMESPACE LITERAL+ S: A00000002 OK CAPABILITY completed. C: A00000003 NAMESPACE S: NAMESPACE (("\" "/\")) NIL NIL S: A00000003 OK NAMESPACE completed. C: A00000004 LIST "\" "INBOX\" S: LIST (\Marked \HasNoChildren) "/\" INBOX S: A00000004 OK LIST completed. C: A00000005 EXAMINE INBOX S: 6 EXISTS S: 0 RECENT S: FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent) S: OK [PERMANENTFLAGS ()] Permanent flags S: OK [UNSEEN 1] Is the first unseen message S: OK [UIDVALIDITY 14] UIDVALIDITY value S: OK [UIDNEXT 1445] The next unique identifier value S: A00000005 OK [READ-ONLY] EXAMINE completed.

jstedfast commented 5 years ago

There should be more to the log... this only gets to the point of opening the folder, but not to the point of making the FETCH request or getting the FETCH responses.

Sicos1977 commented 5 years ago

This is all the logging I get until the exception is thrown.

Connected to imaps://mail.a3cd.local:993/ S: OK The Microsoft Exchange IMAP4 service is ready. C: A00000000 CAPABILITY S: CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN UIDPLUS MOVE ID CHILDREN IDLE NAMESPACE LITERAL+ S: A00000000 OK CAPABILITY completed. C: A00000001 LOGIN XXXXXX XXXXXX S: A00000001 OK LOGIN completed. C: A00000002 CAPABILITY S: CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN UIDPLUS MOVE ID CHILDREN IDLE NAMESPACE LITERAL+ S: A00000002 OK CAPABILITY completed. C: A00000003 NAMESPACE S: NAMESPACE (("\" "\")) NIL NIL S: A00000003 OK NAMESPACE completed. C: A00000004 LIST "\" "INBOX\" S: LIST (\Marked \HasNoChildren) "/\" INBOX S: A00000004 OK LIST completed. C: A00000005 EXAMINE INBOX S: 6 EXISTS S: 0 RECENT S: FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent) S: OK [PERMANENTFLAGS ()] Permanent flags S: OK [UNSEEN 1] Is the first unseen message S: OK [UIDVALIDITY 14] UIDVALIDITY value S: OK [UIDNEXT 1445] The next unique identifier value S: A00000005 OK [READ-ONLY] EXAMINE completed.

jstedfast commented 5 years ago

That doesn't make sense... :-\

Maybe the log isn't getting flushed to disk?

What if you do:

using (var client = new ImapClient (new ProtocolLogger (Console.OpenStandardOutput ()))) {
    ...
}
jstedfast commented 5 years ago

Any update on this?

Sicos1977 commented 5 years ago

At the moment I'm on a skiing holiday so I'll close the issue and reopen it when I'm back.

Sicos1977 commented 5 years ago

Well I just tested with the latest version today and this issue is still in there

Sicos1977 commented 5 years ago

[MailBoxException: Illegal characters in header field: X-E2S-EMAIL_KEYWORDS Parameter name: headers Connected to imaps://....:993/ S: OK The Microsoft Exchange IMAP4 service is ready. C: A00000000 CAPABILITY S: CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN UIDPLUS MOVE ID CHILDREN IDLE NAMESPACE LITERAL+ S: A00000000 OK CAPABILITY completed. ... ... C: A00000002 CAPABILITY S: CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN UIDPLUS MOVE ID CHILDREN IDLE NAMESPACE LITERAL+ S: A00000002 OK CAPABILITY completed. C: A00000003 NAMESPACE S: NAMESPACE (("" "/")) NIL NIL S: A00000003 OK NAMESPACE completed. C: A00000004 LIST "" "INBOX" S: LIST (\Marked \HasNoChildren) "/" INBOX S: A00000004 OK LIST completed. C: A00000005 EXAMINE INBOX S: 7 EXISTS S: 0 RECENT S: FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent) S: OK [PERMANENTFLAGS ()] Permanent flags S: OK [UIDVALIDITY 14] UIDVALIDITY value S: * OK [UIDNEXT 1481] The next unique identifier value S: A00000005 OK [READ-ONLY] EXAMINE completed. ] Email2Storage.Server.BusinessLogic.MailBox.get_ListMessages() in C:\tfs\ECM\Email2Storage\Server\BusinessLogic\MailBoxes.cs:1637 Email2Storage.Website.MailBoxesMonitorDetail.RefreshData(Boolean useCache) in C:\tfs\ECM\Email2Storage\WebSite\MailBoxesMonitorDetail.aspx.cs:288 Email2Storage.Website.MailBoxesMonitorDetail.Page_Load(Object sender, EventArgs e) in C:\tfs\ECM\Email2Storage\WebSite\MailBoxesMonitorDetail.aspx.cs:103 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +85 System.Web.UI.Control.OnLoad(EventArgs e) +79 System.Web.UI.Control.LoadRecursive() +130 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2848

Sicos1977 commented 5 years ago

When downgrading to version 2.0.7 I get this error

Exception Details: Email2Storage.Server.BusinessLogic.Exceptions.MailBoxException: Syntax error in BODYSTRUCTURE. Unexpected token: [atom: -1]

Sicos1977 commented 5 years ago

at MailKit.Net.Imap.ImapUtils.GetUniqueHeaders(IEnumerable1 headers) at MailKit.Net.Imap.ImapFolder.<FetchAsync>d__202.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MailKit.Net.Imap.ImapFolder.Fetch(Int32 min, Int32 max, MessageSummaryItems items, IEnumerable1 headers, CancellationToken cancellationToken)

Sicos1977 commented 5 years ago

image

jstedfast commented 5 years ago

My original diagnosis was wrong. It turn out this exception gets thrown before an IMAP FETCH request is made because of the _ character in the field name.

The logic for validating the header field names was wrong in the IMAP code (but correct in MimeKit which is where I thought the exception was coming from).

Fixed now.

Sicos1977 commented 5 years ago

Thanks for fixing... any idea when you are going to release the fix on nuget?

jstedfast commented 5 years ago

Wasn't planning on making a new release for at least a few months :-\

Sicos1977 commented 5 years ago

:-\