Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3080,14 +3080,25 @@ def _fold_mime_parameters(part, lines, maxlen, encoding):
# have that, we'd be stuck, so in that case fall back to
# the RFC standard width.
maxlen = 78
splitpoint = maxchars = maxlen - chrome_len - 2
while True:
maxchars = maxlen - chrome_len - 2
Comment on lines 3080 to +3083
Copy link
Copy Markdown
Member

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:

-               # have that, we'd be stuck, so in that case fall back to
-               # the RFC standard width.
-               maxlen = 78
+               # the RFC standard width, or the width of the chrome plus
+               # an arbitrary 10 if the parameter name is long.
+               maxlen = max(78, chrome_len + 10

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.

# Ensure maxchars is at least 1 to prevent negative values
if maxchars <= 0:
maxchars = 1
splitpoint = maxchars
splitpoint = max(1, splitpoint) # Ensure splitpoint is always at least 1
Comment on lines +3083 to +3088
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
maxchars = maxlen - chrome_len - 2
# Ensure maxchars is at least 1 to prevent negative values
if maxchars <= 0:
maxchars = 1
splitpoint = maxchars
splitpoint = max(1, splitpoint) # Ensure splitpoint is always at least 1
maxchars = maxlen - chrome_len - 2
splitpoint = maxchars = max(1, maxchars)

while splitpoint > 1:
partial = value[:splitpoint]
encoded_value = urllib.parse.quote(
partial, safe='', errors=error_handler)
if len(encoded_value) <= maxchars:
break
splitpoint -= 1
else:
# If we still can't fit, force a minimal split
splitpoint = 1
partial = value[:splitpoint]
encoded_value = urllib.parse.quote(
partial, safe='', errors=error_handler)
lines.append(" {}*{}*={}{}".format(
name, section, extra_chrome, encoded_value))
extra_chrome = ''
Expand Down
55 changes: 55 additions & 0 deletions Lib/test/test_email/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,61 @@ def test_string_payload_with_multipart_content_type(self):
attachments = msg.iter_attachments()
self.assertEqual(list(attachments), [])

def test_mime_parameter_folding_no_infinite_loop(self):
msg = self._make_message()
msg.add_attachment(
b"test content",
maintype="text",
subtype="plain",
filename="test.txt"
)
maxlen = 78
extra_chrome = "utf-8''"
section = 0

# Test with various parameter name and value lengths
test_cases = [
# (name_len, value_len)
(maxlen - 3 - len(str(section)) - 3 - len(extra_chrome), 100),
(50, 200),
(60, 150),
(70, 50),
]

for name_len, value_len in test_cases:
with self.subTest(name_len=name_len, value_len=value_len):
msg_test = self._make_message()
msg_test.add_attachment(
b"test content",
maintype="text",
subtype="plain",
filename="test.txt"
)
param_name = "a" * name_len
param_value = "b" * value_len
msg_test.set_param(param_name, param_value)
result = msg_test.as_string()

# Check that the result is a valid string
self.assertIsInstance(result, str)

# Check that the parameter name appears in the result
self.assertIn(param_name, result)

# Check that the header is properly formatted:
# - Should have param_name*0*= followed by the first char of encoded value
lines = result.split('\n')
found_param = False
for line in lines:
if param_name in line:
found_param = True
# Check that we have the expected format: param_name*N*=...
self.assertRegex(line, rf'{param_name}\*\d+\*=')
# Verify the line starts with proper continuation if not first
if not line.startswith('Content-'):
self.assertTrue(line.startswith(' '),
f"Continuation line should start with space: {line}")
self.assertTrue(found_param, f"Parameter {param_name} not found in output")

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
:mod:`email.message`: fix an infinite loop :meth:`EmailMessage.as_string
<email.message.EmailMessage.as_string>` when :meth:`~email.message.MIMEPart.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.
Loading