From 08a21510093bd1f51598d6a129118f8e7659669c 2015-01-26 19:11:13 From: Min RK Date: 2015-01-26 19:11:13 Subject: [PATCH] protect websocket against errant messages - message handler being triggered after stream is closed (no channels) - messages sent to invalid channels --- diff --git a/IPython/html/services/kernels/handlers.py b/IPython/html/services/kernels/handlers.py index 3b17f3b..308a5a9 100644 --- a/IPython/html/services/kernels/handlers.py +++ b/IPython/html/services/kernels/handlers.py @@ -214,6 +214,10 @@ class ZMQChannelsHandler(AuthenticatedZMQStreamHandler): stream.on_recv_stream(self._on_zmq_reply) def on_message(self, msg): + if not self.channels: + # already closed, ignore the message + self.log.debug("Received message on closed websocket %r", msg) + return if isinstance(msg, bytes): msg = deserialize_binary_message(msg) else: @@ -222,6 +226,9 @@ class ZMQChannelsHandler(AuthenticatedZMQStreamHandler): if channel is None: self.log.warn("No channel specified, assuming shell: %s", msg) channel = 'shell' + if channel not in self.channels: + self.log.warn("No such channel: %r", channel) + return stream = self.channels[channel] self.session.send(stream, msg)