diff --git a/Lib/smtplib.py b/Lib/smtplib.py --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -62,6 +62,7 @@ SMTP_SSL_PORT = 465 CRLF = "\r\n" bCRLF = b"\r\n" +_MAXLINE = 8192 # more than 8 times larger than RFC 821, 4.5.3 OLDSTYLE_AUTH = re.compile(r"auth=(.*)", re.I) @@ -133,6 +134,10 @@ combination provided. """ +class SMTPTooLongError(SMTPResponseException): + """Line too long.""" + + def quoteaddr(addr): """Quote a subset of the email addresses defined by RFC 821. @@ -188,7 +193,9 @@ def __init__(self, sslobj): self.sslobj = sslobj - def readline(self): + def readline(self, size=-1): + if size < 0: + size = None str = b"" chr = None while chr != b"\n": @@ -196,6 +203,8 @@ if not chr: break str += chr + if size is not None and len(str) == size: + break return str def close(self): @@ -363,7 +372,7 @@ self.file = self.sock.makefile('rb') while 1: try: - line = self.file.readline() + line = self.file.readline(_MAXLINE + 1) except socket.error as e: self.close() raise SMTPServerDisconnected("Connection unexpectedly closed: " @@ -373,6 +382,8 @@ raise SMTPServerDisconnected("Connection unexpectedly closed") if self.debuglevel > 0: print('reply:', repr(line), file=stderr) + if len(line) > _MAXLINE: + raise SMTPTooLongError(500, "Line too long.") resp.append(line[4:].strip(b' \t\r\n')) code = line[:3] # Check that the error code is syntactically correct. diff --git a/Lib/test/mock_socket.py b/Lib/test/mock_socket.py --- a/Lib/test/mock_socket.py +++ b/Lib/test/mock_socket.py @@ -21,7 +21,7 @@ """ def __init__(self, lines): self.lines = lines - def readline(self): + def readline(self, size=-1): return self.lines.pop(0) + b'\r\n' def close(self): pass