Skip to content

Commit f2c1cf7

Browse files
Do not allow emits on a namespace that is not connected (Fixes miguelgrinberg#325)
1 parent 516a295 commit f2c1cf7

File tree

5 files changed

+26
-0
lines changed

5 files changed

+26
-0
lines changed

socketio/asyncio_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ async def emit(self, event, data=None, namespace=None, callback=None):
142142
Note: this method is a coroutine.
143143
"""
144144
namespace = namespace or '/'
145+
if namespace != '/' and namespace not in self.namespaces:
146+
raise exceptions.BadNamespaceError(
147+
namespace + ' is not a connected namespace.')
145148
self.logger.info('Emitting event "%s" [%s]', event, namespace)
146149
if callback is not None:
147150
id = self._generate_ack_id(namespace, callback)

socketio/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ def emit(self, event, data=None, namespace=None, callback=None):
298298
when addressing an individual client.
299299
"""
300300
namespace = namespace or '/'
301+
if namespace != '/' and namespace not in self.namespaces:
302+
raise exceptions.BadNamespaceError(
303+
namespace + ' is not a connected namespace.')
301304
self.logger.info('Emitting event "%s" [%s]', event, namespace)
302305
if callback is not None:
303306
id = self._generate_ack_id(namespace, callback)

socketio/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ def __init__(self, *args):
2424

2525
class TimeoutError(SocketIOError):
2626
pass
27+
28+
29+
class BadNamespaceError(SocketIOError):
30+
pass

tests/asyncio/test_asyncio_client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ def test_emit_two_arguments(self):
194194

195195
def test_emit_namespace(self):
196196
c = asyncio_client.AsyncClient()
197+
c.namespaces = ['/foo']
197198
c._send_packet = AsyncMock()
198199
_run(c.emit('foo', namespace='/foo'))
199200
expected_packet = packet.Packet(packet.EVENT, namespace='/foo',
@@ -202,6 +203,12 @@ def test_emit_namespace(self):
202203
self.assertEqual(c._send_packet.mock.call_args_list[0][0][0].encode(),
203204
expected_packet.encode())
204205

206+
def test_emit_unknown_namespace(self):
207+
c = asyncio_client.AsyncClient()
208+
c.namespaces = ['/foo']
209+
self.assertRaises(exceptions.BadNamespaceError, _run,
210+
c.emit('foo', namespace='/bar'))
211+
205212
def test_emit_with_callback(self):
206213
c = asyncio_client.AsyncClient()
207214
c._send_packet = AsyncMock()
@@ -216,6 +223,7 @@ def test_emit_with_callback(self):
216223

217224
def test_emit_namespace_with_callback(self):
218225
c = asyncio_client.AsyncClient()
226+
c.namespaces = ['/foo']
219227
c._send_packet = AsyncMock()
220228
c._generate_ack_id = mock.MagicMock(return_value=123)
221229
_run(c.emit('foo', namespace='/foo', callback='cb'))

tests/common/test_client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ def test_emit_two_arguments(self):
288288

289289
def test_emit_namespace(self):
290290
c = client.Client()
291+
c.namespaces = ['/foo']
291292
c._send_packet = mock.MagicMock()
292293
c.emit('foo', namespace='/foo')
293294
expected_packet = packet.Packet(packet.EVENT, namespace='/foo',
@@ -296,6 +297,12 @@ def test_emit_namespace(self):
296297
self.assertEqual(c._send_packet.call_args_list[0][0][0].encode(),
297298
expected_packet.encode())
298299

300+
def test_emit_unknown_namespace(self):
301+
c = client.Client()
302+
c.namespaces = ['/foo']
303+
self.assertRaises(exceptions.BadNamespaceError, c.emit, 'foo',
304+
namespace='/bar')
305+
299306
def test_emit_with_callback(self):
300307
c = client.Client()
301308
c._send_packet = mock.MagicMock()
@@ -310,6 +317,7 @@ def test_emit_with_callback(self):
310317

311318
def test_emit_namespace_with_callback(self):
312319
c = client.Client()
320+
c.namespaces = ['/foo']
313321
c._send_packet = mock.MagicMock()
314322
c._generate_ack_id = mock.MagicMock(return_value=123)
315323
c.emit('foo', namespace='/foo', callback='cb')

0 commit comments

Comments
 (0)