]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] Correctly fold unknown-8bit originating from encoded words. (GH-142517) (...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 24 Dec 2025 18:20:05 +0000 (19:20 +0100)
committerGitHub <noreply@github.com>
Wed, 24 Dec 2025 18:20:05 +0000 (13:20 -0500)
The unknown-8bit trick was designed to deal with unknown bytes in an
ASCII message, and it works fine for that.  However, I also tried to
extend it to handle bytes that can't be decoded using the charset
specified in an encoded word, and there it fails because there can be
other non-ASCII characters that were *successfully* decoded.  The fix is
simple: do the unknown-8bit encoding using the utf-8 codec.  This is
especially appropriate since anyone trying to do recovery on an unknown
byte string will probably attempt utf-8 first.
(cherry picked from commit 1e17ccd030a2285ad53db5952360fffa33a8a877)

Co-authored-by: R. David Murray <rdmurray@bitdance.com>
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Lib/email/_encoded_words.py
Lib/test/test_email/test__header_value_parser.py
Misc/NEWS.d/next/Library/2025-12-10-10-00-06.gh-issue-142517.fG4hbe.rst [new file with mode: 0644]

index 6795a606de037e2e428f95087e394e9e16a5ebbb..05a34a4c10523369712c7134a9f092d11cad562b 100644 (file)
@@ -219,7 +219,7 @@ def encode(string, charset='utf-8', encoding=None, lang=''):
 
     """
     if charset == 'unknown-8bit':
-        bstring = string.encode('ascii', 'surrogateescape')
+        bstring = string.encode('utf-8', 'surrogateescape')
     else:
         bstring = string.encode(charset)
     if encoding is None:
index f33844910beee4de08ff69e9aca777e1d4c8b18f..426ec4644e3096124d0e1ef660113e9bd279be2a 100644 (file)
@@ -3340,5 +3340,13 @@ class TestFolding(TestEmailBase):
         token = parser.get_address_list(text)[0]
         self._test(token, expected, policy=policy)
 
+    def test_encoded_word_with_undecodable_bytes(self):
+        self._test(parser.get_address_list(
+            ' =?utf-8?Q?=E5=AE=A2=E6=88=B6=E6=AD=A3=E8=A6=8F=E4=BA=A4=E7?='
+                )[0],
+            ' =?unknown-8bit?b?5a6i5oi25q2j6KaP5Lqk5w==?=\n',
+            )
+
+
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2025-12-10-10-00-06.gh-issue-142517.fG4hbe.rst b/Misc/NEWS.d/next/Library/2025-12-10-10-00-06.gh-issue-142517.fG4hbe.rst
new file mode 100644 (file)
index 0000000..d58e532
--- /dev/null
@@ -0,0 +1,4 @@
+The non-``compat32`` :mod:`email` policies now correctly handle refolding
+encoded words that contain bytes that can not be decoded in their specified
+character set.  Previously this resulted in an encoding exception during
+folding.