Repository navigation
gh-162080: Fix UnicodeEncodeError for non-ASCII MIME-Version header - #162195
Open
xgauravyaduvanshii wants to merge 1 commit into
Open
xgauravyaduvanshii wants to merge 1 commit into
xgauravyaduvanshii wants to merge 1 commit into
Conversation
…ader A MIME-Version header containing undecodable (non-ASCII) bytes parsed without error but could not be serialized again: as_bytes() and as_string() raised UnicodeEncodeError. parse_mime_version() now runs _validate_xtext() on the invalid-text terminals it creates, so an UndecodableBytesDefect is registered and the header is refolded using the unknown-8bit charset, as for other structured headers. Signed-off-by: Gaurav Kumar Yadav <xgauravyaduvanshii@gmail.com>
|
The following commit authors need to sign the Contributor License Agreement: |
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.
Fixes #162080
A message whose
MIME-Versionheader contains an undecodable (non-ASCII) byte parsed without error underpolicy.default, butas_bytes()/as_string()raisedUnicodeEncodeError:Root cause:
parse_mime_version()appended the invalid text as a bareValueTerminal(..., 'xtext')and never called_validate_xtext(), so noUndecodableBytesDefectwas registered and_refold_parse_tree()choseutf-8instead ofunknown-8bitfor the encoded word.Fix: run
_validate_xtext()on the four invalid-text terminals created inparse_mime_version()(invalid major digits, trailing text with no dot, invalid minor digits, excess text after the version). The header is now refolded asMIME-Version: 1.=?unknown-8bit?q?=E9?=, as for other structured headers, and carries anUndecodableBytesDefect.Tests:
Lib/test/test_email/test_headerregistry.py(TestMIMEVersionHeader): non-ASCII byte in the minor version, in the major version, and in excess text after the version.test_emailsuite on a build of this branch: 1,826 tests run, SUCCESS (2 skipped).b'MIME-Version: 1.=?unknown-8bit?q?=E9?=\n\nbody\n'fromas_bytes()instead of raising.