Skip to content

Commit fb602e8

Browse files
authored
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
1 parent 8a316ff commit fb602e8

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎Lib/encodings/uu_codec.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,17 @@ def uu_decode(input, errors='strict'):
4646
s = readline()
4747
if not s:
4848
raise ValueError('Missing "begin" line in input data')
49-
if s[:5] == b'begin':
50-
break
49+
# Only a "begin <octal mode>" line, optionally followed by a
50+
# filename, starts the encoded data (as in
51+
# email.message._decode_uu()); anything else is leading text.
52+
if s[:6] != b'begin ':
53+
continue
54+
mode = s[6:].split(b' ', 1)[0]
55+
try:
56+
int(mode, 8)
57+
except ValueError:
58+
continue
59+
break
5160

5261
# Decode
5362
while True:

‎Lib/test/test_codecs.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3251,6 +3251,27 @@ def test_uu_invalid(self):
32513251
# Missing "begin" line
32523252
self.assertRaises(ValueError, codecs.decode, b"", "uu-codec")
32533253

3254+
def test_uu_begin_line(self):
3255+
# Only a "begin <octal mode> [<filename>]" line starts the encoded
3256+
# data; other lines starting with "begin" are leading text.
3257+
block = codecs.encode(b"data", "uu-codec")
3258+
other = codecs.encode(b"other", "uu-codec").partition(b"\n")[2]
3259+
for line in (b"begin 666 name", b"begin 0 name with spaces",
3260+
b"begin 644 name\r", b"begin 666"):
3261+
with self.subTest(line=line):
3262+
encoded = line + b"\n" + other
3263+
self.assertEqual(codecs.decode(encoded + block, "uu-codec"),
3264+
b"other")
3265+
for line in (b"beginning of the text", b"begin", b"begin ",
3266+
b"begin mode name", b"begin 888 name",
3267+
b"beginx 666 name", b"begin-base64 666 name"):
3268+
with self.subTest(line=line):
3269+
encoded = line + b"\n" + other
3270+
self.assertRaises(ValueError, codecs.decode,
3271+
encoded, "uu-codec")
3272+
self.assertEqual(codecs.decode(encoded + block, "uu-codec"),
3273+
b"data")
3274+
32543275

32553276
# The codec system tries to add notes to exceptions in order to ensure
32563277
# the error mentions the operation being performed and the codec involved.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix the ``uu`` codec decoder taking any line that starts with ``begin``, such
2+
as ``beginning of the report``, as the header of the encoded data. Like the
3+
:mod:`email` package, it now requires ``begin`` followed by an octal mode and
4+
skips other lines as leading text.

0 commit comments

Comments
 (0)