Skip to content

Keep 8-bit bytes in 7bit and 8bit parts when parsing - #233

Open
dickykuang wants to merge 1 commit into
DockYard:masterfrom
dickykuang:lenient-7bit-8bit-parse
Open

dickykuang wants to merge 1 commit into
DockYard:masterfrom
dickykuang:lenient-7bit-8bit-parse

Conversation

@dickykuang

Copy link
Copy Markdown

Note: This pull request was written and submitted by Claude, Anthropic's AI assistant, on behalf of dickykuang.

Changes proposed in this pull request

Mail in the wild sometimes labels 8-bit text as 7bit. A common case is a
bulk sender whose UTF-8 HTML and text parts say
Content-Transfer-Encoding: 7bit and contain non-breaking spaces or curly
quotes. Mail.Parsers.RFC2822.parse/2 passes such a body to
Mail.Encoders.SevenBit.decode/1, which raises
ArgumentError, "invalid character: 194", so the whole message fails to
parse, not just the mislabelled part. The same happens for a NUL in an
8bit part ("illegal NUL character").

RFC 2045 section 6.2 says 7bit, 8bit and binary all mean that no
encoding was applied; the label describes the data, it does not transform
it. This change keeps the encoders as they are (they still reject those
bytes, and their tests still pass) and makes only the parser tolerant: if the
7bit or 8bit decoder rejects a body, the parser keeps the body unchanged
and still applies the charset_handler. Bodies that decoded before decode
exactly as before, including the removal of the encoder's 998-character line
wrapping. Other transfer encodings still raise as before.

If you would rather make SevenBit.decode/1 and EightBit.decode/1 lenient
themselves, I'm happy to rework it that way; I left them strict because their
tests expect them to raise.

Tests: two parser tests. One has UTF-8 text in 7bit parts of a
multipart/alternative message and checks the charset handler still runs; its
text contains non-breaking spaces and a curly apostrophe, which look like
ordinary characters in the diff. The other has a NUL in an 8bit part. Both
fail on master with the errors above.

Would a 0.5.3 release with this be possible? We parse inbound mail with this
library and currently lose these messages.

Mail in the wild often labels UTF-8 text 7bit, or carries a NUL in an 8bit
part. Mail.Parsers.RFC2822 raised ArgumentError from the SevenBit/EightBit
decoders, so one mislabelled part failed the whole message. RFC 2045 6.2:
7bit and 8bit mean no encoding was applied, so the parser now keeps such a
body as it is. The encoders still reject those bytes when called directly.
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.

1 participant