Blame SOURCES/00238-CVE-2016-5699-httplib.patch

854cc6
From e91c46c7a1a904eba04e29cdf896c99cb546d989 Mon Sep 17 00:00:00 2001
854cc6
From: Tomas Orsava <torsava@redhat.com>
854cc6
Date: Wed, 22 Jun 2016 17:06:01 +0200
854cc6
Subject: [PATCH] Disabled HTTP header injections in httplib
854cc6
854cc6
CVE-2016-5699 python: http protocol steam injection attack
854cc6
rhbz#1303699 : https://bugzilla.redhat.com/show_bug.cgi?id=1303699
854cc6
854cc6
Based on an upstream change by Demian Brecht and Serhiy Storchaka
854cc6
- in changeset 94951:1c45047c5102 2.7
854cc6
- https://hg.python.org/cpython/rev/1c45047c5102
854cc6
---
854cc6
 Lib/httplib.py           | 40 +++++++++++++++++++++++++++++++++-
854cc6
 Lib/test/test_httplib.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++
854cc6
 2 files changed, 95 insertions(+), 1 deletion(-)
854cc6
854cc6
diff --git a/Lib/httplib.py b/Lib/httplib.py
854cc6
index 8e02ac3..592ee57 100644
854cc6
--- a/Lib/httplib.py
854cc6
+++ b/Lib/httplib.py
854cc6
@@ -68,6 +68,7 @@ Req-sent-unread-response       _CS_REQ_SENT       <response_class>
854cc6
 
854cc6
 from array import array
854cc6
 import os
854cc6
+import re
854cc6
 import socket
854cc6
 from sys import py3kwarning
854cc6
 from urlparse import urlsplit
854cc6
@@ -218,6 +219,34 @@ _MAXLINE = 65536
854cc6
 # maximum amount of headers accepted
854cc6
 _MAXHEADERS = 100
854cc6
 
854cc6
+# Header name/value ABNF (http://tools.ietf.org/html/rfc7230#section-3.2)
854cc6
+#
854cc6
+# VCHAR          = %x21-7E
854cc6
+# obs-text       = %x80-FF
854cc6
+# header-field   = field-name ":" OWS field-value OWS
854cc6
+# field-name     = token
854cc6
+# field-value    = *( field-content / obs-fold )
854cc6
+# field-content  = field-vchar [ 1*( SP / HTAB ) field-vchar ]
854cc6
+# field-vchar    = VCHAR / obs-text
854cc6
+#
854cc6
+# obs-fold       = CRLF 1*( SP / HTAB )
854cc6
+#                ; obsolete line folding
854cc6
+#                ; see Section 3.2.4
854cc6
+
854cc6
+# token          = 1*tchar
854cc6
+#
854cc6
+# tchar          = "!" / "#" / "$" / "%" / "&" / "'" / "*"
854cc6
+#                / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
854cc6
+#                / DIGIT / ALPHA
854cc6
+#                ; any VCHAR, except delimiters
854cc6
+#
854cc6
+# VCHAR defined in http://tools.ietf.org/html/rfc5234#appendix-B.1
854cc6
+
854cc6
+# the patterns for both name and value are more leniant than RFC
854cc6
+# definitions to allow for backwards compatibility
854cc6
+_is_legal_header_name = re.compile(r'\A[^:\s][^:\r\n]*\Z').match
854cc6
+_is_illegal_header_value = re.compile(r'\n(?![ \t])|\r(?![ \t\n])').search
854cc6
+
854cc6
 
854cc6
 class HTTPMessage(mimetools.Message):
854cc6
 
854cc6
@@ -956,7 +985,16 @@ class HTTPConnection:
854cc6
         if self.__state != _CS_REQ_STARTED:
854cc6
             raise CannotSendHeader()
854cc6
 
854cc6
-        hdr = '%s: %s' % (header, '\r\n\t'.join([str(v) for v in values]))
854cc6
+        header = '%s' % header
854cc6
+        if not _is_legal_header_name(header):
854cc6
+            raise ValueError('Invalid header name %r' % (header,))
854cc6
+
854cc6
+        values = [str(v) for v in values]
854cc6
+        for one_value in values:
854cc6
+            if _is_illegal_header_value(one_value):
854cc6
+                raise ValueError('Invalid header value %r' % (one_value,))
854cc6
+
854cc6
+        hdr = '%s: %s' % (header, '\r\n\t'.join(values))
854cc6
         self._output(hdr)
854cc6
 
854cc6
     def endheaders(self, message_body=None):
854cc6
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
854cc6
index c29aceb..29af589 100644
854cc6
--- a/Lib/test/test_httplib.py
854cc6
+++ b/Lib/test/test_httplib.py
854cc6
@@ -133,6 +133,33 @@ class HeaderTests(TestCase):
854cc6
         conn.putheader('Content-length',42)
