Skip to content

Conversation

@AdSkipper1337
Copy link
Contributor

Hi - I am developing an app using rust-imap to get emails.

I implemented a full mailbox sync for my clients mailbox and started running it, after a few thousand mails in I got a crash. I started investigating and I tracked down the culprit all the way to these lines.
If I remove them it works.

What the lines did was that they throw an error if there is a null chracter somewhere in the email.

Now let me explain: What my client has in his mailbox is a corrupted mail that contains a binary within it.
That cetainly does not meet any standars - however, in practice its bound to be encountered.
My AST parsers and sanitizers that are run after that handle the mess just fine and delete it as is propper, leaving the base message readable, just like it is readable in Thunderbird and webmailers.
Without the checker it works just fine, and it is an unnecessary crash that does not reflect real world data.

The comment said:
// FIXME: what ErrorKind should this have?
Probably this shouldn't be an error at all.
I am not sure if you are encountering this issue in production, and if this handles genuine cases for you.
But just silent failure is definetly not expected behavior so probably this is pretty rare.

I thought about sanitizing the string in this case but it would hurt performance too much to either make it mutable or allocate a new array here.

So here is my working fix, with food for thought.

Best!

Copy link
Owner

@djc djc left a comment

Choose a reason for hiding this comment

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

Suggest you filter out null characters (which I think are never allowed in IMAP connections?) before hitting the parser.

@AdSkipper1337
Copy link
Contributor Author

Thanks for the quick response!

First: I am well aware that this is not a situation that meets the standard. But the question is how to handle it IF it happens, I can not change the fact that someone sends a mail that is messed up in this way and that the IMAP connection is sending it; Also webmailers and thunderbird support that mail anyway so I fear I will have to as well. If its something that shouldn't be given anyway then returning something with null characters would be a sane response.

I just thought about it, and played arround with it - but I think that won't actually work because there could be an attachment in the mail which is a binary file containing valid null characters. And - as far as I understand it - rust-imap uses your library to extract it, so if I put a filter in readline then it will destroy the attachments.
So I would have to parse it in order to know where to filter and where not to filter.
I have started investigating this since yesterday evening so I am not an expert on either of these libraries, so please correct me if I am wrong here.

Copy link
Owner

@djc djc left a comment

Choose a reason for hiding this comment

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

Okay, I'll merge this if you can additionally provide a test.

@AdSkipper1337 AdSkipper1337 force-pushed the corrupted_email_fix branch from 001a2c3 to 551e0c0 Compare July 7, 2025 11:53
@AdSkipper1337
Copy link
Contributor Author

Done. Thank you very much!

@djc djc merged commit e5880b6 into djc:main Jul 7, 2025
@djc
Copy link
Owner

djc commented Jul 7, 2025

Thanks!

@AdSkipper1337 AdSkipper1337 deleted the corrupted_email_fix branch July 7, 2025 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants