Repository navigation
gh-132794: Handle undecodable MIME parameter values without aborting header parsing - #162359
Open
priyanshumakes wants to merge 11 commits into
Open
priyanshumakes wants to merge 11 commits into
priyanshumakes wants to merge 11 commits into
Conversation
…rting header parsing
…-19-19-30.gh-issue-132794.MimeDe.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When an RFC 2231 parameter contains an incomplete sequence in a codec such as ISO-2022-JP or UTF-16, decoding can raise UnicodeDecodeError even with surrogateescape. This currently escapes from MimeParameters.params and can prevent EmailMessage.get_body() from accessing a multipart message with an affected attachment filename.
Catch that decoding error separately, decode with replacement, and record an UndecodableBytesDefect. Keep the existing LookupError and UnicodeEncodeError handling unchanged.
This does not attempt to recover the correct filename from continuation segments that split a multibyte sequence. It keeps the header usable, marks the decoding defect, and leaves the attachment payload unchanged.
Tests cover both encodings at the header-registry level and a multipart message whose body and attachment data remain accessible. A NEWS entry is included.
Validation on a locally built Linux CPython main at 1782f62:
./python -m test test_email test_smtplib test_mailbox -v: 2,296 tests run, five skipped; all three test files pass.git diff --check: passes.The full CPython suite, debug-build reference-leak checks, and Windows/macOS testing have not been completed. The local release build lacks optional modules including SSL. No CI result is claimed.
Fixes #132794. Thanks to aclemons for the original report and the reproduction.