improve downloader/postprocessor module loading

- handle arguments of any type without propagating an exception
- prevent potential security risk through relative imports
This commit is contained in:
Mike Fährmann
2018-09-04 22:49:57 +02:00
parent 712b58a93b
commit e9ae6fd080
2 changed files with 21 additions and 13 deletions

View File

@@ -1,11 +1,13 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright 2015 Mike Fährmann # Copyright 2015-2018 Mike Fährmann
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as # it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation. # published by the Free Software Foundation.
"""Downloader modules"""
import importlib import importlib
@@ -14,13 +16,15 @@ def find(scheme):
try: try:
return _cache[scheme] return _cache[scheme]
except KeyError: except KeyError:
klass = None
try: try:
module = importlib.import_module("."+scheme, __package__) if "." not in scheme: # prevent relative imports
klass = getattr(module, "Downloader") module = importlib.import_module("." + scheme, __package__)
_cache[scheme] = klass klass = module.Downloader
return klass except (ImportError, AttributeError, TypeError):
except ImportError: pass
return None _cache[scheme] = klass
return klass
# -------------------------------------------------------------------- # --------------------------------------------------------------------

View File

@@ -6,6 +6,8 @@
# it under the terms of the GNU General Public License version 2 as # it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation. # published by the Free Software Foundation.
"""Post-processing modules"""
import importlib import importlib
import logging import logging
@@ -17,13 +19,15 @@ def find(name):
try: try:
return _cache[name] return _cache[name]
except KeyError: except KeyError:
klass = None
try: try:
module = importlib.import_module("."+name, __package__) if "." not in name: # prevent relative imports
cls = module.__postprocessor__ module = importlib.import_module("." + name, __package__)
_cache[name] = cls klass = module.__postprocessor__
return cls except (ImportError, AttributeError, TypeError):
except (ImportError, AttributeError): pass
return None _cache[name] = klass
return klass
# -------------------------------------------------------------------- # --------------------------------------------------------------------