Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions in Disposition during folder listing #486

Closed
breedbekkikker opened this issue Apr 6, 2017 · 9 comments
Closed

Exceptions in Disposition during folder listing #486

breedbekkikker opened this issue Apr 6, 2017 · 9 comments
Labels
bug Something isn't working

Comments

@breedbekkikker
Copy link

Hi,

The following exception occurs when listing a folder:

Exception: Illegal characters in disposition value.
Parameter name: value

The IMAP protocol logging:

C: B00000020 FETCH 1:* (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODYSTRUCTURE)
S: * 1 FETCH (UID 3 FLAGS (\Seen) INTERNALDATE " 6-Apr-2017 10:50:20 +0200" RFC822.SIZE 1315 ENVELOPE ("Thu, 6 Apr 2017 10:50:20 +0200" "test" ((NIL NIL "test" "acme.com")) ((NIL NIL "test" "acme.com")) ((NIL NIL "test" "acme.com")) NIL NIL NIL NIL "<58B634343442A1@acme.com>") BODYSTRUCTURE (((("TEXT" "PLAIN" NIL NIL NIL "7bit" 3 1) "ALTERNATIVE" ("boundary" "==boundary-2"))("IMAGE" "GIF" ("name" "image1.gif") "<PHP-image-1>" NIL "base64" 6 NIL ({8}
S: "inline" NIL)) "RELATED" ("boundary" "==boundary-3")) "MIXED" ("boundary" "==boundary-1")))

The original mail contains the following MIME part headers:

--==boundary-3
Content-type: image/gif; name="image1.gif"
Content-Transfer-Encoding: base64
Content-ID: <PHP-image-1>
Content-Disposition: "inline"

Another exception occurring is:

Exception: The disposition is not allowed to be empty.
Parameter name: value

The IMAP protocol logging:

C: M00000020 FETCH 1:* (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODYSTRUCTURE)
S: * 1 FETCH (UID 5 FLAGS (\Seen) INTERNALDATE " 6-Apr-2017 10:59:05 +0200" RFC822.SIZE 1300 ENVELOPE ("Thu, 6 Apr 2017 10:59:05 +0200" "test" ((NIL NIL "test" "acme.com")) ((NIL NIL "test" "acme.com")) ((NIL NIL "test" "acme.com")) NIL NIL NIL NIL "<58B634343442A2@acme.com>") BODYSTRUCTURE (((("TEXT" "PLAIN" NIL NIL NIL "7bit" 0 0) "ALTERNATIVE" ("boundary" "==boundary-2"))("APPLICATION" "PDF" ("name" "file.pdf") NIL NIL "base64" 6 NIL ("" ("filename" "file.pdf"))) "RELATED" ("boundary" "==boundary-3")) "MIXED" ("boundary" "==boundary-1")))

The original mail contains the following MIME part headers:

--==boundary-3
Content-Type: application/pdf; name="file.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: ; filename="file.pdf"

Is there are a solution possible for these invalid disposition headers within MailKit/MimeKit?

Thanks!

@jstedfast
Copy link
Owner

As far as I can tell, this can't happen unless you ToString() the MessageSummary and then parse it back? From what I can tell, these exceptions must be coming from BodyPart.TryParse()?

jstedfast added a commit to jstedfast/MimeKit that referenced this issue Apr 6, 2017
jstedfast added a commit to jstedfast/MimeKit that referenced this issue Apr 6, 2017
@jstedfast
Copy link
Owner

Added 2 work-arounds for these in the ContentDisposition parser in MimeKit, but that's not where you were getting these exceptions from...

@breedbekkikker
Copy link
Author

Here's where the exception occurs:

public async Task<IEnumerable<IMessageSummary>> GetMessageSummaryForAllMessagesAsync(IMailFolder mailFolder)
{
   try
   {
      await mailFolder.OpenAsync(FolderAccess.ReadOnly);
      IEnumerable<IMessageSummary> messageSummaries = await mailFolder.FetchAsync(0, -1, SummaryItems);

      return messageSummaries;
   }
   catch (Exception exception)
   {
      throw new Exception("Retrieving MessageSummaries failed", exception);
   }
}

MessageSummaries:

private const MessageSummaryItems SummaryItems = MessageSummaryItems.BodyStructure |
                                                       MessageSummaryItems.InternalDate |
                                                       MessageSummaryItems.Envelope |
                                                       MessageSummaryItems.Flags |
                                                       MessageSummaryItems.MessageSize |
                                                       MessageSummaryItems.UniqueId;

@jstedfast
Copy link
Owner

Can you provide a stack trace? I get that it's happening there, but I need more specifics because I don't see how it's possible to hit those particular exceptions.

The problem is, the only way to hit both of those exceptions are:

new ContentDisposition ("\"inline\"");

or

disposition.Disposition = "\"inline\"";

The problem is that neither of those occur anywhere in ImapUtils.cs where the ContentDisposition is "parsed" from the IMAP FETCH response.

Instead, what that code does is construct a string from the response and then calls ContentDisposition.TryParse() which sets the disposition member variable directly instead of via the Disposition property which is where the validation occurs.

The only place I can find in both MimeKit and MailKit that could emit these exceptions is in the BodyPart.TryParse() method which is never used within MailKit, but is there for consumers to use if they serialize the IMessageSummary.Body and then parse it back.

@jstedfast
Copy link
Owner

Wait, never mind... I found where ImapUtils.cs calls new ContentDisposition (str)

@jstedfast
Copy link
Owner

ok, fixed now :)

@jstedfast
Copy link
Owner

jstedfast commented Apr 6, 2017

On the plus side, I also added similar work-arounds to MimeKit's parser... (not that it would throw exceptions before, but the MimePart.ContentDisposition would be null before due to being unable to parse those cases - now it'll work around them and try to provide at least something).

@breedbekkikker
Copy link
Author

Perfect! Thanks a lot!

@jstedfast
Copy link
Owner

MimeKit & MailKit 1.14 have been released with this fix.

@jstedfast jstedfast added the bug Something isn't working label Apr 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants