Skip to content

Commit a016d4e

Browse files
stealthcoptergvanrossum
authored andcommitted
[2.7] bpo-38945: UU Encoding: Don't let newline in filename corrupt the output format (pythonGH-17418). (python#17452)
(cherry picked from commit a62ad47) Co-authored-by: Matthew Rollings <1211162+stealthcopter@users.noreply.github.com>
1 parent 8642071 commit a016d4e

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

Lib/encodings/uu_codec.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def uu_encode(input,errors='strict',filename='<data>',mode=0666):
3131
read = infile.read
3232
write = outfile.write
3333

34+
# Remove newline chars from filename
35+
filename = filename.replace('\n','\\n')
36+
filename = filename.replace('\r','\\r')
37+
3438
# Encode
3539
write('begin %o %s\n' % (mode & 0777, filename))
3640
chunk = read(45)

Lib/test/test_uu.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import cStringIO
1010
import sys
1111
import uu
12+
import io
1213

1314
plaintext = "The smooth-scaled python crept over the sleeping dog\n"
1415

@@ -82,6 +83,15 @@ def test_garbage_padding(self):
8283
decoded = codecs.decode(encodedtext, "uu_codec")
8384
self.assertEqual(decoded, plaintext)
8485

86+
def test_newlines_escaped(self):
87+
# Test newlines are escaped with uu.encode
88+
inp = io.BytesIO(plaintext)
89+
out = io.BytesIO()
90+
filename = "test.txt\n\roverflow.txt"
91+
safefilename = b"test.txt\\n\\roverflow.txt"
92+
uu.encode(inp, out, filename)
93+
self.assertIn(safefilename, out.getvalue())
94+
8595
class UUStdIOTest(unittest.TestCase):
8696

8797
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 = 0666
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)