Repository navigation
gh-138223: Fix Infinite loop in email._header_value_parser._fold_mime_parameters when parameter names are too long #138231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChenyangLi4288
wants to merge
16
commits into
python:main
Choose a base branch
from
ChenyangLi4288:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+56
−2
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0a747e5
Fix infinite loop in email.message.EmailMessage.as_string() with long…
ChenyangLi4288 be6f136
Fix infinite loop in email.message.EmailMessage.as_string() with long…
ChenyangLi4288 661b285
Fix infinite loop in email._header_value_parser._fold_mime_parameters…
ChenyangLi4288 eb7028b
trim test script
ChenyangLi4288 94cbe57
Merge branch 'python:main' into main
ChenyangLi4288 02c52aa
remove NEWS in Library
ChenyangLi4288 04debff
remove wrong generated file
ChenyangLi4288 4c93771
gh-138223: Fix infinite loop in email._header_value_parser._fold_mime…
ChenyangLi4288 4bdd6e4
add NEWS
ChenyangLi4288 db6fb5a
Merge branch 'main' of https://github.com/ChenyangLi4288/cpython
ChenyangLi4288 05ccf06
Merge branch 'main' into main
ChenyangLi4288 3a5d21d
Resolve conversation
ChenyangLi4288 8c9c54f
Apply suggestion from @picnixz
ChenyangLi4288 41c6ec5
Address review: simplify maxchars clamp, test non-ASCII values and ex…
ChenyangLi4288 0c2d557
Fix NEWS reference: add_attachment is documented on EmailMessage
ChenyangLi4288 8ac2d45
Merge branch 'main' into main
ChenyangLi4288 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
6 changes: 6 additions & 0 deletions
6
Misc/NEWS.d/next/Security/2025-08-29-14-04-17.gh-issue-138223.gX_GJv.rst
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| :mod:`email.message`: fix an infinite loop in :meth:`EmailMessage.as_string | ||
| <email.message.EmailMessage.as_string>` when :meth:`~email.message.EmailMessage.add_attachment` | ||
| is called with very long parameter keys. | ||
|
|
||
| This could cause denial of service by hanging the application indefinitely | ||
| during MIME parameter folding and :rfc:`2231` encoding. |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct me if I've misunderstood something, but I believe the simpler fix here is the following:
With this change applied instead of yours, your test passes, at least. (I haven't reviewed the test yet; thought I'd post this first). My logic is that if we are already violating the user's request for maxwidth (and we already have a case where we're doing that), we might as well just make the new value something semi-reasonable, rather than "just enough" (one character more than the chrome).
If this isn't enough to protect against an infinite loop (and I haven't tried to reason through that yet, either), then we are going to need a better test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion! I tried maxlen = max(78, chrome_len + 10) in place of my change. It fixes the ASCII cases, but some non-ASCII values still hang, as @devdavidejesus also found:
Here the fallback isn't taken (maxlen > chrome_len + 3), but maxchars is only a few characters, and a single percent-encoded astral character needs 12 (%F0%9F%98%80). The inner loop ends at splitpoint == 0, writes an empty section, and value never gets shorter. Because chrome_len grows once the section number reaches 10, longer values hit this with even more names. In a sweep of names of 40–120 characters and 8 ASCII/non-ASCII values, main hangs in 62 of 112 cases, the max(78, chrome_len + 10) change in 8 (non-ASCII values, names of 60–63 characters), and this PR in none. Every value survives a round trip through message_from_string().
So I kept the forced one-character split. The side effect is that when even one encoded character doesn't fit, that section is written on its own line past maxlen (86 columns for the example above). Since the fallback already exceeds the requested width in this situation, that seemed acceptable to me, but I'm happy to combine both approaches if you prefer.
I've also rewritten the test. It now checks the exact folded output for inputs that hang on main (one of them also hangs with the maxlen change alone), and it checks that a range of long names and non-ASCII values survives a round trip.