python / python/cpython

email: multiline encoded headers

Open
#91,957 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-email type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Bug report

There seems to be a problem with multiline encoded headers, for example subject headers.

from email import message_from_string, policy
import base64

e = """From: <test@test.com>
To: <test@test.com>
Subject: =?UTF-8?B?0JfQsNGB0YLRgNCw0YXQvtCy0LrQsCAi0JDQu9C40LDQvdGGINCX0LTRgNCw0LLQ?=
 =?UTF-8?B?tSIg4oCTINCY0L3QtNC40LLQuNC00YPQsNC70LXQvSDQvdC+0LzQtdGAINC90LAg?=
 =?UTF-8?B?0YPQstC10LTQvtC80LvQtdC90LjQtQ==?=

test"""

e = message_from_string(e, policy=policy.default)

s1 = e["Subject"]

try:
    e.as_bytes()
except Exception:
    print("error")
else:
    print("ok")

s = (base64.b64decode("0JfQsNGB0YLRgNCw0YXQvtCy0LrQsCAi0JDQu9C40LDQvdGGINCX0LTRgNCw0LLQ") + base64.b64decode("tSIg4oCTINCY0L3QtNC40LLQuNC00YPQsNC70LXQvSDQvdC+0LzQtdGAINC90LAg")
    + base64.b64decode("0YPQstC10LTQvtC80LvQtdC90LjQtQ==")).decode("utf-8")

del e["Subject"]
e["Subject"] = s

s2 = e["Subject"]

try:
    e.as_bytes()
except Exception:
    print("error")
else:
    print("ok")

print(s1 == s2)

e = """From: <test@test.com>
To: <test@test.com>
Subject: =?UTF-8?B?0JfQsNGB0YLRgNCw0YXQvtCy0LrQsCAi0JDQu9C40LDQvdGGINCX0LTRgNCw0LLQtSIg4oCTINCY0L3QtNC40LLQuNC00YPQsNC70LXQvSDQvdC+0LzQtdGAINC90LAg0YPQstC10LTQvtC80LvQtdC90LjQtQ==?=

test"""

e = message_from_string(e, policy=policy.default)

s3 = e["Subject"]

try:
    e.as_bytes()
except Exception:
    print("error")
else:
    print("ok")

print(s2 == s3)

Your environment

Tested on Python 3.10.4 and 3.11.0a7 on Arch Linux

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the provided reproduction with Python's email.message_from_string, policy.default, and as_bytes entry points. Compare s1, s2, and s3, including serialization failures, then trace the email package's handling of folded encoded Subject headers. Done means multiline and single-line encoded headers behave consistently without errors and the decoded values match.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.