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

Failed to parse headers #395

Closed
gigi81 opened this issue May 1, 2018 · 13 comments
Closed

Failed to parse headers #395

gigi81 opened this issue May 1, 2018 · 13 comments
Labels
bug Something isn't working

Comments

@gigi81
Copy link

gigi81 commented May 1, 2018

I'm using MimeKit 2.0.3
I'm getting this error trying to parse an email headers.
System.FormatException : Failed to parse headers.
at MimeKit.MimeParser.ParseHeaders(Byte* inbuf, CancellationToken cancellationToken) in C:\GitHub\MimeKit\MimeKit\MimeParser.cs:line 1413
at MimeKit.MimeParser.ParseHeaders(CancellationToken cancellationToken) in C:\GitHub\MimeKit\MimeKit\MimeParser.cs:line 1445

I tried to debug a little bit the issue and I narrowed it down to this bit of code in MimeParser.cs:

if (inptr == inend) {
    // we don't have enough input data
    int available = (int) (inend - start);

    inputIndex = (int) (start - inbuf);

    if (left == available) {
        state = MimeParserState.Error;
        headerIndex = 0;
        return false;
    }

    left = available;
    needInput = true;
    break;
}

If I change this line in the file:
const int BlockSize = 4096;
To
const int BlockSize = 4096 * 2;

The headers are parsed correctly and I don't get any exception.
I hope this is enough info but do please let me know if you need me to provide more info.
Unfortunately I cannot share the email itself.

@jstedfast
Copy link
Owner

I'm going to need a test case. You could just X-out the private info and I likely won't need the message body at all (just the headers).

In other words: replace [email protected] with [email protected] (in other words, make sure that the words in your modified header are the same length).

@gigi81
Copy link
Author

gigi81 commented May 2, 2018

I will have a look and see if I can obfuscate the email and add a unit test specific for that.
Anyway, if you set the blocksize to 16 or 32, the TestJwzMbox unittest fails.
I think I found the problem and I have a fix.
Can you confirm if you are happy for me to add a blockSize (optional) parameter to the constructors of the MimeParser class? This way I can also provide a unit test to reproduce the issue.

@jstedfast
Copy link
Owner

I would prefer not to have a public API that did that, but if the idea is to just unit test it, you could add an internal .ctor that takes a blockSize. The unit tests can invoke internal APIs.

@gigi81
Copy link
Author

gigi81 commented May 2, 2018

I found the commit that introduced the bug: 801ac47
Not sure what you were trying to fix there but my fix was actually to revert this change.

@jstedfast
Copy link
Owner

jstedfast commented May 2, 2018

Ok, I think I have an idea of what the problem is but if you could provide me with a test case that would still rock so that I could have a unit test for this and be sure that any fix I cook up actually works.

@jstedfast
Copy link
Owner

Thanks for digging into this btw, I've been pretty swamped :)

@gigi81
Copy link
Author

gigi81 commented May 2, 2018

No problem at all. I'm just waiting to get approval from my company to send a PR

gigi81 pushed a commit to gigi81/MimeKit that referenced this issue May 15, 2018
jstedfast added a commit that referenced this issue May 15, 2018
Fixes issue #395 as well as reverting commit 801ac47 and fixing
it in a better way.
@jstedfast
Copy link
Owner

Should be fixed now. Thanks for the PR with a way to replicate!

@gigi81
Copy link
Author

gigi81 commented May 15, 2018

It would be great if you can have a new nugget package version for this so I can put it to test. Thanks for having a look into this

@FSGRSolutions
Copy link

Experiencing the same issue, so following this.

@jstedfast
Copy link
Owner

I'm currently away on a business trip of back-to-back meetings (w/ only my macbook) so can';t make/publish any nugets until at the earliest this weekend or early next week. I will do so as soon as I can - this bug is serious enough that it's worth making a new release on its own.

@FSGRSolutions
Copy link

You da man sir!

@jstedfast
Copy link
Owner

I've just published MimeKit 2.0.4 to nuget.org (might take up to an hour before it shows up)

@jstedfast jstedfast added the bug Something isn't working label May 21, 2018
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

3 participants