343676a
From 9a8db191cf8a3557a24e91081bf434d581b98c5a Mon Sep 17 00:00:00 2001
092bdc1
From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= <miro@hroncok.cz>
343676a
Date: Sat, 2 Jul 2016 20:20:58 +0200
092bdc1
Subject: [PATCH] Disabled HTTP header injections in http.client.
092bdc1
092bdc1
CVE-2016-5699 python: http protocol steam injection attack
092bdc1
rhbz#1303699: https://bugzilla.redhat.com/show_bug.cgi?id=1303699
092bdc1
rhbz#1351687: https://bugzilla.redhat.com/show_bug.cgi?id=1351687
092bdc1
092bdc1
Based on an upstream change by Demian Brecht and Serhiy Storchaka
092bdc1
- in changeset 94952:bf3e1c9b80e9 3.4
092bdc1
- https://hg.python.org/cpython/rev/bf3e1c9b80e9
092bdc1
---
092bdc1
 lib-python/3/http/client.py       | 37 +++++++++++++++++++++++++
092bdc1
 lib-python/3/test/test_httplib.py | 57 +++++++++++++++++++++++++++++++++++++++
092bdc1
 2 files changed, 94 insertions(+)
092bdc1
092bdc1
diff --git a/lib-python/3/http/client.py b/lib-python/3/http/client.py
343676a
index e05c84d..476d6c8 100644
092bdc1
--- a/lib-python/3/http/client.py
092bdc1
+++ b/lib-python/3/http/client.py
092bdc1
@@ -70,6 +70,7 @@ import email.parser
092bdc1
 import email.message
092bdc1
 import io
092bdc1
 import os
092bdc1
+import re
092bdc1
 import socket
092bdc1
 import collections
092bdc1
 from urllib.parse import urlsplit
343676a
@@ -217,6 +218,34 @@ _MAXLINE = 65536
343676a
 _MAXHEADERS = 100
343676a
 
092bdc1
 
092bdc1
+# Header name/value ABNF (http://tools.ietf.org/html/rfc7230#section-3.2)
092bdc1
+#
092bdc1
+# VCHAR          = %x21-7E
092bdc1
+# obs-text       = %x80-FF
092bdc1
+# header-field   = field-name ":" OWS field-value OWS
092bdc1
+# field-name     = token
092bdc1
+# field-value    = *( field-content / obs-fold )
092bdc1
+# field-content  = field-vchar [ 1*( SP / HTAB ) field-vchar ]
092bdc1
+# field-vchar    = VCHAR / obs-text
092bdc1
+#
092bdc1
+# obs-fold       = CRLF 1*( SP / HTAB )
092bdc1
+#                ; obsolete line folding
092bdc1
+#                ; see Section 3.2.4
092bdc1
+
092bdc1
+# token          = 1*tchar
092bdc1
+#
092bdc1
+# tchar          = "!" / "#" / "$" / "%" / "&" / "'" / "*"
092bdc1
+#                / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
092bdc1
+#                / DIGIT / ALPHA
092bdc1
+#                ; any VCHAR, except delimiters
092bdc1
+#
092bdc1
+# VCHAR defined in http://tools.ietf.org/html/rfc5234#appendix-B.1
092bdc1
+
092bdc1
+# the patterns for both name and value are more leniant than RFC
092bdc1
+# definitions to allow for backwards compatibility
092bdc1
+_is_legal_header_name = re.compile(b'^[^:\s][^:\r\n]*$').match
092bdc1
+_is_illegal_header_value = re.compile(b'\n(?![ \t])|\r(?![ \t\n])').search
092bdc1
+
092bdc1
 class HTTPMessage(email.message.Message):
092bdc1
     # XXX The only usage of this method is in
092bdc1
     # http.server.CGIHTTPRequestHandler.  Maybe move the code there so
343676a
@@ -1035,12 +1064,20 @@ class HTTPConnection:
092bdc1
 
092bdc1
         if hasattr(header, 'encode'):
092bdc1
             header = header.encode('ascii')
092bdc1
+
092bdc1
+        if not _is_legal_header_name(header):
092bdc1
+            raise ValueError('Invalid header name %r' % (header,))
092bdc1
+
092bdc1
         values = list(values)
092bdc1
         for i, one_value in enumerate(values):
092bdc1
             if hasattr(one_value, 'encode'):
343676a
                 values[i] = one_value.encode('latin-1')
092bdc1
             elif isinstance(one_value, int):
092bdc1
                 values[i] = str(one_value).encode('ascii')
092bdc1
+
092bdc1
+            if _is_illegal_header_value(values[i]):
092bdc1
+                raise ValueError('Invalid header value %r' % (values[i],))
092bdc1
+
092bdc1
         value = b'\r\n\t'.join(values)
092bdc1
         header = header + b': ' + value
092bdc1
         self._output(header)
092bdc1
diff --git a/lib-python/3/test/test_httplib.py b/lib-python/3/test/test_httplib.py
343676a
index c8ded92..fd71bea 100644
092bdc1
--- a/lib-python/3/test/test_httplib.py
092bdc1
+++ b/lib-python/3/test/test_httplib.py
092bdc1
@@ -134,6 +134,33 @@ class HeaderTests(TestCase):
092bdc1
         conn.putheader('Content-length', 42)
