Skip to content
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

gh-91400: make sure email parsing dont unquote realnames with spaces #92638

Open
wants to merge 6 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
4 changes: 3 additions & 1 deletion Lib/email/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
CRLF = '\r\n'
TICK = "'"

specialsre = re.compile(r'[][\\()<>@,:;".]')
# These are used by formataddr() to understand what characters require a name
# field to be quoted and what characters within that must be \escaped.
specialsre = re.compile(r'[][\\()<>@,:;". ]')
escapesre = re.compile(r'[\\"]')


Expand Down
8 changes: 7 additions & 1 deletion Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -3227,7 +3227,7 @@ def test_parseaddr_multiple_domains(self):
def test_noquote_dump(self):
self.assertEqual(
utils.formataddr(('A Silly Person', 'person@dom.ain')),
'A Silly Person <person@dom.ain>')
'"A Silly Person" <person@dom.ain>')

def test_escape_dump(self):
self.assertEqual(
Expand All @@ -3248,6 +3248,12 @@ def test_escape_backslashes(self):
b = 'person@dom.ain'
self.assertEqual(utils.parseaddr(utils.formataddr((a, b))), (a, b))

def test_parseaddr_formataddr_inverse(self):
# gh-91400
identity = '"foo bar" <foo@example.com>'
single = utils.formataddr(utils.parseaddr(identity))
self.assertEqual(identity, single)

def test_quotes_unicode_names(self):
# issue 1690608. email.utils.formataddr() should be rfc2047 aware.
name = "H\u00e4ns W\u00fcrst"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make :func:`email.utils.formataddr` "quote" the name portion of an email address if
it contains spaces.
Loading