From 121d495424fb77f04fad3b225439077ce5cff891 Mon Sep 17 00:00:00 2001 From: Min RK Date: Mon, 26 Jan 2015 11:11:13 -0800 Subject: [PATCH] protect websocket against errant messages - message handler being triggered after stream is closed (no channels) - messages sent to invalid channels --- IPython/html/services/kernels/handlers.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/IPython/html/services/kernels/handlers.py b/IPython/html/services/kernels/handlers.py index 3b17f3bfa..308a5a99a 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)