[3.13] gh-138223: Fix Infinite loop in email._header_value_parser._fold_mime_parameters when parameter names are too long (GH-138223)#138227
Closed
ChenyangLi4288 wants to merge 2 commits intopython:3.13from
Closed
Conversation
… parameter keys The infinite loop occurred in _fold_mime_parameters() when processing MIME parameters with very long keys (≥64 characters) during RFC 2231 encoding. The issue was in two locations: 1. In email._header_value_parser._fold_mime_parameters(): - Replace infinite while True: loop with while splitpoint > 1: - Ensure splitpoint is always at least 1 to prevent getting stuck - Add fallback logic to force minimal splits when values cannot fit 2. In email.header._ValueFormatter._append_chunk(): - Add safety check for extremely long strings that cannot be split - Force line breaks when no suitable split points are found - Prevent infinite loops in header folding for edge cases This fixes GitHub issue python#138223 where add_attachment() with long parameter keys would cause as_string() to hang indefinitely during MIME parameter folding and header processing.
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
… parameter keys The infinite loop occurred in _fold_mime_parameters() when processing MIME parameters with very long keys (64 characters) during RFC 2231 encoding. The issue was in two locations: 1. In email._header_value_parser._fold_mime_parameters(): - Replace infinite 'while True:' loop with 'while splitpoint > 1:' - Ensure splitpoint is always at least 1 to prevent getting stuck - Add fallback logic to force minimal splits when values cannot fit 2. In email.header._ValueFormatter._append_chunk(): - Add safety check for extremely long strings that cannot be split - Force line breaks when no suitable split points are found - Prevent infinite loops in header folding for edge cases This fixes GitHub issue python#138223 where add_attachment() with long parameter keys would cause as_string() to hang indefinitely during MIME parameter folding and header processing.
Member
|
Please create the PR against main, changing it will result in #138226. |
Member
|
Before jumping to a PR, please read https://devguide.python.org/getting-started/pull-request-lifecycle carefully. In addition, please check the RFC for what to do. |
Author
Hi Stan, thank you for your review! Should I create this PR against main other than 3.13? |
Member
|
Please see Benedikts message. It's the branch "main" |
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.
-- Replaced while True: with while splitpoint > 1: to prevent infinite loops
-- Added splitpoint = max(1, splitpoint) to ensure splitpoint is always at least 1
-- Added fallback logic to force a minimal split if splitpoint becomes too small
-- Fixed the outer while value: loop to ensure progress is always made
-- Added force-split logic to handle extremely long strings that can't be split by standard methods
-- Prevents infinite loops when no suitable split points are found
email._header_value_parser._fold_mime_parameterswhen parameter names are too long #138223