Commit 032eed3c authored by R David Murray's avatar R David Murray

Recognize '<>' as a special case of an angle-addr in header_value_parser.

Although '<>' is invalid according to RFC 5322, SMTP uses it for various
things, and it sometimes ends up in email headers.  This patch changes
get_angle_addr to recognize it and just register a Defect instead of raising a
parsing error.
parent d785cb39
...@@ -791,6 +791,8 @@ class AngleAddr(TokenList): ...@@ -791,6 +791,8 @@ class AngleAddr(TokenList):
for x in self: for x in self:
if x.token_type == 'addr-spec': if x.token_type == 'addr-spec':
return x.addr_spec return x.addr_spec
else:
return '<>'
class ObsRoute(TokenList): class ObsRoute(TokenList):
...@@ -1829,6 +1831,14 @@ def get_angle_addr(value): ...@@ -1829,6 +1831,14 @@ def get_angle_addr(value):
"expected angle-addr but found '{}'".format(value)) "expected angle-addr but found '{}'".format(value))
angle_addr.append(ValueTerminal('<', 'angle-addr-start')) angle_addr.append(ValueTerminal('<', 'angle-addr-start'))
value = value[1:] value = value[1:]
# Although it is not legal per RFC5322, SMTP uses '<>' in certain
# circumstances.
if value[0] == '>':
angle_addr.append(ValueTerminal('>', 'angle-addr-end'))
angle_addr.defects.append(errors.InvalidHeaderDefect(
"null addr-spec in angle-addr"))
value = value[1:]
return angle_addr, value
try: try:
token, value = get_addr_spec(value) token, value = get_addr_spec(value)
except errors.HeaderParseError: except errors.HeaderParseError:
...@@ -1838,7 +1848,7 @@ def get_angle_addr(value): ...@@ -1838,7 +1848,7 @@ def get_angle_addr(value):
"obsolete route specification in angle-addr")) "obsolete route specification in angle-addr"))
except errors.HeaderParseError: except errors.HeaderParseError:
raise errors.HeaderParseError( raise errors.HeaderParseError(
"expected addr-spec or but found '{}'".format(value)) "expected addr-spec or obs-route but found '{}'".format(value))
angle_addr.append(token) angle_addr.append(token)
token, value = get_addr_spec(value) token, value = get_addr_spec(value)
angle_addr.append(token) angle_addr.append(token)
......
...@@ -1429,6 +1429,19 @@ class TestParser(TestEmailBase): ...@@ -1429,6 +1429,19 @@ class TestParser(TestEmailBase):
self.assertIsNone(angle_addr.route) self.assertIsNone(angle_addr.route)
self.assertEqual(angle_addr.addr_spec, 'dinsdale@example.com') self.assertEqual(angle_addr.addr_spec, 'dinsdale@example.com')
def test_get_angle_addr_empty(self):
angle_addr = self._test_get_x(parser.get_angle_addr,
'<>',
'<>',
'<>',
[errors.InvalidHeaderDefect],
'')
self.assertEqual(angle_addr.token_type, 'angle-addr')
self.assertIsNone(angle_addr.local_part)
self.assertIsNone(angle_addr.domain)
self.assertIsNone(angle_addr.route)
self.assertEqual(angle_addr.addr_spec, '<>')
def test_get_angle_addr_with_cfws(self): def test_get_angle_addr_with_cfws(self):
angle_addr = self._test_get_x(parser.get_angle_addr, angle_addr = self._test_get_x(parser.get_angle_addr,
' (foo) <dinsdale@example.com>(bar)', ' (foo) <dinsdale@example.com>(bar)',
...@@ -2007,7 +2020,7 @@ class TestParser(TestEmailBase): ...@@ -2007,7 +2020,7 @@ class TestParser(TestEmailBase):
self.assertEqual(group.mailboxes, self.assertEqual(group.mailboxes,
group.all_mailboxes) group.all_mailboxes)
def test_get_troup_null_addr_spec(self): def test_get_group_null_addr_spec(self):
group = self._test_get_x(parser.get_group, group = self._test_get_x(parser.get_group,
'foo: <>;', 'foo: <>;',
'foo: <>;', 'foo: <>;',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment