8000 [3.3] bpo-22928: Disabled HTTP header injections in http.client. (#2817) · stackless-dev/stackless@8e88f6b · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 8e88f6b

Browse files
serhiy-storchakaned-deily
authored andcommitted
[3.3] bpo-22928: Disabled HTTP header injections in http.client. (python#2817)
Original patch by Demian Brecht.. (cherry picked from commit a112a8a)
1 parent 8fbdab5 commit 8e88f6b

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

Lib/http/client.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import email.message
7171
import io
7272
import os
73+
import re
7374
import socket
7475
import collections
7576
from urllib.parse import urlsplit
@@ -216,6 +217,34 @@
216217
_MAXLINE = 65536
217218
_MAXHEADERS = 100
218219

220+
# Header name/value ABNF (http://tools.ietf.org/html/rfc7230#section-3.2)
221+
#
222+
# VCHAR = %x21-7E
223+
# obs-text = %x80-FF
224+
# header-field = field-name ":" OWS field-value OWS
225+
# field-name = token
226+
# field-value = *( field-content / obs-fold )
227+
# field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
228+
# field-vchar = VCHAR / obs-text
229+
#
230+
# obs-fold = CRLF 1*( SP / HTAB )
231+
# ; obsolete line folding
232+
# ; see Section 3.2.4
233+
234+
# token = 1*tchar
235+
#
236+
# tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
237+
# / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
238+
# / DIGIT / ALPHA
239+
# ; any VCHAR, except delimiters
240+
#
241+
# VCHAR defined in http://tools.ietf.org/html/rfc5234#appendix-B.1
242+
243+
# the patterns for both name and value are more leniant than RFC
244+
# definitions to allow for backwards compatibility
245+
_is_legal_header_name = re.compile(rb'[^:\s][^:\r\n]*\Z').match
246+
_is_illegal_header_value = re.compile(rb'\n(?![ \t])|\r(?![ \t\n])').search
247+
219248

220249
class HTTPMessage(email.message.Message):
221250
# XXX The only usage of this method is in
@@ -1035,12 +1064,20 @@ def putheader(self, header, *values):
10351064

10361065
if hasattr(header, 'encode'):
10371066
header = header.encode('ascii')
1067+
1068+
if not _is_legal_header_name(header):
1069+
raise ValueError('Invalid header name %r' % (header,))
1070+
10381071
values = list(values)
10391072
for i, one_value in enumerate(values):
10401073
if hasattr(one_value, 'encode'):
10411074
values[i] = one_value.encode('latin-1')
10421075
elif isinstance(one_value, int):
10431076
values[i] = str(one_value).encode('ascii')
1077+
1078+
if _is_illegal_header_value(values[i]):
1079+
raise ValueError('Invalid header value %r' % (values[i],))
1080+
10441081
value = b'\r\n\t'.join(values)
10451082
header = header + b': ' + value
10461083
self._output(header)

Lib/test/test_httplib.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,33 @@ def test_putheader(self):
134134
conn.putheader('Content-length', 42)
135135
self.assertIn(b'Content-length: 42', conn._buffer)
136136

137+
conn.putheader('Foo', ' bar ')
138+
self.assertIn(b'Foo: bar ', conn._buffer)
139+
conn.putheader('Bar', '\tbaz\t')
140+
self.assertIn(b'Bar: \tbaz\t', conn._buffer)
141+
conn.putheader('Authorization', 'Bearer mytoken')
142+
self.assertIn(b'Authorization: Bearer mytoken', conn._buffer)
143+
conn.putheader('IterHeader', 'IterA', 'IterB')
144+
self.assertIn(b'IterHeader: IterA\r\n\tIterB', conn._buffer)
145+
conn.putheader('LatinHeader', b'\xFF')
146+
self.assertIn(b'LatinHeader: \xFF', conn._buffer)
147+
conn.putheader('Utf8Header', b'\xc3\x80')
148+
self.assertIn(b'Utf8Header: \xc3\x80', conn._buffer)
149+
conn.putheader('C1-Control', b'next\x85line')
150+
self.assertIn(b'C1-Control: next\x85line', conn._buffer)
151+
conn.putheader('Embedded-Fold-Space', 'is\r\n allowed')
152+
self.assertIn(b'Embedded-Fold-Space: is\r\n allowed', conn._buffer)
153+
conn.putheader('Embedded-Fold-Tab', 'is\r\n\tallowed')
154+
self.assertIn(b'Embedded-Fold-Tab: is\r\n\tallowed', conn._buffer)
155+
conn.putheader('Key Space', 'value')
156+
self.assertIn(b'Key Space: value', conn._buffer)
157+
conn.putheader('KeySpace ', 'value')
158+
self.assertIn(b'KeySpace : value', conn._buffer)
159+
conn.putheader(b'Nonbreak\xa0Space', 'value')
160+
self.assertIn(b'Nonbreak\xa0Space: value', conn._buffer)
161+
conn.putheader(b'\xa0NonbreakSpace', 'value')
162+
self.assertIn(b'\xa0NonbreakSpace: value', conn._buffer)
163+
137164
def test_ipv6host_header(self):
138165
# Default host header on IPv6 transaction should wrapped by [] if
139166
# its actual IPv6 address
@@ -153,6 +180,35 @@ def test_ipv6host_header(self):
153180
conn.request('GET', '/foo')
154181
self.assertTrue(sock.data.startswith(expected))
155182

183+
def test_invalid_headers(self):
184+
conn = client.HTTPConnection('example.com')
185+
conn.sock = FakeSocket('')
186+
conn.putrequest('GET', '/')
187+
188+
# http://tools.ietf.org/html/rfc7230#section-3.2.4, whitespace is no
189+
# longer allowed in header names
2364 190+
cases = (
191+
(b'Invalid\r\nName', b'ValidValue'),
192+
(b'Invalid\rName', b'ValidValue'),
193+
(b'Invalid\nName', b'ValidValue'),
194+
(b'\r\nInvalidName', b'ValidValue'),
195+
(b'\rInvalidName', b'ValidValue'),
196+
(b'\nInvalidName', b'ValidValue'),
197+
(b' InvalidName', b'ValidValue'),
198+
(b'\tInvalidName', b'ValidValue'),
199+
(b'Invalid:Name', b'ValidValue'),
200+
(b':InvalidName', b'ValidValue'),
201+
(b'ValidName', b'Invalid\r\nValue'),
202+
(b'ValidName', b'Invalid\rValue'),
203+
(b'ValidName', b'Invalid\nValue'),
204+
(b'ValidName', b'InvalidValue\r\n'),
205+
(b'ValidName', b'InvalidValue\r'),
206+
(b'ValidName', b'InvalidValue\n'),
207+
)
208+
for name, value in cases:
209+
with self.assertRaisesRegex(ValueError, 'Invalid header'):
210+
conn.putheader(name, value)
211+
156212

157213
class BasicTest(TestCase):
158214
def test_status_lines(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Disabled HTTP header injections in http.client. Original patch by Demian
2+
Brecht.

0 commit comments

Comments
 (0)
0