Skip to content

gh-136063: Fix quadratic complexity in email.header.decode_header - #163112

Open
GoranAStojanov wants to merge 1 commit into
python:mainfrom
GoranAStojanov:gh-136063-decode-header
Open

GoranAStojanov wants to merge 1 commit into
python:mainfrom
GoranAStojanov:gh-136063-decode-header

Conversation

@GoranAStojanov

@GoranAStojanov GoranAStojanov commented Dec 3, 2026 •

Copy link
Copy Markdown

email.header.decode_header() takes quadratic time in the number of RFC 2047 encoded words in a header, so a large crafted header can stall a program parsing it. This fixes the three quadratic spots:

  • parts.pop(0) in a loop over the ecre.split() result: iterate instead.
  • del words[d] in a loop to drop whitespace between encoded words: drop it as each encoded word is added. The check still uses each word's original neighbours, so behaviour is unchanged (covered by a new test).
  • last_word += word on bytes when collapsing runs of same-charset words: collect each run in a list and join once.

Output is unchanged: the patched and original implementations give identical results (including raised exceptions) on 300,000 randomized headers, with inputs such as whitespace-only encoded words, malformed encoded words and mixed charsets.

Encoded words Before After
1,000 2.4 ms 1.8 ms
16,000 364 ms 34 ms
100,000 (new test) 39.6 s 0.38 s

Typical small headers (1–3 encoded words) show no measurable change (new/old median 0.96–1.02 over 40 interleaved runs).

Tests added:

  • test_rfc2047_whitespace_between_encoded_words: locks in the existing whitespace-dropping behaviour (expected values taken from the unpatched implementation).
  • test_rfc2047_many_encoded_words: 100,000 encoded words; runs in about 0.4 s with this change and about 40 s without it.

test_email, test_mailbox, test_smtplib and test_imaplib pass.

@GoranAStojanov
GoranAStojanov requested a review from a team as a code owner December 3, 2026 21:14
@python-cla-bot

python-cla-bot Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@GoranAStojanov
GoranAStojanov force-pushed the gh-136063-decode-header branch from cf7279a to 9611881 Compare December 3, 2026 21:31
@picnixz

picnixz commented Dec 3, 2026

Copy link
Copy Markdown
Member

Some core devs are currently rewriting the parser so this will be addresser by themy sorry

@picnixz

picnixz commented Dec 3, 2026

Copy link
Copy Markdown
Member

cc @serhiy-storchaka @bitdancer

@bitdancer

Copy link
Copy Markdown
Member

This PR is actually targeting the legacy API, not the one we are currently refactoring. Since it's the legacy API, I'm not sure when I'll get around to looking at it :(

@picnixz

picnixz commented Dec 4, 2026

Copy link
Copy Markdown
Member

Mmh, should we consider the fix though? is the legacy API still used a lot today?

@bitdancer

Copy link
Copy Markdown
Member

Yes, we should consider this fix, because the legacy API is unfortunately still the default (another thing on my todo list...). But I'm not sure when I'll get to reviewing this myself.

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.

3 participants