summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-01-29 22:27:55 (GMT)
committerMariatta <Mariatta@users.noreply.github.com>2018-01-29 22:27:55 (GMT)
commit6ea75b174da0cf824e2acc5db6b53798f5f4e4f9 (patch)
tree98c7edeeae50796d7f94e03105c63e3074afe4a5
parent586986182343eb7475046a5429a1a0e1f368c7ea (diff)
downloadcpython-6ea75b174da0cf824e2acc5db6b53798f5f4e4f9.zip
cpython-6ea75b174da0cf824e2acc5db6b53798f5f4e4f9.tar.gz
cpython-6ea75b174da0cf824e2acc5db6b53798f5f4e4f9.tar.bz2
bpo-27931: Fix email address header parsing error (GH-5329) (GH-5431)
Correctly handle addresses whose username is an empty quoted string. (cherry picked from commit aa218d1649690d1c1ba86a9972f7fae646bf1a8f) Co-authored-by: jayyyin <jayyin11043@hotmail.com>
-rw-r--r--Lib/email/_header_value_parser.py8
-rw-r--r--Lib/test/test_email/test__header_value_parser.py17
-rw-r--r--Misc/NEWS.d/next/Library/2018-01-25-21-04-11.bpo-27931.e4r52t.rst1
3 files changed, 25 insertions, 1 deletions
diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py
index 3ebbbe5..b23c897 100644
--- a/Lib/email/_header_value_parser.py
+++ b/Lib/email/_header_value_parser.py
@@ -430,7 +430,10 @@ class AngleAddr(TokenList):
def addr_spec(self):
for x in self:
if x.token_type == 'addr-spec':
- return x.addr_spec
+ if x.local_part:
+ return x.addr_spec
+ else:
+ return quote_string(x.local_part) + x.addr_spec
else:
return '<>'
@@ -1165,6 +1168,9 @@ def get_bare_quoted_string(value):
"expected '\"' but found '{}'".format(value))
bare_quoted_string = BareQuotedString()
value = value[1:]
+ if value[0] == '"':
+ token, value = get_qcontent(value)
+ bare_quoted_string.append(token)
while value and value[0] != '"':
if value[0] in WSP:
token, value = get_fws(value)
diff --git a/Lib/test/test_email/test__header_value_parser.py b/Lib/test/test_email/test__header_value_parser.py
index 1667617..5cdc4bc 100644
--- a/Lib/test/test_email/test__header_value_parser.py
+++ b/Lib/test/test_email/test__header_value_parser.py
@@ -490,6 +490,10 @@ class TestParser(TestParserMixin, TestEmailBase):
with self.assertRaises(errors.HeaderParseError):
parser.get_bare_quoted_string(' "foo"')
+ def test_get_bare_quoted_string_only_quotes(self):
+ self._test_get_x(parser.get_bare_quoted_string,
+ '""', '""', '', [], '')
+
def test_get_bare_quoted_string_following_wsp_preserved(self):
self._test_get_x(parser.get_bare_quoted_string,
'"foo"\t bar', '"foo"', 'foo', [], '\t bar')
@@ -1467,6 +1471,19 @@ class TestParser(TestParserMixin, TestEmailBase):
self.assertIsNone(angle_addr.route)
self.assertEqual(angle_addr.addr_spec, '<>')
+ def test_get_angle_addr_qs_only_quotes(self):
+ angle_addr = self._test_get_x(parser.get_angle_addr,
+ '<""@example.com>',
+ '<""@example.com>',
+ '<""@example.com>',
+ [],
+ '')
+ self.assertEqual(angle_addr.token_type, 'angle-addr')
+ self.assertEqual(angle_addr.local_part, '')
+ self.assertEqual(angle_addr.domain, 'example.com')
+ self.assertIsNone(angle_addr.route)
+ self.assertEqual(angle_addr.addr_spec, '""@example.com')
+
def test_get_angle_addr_with_cfws(self):
angle_addr = self._test_get_x(parser.get_angle_addr,
' (foo) <dinsdale@example.com>(bar)',
diff --git a/Misc/NEWS.d/next/Library/2018-01-25-21-04-11.bpo-27931.e4r52t.rst b/Misc/NEWS.d/next/Library/2018-01-25-21-04-11.bpo-27931.e4r52t.rst
new file mode 100644
index 0000000..7324247
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-01-25-21-04-11.bpo-27931.e4r52t.rst
@@ -0,0 +1 @@
+Fix email address header parsing error when the username is an empty quoted string. Patch by Xiang Zhang.