diff options
author | Benjamin Peterson <benjamin@python.org> | 2014-12-06 01:05:18 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2014-12-06 01:05:18 (GMT) |
commit | dabfc56b57f5086eb5522d8e6cd7670c62d2482d (patch) | |
tree | 7f91d7fa465e56973ec6bf5bf7472cac5da70706 /Lib/smtplib.py | |
parent | faad6bbea6c86e30c770eb0a3648e2cd52b2e55e (diff) | |
download | cpython-dabfc56b57f5086eb5522d8e6cd7670c62d2482d.zip cpython-dabfc56b57f5086eb5522d8e6cd7670c62d2482d.tar.gz cpython-dabfc56b57f5086eb5522d8e6cd7670c62d2482d.tar.bz2 |
smtplib: limit amount read from the network (closes #16042)
Diffstat (limited to 'Lib/smtplib.py')
-rwxr-xr-x | Lib/smtplib.py | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/Lib/smtplib.py b/Lib/smtplib.py index 3e1672a..d1c2806 100755 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -57,6 +57,7 @@ __all__ = ["SMTPException", "SMTPServerDisconnected", "SMTPResponseException", SMTP_PORT = 25 SMTP_SSL_PORT = 465 CRLF = "\r\n" +_MAXLINE = 8192 # more than 8 times larger than RFC 821, 4.5.3 OLDSTYLE_AUTH = re.compile(r"auth=(.*)", re.I) @@ -179,10 +180,14 @@ else: def __init__(self, sslobj): self.sslobj = sslobj - def readline(self): + def readline(self, size=-1): + if size < 0: + size = None str = "" chr = None while chr != "\n": + if size is not None and len(str) >= size: + break chr = self.sslobj.read(1) if not chr: break @@ -353,7 +358,7 @@ class SMTP: 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: " @@ -363,6 +368,8 @@ class SMTP: raise SMTPServerDisconnected("Connection unexpectedly closed") if self.debuglevel > 0: print>>stderr, 'reply:', repr(line) + if len(line) > _MAXLINE: + raise SMTPResponseException(500, "Line too long.") resp.append(line[4:].strip()) code = line[:3] # Check that the error code is syntactically correct. |