diff --git a/Lib/email/message.py b/Lib/email/message.py index 41fcc2b9778798..90b135409f5c7e 100644 --- a/Lib/email/message.py +++ b/Lib/email/message.py @@ -862,7 +862,7 @@ def get_boundary(self, failobj=None): parameter, and it is unquoted. """ missing = object() - boundary = self.get_param('boundary', missing) + boundary = self.get_param('boundary', missing, unquote=False) if boundary is missing: return failobj # RFC 2046 says that boundaries may begin but not end in w/s diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py index b8116d073a2670..978a35abbb63ad 100644 --- a/Lib/test/test_email/test_email.py +++ b/Lib/test/test_email/test_email.py @@ -2209,6 +2209,42 @@ def test_boundary_with_leading_space(self): eq(msg.get_boundary(), ' XXXX') eq(len(msg.get_payload()), 2) + def test_boundary_stripped_only_once(self): + eq = self.assertEqual + msg = email.message_from_string(textwrap.dedent('''\ + MIME-Version: 1.0 + Content-Type: multipart/mixed; boundary="<>" + + --<> + Content-Type: text/plain + + + --<> + Content-Type: text/plain + + --<>-- + ''')) + self.assertTrue(msg.is_multipart()) + eq(msg.get_boundary(), '<>') + eq(len(msg.get_payload()), 2) + + msg = email.message_from_string(textwrap.dedent('''\ + MIME-Version: 1.0 + Content-Type: multipart/mixed; boundary=<""> + + --"" + Content-Type: text/plain + + + --"" + Content-Type: text/plain + + --""-- + ''')) + self.assertTrue(msg.is_multipart()) + eq(msg.get_boundary(), '""') + eq(len(msg.get_payload()), 2) + def test_boundary_without_trailing_newline(self): m = Parser().parsestr("""\ Content-Type: multipart/mixed; boundary="===============0012394164==" diff --git a/Misc/NEWS.d/next/Library/2025-06-24-21-08-26.gh-issue-135854.j0WMfD.rst b/Misc/NEWS.d/next/Library/2025-06-24-21-08-26.gh-issue-135854.j0WMfD.rst new file mode 100644 index 00000000000000..95f3ced404c8b1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-24-21-08-26.gh-issue-135854.j0WMfD.rst @@ -0,0 +1 @@ +Fix :func:`email.message.Message.get_boundary` strips boundaries twice.