mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 16:41:32 +00:00
Implement message validation in TFW proxying
This commit is contained in:
parent
ecec1cde59
commit
b082279916
@ -1,7 +1,7 @@
|
|||||||
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
||||||
# All Rights Reserved. See LICENSE file for details.
|
# 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 .zmq_connector_base import ZMQConnectorBase
|
||||||
# from .controller_connector import ControllerConnector # TODO: readd once controller stuff is resolved
|
# from .controller_connector import ControllerConnector # TODO: readd once controller stuff is resolved
|
||||||
from .message_sender import MessageSender
|
from .message_sender import MessageSender
|
||||||
|
@ -24,6 +24,12 @@ The purpose of this module is abstracting away this low level behaviour.
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
def validate_message(message):
|
||||||
|
if 'key' not in message:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def serialize_tfw_msg(message):
|
def serialize_tfw_msg(message):
|
||||||
return _serialize_all(message['key'], message)
|
return _serialize_all(message['key'], message)
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ from abc import ABC, abstractmethod
|
|||||||
|
|
||||||
from tornado.websocket import WebSocketHandler
|
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
|
from tfw.config.logs import logging
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
@ -32,15 +32,17 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC):
|
|||||||
keyhandlers = {'mirror': self.mirror}
|
keyhandlers = {'mirror': self.mirror}
|
||||||
|
|
||||||
message = deserialize_tfw_msg(*msg_parts)
|
message = deserialize_tfw_msg(*msg_parts)
|
||||||
key = message['key']
|
|
||||||
LOG.debug('Received on pull socket: %s', message)
|
LOG.debug('Received on pull socket: %s', message)
|
||||||
|
if not validate_message(message):
|
||||||
|
return
|
||||||
|
|
||||||
self.handle_trigger(message)
|
self.handle_trigger(message)
|
||||||
if key not in keyhandlers:
|
if message['key'] not in keyhandlers:
|
||||||
for instance in ZMQWebSocketHandler.instances:
|
for instance in ZMQWebSocketHandler.instances:
|
||||||
instance.write_message(message)
|
instance.write_message(message)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
keyhandlers[key](message)
|
keyhandlers[message['key']](message)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
LOG.error('Invalid mirror message format! Ignoring.')
|
LOG.error('Invalid mirror message format! Ignoring.')
|
||||||
|
|
||||||
@ -50,7 +52,8 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC):
|
|||||||
|
|
||||||
def on_message(self, message):
|
def on_message(self, message):
|
||||||
LOG.debug('Received on WebSocket: %s', message)
|
LOG.debug('Received on WebSocket: %s', message)
|
||||||
self.send_message(self.make_eventhandler_response(message))
|
if validate_message(message):
|
||||||
|
self.send_message(self.make_eventhandler_response(message))
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def make_eventhandler_response(self, message):
|
def make_eventhandler_response(self, message):
|
||||||
|
Loading…
Reference in New Issue
Block a user