Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,8 @@ The add_argument() method

* deprecated_ - Whether or not use of the argument is deprecated.

The method returns an :class:`Action` object representing the argument.

The following sections describe how each of these are used.


Expand Down
4 changes: 4 additions & 0 deletions Lib/test/pythoninfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,10 @@ def collect_test_socket(info_add):
if name.startswith('HAVE_')]
copy_attributes(info_add, test_socket, 'test_socket.%s', attributes)

# Get IOCTL_VM_SOCKETS_GET_LOCAL_CID of /dev/vsock
cid = test_socket.get_cid()
info_add('test_socket.get_cid', cid)


def collect_support(info_add):
try:
Expand Down
15 changes: 12 additions & 3 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,8 @@ def clientTearDown(self):
@unittest.skipIf(WSL, 'VSOCK does not work on Microsoft WSL')
@unittest.skipUnless(HAVE_SOCKET_VSOCK,
'VSOCK sockets required for this test.')
@unittest.skipUnless(get_cid() != 2, # VMADDR_CID_HOST
"This test can only be run on a virtual guest.")
@unittest.skipIf(get_cid() == getattr(socket, 'VMADDR_CID_HOST', 2),
"This test can only be run on a virtual guest.")
class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest):

def __init__(self, methodName='runTest'):
Expand All @@ -574,7 +574,16 @@ def __init__(self, methodName='runTest'):
def setUp(self):
self.serv = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
self.addCleanup(self.serv.close)
self.serv.bind((socket.VMADDR_CID_ANY, VSOCKPORT))
cid = get_cid()
if cid in (socket.VMADDR_CID_HOST, socket.VMADDR_CID_ANY):
cid = socket.VMADDR_CID_LOCAL
try:
self.serv.bind((cid, VSOCKPORT))
except OSError as exc:
if exc.errno == errno.EADDRNOTAVAIL:
self.skipTest(f"bind() failed with {exc!r}")
else:
raise
self.serv.listen()
self.serverExplicitReady()
self.serv.settimeout(support.LOOPBACK_TIMEOUT)
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_wsgiref.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,25 @@ def write(self, b):
self.assertIsNotNone(h.status)
self.assertIsNotNone(h.environ)

def testRaisesControlCharacters(self):
for c0 in control_characters_c0():
with self.subTest(c0):
base = BaseHandler()
with self.assertRaises(ValueError):
base.start_response(c0, [('x', 'y')])

base = BaseHandler()
with self.assertRaises(ValueError):
base.start_response('200 OK', [(c0, 'y')])

# HTAB (\x09) is allowed in header values, but not in names.
base = BaseHandler()
if c0 != "\t":
with self.assertRaises(ValueError):
base.start_response('200 OK', [('x', c0)])
else:
base.start_response('200 OK', [('x', c0)])


class TestModule(unittest.TestCase):
def test_deprecated__version__(self):
Expand Down
4 changes: 3 additions & 1 deletion Lib/wsgiref/handlers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Base classes for server/gateway implementations"""

from .util import FileWrapper, guess_scheme, is_hop_by_hop
from .headers import Headers
from .headers import Headers, _name_disallowed_re

import sys, os, time

Expand Down Expand Up @@ -250,6 +250,8 @@ def start_response(self, status, headers,exc_info=None):
return self.write

def _validate_status(self, status):
if _name_disallowed_re.search(status):
raise ValueError("Control characters are not allowed in status")
if len(status) < 4:
raise AssertionError("Status must be at least 4 characters")
if not status[:3].isdigit():
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,7 @@ Wolfgang Langner
Detlef Lannert
Rémi Lapeyre
Soren Larsen
Seth Michael Larson
Amos Latteier
Keenan Lau
Piers Lauder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
In the free threading build, skip the stop-the-world pause when reassigning
``__class__`` on a newly created object.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Disallow usage of control characters in status in :mod:`wsgiref.handlers` to prevent HTTP header injections.
Patch by Benedikt Johannes.
15 changes: 12 additions & 3 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -7568,7 +7568,11 @@ object_set_class_world_stopped(PyObject *self, PyTypeObject *newto)

assert(_PyObject_GetManagedDict(self) == dict);

if (_PyDict_DetachFromObject(dict, self) < 0) {
int err;
Py_BEGIN_CRITICAL_SECTION(dict);
err = _PyDict_DetachFromObject(dict, self);
Py_END_CRITICAL_SECTION();
if (err < 0) {
return -1;
}

Expand Down Expand Up @@ -7608,10 +7612,15 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
return -1;
}

types_stop_world();
int unique = _PyObject_IsUniquelyReferenced(self);
if (!unique) {
types_stop_world();
}
PyTypeObject *oldto = Py_TYPE(self);
int res = object_set_class_world_stopped(self, newto);
types_start_world();
if (!unique) {
types_start_world();
}
if (res == 0) {
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Py_DECREF(oldto);
Expand Down
Loading