Skip to content

gh-132794: Handle undecodable MIME parameter values without aborting header parsing - #162359

Open
priyanshumakes wants to merge 11 commits into
python:mainfrom
priyanshumakes:gh-132794-rfc2231-undecodable
Open

priyanshumakes wants to merge 11 commits into
python:mainfrom
priyanshumakes:gh-132794-rfc2231-undecodable

Conversation

@priyanshumakes

@priyanshumakes priyanshumakes commented Nov 21, 2026 •

Copy link
Copy Markdown

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:

  • New regression methods pass with the fix and produce three UnicodeDecodeError errors when only the production change is removed.
  • ./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.

@priyanshumakes
priyanshumakes requested a review from a team as a code owner November 21, 2026 06:58
@python-cla-bot

python-cla-bot Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UnicodeEncodeError during mime header parsing is unhandled in _header_value_parser.py

1 participant