An interpreted, interactive, object-oriented programming language
CentOS Sources
2017-08-01 71084d584ff953f5463757ec6536406320560b4d
commit | author | age
854cc6 1 From e91c46c7a1a904eba04e29cdf896c99cb546d989 Mon Sep 17 00:00:00 2001
CS 2 From: Tomas Orsava <torsava@redhat.com>
3 Date: Wed, 22 Jun 2016 17:06:01 +0200
4 Subject: [PATCH] Disabled HTTP header injections in httplib
5
6 CVE-2016-5699 python: http protocol steam injection attack
7 rhbz#1303699 : https://bugzilla.redhat.com/show_bug.cgi?id=1303699
8
9 Based on an upstream change by Demian Brecht and Serhiy Storchaka
10 - in changeset 94951:1c45047c5102 2.7
11 - https://hg.python.org/cpython/rev/1c45047c5102
12 ---
13  Lib/httplib.py           | 40 +++++++++++++++++++++++++++++++++-
14  Lib/test/test_httplib.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++
15  2 files changed, 95 insertions(+), 1 deletion(-)
16
17 diff --git a/Lib/httplib.py b/Lib/httplib.py
18 index 8e02ac3..592ee57 100644
19 --- a/Lib/httplib.py
20 +++ b/Lib/httplib.py
21 @@ -68,6 +68,7 @@ Req-sent-unread-response       _CS_REQ_SENT       <response_class>
22  
23  from array import array
24  import os
25 +import re
26  import socket
27  from sys import py3kwarning
28  from urlparse import urlsplit
29 @@ -218,6 +219,34 @@ _MAXLINE = 65536
30  # maximum amount of headers accepted
31  _MAXHEADERS = 100
32  
33 +# Header name/value ABNF (http://tools.ietf.org/html/rfc7230#section-3.2)
34 +#
35 +# VCHAR          = %x21-7E
36 +# obs-text       = %x80-FF
37 +# header-field   = field-name ":" OWS field-value OWS
38 +# field-name     = token
39 +# field-value    = *( field-content / obs-fold )
40 +# field-content  = field-vchar [ 1*( SP / HTAB ) field-vchar ]
41 +# field-vchar    = VCHAR / obs-text
42 +#
43 +# obs-fold       = CRLF 1*( SP / HTAB )
44 +#                ; obsolete line folding
45 +#                ; see Section 3.2.4
46 +
47 +# token          = 1*tchar
48 +#
49 +# tchar          = "!" / "#" / "$" / "%" / "&" / "'" / "*"
50 +#                / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
51 +#                / DIGIT / ALPHA
52 +#                ; any VCHAR, except delimiters
53 +#
54 +# VCHAR defined in http://tools.ietf.org/html/rfc5234#appendix-B.1
55 +
56 +# the patterns for both name and value are more leniant than RFC
57 +# definitions to allow for backwards compatibility
58 +_is_legal_header_name = re.compile(r'\A[^:\s][^:\r\n]*\Z').match
59 +_is_illegal_header_value = re.compile(r'\n(?![ \t])|\r(?![ \t\n])').search
60 +
61  
62  class HTTPMessage(mimetools.Message):
63  
64 @@ -956,7 +985,16 @@ class HTTPConnection:
65          if self.__state != _CS_REQ_STARTED:
66              raise CannotSendHeader()
67  
68 -        hdr = '%s: %s' % (header, '\r\n\t'.join([str(v) for v in values]))
69 +        header = '%s' % header
70 +        if not _is_legal_header_name(header):
71 +            raise ValueError('Invalid header name %r' % (header,))
72 +
73 +        values = [str(v) for v in values]
74 +        for one_value in values:
75 +            if _is_illegal_header_value(one_value):
76 +                raise ValueError('Invalid header value %r' % (one_value,))
77 +
78 +        hdr = '%s: %s' % (header, '\r\n\t'.join(values))
79          self._output(hdr)
80  
81      def endheaders(self, message_body=None):
82 diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
83 index c29aceb..29af589 100644
84 --- a/Lib/test/test_httplib.py
85 +++ b/Lib/test/test_httplib.py
86 @@ -133,6 +133,33 @@ class HeaderTests(TestCase):
87          conn.putheader('Content-length',42)
88          self.assertTrue('Content-length: 42' in conn._buffer)
89  
90 +        conn.putheader('Foo', ' bar ')
91 +        self.assertIn(b'Foo:  bar ', conn._buffer)
92 +        conn.putheader('Bar', '\tbaz\t')
93 +        self.assertIn(b'Bar: \tbaz\t', conn._buffer)
94 +        conn.putheader('Authorization', 'Bearer mytoken')
95 +        self.assertIn(b'Authorization: Bearer mytoken', conn._buffer)
96 +        conn.putheader('IterHeader', 'IterA', 'IterB')
97 +        self.assertIn(b'IterHeader: IterA\r\n\tIterB', conn._buffer)
98 +        conn.putheader('LatinHeader', b'\xFF')
99 +        self.assertIn(b'LatinHeader: \xFF', conn._buffer)
100 +        conn.putheader('Utf8Header', b'\xc3\x80')
101 +        self.assertIn(b'Utf8Header: \xc3\x80', conn._buffer)
102 +        conn.putheader('C1-Control', b'next\x85line')
103 +        self.assertIn(b'C1-Control: next\x85line', conn._buffer)
104 +        conn.putheader('Embedded-Fold-Space', 'is\r\n allowed')
105 +        self.assertIn(b'Embedded-Fold-Space: is\r\n allowed', conn._buffer)
106 +        conn.putheader('Embedded-Fold-Tab', 'is\r\n\tallowed')
107 +        self.assertIn(b'Embedded-Fold-Tab: is\r\n\tallowed', conn._buffer)
108 +        conn.putheader('Key Space', 'value')
109 +        self.assertIn(b'Key Space: value', conn._buffer)
110 +        conn.putheader('KeySpace ', 'value')
111 +        self.assertIn(b'KeySpace : value', conn._buffer)
112 +        conn.putheader(b'Nonbreak\xa0Space', 'value')
113 +        self.assertIn(b'Nonbreak\xa0Space: value', conn._buffer)
114 +        conn.putheader(b'\xa0NonbreakSpace', 'value')
115 +        self.assertIn(b'\xa0NonbreakSpace: value', conn._buffer)
116 +
117      def test_ipv6host_header(self):
118          # Default host header on IPv6 transaction should wrapped by [] if
119          # its actual IPv6 address
120 @@ -152,6 +179,35 @@ class HeaderTests(TestCase):
121          conn.request('GET', '/foo')
122          self.assertTrue(sock.data.startswith(expected))
123  
124 +    def test_invalid_headers(self):
125 +        conn = httplib.HTTPConnection('example.com')
126 +        conn.sock = FakeSocket('')
127 +        conn.putrequest('GET', '/')
128 +
129 +        # http://tools.ietf.org/html/rfc7230#section-3.2.4, whitespace is no
130 +        # longer allowed in header names
131 +        cases = (
132 +            (b'Invalid\r\nName', b'ValidValue'),
133 +            (b'Invalid\rName', b'ValidValue'),
134 +            (b'Invalid\nName', b'ValidValue'),
135 +            (b'\r\nInvalidName', b'ValidValue'),
136 +            (b'\rInvalidName', b'ValidValue'),
137 +            (b'\nInvalidName', b'ValidValue'),
138 +            (b' InvalidName', b'ValidValue'),
139 +            (b'\tInvalidName', b'ValidValue'),
140 +            (b'Invalid:Name', b'ValidValue'),
141 +            (b':InvalidName', b'ValidValue'),
142 +            (b'ValidName', b'Invalid\r\nValue'),
143 +            (b'ValidName', b'Invalid\rValue'),
144 +            (b'ValidName', b'Invalid\nValue'),
145 +            (b'ValidName', b'InvalidValue\r\n'),
146 +            (b'ValidName', b'InvalidValue\r'),
147 +            (b'ValidName', b'InvalidValue\n'),
148 +        )
149 +        for name, value in cases:
150 +            with self.assertRaisesRegexp(ValueError, 'Invalid header'):
151 +                conn.putheader(name, value)
152 +
153  
154  class BasicTest(TestCase):
155      def test_status_lines(self):
156 -- 
157 2.9.0
158