Commit e873272
committed
Fix infinite loop in email.message.EmailMessage.as_string() with long 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 #138223 where add_attachment() with long parameter
keys would cause as_string() to hang indefinitely during MIME parameter
folding and header processing.1 parent 502ca0d commit e873272
2 files changed
+7
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3081 | 3081 | | |
3082 | 3082 | | |
3083 | 3083 | | |
3084 | | - | |
| 3084 | + | |
| 3085 | + | |
3085 | 3086 | | |
3086 | 3087 | | |
3087 | 3088 | | |
3088 | 3089 | | |
3089 | 3090 | | |
3090 | 3091 | | |
| 3092 | + | |
| 3093 | + | |
| 3094 | + | |
3091 | 3095 | | |
3092 | 3096 | | |
3093 | 3097 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
527 | 527 | | |
528 | 528 | | |
529 | 529 | | |
| 530 | + | |
| 531 | + | |
530 | 532 | | |
531 | 533 | | |
532 | 534 | | |
| |||
0 commit comments