8000 GH-5054: CGIHTTPRequestHandler.run_cgi() HTTP_ACCEPT improperly parse… · python/cpython@da3d2ab · GitHub
[go: up one dir, main page]

Skip to content

Commit da3d2ab

Browse files
authored
GH-5054: CGIHTTPRequestHandler.run_cgi() HTTP_ACCEPT improperly parsed (#23638)
1 parent 556d97f commit da3d2ab

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

Lib/http/server.py

Original file line numberDiff line numberDiff line change
@@ -1122,12 +1122,7 @@ def run_cgi(self):
11221122
referer = self.headers.get('referer')
11231123
if referer:
11241124
env['HTTP_REFERER'] = referer
1125-
accept = []
1126-
for line in self.headers.getallmatchingheaders('accept'):
1127-
if line[:1] in "\t\n\r ":
1128-
accept.append(line.strip())
1129-
else:
1130-
accept = accept + line[7:].split(',')
1125+
accept = self.headers.get_all('accept', ())
11311126
env['HTTP_ACCEPT'] = ','.join(accept)
11321127
ua = self.headers.get('user-agent')
11331128
if ua:

Lib/test/test_httpservers.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Written by Cody A.W. Somerville <cody-somerville@ubuntu.com>,
44
Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest.
55
"""
6-
6+
from collections import OrderedDict
77
from http.server import BaseHTTPRequestHandler, HTTPServer, \
88
SimpleHTTPRequestHandler, CGIHTTPRequestHandler
99
from http import server, HTTPStatus
@@ -19,7 +19,7 @@
1919
import email.message
2020
import email.utils
2121
import html
22-
import http.client
22+
import http, http.client
2323
import urllib.parse
2424
import tempfile
2525
import time
@@ -588,6 +588,15 @@ def test_html_escape_filename(self):
588588
print(os.environ["%s"])
589589
"""
590590

591+
cgi_file6 = """\
592+
#!%s
593+
import os
594+
595+
print("Content-type: text/plain")
596+
print()
597+
print(repr(os.environ))
598+
"""
599+
591600

592601
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
593602
"This test can't be run reliably as root (issue #13308).")
@@ -666,6 +675,11 @@ def setUp(self):
666675
file5.write(cgi_file1 % self.pythonexe)
667676
os.chmod(self.file5_path, 0o777)
668677

678+
self.file6_path = os.path.join(self.cgi_dir, 'file6.py')
679+
with open(self.file6_path, 'w', encoding='utf-8') as file6:
680+
file6.write(cgi_file6 % self.pythonexe)
681+
os.chmod(self.file6_path, 0o777)
682+
669683
os.chdir(self.parent_dir)
670684

671685
def tearDown(self):
@@ -685,6 +699,8 @@ def tearDown(self):
685699
os.remove(self.file4_path)
686700
if self.file5_path:
687701
os.remove(self.file5_path)
702+
if self.file6_path:
703+
os.remove(self.file6_path)
688704
os.rmdir(self.cgi_child_dir)
689705
os.rmdir(self.cgi_dir)
690706
os.rmdir(self.cgi_dir_in_sub_dir)
@@ -818,6 +834,23 @@ def test_cgi_path_in_sub_directories(self):
818834
finally:
819835
CGIHTTPRequestHandler.cgi_directories.remove('/sub/dir/cgi-bin')
820836

837+
def test_accept(self):
838+
browser_accept = \
839+
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
840+
tests = (
841+
((('Accept', browser_accept),), browser_accept),
842+
((), ''),
843+
# Hack case to get two values for the one header
844+
((('Accept', 'text/html'), ('ACCEPT', 'text/plain')),
845+
'text/html,text/plain'),
846+
)
847+
for headers, expected in tests:
848+
headers = OrderedDict(headers)
849+
with self.subTest(headers):
850+
res = self.request('/cgi-bin/file6.py', 'GET', headers=headers)
851+
self.assertEqual(http.HTTPStatus.OK, res.status)
852+
expected = f"'HTTP_ACCEPT': {expected!r}"
853+
self.assertIn(expected.encode('ascii'), res.read())
821854

822855

823856
class SocketlessRequestHandler(SimpleHTTPRequestHandler):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CGIHTTPRequestHandler.run_cgi() HTTP_ACCEPT improperly parsed. Replace the
2+
special purpose getallmatchingheaders with generic get_all method and add
3+
relevant tests.
4+
5+
Original Patch by Martin Panter. Modified by Senthil Kumaran.

0 commit comments

Comments
 (0)
0