You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Infinite loop in email._header_value_parser._fold_mime_parameters when parameter names are too long #138223
130 characters for a media type parameter name is (a bit) more than https://datatracker.ietf.org/doc/html/rfc6838#section-4.3 allows but I didn't expect it to lead into an infinite loop. I wanted to check if the email module would catch that defect, which apparently it doesn't.
Distribution is openSUSE Leap 15.6, python 3.13.1 was installed from pyenv, 3.6 is from the distribution. The python3.11 binary from the distribution exhibits the same behaviour.
changed the title [-]email.message.EmailMessage().add_attachmen with too long key in params causes infinite loop in as_string()[/-][+]email.message.EmailMessage().add_attachment with too long key in params causes infinite loop in as_string()[/+]on Aug 28, 2025
works perfectly. But changing 'abc' into something else (maybe longer) looks to make it an infinite loop. And I think it's a true infinite loop here. For instance, using abc def 123 instead makes it an infinite loop.
The fix is in _fold_mime_parameters where while True should be replaced by while splitpoint >= 1 and we should forcibly write the line even if we cannot do it in this case (or do whatever the RFC tells us to do)
changed the title [-]email.message.EmailMessage().add_attachment with too long key in params causes infinite loop in as_string()[/-][+]Infinite loop in `email._header_value_parser._fold_mime_parameters` when parameter names are too long[/+]on Aug 28, 2025
Yes, that's because the compat32 policy has a different way to fold thing. The default policy detects that the tokenlist is the part containing MIME parameters so it calls _fold_mime_parameters() which is buggy. For the compat32 policy, we don't hit this (we don't even enter _refold_parse_tree() I think?)
Activity
[-]email.message.EmailMessage().add_attachmen with too long key in params causes infinite loop in as_string()[/-][+]email.message.EmailMessage().add_attachment with too long key in params causes infinite loop in as_string()[/+]StanFromIreland commentedon Aug 28, 2025
StanFromIreland commentedon Aug 28, 2025
Potentially a duplicate of #136063
StanFromIreland commentedon Aug 28, 2025
I can reproduce for length$\ge 62$
picnixz commentedon Aug 28, 2025
picnixz commentedon Aug 28, 2025
Ok, I understand what happens. The issue is with folding. For instance:
works perfectly. But changing 'abc' into something else (maybe longer) looks to make it an infinite loop. And I think it's a true infinite loop here. For instance, using
abc def 123instead makes it an infinite loop.picnixz commentedon Aug 28, 2025
The fix is in
_fold_mime_parameterswherewhile Trueshould be replaced bywhile splitpoint >= 1and we should forcibly write the line even if we cannot do it in this case (or do whatever the RFC tells us to do)[-]email.message.EmailMessage().add_attachment with too long key in params causes infinite loop in as_string()[/-][+]Infinite loop in `email._header_value_parser._fold_mime_parameters` when parameter names are too long[/+]Fix infinite loop in email.message.EmailMessage.as_string() with long…
bedevere-app commentedon Aug 28, 2025
Fix infinite loop in email.message.EmailMessage.as_string() with long…
StanFromIreland commentedon Aug 28, 2025
picnixz commentedon Aug 28, 2025
StanFromIreland commentedon Aug 28, 2025
Fix infinite loop in email.message.EmailMessage.as_string() with long…
Fix infinite loop in email.message.EmailMessage.as_string() with long…
Fix infinite loop in email.message.EmailMessage.as_string() with long…
Fix infinite loop in email._header_value_parser._fold_mime_parameters (…
TauPan commentedon Aug 29, 2025
This is also, probably unsurprisingly, triggered by parsing an email with email.policy.default. It's not triggered with the compat32 policy:
picnixz commentedon Aug 29, 2025
Yes, that's because the compat32 policy has a different way to fold thing. The default policy detects that the tokenlist is the part containing MIME parameters so it calls
_fold_mime_parameters()which is buggy. For the compat32 policy, we don't hit this (we don't even enter_refold_parse_tree()I think?)pythongh-138223: Fix infinite loop in email._header_value_parser._fol…