Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b2d9dd

Browse files
committed
#5871: protect against header injection attacks.
This makes Header.encode throw a HeaderParseError if it winds up formatting a header such that a continuation line has no leading whitespace and looks like a header. Since Header accepts values containing newlines and preserves them (and this is by design), without this fix any program that took user input (say, a subject in a web form) and passed it to the email package as a header was vulnerable to header injection attacks. (As far as we know this has never been exploited.) Thanks to Jakub Wilk for reporting this vulnerability.
1 parent e3ee66f commit 5b2d9dd

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

Lib/email/header.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
# For use with .match()
4848
fcre = re.compile(r'[\041-\176]+:$')
4949

50+
# Find a header embeded in a putative header value. Used to check for
51+
# header injection attack.
52+
_embeded_header = re.compile(r'\n[^ \t]+:')
53+
5054

5155

5256
# Helpers
@@ -320,7 +324,11 @@ def encode(self, splitchars=';, \t', maxlinelen=None, linesep='\n'):
320324
if len(lines) > 1:
321325
formatter.newline()
322326
formatter.add_transition()
323-
return formatter._str(linesep)
327+
value = formatter._str(linesep)
328+
if _embeded_header.search(value):
329+
raise HeaderParseError("header value appears to contain "
330+
"an embedded header: {!r}".format(value))
331+
return value
324332

325333
def _normalize(self):
326334
# Step 1: Normalize the chunks so that all runs of identical charsets

Lib/email/test/test_email.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,18 @@ def test_nonascii_add_header_with_tspecial(self):
561561
"attachment; filename*=utf-8''Fu%C3%9Fballer%20%5Bfilename%5D.ppt",
562562
msg['Content-Disposition'])
563563

564+
# Issue 5871: reject an attempt to embed a header inside a header value
565+
# (header injection attack).
566+
def test_embeded_header_via_Header_rejected(self):
567+
msg = Message()
568+
msg['Dummy'] = Header('dummy\nX-Injected-Header: test')
569+
self.assertRaises(errors.HeaderParseError, msg.as_string)
570+
571+
def test_embeded_header_via_string_rejected(self):
572+
msg = Message()
573+
msg['Dummy'] = 'dummy\nX-Injected-Header: test'
574+
self.assertRaises(errors.HeaderParseError, msg.as_string)
575+
564576

565577
# Test the email.encoders module
566578
class TestEncoders(unittest.TestCase):

Misc/NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ Core and Builtins
4040
Library
4141
-------
4242

43+
- Issue #5871: email.header.Header.encode now raises an error if any
44+
continuation line in the formatted value has no leading white space
45+
and looks like a header. Since Generator uses Header to format all
46+
headers, this check is made for all headers in any serialized message
47+
at serialization time. This provides protection against header
48+
injection attacks.
49+
4350
- Issue #10859: Make ``contextlib.GeneratorContextManager`` officially
4451
private by renaming it to ``_GeneratorContextManager``.
4552

0 commit comments

Comments
 (0)