Skip to content

Commit 2b1e6e9

Browse files
corona10giampaolo
authored andcommitted
bpo-30119: fix ftplib.FTP.putline() to throw an error for a illegal command (#1214)
1 parent 896145d commit 2b1e6e9

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

Lib/ftplib.py

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

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

Lib/test/test_ftplib.py

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

487487
def test_exceptions(self):
488+
self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\r\n0')
489+
self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\n0')
490+
self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\r0')
488491
self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 400')
489492
self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 499')
490493
self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 500')
@@ -493,7 +496,8 @@ def test_exceptions(self):
493496

494497
def test_all_errors(self):
495498
exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm,
496-
ftplib.error_proto, ftplib.Error, OSError, EOFError)
499+
ftplib.error_proto, ftplib.Error, OSError,
500+
EOFError)
497501
for x in exceptions:
498502
try:
499503
raise x('exception not included in all_errors set')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ Extension Modules
376376
Library
377377
-------
378378

379+
- bpo-30119: ftplib.FTP.putline() now throws ValueError on commands that contains
380+
CR or LF. Patch by Dong-hee Na.
381+
379382
- bpo-30879: os.listdir() and os.scandir() now emit bytes names when called
380383
with bytes-like argument.
381384

0 commit comments

Comments
 (0)