Repository navigation
gh-136063: Fix quadratic complexity in email.header.decode_header - #163112
Open
GoranAStojanov wants to merge 1 commit into
Open
GoranAStojanov wants to merge 1 commit into
GoranAStojanov wants to merge 1 commit into
Conversation
|
The following commit authors need to sign the Contributor License Agreement: |
GoranAStojanov
force-pushed
the
gh-136063-decode-header
branch
from
December 3, 2026 21:31
cf7279a to
9611881
Compare
Member
|
Some core devs are currently rewriting the parser so this will be addresser by themy sorry |
Member
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 :( |
Member
|
Mmh, should we consider the fix though? is the legacy API still used a lot today? |
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 theecre.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 += wordonbyteswhen 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.
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_smtplibandtest_imaplibpass.emailModule #136063