Skip to content

Commit 8835f46

Browse files
bpo-38945: UU Encoding: Don't let newline in filename corrupt the output format (GH-17418) (GH-17444) (#17445)
(cherry picked from commit a62ad47) Co-authored-by: Matthew Rollings <1211162+stealthcopter@users.noreply.github.com>
1 parent 276eb67 commit 8835f46

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

Lib/encodings/uu_codec.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def uu_encode(input, errors='strict', filename='<data>', mode=0o666):
2020
read = infile.read
2121
write = outfile.write
2222

23+
# Remove newline chars from filename
24+
filename = filename.replace('\n','\\n')
25+
filename = filename.replace('\r','\\r')
26+
2327
# Encode
2428
write(('begin %o %s\n' % (mode & 0o777, filename)).encode('ascii'))
2529
chunk = read(45)

Lib/test/test_uu.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ def test_garbage_padding(self):
115115
decoded = codecs.decode(encodedtext, "uu_codec")
116116
self.assertEqual(decoded, plaintext)
117117

118+
def test_newlines_escaped(self):
119+
# Test newlines are escaped with uu.encode
120+
inp = io.BytesIO(plaintext)
121+
out = io.BytesIO()
122+
filename = "test.txt\n\roverflow.txt"
123+
safefilename = b"test.txt\\n\\roverflow.txt"
124+
uu.encode(inp, out, filename)
125+
self.assertIn(safefilename, out.getvalue())
126+
118127
class UUStdIOTest(unittest.TestCase):
119128

120129
def setUp(self):

Lib/uu.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ def encode(in_file, out_file, name=None, mode=None):
7373
name = '-'
7474
if mode is None:
7575
mode = 0o666
76+
77+
#
78+
# Remove newline chars from name
79+
#
80+
name = name.replace('\n','\\n')
81+
name = name.replace('\r','\\r')
82+
7683
#
7784
# Write the data
7885
#
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Newline characters have been escaped when performing uu encoding to prevent them from overflowing into to content section of the encoded file. This prevents malicious or accidental modification of data during the decoding process.

0 commit comments

Comments
 (0)