854cc6
         self.assertTrue('Content-length: 42' in conn._buffer)
854cc6
 
854cc6
+        conn.putheader('Foo', ' bar ')
854cc6
+        self.assertIn(b'Foo:  bar ', conn._buffer)
854cc6
+        conn.putheader('Bar', '\tbaz\t')
854cc6
+        self.assertIn(b'Bar: \tbaz\t', conn._buffer)
854cc6
+        conn.putheader('Authorization', 'Bearer mytoken')
854cc6
+        self.assertIn(b'Authorization: Bearer mytoken', conn._buffer)
854cc6
+        conn.putheader('IterHeader', 'IterA', 'IterB')
854cc6
+        self.assertIn(b'IterHeader: IterA\r\n\tIterB', conn._buffer)
854cc6
+        conn.putheader('LatinHeader', b'\xFF')
854cc6
+        self.assertIn(b'LatinHeader: \xFF', conn._buffer)
854cc6
+        conn.putheader('Utf8Header', b'\xc3\x80')
854cc6
+        self.assertIn(b'Utf8Header: \xc3\x80', conn._buffer)
854cc6
+        conn.putheader('C1-Control', b'next\x85line')
854cc6
+        self.assertIn(b'C1-Control: next\x85line', conn._buffer)
854cc6
+        conn.putheader('Embedded-Fold-Space', 'is\r\n allowed')
854cc6
+        self.assertIn(b'Embedded-Fold-Space: is\r\n allowed', conn._buffer)
854cc6
+        conn.putheader('Embedded-Fold-Tab', 'is\r\n\tallowed')
854cc6
+        self.assertIn(b'Embedded-Fold-Tab: is\r\n\tallowed', conn._buffer)
854cc6
+        conn.putheader('Key Space', 'value')
854cc6
+        self.assertIn(b'Key Space: value', conn._buffer)
854cc6
+        conn.putheader('KeySpace ', 'value')
854cc6
+        self.assertIn(b'KeySpace : value', conn._buffer)
854cc6
+        conn.putheader(b'Nonbreak\xa0Space', 'value')
854cc6
+        self.assertIn(b'Nonbreak\xa0Space: value', conn._buffer)
854cc6
+        conn.putheader(b'\xa0NonbreakSpace', 'value')
854cc6
+        self.assertIn(b'\xa0NonbreakSpace: value', conn._buffer)
854cc6
+
854cc6
     def test_ipv6host_header(self):
854cc6
         # Default host header on IPv6 transaction should wrapped by [] if
854cc6
         # its actual IPv6 address
854cc6
@@ -152,6 +179,35 @@ class HeaderTests(TestCase):
854cc6
         conn.request('GET', '/foo')
854cc6
         self.assertTrue(sock.data.startswith(expected))
854cc6
 
854cc6
+    def test_invalid_headers(self):
854cc6
+        conn = httplib.HTTPConnection('example.com')
854cc6
+        conn.sock = FakeSocket('')
854cc6
+        conn.putrequest('GET', '/')
854cc6
+
854cc6
+        # http://tools.ietf.org/html/rfc7230#section-3.2.4, whitespace is no
854cc6
+        # longer allowed in header names
854cc6
+        cases = (
854cc6
+            (b'Invalid\r\nName', b'ValidValue'),
854cc6
+            (b'Invalid\rName', b'ValidValue'),
854cc6
+            (b'Invalid\nName', b'ValidValue'),
854cc6
+            (b'\r\nInvalidName', b'ValidValue'),
854cc6
+            (b'\rInvalidName', b'ValidValue'),
854cc6
+            (b'\nInvalidName', b'ValidValue'),
854cc6
+            (b' InvalidName', b'ValidValue'),
854cc6
+            (b'\tInvalidName', b'ValidValue'),
854cc6
+            (b'Invalid:Name', b'ValidValue'),
854cc6
+            (b':InvalidName', b'ValidValue'),
854cc6
+            (b'ValidName', b'Invalid\r\nValue'),
854cc6
+            (b'ValidName', b'Invalid\rValue'),
854cc6
+            (b'ValidName', b'Invalid\nValue'),
854cc6
+            (b'ValidName', b'InvalidValue\r\n'),
854cc6
+            (b'ValidName', b'InvalidValue\r'),
854cc6
+            (b'ValidName', b'InvalidValue\n'),
854cc6
+        )
854cc6
+        for name, value in cases:
854cc6
+            with self.assertRaisesRegexp(ValueError, 'Invalid header'):
854cc6
+                conn.putheader(name, value)
854cc6
+
854cc6
 
854cc6
 class BasicTest(TestCase):
854cc6
     def test_status_lines(self):
854cc6
-- 
854cc6
2.9.0
854cc6