Skip to content

Commit

Permalink
pythongh-127794: Validate header name according rfc-5322 and allow on…
Browse files Browse the repository at this point in the history
…ly printable ascii characters
  • Loading branch information
srinivasreddy committed Dec 11, 2024
1 parent db9bea0 commit 901a91c
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Lib/email/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,14 @@ def add_header(self, _name, _value, **_params):
msg.add_header('content-disposition', 'attachment',
filename='Fußballer.ppt'))
"""
# Validate header name according to RFC 5322
if not _name or ' ' in _name or '\t' in _name or ':' in _name:
raise ValueError(f"Invalid header field name {_name!r}")

# Only allow printable ASCII characters
if any(ord(c) < 33 or ord(c) > 126 for c in _name):
raise ValueError(f"Header field name {_name!r} contains invalid characters")

parts = []
for k, v in _params.items():
if v is None:
Expand Down

0 comments on commit 901a91c

Please sign in to comment.