Title: email: RFC 2047 encoded-word in an addr-spec local-part corrupts address parsing · Issue #152519 · python/cpython · GitHub
Open Graph Title: email: RFC 2047 encoded-word in an addr-spec local-part corrupts address parsing · Issue #152519 · python/cpython
X Title: email: RFC 2047 encoded-word in an addr-spec local-part corrupts address parsing · Issue #152519 · python/cpython
Description: Bug report The modern email parser (email._header_value_parser, used by email.policy.default and email.headerregistry) decodes an RFC 2047 encoded-word that appears inside an addr-spec local-part, and for an obsolete local-part it then r...
Open Graph Description: Bug report The modern email parser (email._header_value_parser, used by email.policy.default and email.headerregistry) decodes an RFC 2047 encoded-word that appears inside an addr-spec local-part, ...
X Description: Bug report The modern email parser (email._header_value_parser, used by email.policy.default and email.headerregistry) decodes an RFC 2047 encoded-word that appears inside an addr-spec local-part, ...
Opengraph URL: https://github.com/python/cpython/issues/152519
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"email: RFC 2047 encoded-word in an addr-spec local-part corrupts address parsing","articleBody":"# Bug report\n\nThe modern `email` parser (`email._header_value_parser`, used by `email.policy.default` and `email.headerregistry`) decodes an RFC 2047 encoded-word that appears inside an `addr-spec` local-part, and for an *obsolete* local-part it then re-parses the **decoded** text. RFC 2047 §5 forbids encoded-words in an `addr-spec`, and re-parsing decoded text corrupts the result — most visibly when the encoded-word decodes to a character that is special in the address grammar (e.g. `=40` decodes to `@`).\n\n## Reproducers\n\nReproduced on current `main` (3.16.0a0); the code is unchanged since the parser was introduced, so all maintained versions are affected. The legacy `compat32` parser does **not** decode encoded-words during address parsing and is unaffected.\n\nAn encoded-word in the local-part is silently decoded (already a violation of RFC 2047 §5):\n\n```pycon\n>>> from email import message_from_string, policy\n>>> m = message_from_string(\"To: =?utf-8?q?admin?=@example.com\\n\\n\", policy=policy.default)\n>>> m['to'].addresses[0].username\n'admin'\n```\n\nWhen the encoded-word decodes to a bare special, the re-parse misaligns and the address is silently corrupted to the null address:\n\n```pycon\n>>> # =40 is the Q-encoding of '@'\n>>> m = message_from_string(\"To: =?utf-8?q?a=40b?=c@host.com\\n\\n\", policy=policy.default)\n>>> m['to'].addresses[0]\nAddress(display_name='', username='', domain='')\n```\n\nThe same input through `headerregistry.Address` leaks an internal parse error:\n\n```pycon\n>>> from email.headerregistry import Address\n>>> Address(addr_spec='=?utf-8?q?a=40b?=c@host.com')\nTraceback (most recent call last):\n ...\nemail.errors.HeaderParseError: Invalid Domain\n```\n\n## Cause\n\n`get_local_part()` parses an obsolete local-part by rendering the already-parsed tokens back to text with `str()` and re-parsing that text:\n\n```python\nobs_local_part, value = get_obs_local_part(str(local_part) + value)\n```\n\n`str(local_part)` decodes any encoded-word, so the text handed back to `get_obs_local_part()` differs from the source. When the decoded text contains a special such as `@`, the re-parse stops in the middle of the rendered prefix, leaving spurious decoded characters in the remainder and corrupting the address (and, downstream, raising `Invalid Domain`).\n\nPer RFC 2047 §5 an encoded-word may only appear in place of `text`, inside a `comment`, or in place of a `word` within a `phrase` (the display-name) — never inside an `addr-spec`. So the local-part should not be decoded at all.\n\n## Relationship to GH-136063\n\nFound while working on GH-136063: `get_local_part` is one of the quadratic-complexity spots there. The fix re-parses the obsolete local-part from the original source text instead of from the decoded `str()` rendering, which also removes this corruption — an encoded-word in a local-part is then simply reported as an invalid address rather than decoded and misparsed.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-152520\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/serhiy-storchaka","@type":"Person","name":"serhiy-storchaka"},"datePublished":"2026-06-28T18:44:27Z","url":"https://github.com/python/cpython/issues/152519"}
| 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:20d1b481-7723-2ed6-5062-4029189e2b75 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 93B8:31E99C:3634B92:48425DC:6B020A15 |
| html-safe-nonce | 36210085f8614c1be7eea409b0546abaf00e6200a8329e7403985fd830a9bae1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5M0I4OjMxRTk5QzozNjM0QjkyOjQ4NDI1REM6NkIwMjBBMTUiLCJ2aXNpdG9yX2lkIjoiNDkwMTIwOTcwMTIyMTUzNDIyOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6f336df1b0d62c59b5b185f1a9af3228e454d871f669165d5aabfca6b573a76d |
| hovercard-subject-tag | issue:4763247841 |
| 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/152519/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a67a08771bd6269308f246353e0be44582377edbef67cbec12aa6d4108600175/python/cpython/issues/152519 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a67a08771bd6269308f246353e0be44582377edbef67cbec12aa6d4108600175/python/cpython/issues/152519 |
| og:image:alt | Bug report The modern email parser (email._header_value_parser, used by email.policy.default and email.headerregistry) decodes an RFC 2047 encoded-word that appears inside an addr-spec local-part, ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | serhiy-storchaka |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width