Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit 955f5ed

Browse files
committed
Only allow UNIX Stream sockets for loop.create_unix_server/connection
1 parent d4abdd5 commit 955f5ed

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

asyncio/unix_events.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ def create_unix_connection(self, protocol_factory, path, *,
234234
else:
235235
if sock is None:
236236
raise ValueError('no path and sock were specified')
237+
if (sock.family != socket.AF_UNIX or
238+
sock.type != socket.SOCK_STREAM):
239+
raise ValueError(
240+
'A UNIX Domain Stream Socket was expected, got {!r}'
241+
.format(sock))
237242
sock.setblocking(False)
238243

239244
transport, protocol = yield from self._create_connection_transport(
@@ -272,9 +277,11 @@ def create_unix_server(self, protocol_factory, path=None, *,
272277
raise ValueError(
273278
'path was not specified, and no sock specified')
274279

275-
if sock.family != socket.AF_UNIX:
280+
if (sock.family != socket.AF_UNIX or
281+
sock.type != socket.SOCK_STREAM):
276282
raise ValueError(
277-
'A UNIX Domain Socket was expected, got {!r}'.format(sock))
283+
'A UNIX Domain Stream Socket was expected, got {!r}'
284+
.format(sock))
278285

279286
server = base_events.Server(self, [sock])
280287
sock.listen(backlog)

tests/test_unix_events.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,16 @@ def test_create_unix_server_path_inetsock(self):
273273
coro = self.loop.create_unix_server(lambda: None, path=None,
274274
sock=sock)
275275
with self.assertRaisesRegex(ValueError,
276-
'A UNIX Domain Socket was expected'):
276+
'A UNIX Domain Stream.*was expected'):
277+
self.loop.run_until_complete(coro)
278+
279+
def test_create_unix_connection_path_inetsock(self):
280+
sock = socket.socket()
281+
with sock:
282+
coro = self.loop.create_unix_connection(lambda: None, path=None,
283+
sock=sock)
284+
with self.assertRaisesRegex(ValueError,
285+
'A UNIX Domain Stream.*was expected'):
277286
self.loop.run_until_complete(coro)
278287

279288
@mock.patch('asyncio.unix_events.socket')

0 commit comments

Comments
 (0)