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