Skip to content

Conversation

ChenyangLi4288
Copy link

@ChenyangLi4288 ChenyangLi4288 commented Aug 28, 2025

  1. Fixed _fold_mime_parameters in Lib/email/_header_value_parser.py:
    -- 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
  2. Enhanced _append_chunk in Lib/email/header.py:
    -- 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

… 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.
@ChenyangLi4288 ChenyangLi4288 requested a review from a team as a code owner August 28, 2025 16:17
@python-cla-bot
Copy link

python-cla-bot bot commented Aug 28, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Aug 28, 2025

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 skip news label instead.

@ChenyangLi4288 ChenyangLi4288 changed the title Fix infinite loop in email.message.EmailMessage.as_string() with long… [3.13] gh-138223: Fix Infinite loop in email._header_value_parser._fold_mime_parameters when parameter names are too long (GH-138223) Aug 28, 2025
@bedevere-app bedevere-app bot added topic-email type-bug An unexpected behavior, bug, or error type-security A security issue labels Aug 28, 2025
… 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.
@StanFromIreland
Copy link
Member

Please create the PR against main, changing it will result in #138226.

@picnixz
Copy link
Member

picnixz commented Aug 28, 2025

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.

@ChenyangLi4288
Copy link
Author

Please create the PR against main, changing it will result in #138226.

Hi Stan, thank you for your review! Should I create this PR against main other than 3.13?

@StanFromIreland
Copy link
Member

Please see Benedikts message.

It's the branch "main"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review topic-email type-bug An unexpected behavior, bug, or error type-security A security issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants