🥅 Only import plugin if metadata has any control property
Protects against loading things from the plugin folders that aren't
actually plugins, like errant setup.py files, see for example
https://community.octoprint.org/t/26143
This commit is contained in:
1 parent
c63fdb2e1a
commit
8fce191c55
2 files changed
+48
-2
No files matched your search
@@ -619,16 +619,34 @@ def init_pluginsystem(
|
||||
)
|
||||
|
||||
plugin_validators = []
|
||||
|
||||
def implementation_or_hook_validator(phase, plugin_info):
|
||||
if phase in ("before_import", "before_load", "before_enable"):
|
||||
result = plugin_info.parsed_metadata.get("has_control_properties", False)
|
||||
if not result:
|
||||
logger.info(
|
||||
"Plugin {} declares no control properties, "
|
||||
"might not even be a plugin, disabling".format(plugin_info.name)
|
||||
)
|
||||
return result
|
||||
return True
|
||||
|
||||
plugin_validators.append(implementation_or_hook_validator)
|
||||
|
||||
if safe_mode:
|
||||
|
||||
def validator(phase, plugin_info):
|
||||
def safe_mode_validator(phase, plugin_info):
|
||||
if phase in ("before_import", "before_load", "before_enable"):
|
||||
plugin_info.safe_mode_victim = not plugin_info.bundled
|
||||
if not plugin_info.bundled:
|
||||
logger.info(
|
||||
"Plugin {} is not bundled, safe mode is active, "
|
||||
"disabling".format(plugin_info.name)
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
plugin_validators.append(validator)
|
||||
plugin_validators.append(safe_mode_validator)
|
||||
|
||||
compatibility_ignored_list = settings.get(["plugins", "_forcedCompatible"])
|
||||
|
||||
|
||||
@@ -734,6 +734,14 @@ class PluginInfo(object):
|
||||
"""The plugin metadata parsed from the plugin ``__init__.py``'s AST."""
|
||||
return self._cached_parsed_metadata
|
||||
|
||||
@property
|
||||
def control_properties(self):
|
||||
return [
|
||||
getattr(self.__class__, key)
|
||||
for key in dir(self.__class__)
|
||||
if key.startswith("attr_")
|
||||
]
|
||||
|
||||
def _parse_metadata(self):
|
||||
result = {}
|
||||
|
||||
@@ -764,6 +772,10 @@ class PluginInfo(object):
|
||||
assignments = list(
|
||||
filter(lambda x: isinstance(x, ast.Assign) and x.targets, root.body)
|
||||
)
|
||||
function_defs = list(
|
||||
filter(lambda x: isinstance(x, ast.FunctionDef) and x.name, root.body)
|
||||
)
|
||||
all_relevant = assignments + function_defs
|
||||
|
||||
def extract_target_ids(node):
|
||||
return list(
|
||||
@@ -773,6 +785,16 @@ class PluginInfo(object):
|
||||
)
|
||||
)
|
||||
|
||||
def extract_names(node):
|
||||
if isinstance(node, ast.Assign):
|
||||
return extract_target_ids(node)
|
||||
elif isinstance(node, ast.FunctionDef):
|
||||
return [
|
||||
node.name,
|
||||
]
|
||||
else:
|
||||
return []
|
||||
|
||||
for key in (
|
||||
self.__class__.attr_name,
|
||||
self.__class__.attr_version,
|
||||
@@ -811,6 +833,12 @@ class PluginInfo(object):
|
||||
|
||||
break
|
||||
|
||||
for a in reversed(all_relevant):
|
||||
targets = extract_names(a)
|
||||
if any(map(lambda x: x in targets, self.control_properties)):
|
||||
result["has_control_properties"] = True
|
||||
break
|
||||
|
||||
except SyntaxError:
|
||||
self._logger.exception(
|
||||
"Invalid syntax in {} for plugin {}".format(path, self.key)
|
||||
|
||||
Reference in new issue
Block a user