Skip to content

bpo-36226: Fix multipart false positive header defects #12214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Lib/email/feedparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def close(self):
assert not self._msgstack
# Look for final set of defects
if root.get_content_maintype() == 'multipart' \
and not self._headersonly \
and not root.is_multipart():
defect = errors.MultipartInvariantViolationDefect()
self.policy.handle_defect(root, defect)
Expand Down
2 changes: 1 addition & 1 deletion Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def parse_headers(fp, _class=HTTPMessage):
if line in (b'\r\n', b'\n', b''):
break
hstring = b''.join(headers).decode('iso-8859-1')
return email.parser.Parser(_class=_class).parsestr(hstring)
return email.parser.Parser(_class=_class).parsestr(hstring, headersonly=True)


class HTTPResponse(io.BufferedIOBase):
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_email/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
from email.policy import default
from test.test_email import TestEmailBase

class TestFeedParser(TestEmailBase):
def test_multipart_message_with_headers_only(self):
import email.parser
header = 'Content-Type: multipart/related; boundary="==="\r\n\r\n'
msg = email.parser.Parser().parsestr(header, headersonly=True)
self.assertDefectsEqual(msg.defects, [])

class TestCustomMessage(TestEmailBase):

Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ def test_headers_debuglevel(self):
self.assertEqual(lines[1], "header: First: val")
self.assertEqual(lines[2], "header: Second: val")

def test_parse_valid_multipart_header(self):
header = b'Content-Type: multipart/related; boundary="==="\r\n\r\n'
fp = io.BytesIO(header)
message = client.parse_headers(fp)
self.assertListEqual(message.defects, [])

class TransferEncodingTest(TestCase):
expected_body = b"It's just a flesh wound"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes false MultipartInvariantViolationDefects which were triggered when
only headers of a multipart message were parsed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes false StartBoundaryNotFoundDefects which were triggered by
parse_headers not forwarding the headersonly to the email parser.