Skip to content

Commit

Permalink
asyncio: Only allow Unix Stream sockets for loop.create_unix_server/c…
Browse files Browse the repository at this point in the history
…onnection
  • Loading branch information
1st1 committed Oct 7, 2016
1 parent 95cdf36 commit 36e7e97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
11 changes: 9 additions & 2 deletions Lib/asyncio/unix_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ def create_unix_connection(self, protocol_factory, path, *,
else:
if sock is None:
raise ValueError('no path and sock were specified')
if (sock.family != socket.AF_UNIX or
sock.type != socket.SOCK_STREAM):
raise ValueError(
'A UNIX Domain Stream Socket was expected, got {!r}'
.format(sock))
sock.setblocking(False)

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

if sock.family != socket.AF_UNIX:
if (sock.family != socket.AF_UNIX or
sock.type != socket.SOCK_STREAM):
raise ValueError(
'A UNIX Domain Socket was expected, got {!r}'.format(sock))
'A UNIX Domain Stream Socket was expected, got {!r}'
.format(sock))

server = base_events.Server(self, [sock])
sock.listen(backlog)
Expand Down
11 changes: 10 additions & 1 deletion Lib/test/test_asyncio/test_unix_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,16 @@ def test_create_unix_server_path_inetsock(self):
coro = self.loop.create_unix_server(lambda: None, path=None,
sock=sock)
with self.assertRaisesRegex(ValueError,
'A UNIX Domain Socket was expected'):
'A UNIX Domain Stream.*was expected'):
self.loop.run_until_complete(coro)

def test_create_unix_connection_path_inetsock(self):
sock = socket.socket()
with sock:
coro = self.loop.create_unix_connection(lambda: None, path=None,
sock=sock)
with self.assertRaisesRegex(ValueError,
'A UNIX Domain Stream.*was expected'):
self.loop.run_until_complete(coro)

@mock.patch('asyncio.unix_events.socket')
Expand Down

0 comments on commit 36e7e97

Please sign in to comment.