Skip to content

Commit 49fe757

Browse files
authored
[3.14] Revert "pythongh-119452: Fix a potential virtual memory allocation denial of service in http.server (pythonGH-119455)" (python#142184)
Fix a potential virtual memory allocation denial of service in http.server (pythonGH-119455)"
1 parent 82274c4 commit 49fe757

File tree

3 files changed

+1
-57
lines changed

3 files changed

+1
-57
lines changed

Lib/http/server.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,6 @@
134134

135135
DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"
136136

137-
# Data larger than this will be read in chunks, to prevent extreme
138-
# overallocation.
139-
_MIN_READ_BUF_SIZE = 1 << 20
140-
141137
class HTTPServer(socketserver.TCPServer):
142138

143139
allow_reuse_address = True # Seems to make sense in testing environment
@@ -1288,16 +1284,7 @@ def run_cgi(self):
12881284
env = env
12891285
)
12901286
if self.command.lower() == "post" and nbytes > 0:
1291-
cursize = 0
1292-
data = self.rfile.read(min(nbytes, _MIN_READ_BUF_SIZE))
1293-
while (len(data) < nbytes and len(data) != cursize and
1294-
select.select([self.rfile._sock], [], [], 0)[0]):
1295-
cursize = len(data)
1296-
# This is a geometric increase in read size (never more
1297-
# than doubling our the current length of data per loop
1298-
# iteration).
1299-
delta = min(cursize, nbytes - cursize)
1300-
data += self.rfile.read(delta)
1287+
data = self.rfile.read(nbytes)
13011288
else:
13021289
data = None
13031290
# throw away additional data [see bug #427345]

Lib/test/test_httpservers.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -913,20 +913,6 @@ def test_path_without_leading_slash(self):
913913
print("</pre>")
914914
"""
915915

916-
cgi_file7 = """\
917-
#!%s
918-
import os
919-
import sys
920-
921-
print("Content-type: text/plain")
922-
print()
923-
924-
content_length = int(os.environ["CONTENT_LENGTH"])
925-
body = sys.stdin.buffer.read(content_length)
926-
927-
print(f"{content_length} {len(body)}")
928-
"""
929-
930916

931917
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
932918
"This test can't be run reliably as root (issue #13308).")
@@ -966,8 +952,6 @@ def setUp(self):
966952
self.file3_path = None
967953
self.file4_path = None
968954
self.file5_path = None
969-
self.file6_path = None
970-
self.file7_path = None
971955

972956
# The shebang line should be pure ASCII: use symlink if possible.
973957
# See issue #7668.
@@ -1022,11 +1006,6 @@ def setUp(self):
10221006
file6.write(cgi_file6 % self.pythonexe)
10231007
os.chmod(self.file6_path, 0o777)
10241008

1025-
self.file7_path = os.path.join(self.cgi_dir, 'file7.py')
1026-
with open(self.file7_path, 'w', encoding='utf-8') as file7:
1027-
file7.write(cgi_file7 % self.pythonexe)
1028-
os.chmod(self.file7_path, 0o777)
1029-
10301009
os.chdir(self.parent_dir)
10311010

10321011
def tearDown(self):
@@ -1049,8 +1028,6 @@ def tearDown(self):
10491028
os.remove(self.file5_path)
10501029
if self.file6_path:
10511030
os.remove(self.file6_path)
1052-
if self.file7_path:
1053-
os.remove(self.file7_path)
10541031
os.rmdir(self.cgi_child_dir)
10551032
os.rmdir(self.cgi_dir)
10561033
os.rmdir(self.cgi_dir_in_sub_dir)
@@ -1123,21 +1100,6 @@ def test_post(self):
11231100

11241101
self.assertEqual(res.read(), b'1, python, 123456' + self.linesep)
11251102

1126-
def test_large_content_length(self):
1127-
for w in range(15, 25):
1128-
size = 1 << w
1129-
body = b'X' * size
1130-
headers = {'Content-Length' : str(size)}
1131-
res = self.request('/cgi-bin/file7.py', 'POST', body, headers)
1132-
self.assertEqual(res.read(), b'%d %d' % (size, size) + self.linesep)
1133-
1134-
def test_large_content_length_truncated(self):
1135-
for w in range(18, 65):
1136-
size = 1 << w
1137-
headers = {'Content-Length' : str(size)}
1138-
res = self.request('/cgi-bin/file1.py', 'POST', b'x', headers)
1139-
self.assertEqual(res.read(), b'Hello World' + self.linesep)
1140-
11411103
def test_invaliduri(self):
11421104
res = self.request('/cgi-bin/invalid')
11431105
res.read()

Misc/NEWS.d/next/Security/2024-05-23-11-44-41.gh-issue-119452.PRfsSv.rst

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)