Compare commits

..

2 Commits

Author SHA1 Message Date
Miss Islington (bot) d5c029b1a9
bpo-42576: Raise TypeError when passing in keyword arguments to GenericAlias (GH-23656)
Use `_PyArg_NoKeywords` instead of `_PyArg_NoKwnames` when checking the `kwds` tuple when creating `GenericAlias`. This fixes an interpreter crash when passing in keyword arguments to `GenericAlias`'s constructor.

Needs backport to 3.9.

Automerge-Triggered-By: GH:gvanrossum
(cherry picked from commit 804d6893b8)

Co-authored-by: kj <28750310+Fidget-Spinner@users.noreply.github.com>
2020-12-05 08:24:38 -08:00
Miss Islington (bot) b630ca7bc1
[3.9] bpo-5054: CGIHTTPRequestHandler.run_cgi() HTTP_ACCEPT improperly parsed (GH-23638) (GH-23657)
(cherry picked from commit da3d2abe6b)


Co-authored-by: Senthil Kumaran <senthil@uthcode.com>

Automerge-Triggered-By: GH:orsenthil
2020-12-05 07:26:37 -08:00
6 changed files with 50 additions and 9 deletions

View File

@ -1123,12 +1123,7 @@ class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
referer = self.headers.get('referer')
if referer:
env['HTTP_REFERER'] = referer
accept = []
for line in self.headers.getallmatchingheaders('accept'):
if line[:1] in "\t\n\r ":
accept.append(line.strip())
else:
accept = accept + line[7:].split(',')
accept = self.headers.get_all('accept', ())
env['HTTP_ACCEPT'] = ','.join(accept)
ua = self.headers.get('user-agent')
if ua:

View File

@ -302,6 +302,11 @@ class BaseTest(unittest.TestCase):
alias = t[int]
self.assertEqual(ref(alias)(), alias)
def test_no_kwargs(self):
# bpo-42576
with self.assertRaises(TypeError):
GenericAlias(bad=float)
if __name__ == "__main__":
unittest.main()

View File

@ -3,7 +3,7 @@
Written by Cody A.W. Somerville <cody-somerville@ubuntu.com>,
Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest.
"""
from collections import OrderedDict
from http.server import BaseHTTPRequestHandler, HTTPServer, \
SimpleHTTPRequestHandler, CGIHTTPRequestHandler
from http import server, HTTPStatus
@ -19,7 +19,7 @@ import shutil
import email.message
import email.utils
import html
import http.client
import http, http.client
import urllib.parse
import tempfile
import time
@ -586,6 +586,15 @@ print()
print(os.environ["%s"])
"""
cgi_file6 = """\
#!%s
import os
print("Content-type: text/plain")
print()
print(repr(os.environ))
"""
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
"This test can't be run reliably as root (issue #13308).")
@ -664,6 +673,11 @@ class CGIHTTPServerTestCase(BaseTestCase):
file5.write(cgi_file1 % self.pythonexe)
os.chmod(self.file5_path, 0o777)
self.file6_path = os.path.join(self.cgi_dir, 'file6.py')
with open(self.file6_path, 'w', encoding='utf-8') as file6:
file6.write(cgi_file6 % self.pythonexe)
os.chmod(self.file6_path, 0o777)
os.chdir(self.parent_dir)
def tearDown(self):
@ -683,6 +697,8 @@ class CGIHTTPServerTestCase(BaseTestCase):
os.remove(self.file4_path)
if self.file5_path:
os.remove(self.file5_path)
if self.file6_path:
os.remove(self.file6_path)
os.rmdir(self.cgi_child_dir)
os.rmdir(self.cgi_dir)
os.rmdir(self.cgi_dir_in_sub_dir)
@ -816,6 +832,23 @@ class CGIHTTPServerTestCase(BaseTestCase):
finally:
CGIHTTPRequestHandler.cgi_directories.remove('/sub/dir/cgi-bin')
def test_accept(self):
browser_accept = \
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
tests = (
((('Accept', browser_accept),), browser_accept),
((), ''),
# Hack case to get two values for the one header
((('Accept', 'text/html'), ('ACCEPT', 'text/plain')),
'text/html,text/plain'),
)
for headers, expected in tests:
headers = OrderedDict(headers)
with self.subTest(headers):
res = self.request('/cgi-bin/file6.py', 'GET', headers=headers)
self.assertEqual(http.HTTPStatus.OK, res.status)
expected = f"'HTTP_ACCEPT': {expected!r}"
self.assertIn(expected.encode('ascii'), res.read())
class SocketlessRequestHandler(SimpleHTTPRequestHandler):

View File

@ -0,0 +1,3 @@
``types.GenericAlias`` will now raise a ``TypeError`` when attempting to
initialize with a keyword argument. Previously, this would cause the
interpreter to crash. Patch by Ken Jin.

View File

@ -0,0 +1,5 @@
CGIHTTPRequestHandler.run_cgi() HTTP_ACCEPT improperly parsed. Replace the
special purpose getallmatchingheaders with generic get_all method and add
relevant tests.
Original Patch by Martin Panter. Modified by Senthil Kumaran.

View File

@ -566,7 +566,7 @@ static PyGetSetDef ga_properties[] = {
static PyObject *
ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
if (!_PyArg_NoKwnames("GenericAlias", kwds)) {
if (!_PyArg_NoKeywords("GenericAlias", kwds)) {
return NULL;
}
if (!_PyArg_CheckPositional("GenericAlias", PyTuple_GET_SIZE(args), 2, 2)) {