Skip to content

Commit f200776

Browse files
miss-islingtonnillebvadmiumIvo Bellin Salarin
authored
[3.14] gh-68552: fix defects policy (GH-138579) (#142366)
Co-authored-by: Ivo Bellin Salarin <[email protected]> Co-authored-by: Martin Panter <[email protected]> Co-authored-by: Ivo Bellin Salarin <[email protected]>
1 parent 378b24b commit f200776

File tree

4 files changed

+48
-94
lines changed

4 files changed

+48
-94
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: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,10 @@ def test_multipart_invalid_cte(self):
126126
errors.InvalidMultipartContentTransferEncodingDefect)
127127

128128
def test_multipart_no_cte_no_defect(self):
129-
if self.raise_expected: return
130129
msg = self._str_msg(self.multipart_msg.format(''))
131130
self.assertEqual(len(self.get_defects(msg)), 0)
132131

133132
def test_multipart_valid_cte_no_defect(self):
134-
if self.raise_expected: return
135133
for cte in ('7bit', '8bit', 'BINary'):
136134
msg = self._str_msg(
137135
self.multipart_msg.format("\nContent-Transfer-Encoding: "+cte))
@@ -300,6 +298,47 @@ def test_missing_ending_boundary(self):
300298
self.assertDefectsEqual(self.get_defects(msg),
301299
[errors.CloseBoundaryNotFoundDefect])
302300

301+
def test_line_beginning_colon(self):
302+
string = (
303+
"Subject: Dummy subject\r\n: faulty header line\r\n\r\nbody\r\n"
304+
)
305+
306+
with self._raise_point(errors.InvalidHeaderDefect):
307+
msg = self._str_msg(string)
308+
self.assertEqual(len(self.get_defects(msg)), 1)
309+
self.assertDefectsEqual(
310+
self.get_defects(msg), [errors.InvalidHeaderDefect]
311+
)
312+
313+
if msg:
314+
self.assertEqual(msg.items(), [("Subject", "Dummy subject")])
315+
self.assertEqual(msg.get_payload(), "body\r\n")
316+
317+
def test_misplaced_envelope(self):
318+
string = (
319+
"Subject: Dummy subject\r\nFrom wtf\r\nTo: abc\r\n\r\nbody\r\n"
320+
)
321+
with self._raise_point(errors.MisplacedEnvelopeHeaderDefect):
322+
msg = self._str_msg(string)
323+
self.assertEqual(len(self.get_defects(msg)), 1)
324+
self.assertDefectsEqual(
325+
self.get_defects(msg), [errors.MisplacedEnvelopeHeaderDefect]
326+
)
327+
328+
if msg:
329+
headers = [("Subject", "Dummy subject"), ("To", "abc")]
330+
self.assertEqual(msg.items(), headers)
331+
self.assertEqual(msg.get_payload(), "body\r\n")
332+
333+
334+
335+
class TestCompat32(TestDefectsBase, TestEmailBase):
336+
337+
policy = policy.compat32
338+
339+
def get_defects(self, obj):
340+
return obj.defects
341+
303342

304343
class TestDefectDetection(TestDefectsBase, TestEmailBase):
305344

@@ -332,6 +371,9 @@ def _raise_point(self, defect):
332371
with self.assertRaises(defect):
333372
yield
334373

374+
def get_defects(self, obj):
375+
return obj.defects
376+
335377

336378
if __name__ == '__main__':
337379
unittest.main()

Lib/test/test_email/test_email.py

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,70 +2262,6 @@ def test_parse_missing_minor_type(self):
22622262
eq(msg.get_content_maintype(), 'text')
22632263
eq(msg.get_content_subtype(), 'plain')
22642264

