More messagetypes hardening

- only load modules at program start
This commit is contained in:
Peter Šurda 2018-02-19 16:57:47 +01:00
parent 757740369b
commit fde194f9b0
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87

View File

@ -3,6 +3,7 @@ from os import path, listdir
from string import lower from string import lower
from debug import logger from debug import logger
import messagetypes
import paths import paths
class MsgBase(object): class MsgBase(object):
@ -15,9 +16,8 @@ def constructObject(data):
if data[""] not in whitelist: if data[""] not in whitelist:
return None return None
try: try:
m = import_module("messagetypes." + data[""]) classBase = getattr(getattr(messagetypes, data[""]), data[""].title())
classBase = getattr(m, data[""].title()) except (NameError, AttributeError):
except (NameError, ImportError):
logger.error("Don't know how to handle message type: \"%s\"", data[""], exc_info=True) logger.error("Don't know how to handle message type: \"%s\"", data[""], exc_info=True)
return None return None
try: try:
@ -43,7 +43,7 @@ else:
if splitted[1] != ".py": if splitted[1] != ".py":
continue continue
try: try:
import_module("." + splitted[0], "messagetypes") import_module(".{}".format(splitted[0]), "messagetypes")
except ImportError: except ImportError:
logger.error("Error importing %s", mod, exc_info=True) logger.error("Error importing %s", mod, exc_info=True)
else: else: