Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7cf9ce2

Browse files
committed
Fixes for SF #1076485, which I'll apply to the CVS head too. The problem was
caused by a self._input.readline() call that wasn't checking for the NeedsMoreData marker. msg_43.txt contains a message that illustrates the problem, when email.message_from_*() is called. That interface uses the Parser API, which splits reads into 8192 byte chunks. It so happens that for the test message, the 8192 chunk falls inside a message/delivery-status, which is where in the FeedParser the readline() call was that didn't check for NeedsMoreData. I also added an assert to unreadline() so it'll be more evident if an attempt to push back NeedsMoreData ever happens again. Bump the email package version number.
1 parent 6c92d76 commit 7cf9ce2

4 files changed

Lines changed: 274 additions & 4 deletions

File tree

Lib/email/FeedParser.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def readline(self):
8787

8888
def unreadline(self, line):
8989
# Let the consumer push a line back into the buffer.
90+
assert line is not NeedMoreData
9091
self._lines.append(line)
9192

9293
def push(self, data):
@@ -242,8 +243,18 @@ def _parsegen(self):
242243
# EOF. We want to see if we're at the end of this subpart, so
243244
# first consume the blank line, then test the next line to see
244245
# if we're at this subpart's EOF.
245-
line = self._input.readline()
246-
line = self._input.readline()
246+
while True:
247+
line = self._input.readline()
248+
if line is NeedMoreData:
249+
yield NeedMoreData
250+
continue
251+
break
252+
while True:
253+
line = self._input.readline()
254+
if line is NeedMoreData:
255+
yield NeedMoreData
256+
continue
257+
break
247258
if line == '':
248259
break
249260
# Not at EOF so this is a line we're going to need.

Lib/email/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
"""A package for parsing, handling, and generating email messages."""
66

7-
__version__ = '3.0'
7+
__version__ = '3.0+'
88

