]> granicus.if.org Git - python/commitdiff
bpo-30119: fix ftplib.FTP.putline() to throw an error for a illegal command (#1214)
authorDong-hee Na <donghee.na92@gmail.com>
Sat, 22 Jul 2017 17:20:22 +0000 (02:20 +0900)
committerGiampaolo Rodola <g.rodola@gmail.com>
Sat, 22 Jul 2017 17:20:22 +0000 (19:20 +0200)
Lib/ftplib.py
Lib/test/test_ftplib.py
Misc/NEWS

index 8f36f537e8a54f550a4678a5afeb57aa070918bf..a02e595cb0226f7fa0a0cbcf0756f24e7421631e 100644 (file)
@@ -186,6 +186,8 @@ class FTP:
 
     # Internal: send one line to the server, appending CRLF
     def putline(self, line):
+        if '\r' in line or '\n' in line:
+            raise ValueError('an illegal newline character should not be contained')
         line = line + CRLF
         if self.debugging > 1:
             print('*put*', self.sanitize(line))
index 6571816bfdf334f79f49f02d9c494f7cf1e02b43..24ea382ff292a6da376d038c86bf471a0a800cd4 100644 (file)
@@ -485,6 +485,9 @@ class TestFTPClass(TestCase):
         self.assertEqual(self.client.sanitize('PASS 12345'), repr('PASS *****'))
 
     def test_exceptions(self):
+        self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\r\n0')
+        self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\n0')
+        self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\r0')
         self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 400')
         self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 499')
         self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 500')
@@ -493,7 +496,8 @@ class TestFTPClass(TestCase):
 
     def test_all_errors(self):
         exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm,
-                      ftplib.error_proto, ftplib.Error, OSError, EOFError)
+                      ftplib.error_proto, ftplib.Error, OSError,
+                      EOFError)
         for x in exceptions:
             try:
                 raise x('exception not included in all_errors set')
index ce4874ac36df6a4b2c6da9937c3187e93ba9c300..38449c0409f458ff7cd73ba0ecc502c7b502a2cd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -376,6 +376,9 @@ Extension Modules
 Library
 -------
 
+- bpo-30119: ftplib.FTP.putline() now throws ValueError on commands that contains
+  CR or LF. Patch by Dong-hee Na.
+
 - bpo-30879: os.listdir() and os.scandir() now emit bytes names when called
   with bytes-like argument.