2265-
# test_defect_handling
2266-
def test_same_boundary_inner_outer(self):
2267-
msg = self._msgobj('msg_15.txt')
2268-
# XXX We can probably eventually do better
2269-
inner = msg.get_payload(0)
2270-
self.assertHasAttr(inner, 'defects')
2271-
self.assertEqual(len(inner.defects), 1)
2272-
self.assertIsInstance(inner.defects[0],
2273-
errors.StartBoundaryNotFoundDefect)
2274-
2275-
# test_defect_handling
2276-
def test_multipart_no_boundary(self):
2277-
msg = self._msgobj('msg_25.txt')
2278-
self.assertIsInstance(msg.get_payload(), str)
2279-
self.assertEqual(len(msg.defects), 2)
2280-
self.assertIsInstance(msg.defects[0],
2281-
errors.NoBoundaryInMultipartDefect)
2282-
self.assertIsInstance(msg.defects[1],
2283-
errors.MultipartInvariantViolationDefect)
2284-
2285-
multipart_msg = textwrap.dedent("""\
2286-
Date: Wed, 14 Nov 2007 12:56:23 GMT
2287-
2288-
2289-
Subject: Content-Transfer-Encoding: base64 and multipart
2290-
MIME-Version: 1.0
2291-
Content-Type: multipart/mixed;
2292-
boundary="===============3344438784458119861=="{}
2293-
2294-
--===============3344438784458119861==
2295-
Content-Type: text/plain
2296-
2297-
Test message
2298-
2299-
--===============3344438784458119861==
2300-
Content-Type: application/octet-stream
2301-
Content-Transfer-Encoding: base64
2302-
2303-
YWJj
2304-
2305-
--===============3344438784458119861==--
2306-
""")
2307-
2308-
# test_defect_handling
2309-
def test_multipart_invalid_cte(self):
2310-
msg = self._str_msg(
2311-
self.multipart_msg.format("\nContent-Transfer-Encoding: base64"))
2312-
self.assertEqual(len(msg.defects), 1)
2313-
self.assertIsInstance(msg.defects[0],
2314-
errors.InvalidMultipartContentTransferEncodingDefect)
2315-
2316-
# test_defect_handling
2317-
def test_multipart_no_cte_no_defect(self):
2318-
msg = self._str_msg(self.multipart_msg.format(''))
2319-
self.assertEqual(len(msg.defects), 0)
2320-
2321-
# test_defect_handling
2322-
def test_multipart_valid_cte_no_defect(self):
2323-
for cte in ('7bit', '8bit', 'BINary'):
2324-
msg = self._str_msg(
2325-
self.multipart_msg.format(
2326-
"\nContent-Transfer-Encoding: {}".format(cte)))
2327-
self.assertEqual(len(msg.defects), 0)
2328-
23292265
# test_headerregistry.TestContentTypeHeader invalid_1 and invalid_2.
23302266
def test_invalid_content_type(self):
23312267
eq = self.assertEqual
@@ -2402,30 +2338,6 @@ def test_missing_start_boundary(self):
24022338
self.assertIsInstance(bad.defects[0],
24032339
errors.StartBoundaryNotFoundDefect)
24042340

2405-
# test_defect_handling
2406-
def test_first_line_is_continuation_header(self):
2407-
eq = self.assertEqual
2408-
m = ' Line 1\nSubject: test\n\nbody'
2409-
msg = email.message_from_string(m)
2410-
eq(msg.keys(), ['Subject'])
2411-
eq(msg.get_payload(), 'body')
2412-
eq(len(msg.defects), 1)
2413-
self.assertDefectsEqual(msg.defects,
2414-
[errors.FirstHeaderLineIsContinuationDefect])
2415-
eq(msg.defects[0].line, ' Line 1\n')
2416-
2417-
# test_defect_handling
2418-
def test_missing_header_body_separator(self):
2419-
# Our heuristic if we see a line that doesn't look like a header (no
2420-
# leading whitespace but no ':') is to assume that the blank line that
2421-
# separates the header from the body is missing, and to stop parsing
2422-
# headers and start parsing the body.
2423-
msg = self._str_msg('Subject: test\nnot a header\nTo: abc\n\nb\n')
2424-
self.assertEqual(msg.keys(), ['Subject'])
2425-
self.assertEqual(msg.get_payload(), 'not a header\nTo: abc\n\nb\n')
2426-
self.assertDefectsEqual(msg.defects,
2427-
[errors.MissingHeaderBodySeparatorDefect])
2428-
24292341
def test_string_payload_with_extra_space_after_cte(self):
24302342
# https://github.com/python/cpython/issues/98188
24312343
cte = "base64 "
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``MisplacedEnvelopeHeaderDefect`` and ``Missing header name`` defects are now correctly passed to the ``handle_defect`` method of ``policy`` in :class:`~email.parser.FeedParser`.

0 commit comments

Comments
 (0)