1 This patch comes from upstream: http://bugs.python.org/issue16042 |
|
2 |
|
3 --- Python-2.6.8/Lib/smtplib.py |
|
4 +++ Python-2.6.8/Lib/smtplib.py |
|
5 @@ -57,6 +57,7 @@ from sys import stderr |
|
6 SMTP_PORT = 25 |
|
7 SMTP_SSL_PORT = 465 |
|
8 CRLF="\r\n" |
|
9 +_MAXLINE = 8192 # more than 8 times larger than RFC 821, 4.5.3 |
|
10 |
|
11 OLDSTYLE_AUTH = re.compile(r"auth=(.*)", re.I) |
|
12 |
|
13 @@ -170,10 +171,14 @@ else: |
|
14 def __init__(self, sslobj): |
|
15 self.sslobj = sslobj |
|
16 |
|
17 - def readline(self): |
|
18 + def readline(self, size=-1): |
|
19 + if size < 0: |
|
20 + size = None |
|
21 str = "" |
|
22 chr = None |
|
23 while chr != "\n": |
|
24 + if size is not None and len(str) >= size: |
|
25 + break |
|
26 chr = self.sslobj.read(1) |
|
27 if not chr: break |
|
28 str += chr |
|
29 @@ -334,11 +339,13 @@ class SMTP: |
|
30 if self.file is None: |
|
31 self.file = self.sock.makefile('rb') |
|
32 while 1: |
|
33 - line = self.file.readline() |
|
34 + line = self.file.readline(_MAXLINE + 1) |
|
35 if line == '': |
|
36 self.close() |
|
37 raise SMTPServerDisconnected("Connection unexpectedly closed") |
|
38 - if self.debuglevel > 0: print>>stderr, 'reply:', repr(line) |
|
39 + if self.debuglevel > 0: print >>stderr, 'reply:', repr(line) |
|
40 + if len(line) > _MAXLINE: |
|
41 + raise SMTPResponseException(500, "Line too long.") |
|
42 resp.append(line[4:].strip()) |
|
43 code=line[:3] |
|
44 # Check that the error code is syntactically correct. |
|
45 |
|
46 --- Python-2.6.8/Lib/test/test_smtplib.py |
|
47 +++ Python-2.6.8/Lib/test/test_smtplib.py |
|
48 @@ -273,6 +273,32 @@ class BadHELOServerTests(TestCase): |
|
49 HOST, self.port, 'localhost', 3) |
|
50 |
|
51 |
|
52 +class TooLongLineTests(TestCase): |
|
53 + respdata = '250 OK' + ('.' * smtplib._MAXLINE * 2) + '\n' |
|
54 + |
|
55 + def setUp(self): |
|
56 + self.old_stdout = sys.stdout |
|
57 + self.output = StringIO.StringIO() |
|
58 + sys.stdout = self.output |
|
59 + |
|
60 + self.evt = threading.Event() |
|
61 + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
62 + self.sock.settimeout(15) |
|
63 + self.port = test_support.bind_port(self.sock) |
|
64 + servargs = (self.evt, self.respdata, self.sock) |
|
65 + threading.Thread(target=server, args=servargs).start() |
|
66 + self.evt.wait() |
|
67 + self.evt.clear() |
|
68 + |
|
69 + def tearDown(self): |
|
70 + self.evt.wait() |
|
71 + sys.stdout = self.old_stdout |
|
72 + |
|
73 + def testLineTooLong(self): |
|
74 + self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP, |
|
75 + HOST, self.port, 'localhost', 3) |
|
76 + |
|
77 + |
|
78 sim_users = {'[email protected]':'John A', |
|
79 '[email protected]':'Sally B', |
|
80 '[email protected]':'Ruth C', |
|
81 @@ -482,7 +508,8 @@ class SMTPSimTests(TestCase): |
|
82 def test_main(verbose=None): |
|
83 test_support.run_unittest(GeneralTests, DebuggingServerTests, |
|
84 NonConnectingTests, |
|
85 - BadHELOServerTests, SMTPSimTests) |
|
86 + BadHELOServerTests, SMTPSimTests, |
|
87 + TooLongLineTests) |
|
88 |
|
89 if __name__ == '__main__': |
|
90 test_main() |
|
91 |
|