From 6c16b108edb731333cbdf2d3f0235605004f4908 Mon Sep 17 00:00:00 2001 From: ruang Date: Fri, 6 Sep 2024 11:51:25 +0800 Subject: [PATCH 01/11] After getting the email encoding format, added strip() to remove unexpected spaces --- Lib/email/message.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/email/message.py b/Lib/email/message.py index 08192c50a8ff5c..9851074b891724 100644 --- a/Lib/email/message.py +++ b/Lib/email/message.py @@ -287,7 +287,7 @@ def get_payload(self, i=None, decode=False): raise TypeError('Expected list, got %s' % type(self._payload)) payload = self._payload # cte might be a Header, so for now stringify it. - cte = str(self.get('content-transfer-encoding', '')).lower() + cte = str(self.get('content-transfer-encoding', '')).lower().strip(' ') # payload may be bytes here. if not decode: if isinstance(payload, str) and utils._has_surrogates(payload): From c09f7de91bb076da9ff128b396915b858fb30ce1 Mon Sep 17 00:00:00 2001 From: ruang Date: Fri, 6 Sep 2024 12:24:49 +0800 Subject: [PATCH 02/11] Added NEWS --- .../Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst diff --git a/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst b/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst new file mode 100644 index 00000000000000..cc1342cef0f1f5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst @@ -0,0 +1,3 @@ +modified the get_payload method in :mod:`email.message` by adding strip() +to the ``cte = str(self.get('content-transfer-encoding', '')).lower()`` line to +remove unwanted spaces. From c797db7633cf59da4d9e7cc906d91d16914f5e2e Mon Sep 17 00:00:00 2001 From: ruang Date: Fri, 6 Sep 2024 12:27:23 +0800 Subject: [PATCH 03/11] Change NEWS --- .../next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst b/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst index cc1342cef0f1f5..22599fe87f3daf 100644 --- a/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst +++ b/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst @@ -1,3 +1,3 @@ -modified the get_payload method in :mod:`email.message` by adding strip() +modified the get_payload method in :mod:`email.message` by adding ``.strip()`` to the ``cte = str(self.get('content-transfer-encoding', '')).lower()`` line to remove unwanted spaces. From 7a2e33ec803af9cde07b4282275e25fe337f299d Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 15 Oct 2024 18:04:15 +0800 Subject: [PATCH 04/11] Change NEWS --- .../Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst b/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst index 22599fe87f3daf..44aab6ee169f6a 100644 --- a/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst +++ b/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst @@ -1,3 +1,2 @@ -modified the get_payload method in :mod:`email.message` by adding ``.strip()`` -to the ``cte = str(self.get('content-transfer-encoding', '')).lower()`` line to -remove unwanted spaces. +Added ``.strip()`` to the :meth:`email.Message.get_payload` +to remove spaces in ``content-transfer-encoding``. From f1b75225c4dd7f6d081efd827c326e87d1afcdb3 Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 15 Oct 2024 18:14:22 +0800 Subject: [PATCH 05/11] Change NEWS --- .../Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst b/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst index 44aab6ee169f6a..2cd7c56d914d85 100644 --- a/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst +++ b/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst @@ -1,2 +1,2 @@ -Added ``.strip()`` to the :meth:`email.Message.get_payload` -to remove spaces in ``content-transfer-encoding``. +Fix :meth:`email.Message.get_payload` couldn't parse headers +with extra spaces. Patch by RUANG. From c00f6f63abc0a212f1f740667e767fb82886dd44 Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 15 Oct 2024 18:34:57 +0800 Subject: [PATCH 06/11] Change NEWS --- .../Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst b/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst index 2cd7c56d914d85..f50877fdc3df64 100644 --- a/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst +++ b/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst @@ -1,2 +1,2 @@ -Fix :meth:`email.Message.get_payload` couldn't parse headers -with extra spaces. Patch by RUANG. +Fix :meth:`email.Message.get_payload` cannot parse +``content-transfer-encoding`` field with extra spaces. Patch by RUANG. From 3f6e880eb2ebf9cbcf764530e8c3e87824bc28d7 Mon Sep 17 00:00:00 2001 From: "RUANG (James Roy)" Date: Tue, 5 Nov 2024 22:12:05 +0800 Subject: [PATCH 07/11] Change NEWS --- .../Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst b/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst index f50877fdc3df64..daa63024a48e96 100644 --- a/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst +++ b/Misc/NEWS.d/next/Library/2024-09-06-11-58-17.gh-issue-123742.jSBNUv.rst @@ -1,2 +1,3 @@ -Fix :meth:`email.Message.get_payload` cannot parse -``content-transfer-encoding`` field with extra spaces. Patch by RUANG. +Fixed :meth:`!email.Message.get_payload` failing to parse the +``content-transfer-encoding`` field with extra spaces. It will now +automatically strip spaces from ``content-transfer-encoding``. From da81539813a6dacf055702680ada9b0c1313e070 Mon Sep 17 00:00:00 2001 From: RUANG Date: Wed, 6 Nov 2024 19:48:26 +0800 Subject: [PATCH 08/11] Add test --- Lib/test/test_email/test_message.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Lib/test/test_email/test_message.py b/Lib/test/test_email/test_message.py index 034f7626c1fc7c..59940dd275bce9 100644 --- a/Lib/test/test_email/test_message.py +++ b/Lib/test/test_email/test_message.py @@ -985,6 +985,30 @@ def test_get_body_malformed(self): # AttributeError: 'str' object has no attribute 'is_attachment' m.get_body() + def test_get_payload_strip_ex_space(self): + """test for gh-123742""" + msg = textwrap.dedent("""\ + From: no-reply@example.com + To: reciever@example.com + Subject: Report + MIME-Version: 1.0 + Content-Type: multipart/mixed; boundary="----=_Part3035080226180533" + Message-ID: <01000191b7fd06ba-d3c9ca36-5171-4a1d-ba3c-b378336cccbc-000000@email.amazonses.com> + Date: Tue, 3 Sep 2024 13:04:58 +0000 + + ------=_Part3035080226180533 + Content-Disposition: attachment; + Content-Type: text/plain; charset=UTF-8 + Content-Transfer-Encoding: base64 + + aGVsbG8= + ------=_Part3035080226180533 + Content-Type: text/html; charset=UTF-8 + Content-Transfer-Encoding: 7bit + """) + m = self._str_msg(msg) + recv_m = m.get_payload(0).get_payload(decode=True) + self.assertEqual(recv_m, b"hello") class TestMIMEPart(TestEmailMessageBase, TestEmailBase): # Doing the full test run here may seem a bit redundant, since the two From 4a8e0e2f0d6a52ca575df064dde9d7855f36b22a Mon Sep 17 00:00:00 2001 From: RUANG Date: Wed, 6 Nov 2024 19:48:43 +0800 Subject: [PATCH 09/11] strip(' ') change to strip() --- Lib/email/message.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/email/message.py b/Lib/email/message.py index 9851074b891724..e7c3a13ca7dd7f 100644 --- a/Lib/email/message.py +++ b/Lib/email/message.py @@ -287,7 +287,7 @@ def get_payload(self, i=None, decode=False): raise TypeError('Expected list, got %s' % type(self._payload)) payload = self._payload # cte might be a Header, so for now stringify it. - cte = str(self.get('content-transfer-encoding', '')).lower().strip(' ') + cte = str(self.get('content-transfer-encoding', '')).lower().strip() # payload may be bytes here. if not decode: if isinstance(payload, str) and utils._has_surrogates(payload): From 1f67d98e0fdc2a63fe9ce0ac92b77530e4d3b72a Mon Sep 17 00:00:00 2001 From: RUANG Date: Wed, 6 Nov 2024 20:08:24 +0800 Subject: [PATCH 10/11] Chang docstring --- Lib/test/test_email/test_message.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_email/test_message.py b/Lib/test/test_email/test_message.py index 59940dd275bce9..116b4b9050b61a 100644 --- a/Lib/test/test_email/test_message.py +++ b/Lib/test/test_email/test_message.py @@ -986,7 +986,11 @@ def test_get_body_malformed(self): m.get_body() def test_get_payload_strip_ex_space(self): - """test for gh-123742""" + # gh-123742 + # + # Test whether get_payload can handle extra + # spaces in 'content-transfer-encoding' field. + msg = textwrap.dedent("""\ From: no-reply@example.com To: reciever@example.com From a48deb7c95659433d3399367c3d9b6a3d81e4330 Mon Sep 17 00:00:00 2001 From: RUANG Date: Wed, 6 Nov 2024 20:38:58 +0800 Subject: [PATCH 11/11] clean ci error --- Lib/test/test_email/test_message.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_email/test_message.py b/Lib/test/test_email/test_message.py index 116b4b9050b61a..3d832aa4cca14e 100644 --- a/Lib/test/test_email/test_message.py +++ b/Lib/test/test_email/test_message.py @@ -991,7 +991,8 @@ def test_get_payload_strip_ex_space(self): # Test whether get_payload can handle extra # spaces in 'content-transfer-encoding' field. - msg = textwrap.dedent("""\ + err_field = 'Content-Transfer-Encoding: base64 ' + msg = textwrap.dedent(f"""\ From: no-reply@example.com To: reciever@example.com Subject: Report @@ -1003,7 +1004,7 @@ def test_get_payload_strip_ex_space(self): ------=_Part3035080226180533 Content-Disposition: attachment; Content-Type: text/plain; charset=UTF-8 - Content-Transfer-Encoding: base64 + {err_field} aGVsbG8= ------=_Part3035080226180533