Title: EmailMessage objects break when folding malformed header · Issue #132105 · python/cpython · GitHub
Open Graph Title: EmailMessage objects break when folding malformed header · Issue #132105 · python/cpython
X Title: EmailMessage objects break when folding malformed header · Issue #132105 · python/cpython
Description: Bug report Bug description: In certain cases, with EmailMessage objects, encoded headers can fold with double line endings, causing breakage of flattened message objects, making messages display improperly in email clients and rendering ...
Open Graph Description: Bug report Bug description: In certain cases, with EmailMessage objects, encoded headers can fold with double line endings, causing breakage of flattened message objects, making messages display im...
X Description: Bug report Bug description: In certain cases, with EmailMessage objects, encoded headers can fold with double line endings, causing breakage of flattened message objects, making messages display im...
Opengraph URL: https://github.com/python/cpython/issues/132105
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"EmailMessage objects break when folding malformed header","articleBody":"# Bug report\n\n### Bug description:\n\nIn certain cases, with `EmailMessage` objects, encoded headers can fold with double line endings, causing breakage of flattened message objects, making messages display improperly in email clients and rendering attachments inaccessible.\n\nPython3.11.11 does not exhibit this behaviour. But numerous other Python versions eg `3.11.9`, `3.12.x`, `3.10.x` and `3.9.x` are affected by the bug.\n\nThe specific malformation is where:\n* a header is long\n* a header is presented with its value presented as RFC 2047 UTF8 base64\n* when decoded, the header's payload ends with a newline `\\n`\n\nAn example of this:\n```\nSubject: =?utf-8?B?Vm9pY2Vib3ggRmlybWE6IFRlc3QtTmFjaHJpY2h0IHVtIDEwOjAzOjM1IDA0LjA0LjI1IC0gQUdGRU8gRVMgNTIyIElUIHVwIC0gc3RhaXBzbmV0Cg==?=\n```\n\nHere is a test script which reproduces the problem on many/most recent Python versions:\n```python\n#!/usr/bin/env python3\n\"\"\"\nReproduces a bug with flattening email headers whose encoded payloads end in a newline\n\"\"\"\nimport sys\nfrom email import policy\nfrom email.parser import Parser, BytesParser, HeaderParser\n\nsampleRaw = \"\"\"Date: Fri, 04 Apr 2025 10:03:35 +0200\\r\nFrom: sender@foo.com\\r\nSubject: =?utf-8?B?Vm9pY2Vib3ggRmlybWE6IFRlc3QtTmFjaHJpY2h0IHVtIDEwOjAzOjM1IDA0LjA0LjI1IC0gQUdGRU8gRVMgNTIyIElUIHVwIC0gc3RhaXBzbmV0Cg==?=\\r\nMIME-Version: 1.0\\r\nContent-Type: multipart/mixed;\\r\n boundary=\"235711131719\"\\r\nTo: recipient@bar.com\\r\n\\r\nThis is a multi-part message in MIME format.\\r\n--235711131719\\r\nContent-Type: text/plain; charset=UTF-8; format=flowed\\r\nContent-Transfer-Encoding: 7bit\\r\n\\r\nThis is readable part of the body\\r\n--235711131719--\\r\n\\r\n\"\"\"\n\nmessageObj = Parser(policy=policy.default).parsestr(sampleRaw)\n\nprint(sys.version)\nprint(\"--------\")\nprint(messageObj.as_string())\nprint(\"--------\")\n```\n\nPython 3.11.11 flattens the message correctly. Here is an excerpt in/around the `Subject:` header:\n```\nFrom: sender@foo.com\nSubject: Voicebox Firma: Test-Nachricht um 10:03:35 04.04.25 - AGFEO ES 522 IT\n up - =?utf-8?q?staipsnet=0A?=\nMIME-Version: 1.0\nContent-Type: multipart/mixed;\n boundary=\"235711131719\"\nTo: recipient@bar.com\n\nThis is a multi-part message in MIME format.\n```\nAs 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.\n\nBut other pythons I've tested with come up with:\n```\nFrom: sender@foo.com\nSubject: Voicebox Firma: Test-Nachricht um 10:03:35 04.04.25 - AGFEO ES 522 IT\n up - staipsnet\n\nMIME-Version: 1.0\nContent-Type: multipart/mixed;\n boundary=\"235711131719\"\nTo: recipient@bar.com\n\nThis is a multi-part message in MIME format.\n--235711131719\nContent-Type: text/plain; charset=UTF-8; format=flowed\nContent-Transfer-Encoding: 7bit\n\nThis is readable part of the body\n```\nNote the blank line after `Subject:`.\n\nWhen messages folded and delivered with this breakage are delivered, MTAs will often add `Content-Type: text/plain` because the headers after Subject have been lost into the body.\n\nThe recipient of the above message will see this formatted as plain text in their viewing pane:\n```\nMIME-Version: 1.0\nContent-Type: multipart/mixed;\n boundary=\"235711131719\"\nTo: recipient@bar.com\n\nThis is a multi-part message in MIME format.\n--235711131719\nContent-Type: text/plain; charset=UTF-8; format=flowed\nContent-Transfer-Encoding: 7bit\n\nThis is readable part of the body\n```\nIf 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.\n\nThis 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 \n- evade sanitisation\n- nullify and/or replace headers added upstream in the chain\n- inject malicious extra headers and MIME-encoded context which may cause subsequent handling steps, and/or the final MUA, to be hijacked for exploit attempts.\n\n\nWorkaround for me has been to subclass a policy, and implement a `_fold()` method to get the parent class' folding, then `.rstrip()` it, then add `\"\\r\\n\"` to the end.\n\n\n\n\n\n### CPython versions tested on:\n\n3.12\n\n### Operating systems tested on:\n\nLinux","author":{"url":"https://github.com/davidmcnabnz","@type":"Person","name":"davidmcnabnz"},"datePublished":"2025-04-05T01:42:19Z","url":"https://github.com/python/cpython/issues/132105"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:20b9c065-9c1e-b521-276e-77edae9d12d7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DB60:4D1C6:2B3B9CB:3B10EDD:6B00589D |
| html-safe-nonce | 73e6ddff37d1a10ac99390090b86bf03f880c2e975e86d1a83a0965d44cd3745 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQjYwOjREMUM2OjJCM0I5Q0I6M0IxMEVERDo2QjAwNTg5RCIsInZpc2l0b3JfaWQiOiI3NTEwMjYwOTIwNzE5OTk2NDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4ea51b31e29e35764c86dae6510bf7a30641ee48ebc8b5ed533e6a9237508df8 |
| hovercard-subject-tag | issue:2973726664 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/132105/issue_layout |
| twitter:image | https://opengraph.githubassets.com/292d8c5667ec72e77ca350a6c26da2f121ebaf562691ac6d28de14fb7ab9221f/python/cpython/issues/132105 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/292d8c5667ec72e77ca350a6c26da2f121ebaf562691ac6d28de14fb7ab9221f/python/cpython/issues/132105 |
| og:image:alt | Bug report Bug description: In certain cases, with EmailMessage objects, encoded headers can fold with double line endings, causing breakage of flattened message objects, making messages display im... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | davidmcnabnz |
| hostname | github.com |
| expected-hostname | github.com |
| None | 29be975efff6507a5d135de9c7d634cf7ff8f0c55222a64386ab3202209aaf73 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | eacd927ba6112e3b01ba99c762bd699a4a4f5df7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width