Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
srinivasreddy committed Dec 23, 2024
1 parent b7bfacb commit 3ce49be
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Lib/email/_policybase.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
'compat32',
]

# equivalent to pattern re.compile("[!-9;-~]+$")
valid_header_name_re = re.compile("[\041-\071\073-\176]+$")

def validate_header_name(name):
# Validate header name according to RFC 5322
if not valid_header_name_re.match(name):
raise ValueError(f"Header field name contains invalid characters: {name!r}")
raise ValueError(
f"Header field name contains invalid characters: {name!r}")

class _PolicyBase:

Expand Down
13 changes: 8 additions & 5 deletions Lib/test/test_email/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,11 +1024,14 @@ def test_invalid_header_names(self):
policy=thispolicy.__class__.__name__,
method=method,
):
with self.assertRaises(ValueError) as cm:
getattr(EmailMessage(policy=thispolicy), method)(name, value)
msg = str(cm.exception)
self.assertRegex( msg, '(?i)(?=.*invalid)(?=.*header)(?=.*name)')
self.assertIn(f"{name!r}", msg)
message = EmailMessage(policy=thispolicy)
method = getattr(message, method)
with self.assertRaisesRegex(
ValueError,
'(?i)(?=.*invalid)(?=.*header)(?=.*name)'
) as cm:
method(name,value)
self.assertIn(f"{name!r}", str(cm.exception))

def test_get_body_malformed(self):
"""test for bpo-42892"""
Expand Down

0 comments on commit 3ce49be

Please sign in to comment.