Implement message validation in TFW proxying

This commit is contained in:
Kristóf Tóth 2018-04-13 20:56:28 +02:00
parent ecec1cde59
commit b082279916
3 changed files with 15 additions and 6 deletions

View File

@ -1,7 +1,7 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
from .serialization import serialize_tfw_msg, deserialize_tfw_msg
from .serialization import serialize_tfw_msg, deserialize_tfw_msg, validate_message
from .zmq_connector_base import ZMQConnectorBase
# from .controller_connector import ControllerConnector # TODO: readd once controller stuff is resolved
from .message_sender import MessageSender

View File

@ -24,6 +24,12 @@ The purpose of this module is abstracting away this low level behaviour.
import json
def validate_message(message):
if 'key' not in message:
return False
return True
def serialize_tfw_msg(message):
return _serialize_all(message['key'], message)

View File

@ -6,7 +6,7 @@ from abc import ABC, abstractmethod
from tornado.websocket import WebSocketHandler
from tfw.networking import deserialize_tfw_msg
from tfw.networking import deserialize_tfw_msg, validate_message
from tfw.config.logs import logging
LOG = logging.getLogger(__name__)
@ -32,15 +32,17 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC):
keyhandlers = {'mirror': self.mirror}
message = deserialize_tfw_msg(*msg_parts)
key = message['key']
LOG.debug('Received on pull socket: %s', message)
if not validate_message(message):
return
self.handle_trigger(message)
if key not in keyhandlers:
if message['key'] not in keyhandlers:
for instance in ZMQWebSocketHandler.instances:
instance.write_message(message)
else:
try:
keyhandlers[key](message)
keyhandlers[message['key']](message)
except KeyError:
LOG.error('Invalid mirror message format! Ignoring.')
@ -50,6 +52,7 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC):
def on_message(self, message):
LOG.debug('Received on WebSocket: %s', message)
if validate_message(message):
self.send_message(self.make_eventhandler_response(message))
@abstractmethod