diff --git a/Doc/library/email.utils.rst b/Doc/library/email.utils.rst index e0d2c19a3b0737..da55341a92c787 100644 --- a/Doc/library/email.utils.rst +++ b/Doc/library/email.utils.rst @@ -212,7 +212,7 @@ of the new API. :rfc:`2231` header is not known by Python; it defaults to ``'us-ascii'``. For convenience, if the *value* passed to :func:`collapse_rfc2231_value` is not - a tuple, it should be a string and it is returned unquoted. + a 3-tuple, it should be a string and it is returned unquoted. .. function:: decode_params(params) diff --git a/Lib/email/utils.py b/Lib/email/utils.py index d4824dc3601b2d..0c8bf384249bb1 100644 --- a/Lib/email/utils.py +++ b/Lib/email/utils.py @@ -449,6 +449,10 @@ def decode_params(params): def collapse_rfc2231_value(value, errors='replace', fallback_charset='us-ascii'): if not isinstance(value, tuple) or len(value) != 3: + if not isinstance(value, str): + raise TypeError( + f"expected str or 3-tuple, got {type(value).__name__}" + ) return unquote(value) # While value comes to us as a unicode string, we need it to be a bytes # object. We do not want bytes() normal utf-8 decoder, we want a straight diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py index 4e6c213510c74c..aa6053002c2977 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -5789,6 +5789,12 @@ def test_should_not_hang_on_invalid_ew_messages(self): with self.subTest(m=m): msg = email.message_from_string(m) + def test_collapse_rfc2231_value_non_3_tuple(self): + for val in [(), ('a',), ('a', 'b'), ('a', 'b', 'c', 'd'), 42, None]: + with self.subTest(val=val): + with self.assertRaises(TypeError): + email.utils.collapse_rfc2231_value(val) + # Tests to ensure that signed parts of an email are completely preserved, as # required by RFC1847 section 2.1. Note that these are incomplete, because the diff --git a/Misc/NEWS.d/next/Library/2026-03-11-14-42-23.gh-issue-145824.HMjVqq.rst b/Misc/NEWS.d/next/Library/2026-03-11-14-42-23.gh-issue-145824.HMjVqq.rst new file mode 100644 index 00000000000000..b6c56519b87a17 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-03-11-14-42-23.gh-issue-145824.HMjVqq.rst @@ -0,0 +1,3 @@ +:func:`email.utils.collapse_rfc2231_value` now raises :exc:`TypeError` +instead of :exc:`AttributeError` when called with a value that is neither a +string nor a 3-tuple.