Skip to content

Commit 30afc91

Browse files
miss-islingtonstealthcopter
authored andcommitted
bpo-38945: UU Encoding: Don't let newline in filename corrupt the output format (GH-17418) (GH-17444)
(cherry picked from commit a62ad47) Co-authored-by: Matthew Rollings <[email protected]>
1 parent 0716056 commit 30afc91

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
@@ -114,6 +114,15 @@ def test_garbage_padding(self):
114114
decoded = codecs.decode(encodedtext, "uu_codec")
115115
self.assertEqual(decoded, plaintext)
116116

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

119128
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)