Brad Bishop | f3f93bb | 2019-10-16 14:33:32 -0400 | [diff] [blame^] | 1 | From 90d56127ae15b1e452755e62c77dc475dedf7161 Mon Sep 17 00:00:00 2001 |
| 2 | From: jpic <jpic@users.noreply.github.com> |
| 3 | Date: Wed, 17 Jul 2019 23:54:25 +0200 |
| 4 | Subject: [PATCH] bpo-34155: Dont parse domains containing @ (GH-13079) |
| 5 | |
| 6 | Before: |
| 7 | |
| 8 | >>> email.message_from_string('From: a@malicious.org@important.com', policy=email.policy.default)['from'].addresses |
| 9 | (Address(display_name='', username='a', domain='malicious.org'),) |
| 10 | |
| 11 | >>> parseaddr('a@malicious.org@important.com') |
| 12 | ('', 'a@malicious.org') |
| 13 | |
| 14 | After: |
| 15 | |
| 16 | >>> email.message_from_string('From: a@malicious.org@important.com', policy=email.policy.default)['from'].addresses |
| 17 | (Address(display_name='', username='', domain=''),) |
| 18 | |
| 19 | >>> parseaddr('a@malicious.org@important.com') |
| 20 | ('', 'a@') |
| 21 | |
| 22 | https://bugs.python.org/issue34155 |
| 23 | |
| 24 | Upstream-Status: Backport [https://github.com/python/cpython/commit/8cb65d1381b027f0b09ee36bfed7f35bb4dec9a9] |
| 25 | |
| 26 | CVE: CVE-2019-16056 |
| 27 | |
| 28 | Signed-off-by: Chen Qi <Qi.Chen@windriver.com> |
| 29 | --- |
| 30 | Lib/email/_header_value_parser.py | 2 ++ |
| 31 | Lib/email/_parseaddr.py | 11 ++++++++++- |
| 32 | Lib/test/test_email/test__header_value_parser.py | 10 ++++++++++ |
| 33 | Lib/test/test_email/test_email.py | 14 ++++++++++++++ |
| 34 | .../2019-05-04-13-33-37.bpo-34155.MJll68.rst | 1 + |
| 35 | 5 files changed, 37 insertions(+), 1 deletion(-) |
| 36 | create mode 100644 Misc/NEWS.d/next/Security/2019-05-04-13-33-37.bpo-34155.MJll68.rst |
| 37 | |
| 38 | diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py |
| 39 | index fc00b4a098..bbc026ec71 100644 |
| 40 | --- a/Lib/email/_header_value_parser.py |
| 41 | +++ b/Lib/email/_header_value_parser.py |
| 42 | @@ -1582,6 +1582,8 @@ def get_domain(value): |
| 43 | token, value = get_dot_atom(value) |
| 44 | except errors.HeaderParseError: |
| 45 | token, value = get_atom(value) |
| 46 | + if value and value[0] == '@': |
| 47 | + raise errors.HeaderParseError('Invalid Domain') |
| 48 | if leader is not None: |
| 49 | token[:0] = [leader] |
| 50 | domain.append(token) |
| 51 | diff --git a/Lib/email/_parseaddr.py b/Lib/email/_parseaddr.py |
| 52 | index cdfa3729ad..41ff6f8c00 100644 |
| 53 | --- a/Lib/email/_parseaddr.py |
| 54 | +++ b/Lib/email/_parseaddr.py |
| 55 | @@ -379,7 +379,12 @@ class AddrlistClass: |
| 56 | aslist.append('@') |
| 57 | self.pos += 1 |
| 58 | self.gotonext() |
| 59 | - return EMPTYSTRING.join(aslist) + self.getdomain() |
| 60 | + domain = self.getdomain() |
| 61 | + if not domain: |
| 62 | + # Invalid domain, return an empty address instead of returning a |
| 63 | + # local part to denote failed parsing. |
| 64 | + return EMPTYSTRING |
| 65 | + return EMPTYSTRING.join(aslist) + domain |
| 66 | |
| 67 | def getdomain(self): |
| 68 | """Get the complete domain name from an address.""" |
| 69 | @@ -394,6 +399,10 @@ class AddrlistClass: |
| 70 | elif self.field[self.pos] == '.': |
| 71 | self.pos += 1 |
| 72 | sdlist.append('.') |
| 73 | + elif self.field[self.pos] == '@': |
| 74 | + # bpo-34155: Don't parse domains with two `@` like |
| 75 | + # `a@malicious.org@important.com`. |
| 76 | + return EMPTYSTRING |
| 77 | elif self.field[self.pos] in self.atomends: |
| 78 | break |
| 79 | else: |
| 80 | diff --git a/Lib/test/test_email/test__header_value_parser.py b/Lib/test/test_email/test__header_value_parser.py |
| 81 | index 693487bc96..7dc4de1b7b 100644 |
| 82 | --- a/Lib/test/test_email/test__header_value_parser.py |
| 83 | +++ b/Lib/test/test_email/test__header_value_parser.py |
| 84 | @@ -1438,6 +1438,16 @@ class TestParser(TestParserMixin, TestEmailBase): |
| 85 | self.assertEqual(addr_spec.domain, 'example.com') |
| 86 | self.assertEqual(addr_spec.addr_spec, 'star.a.star@example.com') |
| 87 | |
| 88 | + def test_get_addr_spec_multiple_domains(self): |
| 89 | + with self.assertRaises(errors.HeaderParseError): |
| 90 | + parser.get_addr_spec('star@a.star@example.com') |
| 91 | + |
| 92 | + with self.assertRaises(errors.HeaderParseError): |
| 93 | + parser.get_addr_spec('star@a@example.com') |
| 94 | + |
| 95 | + with self.assertRaises(errors.HeaderParseError): |
| 96 | + parser.get_addr_spec('star@172.17.0.1@example.com') |
| 97 | + |
| 98 | # get_obs_route |
| 99 | |
| 100 | def test_get_obs_route_simple(self): |
| 101 | diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py |
| 102 | index c29cc56203..aa775881c5 100644 |
| 103 | --- a/Lib/test/test_email/test_email.py |
| 104 | +++ b/Lib/test/test_email/test_email.py |
| 105 | @@ -3041,6 +3041,20 @@ class TestMiscellaneous(TestEmailBase): |
| 106 | self.assertEqual(utils.parseaddr('<>'), ('', '')) |
| 107 | self.assertEqual(utils.formataddr(utils.parseaddr('<>')), '') |
| 108 | |
| 109 | + def test_parseaddr_multiple_domains(self): |
| 110 | + self.assertEqual( |
| 111 | + utils.parseaddr('a@b@c'), |
| 112 | + ('', '') |
| 113 | + ) |
| 114 | + self.assertEqual( |
| 115 | + utils.parseaddr('a@b.c@c'), |
| 116 | + ('', '') |
| 117 | + ) |
| 118 | + self.assertEqual( |
| 119 | + utils.parseaddr('a@172.17.0.1@c'), |
| 120 | + ('', '') |
| 121 | + ) |
| 122 | + |
| 123 | def test_noquote_dump(self): |
| 124 | self.assertEqual( |
| 125 | utils.formataddr(('A Silly Person', 'person@dom.ain')), |
| 126 | diff --git a/Misc/NEWS.d/next/Security/2019-05-04-13-33-37.bpo-34155.MJll68.rst b/Misc/NEWS.d/next/Security/2019-05-04-13-33-37.bpo-34155.MJll68.rst |
| 127 | new file mode 100644 |
| 128 | index 0000000000..50292e29ed |
| 129 | --- /dev/null |
| 130 | +++ b/Misc/NEWS.d/next/Security/2019-05-04-13-33-37.bpo-34155.MJll68.rst |
| 131 | @@ -0,0 +1 @@ |
| 132 | +Fix parsing of invalid email addresses with more than one ``@`` (e.g. a@b@c.com.) to not return the part before 2nd ``@`` as valid email address. Patch by maxking & jpic. |