Skip to content

Commit 901a91c

Browse files
committed
pythongh-127794: Validate header name according rfc-5322 and allow only printable ascii characters
1 parent db9bea0 commit 901a91c

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

Lib/email/message.py

+8
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,14 @@ def add_header(self, _name, _value, **_params):
564564
msg.add_header('content-disposition', 'attachment',
565565
filename='Fußballer.ppt'))
566566
"""
567+
# Validate header name according to RFC 5322
568+
if not _name or ' ' in _name or '\t' in _name or ':' in _name:
569+
raise ValueError(f"Invalid header field name {_name!r}")
570+
571+
# Only allow printable ASCII characters
572+
if any(ord(c) < 33 or ord(c) > 126 for c in _name):
573+
raise ValueError(f"Header field name {_name!r} contains invalid characters")
574+
567575
parts = []
568576
for k, v in _params.items():
569577
if v is None:

0 commit comments

Comments
 (0)