99
__all__ = [
1010
'base64MIME',

Lib/email/test/data/msg_43.txt

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
From SRS0=aO/[email protected] Fri Nov 26 21:40:36 2004
2+
X-VM-v5-Data: ([nil nil nil nil nil nil nil nil nil]
3+
[nil nil nil nil nil nil nil "MAILER DAEMON <>" "MAILER DAEMON <>" nil nil "Banned file: auto__mail.python.bat in mail from you" "^From:" nil nil nil nil "Banned file: auto__mail.python.bat in mail from you" nil nil nil nil nil nil nil]
4+
nil)
5+
MIME-Version: 1.0
6+
Message-Id: <[email protected]>
7+
Content-Type: multipart/report; report-type=delivery-status;
8+
charset=utf-8;
9+
boundary="----------=_1101526904-1956-5"
10+
X-Virus-Scanned: by XS4ALL Virus Scanner
11+
X-UIDL: 4\G!!!<c"!UV["!M7C!!
12+
From: MAILER DAEMON <>
13+
14+
Subject: Banned file: auto__mail.python.bat in mail from you
15+
Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
16+
17+
This is a multi-part message in MIME format...
18+
19+
------------=_1101526904-1956-5
20+
Content-Type: text/plain; charset="utf-8"
21+
Content-Disposition: inline
22+
Content-Transfer-Encoding: 7bit
23+
24+
BANNED FILENAME ALERT
25+
26+
27+
was blocked by our Spam Firewall. The email you sent with the following subject has NOT BEEN DELIVERED:
28+
29+
Subject: Delivery_failure_notice
30+
31+
An attachment in that mail was of a file type that the Spam Firewall is set to block.
32+
33+
34+
35+
------------=_1101526904-1956-5
36+
Content-Type: message/delivery-status
37+
Content-Disposition: inline
38+
Content-Transfer-Encoding: 7bit
39+
Content-Description: Delivery error report
40+
41+
Reporting-MTA: dns; sacspam01.dot.ca.gov
42+
Received-From-MTA: smtp; sacspam01.dot.ca.gov ([127.0.0.1])
43+
Arrival-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
44+
45+
Final-Recipient: rfc822; [email protected]
46+
Action: failed
47+
Status: 5.7.1
48+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
49+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
50+
51+
Final-Recipient: rfc822; [email protected]
52+
Action: failed
53+
Status: 5.7.1
54+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
55+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
56+
57+
Final-Recipient: rfc822; [email protected]
58+
Action: failed
59+
Status: 5.7.1
60+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
61+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
62+
63+
Final-Recipient: rfc822; [email protected]
64+
Action: failed
65+
Status: 5.7.1
66+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
67+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
68+
69+
Final-Recipient: rfc822; [email protected]
70+
Action: failed
71+
Status: 5.7.1
72+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
73+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
74+
75+
Final-Recipient: rfc822; [email protected]
76+
Action: failed
77+
Status: 5.7.1
78+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
79+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
80+
81+
Final-Recipient: rfc822; [email protected]
82+
Action: failed
83+
Status: 5.7.1
84+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
85+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
86+
87+
Final-Recipient: rfc822; [email protected]
88+
Action: failed
89+
Status: 5.7.1
90+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
91+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
92+
93+
Final-Recipient: rfc822; [email protected]
94+
Action: failed
95+
Status: 5.7.1
96+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
97+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
98+
99+
Final-Recipient: rfc822; [email protected]
100+
Action: failed
101+
Status: 5.7.1
102+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
103+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
104+
105+
Final-Recipient: rfc822; [email protected]
106+
Action: failed
107+
Status: 5.7.1
108+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
109+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
110+
111+
Final-Recipient: rfc822; [email protected]
112+
Action: failed
113+
Status: 5.7.1
114+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
115+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
116+
117+
Final-Recipient: rfc822; [email protected]
118+
Action: failed
119+
Status: 5.7.1
120+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
121+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
122+
123+
Final-Recipient: rfc822; [email protected]
124+
Action: failed
125+
Status: 5.7.1
126+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
127+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
128+
129+
Final-Recipient: rfc822; [email protected]
130+
Action: failed
131+
Status: 5.7.1
132+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
133+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
134+
135+
Final-Recipient: rfc822; [email protected]
136+
Action: failed
137+
Status: 5.7.1
138+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
139+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
140+
141+
Final-Recipient: rfc822; [email protected]
142+
Action: failed
143+
Status: 5.7.1
144+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
145+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
146+
147+
Final-Recipient: rfc822; [email protected]
148+
Action: failed
149+
Status: 5.7.1
150+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
151+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
152+
153+
Final-Recipient: rfc822; [email protected]
154+
Action: failed
155+
Status: 5.7.1
156+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
157+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
158+
159+
Final-Recipient: rfc822; [email protected]
160+
Action: failed
161+
Status: 5.7.1
162+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
163+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
164+
165+
Final-Recipient: rfc822; [email protected]
166+
Action: failed
167+
Status: 5.7.1
168+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
169+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
170+
171+
Final-Recipient: rfc822; [email protected]
172+
Action: failed
173+
Status: 5.7.1
174+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
175+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
176+
177+
Final-Recipient: rfc822; [email protected]
178+
Action: failed
179+
Status: 5.7.1
180+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
181+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
182+
183+
Final-Recipient: rfc822; [email protected]
184+
Action: failed
185+
Status: 5.7.1
186+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
187+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
188+
189+
Final-Recipient: rfc822; [email protected]
190+
Action: failed
191+
Status: 5.7.1
192+
Diagnostic-Code: smtp; 550 5.7.1 Message content rejected, id=01956-02-2 - BANNED: auto__mail.python.bat
193+
Last-Attempt-Date: Fri, 26 Nov 2004 19:41:44 -0800 (PST)
194+
195+
------------=_1101526904-1956-5
196+
Content-Type: text/rfc822-headers
197+
Content-Disposition: inline
198+
Content-Transfer-Encoding: 7bit
199+
Content-Description: Undelivered-message headers
200+
201+
Received: from kgsav.org (ppp-70-242-162-63.dsl.spfdmo.swbell.net [70.242.162.63])
202+
by sacspam01.dot.ca.gov (Spam Firewall) with SMTP
203+
id A232AD03DE3A; Fri, 26 Nov 2004 19:41:35 -0800 (PST)
204+
205+
206+
Date: Sat, 27 Nov 2004 03:35:30 UTC
207+
Subject: Delivery_failure_notice
208+
Importance: Normal
209+
X-Priority: 3 (Normal)
210+
X-MSMail-Priority: Normal
211+
Message-ID: <[email protected]>
212+
MIME-Version: 1.0
213+
Content-Type: multipart/mixed; boundary="====67bd2b7a5.f99f7"
214+
Content-Transfer-Encoding: 7bit
215+
216+
------------=_1101526904-1956-5--
217+

Lib/email/test/test_email.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1973,7 +1973,7 @@ def test_parser(self):
19731973

19741974

19751975
# Test various other bits of the package's functionality
1976-
class TestMiscellaneous(unittest.TestCase):
1976+
class TestMiscellaneous(TestEmailBase):
19771977
def test_message_from_string(self):
19781978
fp = openfile('msg_01.txt')
19791979
try:
@@ -2222,6 +2222,48 @@ def test_charsets_case_insensitive(self):
22222222
uc = Charset('US-ASCII')
22232223
self.assertEqual(lc.get_body_encoding(), uc.get_body_encoding())
22242224

2225+
def test_partial_falls_inside_message_delivery_status(self):
2226+
eq = self.ndiffAssertEqual
2227+
# The Parser interface provides chunks of data to FeedParser in 8192
2228+
# byte gulps. SF bug #1076485 found one of those chunks inside
2229+
# message/delivery-status header block, which triggered an
2230+
# unreadline() of NeedMoreData.
2231+
msg = self._msgobj('msg_43.txt')
2232+
sfp = StringIO()
2233+
Iterators._structure(msg, sfp)
2234+
eq(sfp.getvalue(), """\
2235+
multipart/report
2236+
text/plain
2237+
message/delivery-status
2238+
text/plain
2239+
text/plain
2240+
text/plain
2241+
text/plain
2242+
text/plain
2243+
text/plain
2244+
text/plain
2245+
text/plain
2246+
text/plain
2247+
text/plain
2248+
text/plain
2249+
text/plain
2250+
text/plain
2251+
text/plain
2252+
text/plain
2253+
text/plain
2254+
text/plain
2255+
text/plain
2256+
text/plain
2257+
text/plain
2258+
text/plain
2259+
text/plain
2260+
text/plain
2261+
text/plain
2262+
text/plain
2263+
text/plain
2264+
text/rfc822-headers
2265+
""")
2266+
22252267

22262268

22272269
# Test the iterator/generators

0 commit comments

Comments
 (0)