mirror of
https://github.com/python/cpython.git
synced 2025-10-31 21:51:50 +00:00
gh-127794: Validate email header names according to RFC 5322 (#127820)
`email.message.Message` objects now validate header names specified via `__setitem__` or `add_header` according to RFC 5322, §2.2 [1]. In particular, callers should expect a ValueError to be raised for invalid header names. [1]: https://datatracker.ietf.org/doc/html/rfc5322#section-2.2 --------- Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: R. David Murray <rdmurray@bitdance.com>
This commit is contained in:
parent
55150a79ca
commit
c432d0147b
5 changed files with 71 additions and 1 deletions
|
|
@ -1004,6 +1004,30 @@ def test_folding_with_long_nospace_http_policy_1(self):
|
|||
parsed_msg = message_from_bytes(m.as_bytes(), policy=policy.default)
|
||||
self.assertEqual(parsed_msg['Message-ID'], m['Message-ID'])
|
||||
|
||||
def test_invalid_header_names(self):
|
||||
invalid_headers = [
|
||||
('Invalid Header', 'contains space'),
|
||||
('Tab\tHeader', 'contains tab'),
|
||||
('Colon:Header', 'contains colon'),
|
||||
('', 'Empty name'),
|
||||
(' LeadingSpace', 'starts with space'),
|
||||
('TrailingSpace ', 'ends with space'),
|
||||
('Header\x7F', 'Non-ASCII character'),
|
||||
('Header\x80', 'Extended ASCII'),
|
||||
]
|
||||
for email_policy in (policy.default, policy.compat32):
|
||||
for setter in (EmailMessage.__setitem__, EmailMessage.add_header):
|
||||
for name, value in invalid_headers:
|
||||
self.do_test_invalid_header_names(email_policy, setter, name, value)
|
||||
|
||||
def do_test_invalid_header_names(self, policy, setter, name, value):
|
||||
with self.subTest(policy=policy, setter=setter, name=name, value=value):
|
||||
message = EmailMessage(policy=policy)
|
||||
pattern = r'(?i)(?=.*invalid)(?=.*header)(?=.*name)'
|
||||
with self.assertRaisesRegex(ValueError, pattern) as cm:
|
||||
setter(message, name, value)
|
||||
self.assertIn(f"{name!r}", str(cm.exception))
|
||||
|
||||
def test_get_body_malformed(self):
|
||||
"""test for bpo-42892"""
|
||||
msg = textwrap.dedent("""\
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue