Skip to content

Commit f0746ca

Browse files
committed
Issue #16037: HTTPMessage.readheaders() raises an HTTPException when more than
100 headers are read. Adapted from patch by Jyrki Pulliainen.
1 parent ec3c103 commit f0746ca

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

Doc/library/http.client.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ The following exceptions are raised as appropriate:
169169
A subclass of :exc:`HTTPException`. Raised if a server responds with a HTTP
170170
status code that we don't understand.
171171

172-
The constants defined in this module are:
173172

173+
The constants defined in this module are:
174174

175175
.. data:: HTTP_PORT
176176

Lib/http/client.py

+4
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@
206206

207207
# maximal line length when calling readline().
208208
_MAXLINE = 65536
209+
_MAXHEADERS = 100
210+
209211

210212
class HTTPMessage(email.message.Message):
211213
# XXX The only usage of this method is in
@@ -253,6 +255,8 @@ def parse_headers(fp, _class=HTTPMessage):
253255
if len(line) > _MAXLINE:
254256
raise LineTooLong("header line")
255257
headers.append(line)
258+
if len(headers) > _MAXHEADERS:
259+
raise HTTPException("got more than %d headers" % _MAXHEADERS)
256260
if line in (b'\r\n', b'\n', b''):
257261
break
258262
hstring = b''.join(headers).decode('iso-8859-1')

Lib/test/test_httplib.py

+9
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ def test_read_head(self):
272272
if resp.read():
273273
self.fail("Did not expect response from HEAD request")
274274

275+
def test_too_many_headers(self):
276+
headers = '\r\n'.join('Header%d: foo' % i
277+
for i in range(client._MAXHEADERS + 1)) + '\r\n'
278+
text = ('HTTP/1.1 200 OK\r\n' + headers)
279+
s = FakeSocket(text)
280+
r = client.HTTPResponse(s)
281+
self.assertRaisesRegex(client.HTTPException,
282+
r"got more than \d+ headers", r.begin)
283+
275284
def test_send_file(self):
276285
expected = (b'GET /foo HTTP/1.1\r\nHost: example.com\r\n'
277286
b'Accept-Encoding: identity\r\nContent-Length:')

Misc/NEWS

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
++++++++++
1+
+++++++++++
22
Python News
33
+++++++++++
44

@@ -10,6 +10,9 @@ What's New in Python 3.2.6?
1010
Library
1111
-------
1212

13+
- Issue #16037: HTTPMessage.readheaders() raises an HTTPException when more than
14+
100 headers are read. Adapted from patch by Jyrki Pulliainen.
15+
1316
- Issue #18709: Fix CVE-2013-4238. The SSL module now handles NULL bytes
1417
inside subjectAltName correctly. Formerly the module has used OpenSSL's
1518
GENERAL_NAME_print() function to get the string represention of ASN.1

0 commit comments

Comments
 (0)