Skip to content

Commit 2ecaa72

Browse files
committed
Prohibit DNS lookups in UDP.sendto()
See issue #91.
1 parent 0674b69 commit 2ecaa72

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

tests/test_udp.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,23 @@ def test_create_datagram_endpoint_wrong_sock(self):
167167
'A UDP Socket was expected'):
168168
self.loop.run_until_complete(coro)
169169

170+
def test_udp_sendto_dns(self):
171+
coro = self.loop.create_datagram_endpoint(
172+
asyncio.DatagramProtocol,
173+
local_addr=('127.0.0.1', 0),
174+
family=socket.AF_INET)
175+
176+
s_transport, server = self.loop.run_until_complete(coro)
177+
178+
with self.assertRaisesRegex(ValueError, 'DNS lookup'):
179+
s_transport.sendto(b'aaaa', ('example.com', 80))
180+
181+
with self.assertRaisesRegex(ValueError, 'socket family mismatch'):
182+
s_transport.sendto(b'aaaa', ('::1', 80))
183+
184+
s_transport.close()
185+
self.loop.run_until_complete(asyncio.sleep(0.01, loop=self.loop))
186+
170187

171188
class Test_AIO_UDP(_TestUDP, tb.AIOTestCase):
172189
pass

uvloop/handles/udp.pyx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ cdef class UDPTransport(UVBaseTransport):
139139
raise ValueError(
140140
'Invalid address: must be None or {}'.format(self.address))
141141

142+
if addr is not None:
143+
addrinfo = __static_getaddrinfo_pyaddr(
144+
addr[0], addr[1],
145+
uv.AF_UNSPEC, self.sock.type, self.sock.proto, 0)
146+
if addrinfo is None:
147+
raise ValueError(
148+
'UDP.sendto(): address {!r} requires a DNS lookup'.format(
149+
addr))
150+
if addrinfo[0] != self.sock.family:
151+
raise ValueError(
152+
'UDP.sendto(): {!r} socket family mismatch'.format(
153+
addr))
154+
142155
if self._conn_lost and self._address:
143156
if self._conn_lost >= LOG_THRESHOLD_FOR_CONNLOST_WRITES:
144157
aio_logger.warning('socket.send() raised exception.')

0 commit comments

Comments
 (0)