From 8e662fea9e99ac7fbd7aa8bee674cce1f5ed3793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 10 Jun 2020 14:38:27 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20PMGR:=20single=20file=20validation?= =?UTF-8?q?=20&=20install=20from=20upload?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/pluginmanager/__init__.py | 102 +++++++++++------- .../pluginmanager/static/js/pluginmanager.js | 30 +++--- .../templates/pluginmanager_settings.jinja2 | 4 +- 3 files changed, 83 insertions(+), 53 deletions(-) diff --git a/src/octoprint/plugins/pluginmanager/__init__.py b/src/octoprint/plugins/pluginmanager/__init__.py index 8061085f4..6ec25b75d 100644 --- a/src/octoprint/plugins/pluginmanager/__init__.py +++ b/src/octoprint/plugins/pluginmanager/__init__.py @@ -244,7 +244,7 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, return dict( all=plugins, thirdparty=list(filter(lambda p: not p["bundled"], plugins)), - file_extensions=self.ARCHIVE_EXTENSIONS + file_extensions=self.ARCHIVE_EXTENSIONS + self.PYTHON_EXTENSIONS ) def get_template_types(self, template_sorting, template_rules, *args, **kwargs): @@ -269,9 +269,9 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, upload_path = flask.request.values[input_upload_path] upload_name = flask.request.values[input_upload_name] - exts = list(filter(lambda x: upload_name.lower().endswith(x), self.ARCHIVE_EXTENSIONS)) + exts = list(filter(lambda x: upload_name.lower().endswith(x), self.ARCHIVE_EXTENSIONS + self.PYTHON_EXTENSIONS)) if not len(exts): - return flask.make_response("File doesn't have a valid extension for a plugin archive", 400) + return flask.make_response("File doesn't have a valid extension for a plugin archive or a single file plugin", 400) ext = exts[0] @@ -279,7 +279,9 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, try: archive.close() shutil.copy(upload_path, archive.name) - return self.command_install(path=archive.name, force="force" in flask.request.values and flask.request.values["force"] in valid_boolean_trues) + return self.command_install(path=archive.name, + name=upload_name, + force="force" in flask.request.values and flask.request.values["force"] in valid_boolean_trues) finally: try: os.remove(archive.name) @@ -421,26 +423,26 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, return self.command_toggle(plugin, command) @staticmethod - def download_file(url, folder): + def _download_file(url, folder): with requests.get(url, stream=True) as r: r.raise_for_status() if "Content-Disposition" in r.headers.keys(): - local_filename = re.findall("filename=(.+)", r.headers["Content-Disposition"])[0] + filename = re.findall("filename=(.+)", r.headers["Content-Disposition"])[0] else: - local_filename = url.split("/")[-1] + filename = url.split("/")[-1] - assert len(local_filename) > 0 + assert len(filename) > 0 # TODO check content-length against safety limit - path = os.path.join(folder, local_filename) - with open(path, 'wb') as f: + path = os.path.join(folder, filename) + with io.open(path, 'wb') as f: for chunk in r.iter_content(chunk_size=8192): f.write(chunk) return path - @staticmethod - def is_archive(path): + # noinspection PyMethodMayBeStatic + def _is_archive(self, path): _, ext = os.path.splitext(path) if ext in PluginManagerPlugin.ARCHIVE_EXTENSIONS: return True @@ -450,14 +452,20 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, return ".{}".format(kind.extension) in PluginManagerPlugin.ARCHIVE_EXTENSIONS return False - @staticmethod - def is_pythonfile(path): + def _is_pythonfile(self, path): _, ext = os.path.splitext(path) if ext in PluginManagerPlugin.PYTHON_EXTENSIONS: - return True + import ast + try: + with io.open(path, 'rb') as f: + ast.parse(f.read(), filename=path) + return True + except Exception as exc: + self._logger.exception("Could not parse {} as python file: {}".format(path, exc)) + return False - def command_install(self, url=None, path=None, force=False, reinstall=None, dependency_links=False): + def command_install(self, url=None, path=None, name=None, force=False, reinstall=None, dependency_links=False): folder = None try: @@ -467,22 +475,41 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, if url is not None: # fetch URL folder = tempfile.TemporaryDirectory() - path = self.download_file(url, folder.name) + path = self._download_file(url, folder.name) source = url source_type = "url" # determine type of path - if self.is_archive(path): - return self.command_install_archive(path, source=source, source_type=source_type, force=force, reinstall=reinstall, dependency_links=dependency_links) - elif self.is_pythonfile(path): - return self.command_install_pythonfile(path, source=source, source_type=source_type) + if self._is_archive(path): + return self._command_install_archive(path, + source=source, + source_type=source_type, + force=force, + reinstall=reinstall, + dependency_links=dependency_links) + + elif self._is_pythonfile(path): + return self._command_install_pythonfile(path, + source=source, + source_type=source_type, + name=name) + else: - return make_response("Need either an archive or a python file", 400) + self._logger.error("{} is neither an archive nor a python file, can't install that.".format(source)) + result = dict(result=False, + source=source, + source_type=source_type, + reason="Could not install plugin from {}, was neither " + "a plugin archive nor a single file plugin".format(source)) + self._send_result_notification("install", result) + return jsonify(result) + finally: if folder is not None: folder.cleanup() - def command_install_archive(self, path, source=None, source_type=None, force=False, reinstall=None, dependency_links=False): + # noinspection DuplicatedCode + def _command_install_archive(self, path, source=None, source_type=None, force=False, reinstall=None, dependency_links=False): throttled = self._get_throttled() if throttled and isinstance(throttled, dict) and throttled.get("current_issue", False) and not self._settings.get_boolean(["ignore_throttled"]): # currently throttled, we refuse to run @@ -591,9 +618,10 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, all_plugins_after = self._plugin_manager.find_plugins(existing=dict(), ignore_uninstalled=False) new_plugin = self._find_installed_plugin(installed, plugins=all_plugins_after) + if new_plugin is None: self._logger.warning("The plugin was installed successfully, but couldn't be found afterwards to " - "initialize properly during runtime. Please restart OctoPrint.") + "initialize properly during runtime. Please restart OctoPrint.") result = dict(result=True, source=source, source_type=source_type, @@ -639,24 +667,27 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, self._send_result_notification("install", result) return jsonify(result) - def command_install_pythonfile(self, path, source=None, source_type=None): - self._logger.info("Installing single file plugin from {}".format(source)) - self._log_message("Installing single file plugin from {}".format(source)) + # noinspection DuplicatedCode + def _command_install_pythonfile(self, path, source=None, source_type=None, name=None): + if name is None: + name = os.path.basename(path) + + self._logger.info("Installing single file plugin {} from {}".format(name, source)) all_plugins_before = self._plugin_manager.find_plugins(existing=dict()) - destination = self._settings.global_get_basefolder("plugins") - filename = os.path.basename(path) - plugin_id, _ = os.path.splitext(filename) + destination = os.path.join(self._settings.global_get_basefolder("plugins"), name) + plugin_id, _ = os.path.splitext(name) try: - shutil.copy2(path, destination) + self._log_call("cp {} {}".format(path, destination)) + shutil.copy(path, destination) except: - self._logger.exception("Could not install plugin from %s" % source) + self._logger.exception("Could not install plugin from {}".format(source)) return make_response("Could not install plugin from URL, see the log for more details", 500) - self._plugin_manager.find_plugins(existing=dict(), ignore_uninstalled=False) - new_plugin = self._plugin_manager.plugins.get(plugin_id) + plugins = self._plugin_manager.find_plugins(existing=dict(), ignore_uninstalled=False) + new_plugin = plugins.get(plugin_id) if new_plugin is None: self._logger.warning("The plugin was installed successfully, but couldn't be found afterwards to " "initialize properly during runtime. Please restart OctoPrint.") @@ -678,9 +709,8 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, and isinstance(new_plugin.implementation, octoprint.plugin.ReloadNeedingPlugin) needs_reconnect = self._plugin_manager.has_any_of_hooks(new_plugin, self._reconnect_hooks) and self._printer.is_operational() - self._plugin_manager.log_all_plugins() - self._logger.info("The plugin was installed successfully: {}, version {}".format(new_plugin.name, new_plugin.version)) + self._plugin_manager.log_all_plugins() # noinspection PyUnresolvedReferences self._event_bus.fire(Events.PLUGIN_PLUGINMANAGER_INSTALL_PLUGIN, dict(id=new_plugin.key, diff --git a/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js b/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js index 4488def41..ce422f0aa 100644 --- a/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js +++ b/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js @@ -193,9 +193,9 @@ $(function() { self.noticeCountText = ko.pureComputed(function() { var count = self.noticeCount(); - if (count == 0) { + if (count === 0) { return gettext("There are no plugin notices. Great!"); - } else if (count == 1) { + } else if (count === 1) { return gettext("There is a plugin notice for one of your installed plugins."); } else { return _.sprintf(gettext("There are %(count)d plugin notices for one or more of your installed plugins."), {count: count}); @@ -269,7 +269,7 @@ $(function() { }); self.invalidFile = ko.pureComputed(function() { - var allowedFileExtensions = [".zip", ".tar.gz", ".tgz", ".tar"]; + var allowedFileExtensions = [".zip", ".tar.gz", ".tgz", ".tar", ".py"]; var name = self.uploadFilename(); var lowerName = name !== undefined ? name.toLocaleLowerCase() : undefined; @@ -298,7 +298,7 @@ $(function() { maxNumberOfFiles: 1, autoUpload: false, add: function(e, data) { - if (data.files.length == 0) { + if (data.files.length === 0) { return false; } @@ -472,7 +472,7 @@ $(function() { return; } - if (data.key == "pluginmanager") return; + if (data.key === "pluginmanager") return; var onSuccess = function() { self.requestData(); @@ -486,7 +486,7 @@ $(function() { }) }; - if (self._getToggleCommand(data) == "enable") { + if (self._getToggleCommand(data) === "enable") { if (data.safe_mode_victim) return; OctoPrint.plugins.pluginmanager.enable(data.key) .done(onSuccess) @@ -768,7 +768,7 @@ $(function() { self.savePluginSettings = function() { var repository = self.config_repositoryUrl(); - if (repository != undefined && repository.trim() == "") { + if (repository !== undefined && repository.trim() === "") { repository = null; } @@ -780,7 +780,7 @@ $(function() { } var notices = self.config_noticesUrl(); - if (notices != undefined && notices.trim() == "") { + if (notices !== undefined && notices.trim() === "") { notices = null; } @@ -792,7 +792,7 @@ $(function() { } var pipArgs = self.config_pipAdditionalArgs(); - if (pipArgs != undefined && pipArgs.trim() == "") { + if (pipArgs !== undefined && pipArgs.trim() === "") { pipArgs = null; } @@ -1013,7 +1013,7 @@ $(function() { }; self.toggleButtonCss = function(data) { - var icon = self._getToggleCommand(data) == "enable" ? "fa fa-toggle-off" : "fa fa-toggle-on"; + var icon = self._getToggleCommand(data) === "enable" ? "fa fa-toggle-off" : "fa fa-toggle-on"; var disabled = (self.enableToggle(data)) ? "" : " disabled"; return icon + disabled; @@ -1021,7 +1021,7 @@ $(function() { self.toggleButtonTitle = function(data) { var command = self._getToggleCommand(data); - if (command == "enable") { + if (command === "enable") { if (data.blacklisted) { return gettext("Blacklisted"); } else if (data.safe_mode_victim) { @@ -1035,7 +1035,7 @@ $(function() { }; self.showPluginNotifications = function(plugin) { - if (!plugin.notifications || plugin.notifications.length == 0) return; + if (!plugin.notifications || plugin.notifications.length === 0) return; self._removeAllNoticeNotificationsForPlugin(plugin.key); _.each(plugin.notifications, function(notification) { @@ -1044,7 +1044,7 @@ $(function() { }; self.showPluginNotificationsLinkText = function(plugins) { - if (!plugins.notifications || plugins.notifications.length == 0) return; + if (!plugins.notifications || plugins.notifications.length === 0) return; var count = plugins.notifications.length; var importantCount = _.filter(plugins.notifications, function(notification) { return notification.important }).length; @@ -1080,7 +1080,7 @@ $(function() { var text = ""; if (notification.versions && notification.versions.length > 0) { - var versions = _.map(notification.versions, function(v) { return (v == version) ? "" + v + "" : v; }).join(", "); + var versions = _.map(notification.versions, function(v) { return (v === version) ? "" + v + "" : v; }).join(", "); text += "" + _.sprintf(gettext("Affected versions: %(versions)s"), {versions: _.escape(versions)}) + ""; } else { text += "" + gettext("Affected versions: all") + ""; @@ -1190,7 +1190,7 @@ $(function() { if (!Modernizr.localstorage) return false; - if (localStorage[noticeLocalStorageKey] == undefined) + if (localStorage[noticeLocalStorageKey] === undefined) return false; var knownData = JSON.parse(localStorage[noticeLocalStorageKey]); diff --git a/src/octoprint/plugins/pluginmanager/templates/pluginmanager_settings.jinja2 b/src/octoprint/plugins/pluginmanager/templates/pluginmanager_settings.jinja2 index 757d60770..232618952 100644 --- a/src/octoprint/plugins/pluginmanager/templates/pluginmanager_settings.jinja2 +++ b/src/octoprint/plugins/pluginmanager/templates/pluginmanager_settings.jinja2 @@ -287,7 +287,7 @@ {{ _('This does not look like a valid URL. Expected http, https or any of the supported VCS URLs.', url='https://pip.pypa.io/en/stable/reference/pip_install/#vcs-support') }} -

{{ _('... from an uploaded archive') }}

+

{{ _('... from an uploaded file') }}

@@ -300,7 +300,7 @@
- {{ _('This does not look like a valid plugin archive. Valid plugin archives should be either zip files or tarballs and have the extension ".zip", ".tar.gz", ".tgz" or ".tar"') }} + {{ _('This does not look like a valid plugin. Valid plugins should be either zip files or tarballs installable via pip that have the extension ".zip", ".tar.gz", ".tgz" or ".tar", or single file python plugins with the extension ".py"') }}