https://github.com/python/cpython/commit/fb602e83624237c6be62b025127f7652bef9769b
commit: fb602e83624237c6be62b025127f7652bef9769b
branch: main
author: Gregory P. Smith <[email protected]>
committer: gpshead <[email protected]>
date: 2026-11-01T18:21:42Z
summary:

gh-161092: Require an octal mode in the uu codec "begin" line (GH-161093)

Skip lines starting with "begin" that are not followed by an octal mode,
as email.message._decode_uu() does, instead of taking them as the header.
ANT-2026-XQTI5P71

files:
A Misc/NEWS.d/next/Library/2026-10-22-05-20-02.gh-issue-161092.OtC5l6.rst
M Lib/encodings/uu_codec.py
M Lib/test/test_codecs.py

diff --git a/Lib/encodings/uu_codec.py b/Lib/encodings/uu_codec.py
index 4f8704016e2131f..56ea394a7806de9 100644
--- a/Lib/encodings/uu_codec.py
+++ b/Lib/encodings/uu_codec.py
@@ -46,8 +46,17 @@ def uu_decode(input, errors='strict'):
         s = readline()
         if not s:
             raise ValueError('Missing "begin" line in input data')
-        if s[:5] == b'begin':
-            break
+        # Only a "begin <octal mode>" line, optionally followed by a
+        # filename, starts the encoded data (as in
+        # email.message._decode_uu()); anything else is leading text.
+        if s[:6] != b'begin ':
+            continue
+        mode = s[6:].split(b' ', 1)[0]
+        try:
+            int(mode, 8)
+        except ValueError:
+            continue
+        break
 
     # Decode
     while True:
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
index 37ac57063c35390..3e174fb0130c262 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -3251,6 +3251,27 @@ def test_uu_invalid(self):
         # Missing "begin" line
         self.assertRaises(ValueError, codecs.decode, b"", "uu-codec")
 
+    def test_uu_begin_line(self):
+        # Only a "begin <octal mode> [<filename>]" line starts the encoded
+        # data; other lines starting with "begin" are leading text.
+        block = codecs.encode(b"data", "uu-codec")
+        other = codecs.encode(b"other", "uu-codec").partition(b"\n")[2]
+        for line in (b"begin 666 name", b"begin 0 name with spaces",
+                     b"begin 644 name\r", b"begin 666"):
+            with self.subTest(line=line):
+                encoded = line + b"\n" + other
+                self.assertEqual(codecs.decode(encoded + block, "uu-codec"),
+                                 b"other")
+        for line in (b"beginning of the text", b"begin", b"begin ",
+                     b"begin mode name", b"begin 888 name",
+                     b"beginx 666 name", b"begin-base64 666 name"):
+            with self.subTest(line=line):
+                encoded = line + b"\n" + other
+                self.assertRaises(ValueError, codecs.decode,
+                                  encoded, "uu-codec")
+                self.assertEqual(codecs.decode(encoded + block, "uu-codec"),
+                                 b"data")
+
 
 # The codec system tries to add notes to exceptions in order to ensure
 # the error mentions the operation being performed and the codec involved.
diff --git 
a/Misc/NEWS.d/next/Library/2026-10-22-05-20-02.gh-issue-161092.OtC5l6.rst 
b/Misc/NEWS.d/next/Library/2026-10-22-05-20-02.gh-issue-161092.OtC5l6.rst
new file mode 100644
index 000000000000000..abdc68c7f7a5dbd
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-10-22-05-20-02.gh-issue-161092.OtC5l6.rst
@@ -0,0 +1,4 @@
+Fix the ``uu`` codec decoder taking any line that starts with ``begin``, such
+as ``beginning of the report``, as the header of the encoded data. Like the
+:mod:`email` package, it now requires ``begin`` followed by an octal mode and
+skips other lines as leading text.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to