diff --git a/Lib/ftplib.py b/Lib/ftplib.py --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -54,6 +54,8 @@ # The standard FTP server control port FTP_PORT = 21 +# The sizehint parameter passed to readline() calls +MAXLINE = 8192 # Exception raised when an error or invalid response is received @@ -62,11 +64,15 @@ class error_temp(Error): pass # 4xx errors class error_perm(Error): pass # 5xx errors class error_proto(Error): pass # response does not begin with [1-5] +class LineTooLong(Error): + def __init__(self, nbytes): + Error.__init__(self, "got more than %d bytes" % nbytes) + # All exceptions (hopefully) that may be raised here and that aren't # (always) programming errors on our side -all_errors = (Error, IOError, EOFError) +all_errors = (Error, IOError, EOFError, LineTooLong) # Line terminators (we always output CRLF, but accept any of CRLF, CR, LF) @@ -100,6 +106,7 @@ debugging = 0 host = '' port = FTP_PORT + maxline = MAXLINE sock = None file = None welcome = None @@ -179,7 +186,9 @@ # Internal: return one line from the server, stripping CRLF. # Raise EOFError if the connection is closed def getline(self): - line = self.file.readline() + line = self.file.readline(self.maxline + 1) + if len(line) > self.maxline: + raise LineTooLong(self.maxline) if self.debugging > 1: print '*get*', self.sanitize(line) if not line: raise EOFError @@ -421,7 +430,9 @@ conn = self.transfercmd(cmd) fp = conn.makefile('rb') while 1: - line = fp.readline() + line = fp.readline(self.maxline + 1) + if len(line) > self.maxline: + raise LineTooLong(self.maxline) if self.debugging > 2: print '*retr*', repr(line) if not line: break @@ -473,7 +484,9 @@ self.voidcmd('TYPE A') conn = self.transfercmd(cmd) while 1: - buf = fp.readline() + buf = fp.readline(self.maxline + 1) + if len(buf) > self.maxline: + raise LineTooLong(self.maxline) if not buf: break if buf[-2:] != CRLF: if buf[-1] in CRLF: buf = buf[:-1] diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py --- a/Lib/test/test_ftplib.py +++ b/Lib/test/test_ftplib.py @@ -46,6 +46,7 @@ self.last_received_cmd = None self.last_received_data = '' self.next_response = '' + self.next_retr_data = '' self.push('220 welcome') def collect_incoming_data(self, data): @@ -162,7 +163,7 @@ def cmd_retr(self, arg): self.push('125 retr ok') - self.dtp.push(RETR_DATA) + self.dtp.push(self.next_retr_data or RETR_DATA) self.dtp.close_when_done() def cmd_list(self, arg): @@ -362,6 +363,20 @@ # IPv4 is in use, just make sure send_epsv has not been used self.assertEqual(self.server.handler.last_received_cmd, 'pasv') + def test_line_too_long(self): + self.assertRaises(ftplib.LineTooLong, self.client.sendcmd, + 'x' * self.client.maxline * 2) + + def test_retrlines_too_long(self): + self.server.handler.next_retr_data = 'x' * self.client.maxline * 2 + received = [] + self.assertRaises(ftplib.LineTooLong, + self.client.retrlines, 'retr', received.append) + + def test_storlines_too_long(self): + f = StringIO.StringIO('x' * self.client.maxline * 2) + self.assertRaises(ftplib.LineTooLong, self.client.storlines, 'stor', f) + class TestIPv6Environment(TestCase):