Skip to content

Commit 8e44cc3

Browse files
author
Ivo Bellin Salarin
committed
fix: reporting the patch made by @vadmium
1 parent d1d8409 commit 8e44cc3

File tree

3 files changed

+44
-74
lines changed

3 files changed

+44
-74
lines changed

Lib/email/feedparser.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,9 @@ def _parse_headers(self, lines):
504504
self._input.unreadline(line)
505505
return
506506
else:
507-
# Weirdly placed unix-from line. Note this as a defect
508-
# and ignore it.
507+
# Weirdly placed unix-from line.
509508
defect = errors.MisplacedEnvelopeHeaderDefect(line)
510-
self._cur.defects.append(defect)
509+
self.policy.handle_defect(self._cur, defect)
511510
continue
512511
# Split the line on the colon separating field name from value.
513512
# There will always be a colon, because if there wasn't the part of
@@ -519,7 +518,7 @@ def _parse_headers(self, lines):
519518
# message. Track the error but keep going.
520519
if i == 0:
521520
defect = errors.InvalidHeaderDefect("Missing header name.")
522-
self._cur.defects.append(defect)
521+
self.policy.handle_defect(self._cur, defect)
523522
continue
524523

525524
assert i>0, "_parse_headers fed line with no : and no leading WS"

Lib/test/test_email/test_defect_handling.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ class TestDefectsBase:
1515
def _raise_point(self, defect):
1616
yield
1717

