Skip to content

Commit 2a5a26c

Browse files
corona10larryhastings
authored andcommitted
[3.4] bpo-30119: fix ftplib.FTP.putline() to throw an error for a illegal command (#1214) (#2893)
1 parent 5c673dd commit 2a5a26c

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

Lib/ftplib.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ def sanitize(self, s):
187187

188188
# Internal: send one line to the server, appending CRLF
189189
def putline(self, line):
190+
if '\r' in line or '\n' in line:
191+
raise ValueError('an illegal newline character should not be contained')
190192
line = line + CRLF
191193
if self.debugging > 1:
192194
print('*put*', self.sanitize(line))

Lib/test/test_ftplib.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ def test_sanitize(self):
482482
self.assertEqual(self.client.sanitize('PASS 12345'), repr('PASS *****'))
483483

484484
def test_exceptions(self):
485+
self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\r\n0')
486+
self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\n0')
487+
self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\r0')
485488
self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 400')
486489
self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 499')
487490
self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 500')
@@ -490,7 +493,8 @@ def test_exceptions(self):
490493

491494
def test_all_errors(self):
492495
exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm,
493-
ftplib.error_proto, ftplib.Error, OSError, EOFError)
496+
ftplib.error_proto, ftplib.Error, OSError,
497+
EOFError)
494498
for x in exceptions:
495499
try:
496500
raise x('exception not included in all_errors set')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ftplib.FTP.putline() now throws ValueError on commands that contains CR or
2+
LF. Patch by Dong-hee Na.

0 commit comments

Comments
 (0)