Skip to content

Commit

Permalink
bpo-38698: Add a new InvalidMessageID token to email header parser. (G…
Browse files Browse the repository at this point in the history
…H-17503)

This adds a new InvalidMessageID token to the email header parser which can be
used to represent invalid message-id headers in the parse tree.
(cherry picked from commit 68157da)

Co-authored-by: Abhilash Raj <maxking@users.noreply.github.com>
  • Loading branch information
miss-islington and maxking committed Dec 9, 2019
1 parent 960fca1 commit f66f4a0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
20 changes: 16 additions & 4 deletions Lib/email/_header_value_parser.py
Expand Up @@ -850,10 +850,15 @@ def fold(self, policy):
# message-id tokens may not be folded.
return str(self) + policy.linesep


class MessageID(MsgID):
token_type = 'message-id'


class InvalidMessageID(MessageID):
token_type = 'invalid-message-id'


class Header(TokenList):
token_type = 'header'

Expand Down Expand Up @@ -2110,11 +2115,18 @@ def parse_message_id(value):
message_id = MessageID()
try:
token, value = get_msg_id(value)
except errors.HeaderParseError:
message_id.defects.append(errors.InvalidHeaderDefect(
"Expected msg-id but found {!r}".format(value)))
else:
message_id.append(token)
except errors.HeaderParseError as ex:
token = get_unstructured(value)
message_id = InvalidMessageID(token)
message_id.defects.append(
errors.InvalidHeaderDefect("Invalid msg-id: {!r}".format(ex)))
else:
# Value after parsing a valid msg_id should be None.
if value:
message_id.defects.append(errors.InvalidHeaderDefect(
"Unexpected {!r}".format(value)))

return message_id

#
Expand Down
40 changes: 37 additions & 3 deletions Lib/test/test_email/test__header_value_parser.py
Expand Up @@ -2639,10 +2639,44 @@ def test_get_msg_id_no_id_right_part(self):
self.assertEqual(msg_id.token_type, 'msg-id')

def test_get_msg_id_invalid_expected_msg_id_not_found(self):
text = "Message-Id: 935-XPB-567:0:86089:180874:0:45327:9:90305:17843586-40@example.com"
text = "935-XPB-567:0:45327:9:90305:17843586-40@example.com"
msg_id = parser.parse_message_id(text)
self.assertDefectsEqual(msg_id.all_defects,
[errors.InvalidHeaderDefect])
self.assertDefectsEqual(
msg_id.all_defects,
[errors.InvalidHeaderDefect])

def test_parse_invalid_message_id(self):
message_id = self._test_parse_x(
parser.parse_message_id,
"935-XPB-567:0:45327:9:90305:17843586-40@example.com",
"935-XPB-567:0:45327:9:90305:17843586-40@example.com",
"935-XPB-567:0:45327:9:90305:17843586-40@example.com",
[errors.InvalidHeaderDefect],
)
self.assertEqual(message_id.token_type, 'invalid-message-id')

def test_parse_valid_message_id(self):
message_id = self._test_parse_x(
parser.parse_message_id,
"<aperson@somedomain>",
"<aperson@somedomain>",
"<aperson@somedomain>",
[],
)
self.assertEqual(message_id.token_type, 'message-id')

def test_parse_message_id_with_remaining(self):
message_id = self._test_parse_x(
parser.parse_message_id,
"<validmessageid@example>thensomething",
"<validmessageid@example>",
"<validmessageid@example>",
[errors.InvalidHeaderDefect],
[],
)
self.assertEqual(message_id.token_type, 'message-id')
self.assertEqual(str(message_id.all_defects[0]),
"Unexpected 'thensomething'")

def test_get_msg_id_no_angle_start(self):
with self.assertRaises(errors.HeaderParseError):
Expand Down
@@ -0,0 +1,3 @@
Add a new ``InvalidMessageID`` token to email parser to represent invalid
Message-ID headers. Also, add defects when there is remaining value after
parsing the header.

0 comments on commit f66f4a0

Please sign in to comment.