Skip to content

Commit 4cbcd2f

Browse files
rcsanchez97miss-islington
authored andcommitted
[2.7] bpo-34155: Dont parse domains containing @ (GH-13079) (GH-16006)
This change skips parsing of email addresses where domains include a "@" character, which can be maliciously used since the local part is returned as a complete address. (cherry picked from commit 8cb65d1) Excludes changes to Lib/email/_header_value_parser.py, which did not exist in 2.7. Co-authored-by: jpic <[email protected]> https://bugs.python.org/issue34155
1 parent 0d63669 commit 4cbcd2f

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Lib/email/_parseaddr.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,12 @@ def getaddrspec(self):
336336
aslist.append('@')
337337
self.pos += 1
338338
self.gotonext()
339-
return EMPTYSTRING.join(aslist) + self.getdomain()
339+
domain = self.getdomain()
340+
if not domain:
341+
# Invalid domain, return an empty address instead of returning a
342+
# local part to denote failed parsing.
343+
return EMPTYSTRING
344+
return EMPTYSTRING.join(aslist) + domain
340345

341346
def getdomain(self):
342347
"""Get the complete domain name from an address."""
@@ -351,6 +356,10 @@ def getdomain(self):
351356
elif self.field[self.pos] == '.':
352357
self.pos += 1
353358
sdlist.append('.')
359+
elif self.field[self.pos] == '@':
360+
# bpo-34155: Don't parse domains with two `@` like
361+
# `[email protected]@important.com`.
362+
return EMPTYSTRING
354363
elif self.field[self.pos] in self.atomends:
355364
break
356365
else:

Lib/email/test/test_email.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,6 +2306,20 @@ def test_parseaddr_empty(self):
23062306
self.assertEqual(Utils.parseaddr('<>'), ('', ''))
23072307
self.assertEqual(Utils.formataddr(Utils.parseaddr('<>')), '')
23082308

2309+
def test_parseaddr_multiple_domains(self):
2310+
self.assertEqual(
2311+
Utils.parseaddr('a@b@c'),
2312+
('', '')
2313+
)
2314+
self.assertEqual(
2315+
Utils.parseaddr('[email protected]@c'),
2316+
('', '')
2317+
)
2318+
self.assertEqual(
2319+
Utils.parseaddr('[email protected]@c'),
2320+
('', '')
2321+
)
2322+
23092323
def test_noquote_dump(self):
23102324
self.assertEqual(
23112325
Utils.formataddr(('A Silly Person', '[email protected]')),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix parsing of invalid email addresses with more than one ``@`` (e.g. a@[email protected].) to not return the part before 2nd ``@`` as valid email address. Patch by maxking & jpic.

0 commit comments

Comments
 (0)