GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — EmailMessage objects break when folding malformed header · Issue #132105 · python/cpython Closed as duplicate Labels stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-emailtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error DescriptionBug reportBug description:In certain cases, with Python3.11.11 does not exhibit this behaviour. But numerous other Python versions eg The specific malformation is where:
An example of this: Here is a test script which reproduces the problem on many/most recent Python versions: #!/usr/bin/env python3 """ Reproduces a bug with flattening email headers whose encoded payloads end in a newline """ import sys from email import policy from email.parser import Parser, BytesParser, HeaderParser sampleRaw = """Date: Fri, 04 Apr 2025 10:03:35 +0200\r From: [email protected]\r Subject: =?utf-8?B?Vm9pY2Vib3ggRmlybWE6IFRlc3QtTmFjaHJpY2h0IHVtIDEwOjAzOjM1IDA0LjA0LjI1IC0gQUdGRU8gRVMgNTIyIElUIHVwIC0gc3RhaXBzbmV0Cg==?=\r MIME-Version: 1.0\r Content-Type: multipart/mixed;\r boundary="235711131719"\r To: [email protected]\r \r This is a multi-part message in MIME format.\r --235711131719\r Content-Type: text/plain; charset=UTF-8; format=flowed\r Content-Transfer-Encoding: 7bit\r \r This is readable part of the body\r --235711131719--\r \r """ messageObj = Parser(policy=policy.default).parsestr(sampleRaw) print(sys.version) print("--------") print(messageObj.as_string()) print("--------") Python 3.11.11 flattens the message correctly. Here is an excerpt in/around the As can be seen here, the header has been re-wrapped to quoted-printable (which is fine), the embedded newline is present in the encoded payload, but when the header is folded out, it has only the one line ending. Great. But other pythons I've tested with come up with: Note the blank line after When messages folded and delivered with this breakage are delivered, MTAs will often add The recipient of the above message will see this formatted as plain text in their viewing pane: If there were any attachments, their encoded representations will appear after that as gibberish strings, and the mail client won't indicate an attachment is present. This has potentially serious security implications. If an email delivery chain has python stdlib-based mail processing at or near the end, a carefully structured malicious header can
Workaround for me has been to subclass a policy, and implement a CPython versions tested on:3.12 Operating systems tested on:Linux Activity
MetadataMetadataAssigneesLabelsstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-emailtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error ProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |