Skip to content

Commit 3b0322f

Browse files
committed
gh-101034: Clarify error for email domain ending with a dot
get_domain's obs-domain loop called get_atom on an empty string after a trailing dot, surfacing the internal "expected atext but found ''" instead of a meaningful message. Raise a clear HeaderParseError instead. A trailing-dot domain remains invalid per the RFC 5322 dot-atom and obs-domain grammars; only the diagnostic changes.
1 parent ad38cf8 commit 3b0322f

4 files changed

Lines changed: 23 additions & 0 deletions

File tree

Lib/email/_header_value_parser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,11 @@ def get_domain(value):
16681668
domain[:] = domain[0]
16691669
while value and value[0] == '.':
16701670
domain.append(DOT)
1671+
# A trailing dot leaves get_atom an empty string, whose error
1672+
# ("expected atext but found ''") hides the real problem.
1673+
if len(value) == 1:
1674+
raise errors.HeaderParseError(
1675+
"expected atom after '.' but found end of domain")
16711676
token, value = get_atom(value[1:])
16721677
domain.append(token)
16731678
return domain, value

Lib/test/test_email/test__header_value_parser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,13 @@ def test_get_domain_no_atom_raises(self):
15011501
with self.assertRaises(errors.HeaderParseError):
15021502
parser.get_domain(" (foo)\t, broken")
15031503

1504+
def test_get_domain_obsolete_trailing_dot_raises(self):
1505+
# gh-101034: a trailing dot must not surface get_atom's internal
1506+
# "expected atext but found ''" error.
1507+
with self.assertRaisesRegex(errors.HeaderParseError,
1508+
"expected atom after '.'"):
1509+
parser.get_domain("example.org.")
1510+
15041511

15051512
# get_addr_spec
15061513

Lib/test/test_email/test_headerregistry.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,13 @@ def test_bad_addr_sepc_raises(self):
15961596
with self.assertRaises(ValueError):
15971597
Address('foo', addr_spec="name@ex[]ample.com")
15981598

1599+
def test_trailing_dot_in_addr_spec_domain_raises(self):
1600+
# gh-101034: report a clear error instead of the internal
1601+
# "expected atext but found ''".
1602+
with self.assertRaisesRegex(errors.HeaderParseError,
1603+
"expected atom after '.'"):
1604+
Address('Jane Doe', addr_spec="jane_doe@example.org.")
1605+
15991606
def test_empty_group(self):
16001607
g = Group('foo')
16011608
self.assertEqual(g.display_name, 'foo')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix the misleading ``HeaderParseError`` raised for an email address whose
2+
domain ends with a dot (such as ``user@example.org.``) in
3+
:mod:`email.headerregistry`. It now reports a clear error instead of the
4+
internal ``expected atext but found ''``.

0 commit comments

Comments
 (0)