18+
def get_defects(self, obj):
19+
return obj.defects
20+
21+
def check_defect(self, defect, string):
22+
msg = None
23+
with self._raise_point(defect):
24+
msg = self._str_msg(string)
25+
self.assertEqual(len(self.get_defects(msg)), 1)
26+
self.assertDefectsEqual(self.get_defects(msg), [defect])
27+
return msg
28+
1829
def test_same_boundary_inner_outer(self):
1930
source = textwrap.dedent("""\
2031
Subject: XX
@@ -126,12 +137,10 @@ def test_multipart_invalid_cte(self):
126137
errors.InvalidMultipartContentTransferEncodingDefect)
127138

128139
def test_multipart_no_cte_no_defect(self):
129-
if self.raise_expected: return
130140
msg = self._str_msg(self.multipart_msg.format(''))
131141
self.assertEqual(len(self.get_defects(msg)), 0)
132142

133143
def test_multipart_valid_cte_no_defect(self):
134-
if self.raise_expected: return
135144
for cte in ('7bit', '8bit', 'BINary'):
136145
msg = self._str_msg(
137146
self.multipart_msg.format("\nContent-Transfer-Encoding: "+cte))
@@ -300,8 +309,38 @@ def test_missing_ending_boundary(self):
300309
self.assertDefectsEqual(self.get_defects(msg),
301310
[errors.CloseBoundaryNotFoundDefect])
302311

312+
def test_line_beginning_colon(self):
313+
msg = self.check_defect(errors.InvalidHeaderDefect,
314+
'Subject: Dummy subject\r\n'
315+
': faulty header line\r\n'
316+
'\r\n'
317+
'body\r\n'
318+
)
319+
if msg:
320+
self.assertEqual(msg.items(), [('Subject', 'Dummy subject')])
321+
self.assertEqual(msg.get_payload(), 'body\r\n')
322+
323+
def test_misplaced_envelope(self):
324+
msg = self.check_defect(errors.MisplacedEnvelopeHeaderDefect,
325+
'Subject: Dummy subject\r\n'
326+
'From wtf\r\n'
327+
'To: abc\r\n'
328+
'\r\n'
329+
'body\r\n'
330+
)
331+
if msg:
332+
headers = [('Subject', 'Dummy subject'), ('To', 'abc')]
333+
self.assertEqual(msg.items(), headers)
334+
self.assertEqual(msg.get_payload(), 'body\r\n')
335+
336+
337+
class TestCompat32(TestDefectsBase, TestEmailBase):
338+
339+
policy = policy.compat32
340+
303341

304342
class TestDefectDetection(TestDefectsBase, TestEmailBase):
343+
pass
305344

306345
def get_defects(self, obj):
307346
return obj.defects

Lib/test/test_email/test_email.py

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,50 +2261,6 @@ def test_multipart_no_boundary(self):
22612261
self.assertIsInstance(msg.defects[1],
22622262
errors.MultipartInvariantViolationDefect)
22632263

2264-
multipart_msg = textwrap.dedent("""\
2265-
Date: Wed, 14 Nov 2007 12:56:23 GMT
2266-
2267-
2268-
Subject: Content-Transfer-Encoding: base64 and multipart
2269-
MIME-Version: 1.0
2270-
Content-Type: multipart/mixed;
2271-
boundary="===============3344438784458119861=="{}
2272-
2273-
--===============3344438784458119861==
2274-
Content-Type: text/plain
2275-
2276-
Test message
2277-
2278-
--===============3344438784458119861==
2279-
Content-Type: application/octet-stream
2280-
Content-Transfer-Encoding: base64
2281-
2282-
YWJj
2283-
2284-
--===============3344438784458119861==--
2285-
""")
2286-
2287-
# test_defect_handling
2288-
def test_multipart_invalid_cte(self):
2289-
msg = self._str_msg(
2290-
self.multipart_msg.format("\nContent-Transfer-Encoding: base64"))
2291-
self.assertEqual(len(msg.defects), 1)
2292-
self.assertIsInstance(msg.defects[0],
2293-
errors.InvalidMultipartContentTransferEncodingDefect)
2294-
2295-
# test_defect_handling
2296-
def test_multipart_no_cte_no_defect(self):
2297-
msg = self._str_msg(self.multipart_msg.format(''))
2298-
self.assertEqual(len(msg.defects), 0)
2299-
2300-
# test_defect_handling
2301-
def test_multipart_valid_cte_no_defect(self):
2302-
for cte in ('7bit', '8bit', 'BINary'):
2303-
msg = self._str_msg(
2304-
self.multipart_msg.format(
2305-
"\nContent-Transfer-Encoding: {}".format(cte)))
2306-
self.assertEqual(len(msg.defects), 0)
2307-
23082264
# test_headerregistry.TestContentTypeHeader invalid_1 and invalid_2.
23092265
def test_invalid_content_type(self):
23102266
eq = self.assertEqual
@@ -2381,30 +2337,6 @@ def test_missing_start_boundary(self):
23812337
self.assertIsInstance(bad.defects[0],
23822338
errors.StartBoundaryNotFoundDefect)
23832339

2384-
# test_defect_handling
2385-
def test_first_line_is_continuation_header(self):
2386-
eq = self.assertEqual
2387-
m = ' Line 1\nSubject: test\n\nbody'
2388-
msg = email.message_from_string(m)
2389-
eq(msg.keys(), ['Subject'])
2390-
eq(msg.get_payload(), 'body')
2391-
eq(len(msg.defects), 1)
2392-
self.assertDefectsEqual(msg.defects,
2393-
[errors.FirstHeaderLineIsContinuationDefect])
2394-
eq(msg.defects[0].line, ' Line 1\n')
2395-
2396-
# test_defect_handling
2397-
def test_missing_header_body_separator(self):
2398-
# Our heuristic if we see a line that doesn't look like a header (no
2399-
# leading whitespace but no ':') is to assume that the blank line that
2400-
# separates the header from the body is missing, and to stop parsing
2401-
# headers and start parsing the body.
2402-
msg = self._str_msg('Subject: test\nnot a header\nTo: abc\n\nb\n')
2403-
self.assertEqual(msg.keys(), ['Subject'])
2404-
self.assertEqual(msg.get_payload(), 'not a header\nTo: abc\n\nb\n')
2405-
self.assertDefectsEqual(msg.defects,
2406-
[errors.MissingHeaderBodySeparatorDefect])
2407-
24082340
def test_string_payload_with_extra_space_after_cte(self):
24092341
# https://github.com/python/cpython/issues/98188
24102342
cte = "base64 "

0 commit comments

Comments
 (0)