343676a
         self.assertIn(b'Content-length: 42', conn._buffer)
092bdc1
 
092bdc1
+        conn.putheader('Foo', ' bar ')
092bdc1
+        self.assertIn(b'Foo:  bar ', conn._buffer)
092bdc1
+        conn.putheader('Bar', '\tbaz\t')
092bdc1
+        self.assertIn(b'Bar: \tbaz\t', conn._buffer)
092bdc1
+        conn.putheader('Authorization', 'Bearer mytoken')
092bdc1
+        self.assertIn(b'Authorization: Bearer mytoken', conn._buffer)
092bdc1
+        conn.putheader('IterHeader', 'IterA', 'IterB')
092bdc1
+        self.assertIn(b'IterHeader: IterA\r\n\tIterB', conn._buffer)
092bdc1
+        conn.putheader('LatinHeader', b'\xFF')
092bdc1
+        self.assertIn(b'LatinHeader: \xFF', conn._buffer)
092bdc1
+        conn.putheader('Utf8Header', b'\xc3\x80')
092bdc1
+        self.assertIn(b'Utf8Header: \xc3\x80', conn._buffer)
092bdc1
+        conn.putheader('C1-Control', b'next\x85line')
092bdc1
+        self.assertIn(b'C1-Control: next\x85line', conn._buffer)
092bdc1
+        conn.putheader('Embedded-Fold-Space', 'is\r\n allowed')
092bdc1
+        self.assertIn(b'Embedded-Fold-Space: is\r\n allowed', conn._buffer)
092bdc1
+        conn.putheader('Embedded-Fold-Tab', 'is\r\n\tallowed')
092bdc1
+        self.assertIn(b'Embedded-Fold-Tab: is\r\n\tallowed', conn._buffer)
092bdc1
+        conn.putheader('Key Space', 'value')
092bdc1
+        self.assertIn(b'Key Space: value', conn._buffer)
092bdc1
+        conn.putheader('KeySpace ', 'value')
092bdc1
+        self.assertIn(b'KeySpace : value', conn._buffer)
092bdc1
+        conn.putheader(b'Nonbreak\xa0Space', 'value')
092bdc1
+        self.assertIn(b'Nonbreak\xa0Space: value', conn._buffer)
092bdc1
+        conn.putheader(b'\xa0NonbreakSpace', 'value')
092bdc1
+        self.assertIn(b'\xa0NonbreakSpace: value', conn._buffer)
092bdc1
+
092bdc1
     def test_ipv6host_header(self):
092bdc1
         # Default host header on IPv6 transaction should wrapped by [] if
092bdc1
         # its actual IPv6 address
092bdc1
@@ -153,6 +180,36 @@ class HeaderTests(TestCase):
092bdc1
         conn.request('GET', '/foo')
092bdc1
         self.assertTrue(sock.data.startswith(expected))
092bdc1
 
092bdc1
+    def test_invalid_headers(self):
092bdc1
+        conn = client.HTTPConnection('example.com')
092bdc1
+        conn.sock = FakeSocket('')
092bdc1
+        conn.putrequest('GET', '/')
092bdc1
+
092bdc1
+        # http://tools.ietf.org/html/rfc7230#section-3.2.4, whitespace is no
092bdc1
+        # longer allowed in header names
092bdc1
+        cases = (
092bdc1
+            (b'Invalid\r\nName', b'ValidValue'),
092bdc1
+            (b'Invalid\rName', b'ValidValue'),
092bdc1
+            (b'Invalid\nName', b'ValidValue'),
092bdc1
+            (b'\r\nInvalidName', b'ValidValue'),
092bdc1
+            (b'\rInvalidName', b'ValidValue'),
092bdc1
+            (b'\nInvalidName', b'ValidValue'),
092bdc1
+            (b' InvalidName', b'ValidValue'),
092bdc1
+            (b'\tInvalidName', b'ValidValue'),
092bdc1
+            (b'Invalid:Name', b'ValidValue'),
092bdc1
+            (b':InvalidName', b'ValidValue'),
092bdc1
+            (b'ValidName', b'Invalid\r\nValue'),
092bdc1
+            (b'ValidName', b'Invalid\rValue'),
092bdc1
+            (b'ValidName', b'Invalid\nValue'),
092bdc1
+            (b'ValidName', b'InvalidValue\r\n'),
092bdc1
+            (b'ValidName', b'InvalidValue\r'),
092bdc1
+            (b'ValidName', b'InvalidValue\n'),
092bdc1
+        )
092bdc1
+        for name, value in cases:
092bdc1
+            with self.subTest((name, value)):
092bdc1
+                with self.assertRaisesRegex(ValueError, 'Invalid header'):
092bdc1
+                    conn.putheader(name, value)
092bdc1
+
092bdc1
 
092bdc1
 class BasicTest(TestCase):
092bdc1
     def test_status_lines(self):
092bdc1
-- 
092bdc1
2.9.0
092bdc1