Skip to content

Commit 506a2aa

Browse files
committed
apply bpo-37228
* refs python/cpython#17311
1 parent 9e017e6 commit 506a2aa

File tree

5 files changed

+62
-16
lines changed

5 files changed

+62
-16
lines changed

tests/test_udp.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,47 @@ def datagram_received(self, data, addr):
328328
tr.close()
329329
self.loop.run_until_complete(pr.done)
330330

331+
def _skip_create_datagram_endpoint_reuse_addr(self):
332+
if self.implementation == 'asyncio':
333+
if sys.version_info[:2] >= (3, 11):
334+
raise unittest.SkipTest()
335+
if (3, 8, 0) <= sys.version_info < (3, 8, 1):
336+
raise unittest.SkipTest()
337+
if (3, 7, 0) <= sys.version_info < (3, 7, 6):
338+
raise unittest.SkipTest()
339+
if sys.version_info < (3, 6, 10):
340+
raise unittest.SkipTest()
341+
342+
def test_create_datagram_endpoint_reuse_address_error(self):
343+
# bpo-37228: Ensure that explicit passing of `reuse_address=True`
344+
# raises an error, as it is not safe to use SO_REUSEADDR when using UDP
345+
346+
self._skip_create_datagram_endpoint_reuse_addr()
347+
348+
coro = self.loop.create_datagram_endpoint(
349+
lambda: MyDatagramProto(loop=self.loop),
350+
local_addr=('127.0.0.1', 0),
351+
reuse_address=True)
352+
353+
with self.assertRaises(ValueError):
354+
self.loop.run_until_complete(coro)
355+
356+
def test_create_datagram_endpoint_reuse_address_warning(self):
357+
# bpo-37228: Deprecate *reuse_address* parameter
358+
359+
self._skip_create_datagram_endpoint_reuse_addr()
360+
361+
coro = self.loop.create_datagram_endpoint(
362+
lambda: MyDatagramProto(loop=self.loop),
363+
local_addr=('127.0.0.1', 0),
364+
reuse_address=False)
365+
366+
with self.assertWarns(DeprecationWarning):
367+
tr, pr = self.loop.run_until_complete(coro)
368+
369+
tr.close()
370+
self.loop.run_until_complete(pr.done)
371+
331372

332373
class Test_UV_UDP(_TestUDP, tb.UVTestCase):
333374

uvloop/handles/udp.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ cdef class UDPTransport(UVBaseTransport):
99

1010
cdef _connect(self, system.sockaddr* addr, size_t addr_len)
1111

12-
cdef _bind(self, system.sockaddr* addr, bint reuse_addr)
12+
cdef _bind(self, system.sockaddr* addr)
1313
cdef open(self, int family, int sockfd)
1414
cdef _set_broadcast(self, bint on)
1515

uvloop/handles/udp.pyx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,13 @@ cdef class UDPTransport(UVBaseTransport):
104104
exc = convert_error(err)
105105
raise exc
106106

107-
cdef _bind(self, system.sockaddr* addr, bint reuse_addr):
107+
cdef _bind(self, system.sockaddr* addr):
108108
cdef:
109109
int err
110110
int flags = 0
111111

112112
self._ensure_alive()
113113

114-
if reuse_addr:
115-
flags |= uv.UV_UDP_REUSEADDR
116-
117114
err = uv.uv_udp_bind(<uv.uv_udp_t*>self._handle, addr, flags)
118115
if err < 0:
119116
exc = convert_error(err)

uvloop/includes/uv.pxd

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ cdef extern from "uv.h" nogil:
199199

200200
ctypedef enum uv_udp_flags:
201201
UV_UDP_IPV6ONLY = 1,
202-
UV_UDP_PARTIAL = 2,
203-
UV_UDP_REUSEADDR = 4
202+
UV_UDP_PARTIAL = 2
204203

205204
ctypedef enum uv_membership:
206205
UV_LEAVE_GROUP = 0,

uvloop/loop.pyx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ cdef inline socket_dec_io_ref(sock):
9090
sock._decref_socketios()
9191

9292

93+
# Used for deprecation and removal of `loop.create_datagram_endpoint()`'s
94+
# *reuse_address* parameter
95+
_unset = object()
96+
97+
9398
@cython.no_gc_clear
9499
cdef class Loop:
95100
def __cinit__(self):
@@ -2924,7 +2929,7 @@ cdef class Loop:
29242929
async def create_datagram_endpoint(self, protocol_factory,
29252930
local_addr=None, remote_addr=None, *,
29262931
family=0, proto=0, flags=0,
2927-
reuse_address=None, reuse_port=None,
2932+
reuse_address=_unset, reuse_port=None,
29282933
allow_broadcast=None, sock=None):
29292934
"""A coroutine which creates a datagram endpoint.
29302935
@@ -2936,11 +2941,6 @@ cdef class Loop:
29362941
socket family AF_INET or socket.AF_INET6 depending on host (or
29372942
family if specified), socket type SOCK_DGRAM.
29382943
2939-
reuse_address tells the kernel to reuse a local socket in
2940-
TIME_WAIT state, without waiting for its natural timeout to
2941-
expire. If not specified it will automatically be set to True on
2942-
UNIX.
2943-
29442944
reuse_port tells the kernel to allow this endpoint to be bound to
29452945
the same port as other existing endpoints are bound to, so long as
29462946
they all set this flag when being created. This option is not
@@ -2965,7 +2965,7 @@ cdef class Loop:
29652965
'A UDP Socket was expected, got {!r}'.format(sock))
29662966
if (local_addr or remote_addr or
29672967
family or proto or flags or
2968-
reuse_address or reuse_port or allow_broadcast):
2968+
reuse_port or allow_broadcast):
29692969
# show the problematic kwargs in exception msg
29702970
opts = dict(local_addr=local_addr, remote_addr=remote_addr,
29712971
family=family, proto=proto, flags=flags,
@@ -2982,7 +2982,16 @@ cdef class Loop:
29822982
udp.open(sock.family, sock.fileno())
29832983
udp._attach_fileobj(sock)
29842984
else:
2985-
reuse_address = bool(reuse_address)
2985+
if reuse_address is not _unset:
2986+
if reuse_address:
2987+
raise ValueError("Passing `reuse_address=True` is no "
2988+
"longer supported, as the usage of "
2989+
"SO_REUSEPORT in UDP poses a significant "
2990+
"security concern.")
2991+
else:
2992+
warnings_warn("The *reuse_address* parameter has been "
2993+
"deprecated as of 0.15.", DeprecationWarning,
2994+
stacklevel=2)
29862995
reuse_port = bool(reuse_port)
29872996
if reuse_port and not has_SO_REUSEPORT:
29882997
raise ValueError(
@@ -3035,7 +3044,7 @@ cdef class Loop:
30353044
udp._init(self, lai.ai_family)
30363045
if reuse_port:
30373046
self._sock_set_reuseport(udp._fileno())
3038-
udp._bind(lai.ai_addr, reuse_address)
3047+
udp._bind(lai.ai_addr)
30393048
except (KeyboardInterrupt, SystemExit):
30403049
raise
30413050
except BaseException as ex:

0 commit comments

Comments
 (0)