From 0f41a4219b4069aba5ecf26f0812ee629e0fe144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 13 Dec 2021 17:37:23 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=92=EF=B8=8F=20Use=20custom=20safe(r)?= =?UTF-8?q?=20pickler=20for=20webassets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Just in case someone might somehow find a way to tamper with the cached compiled assets on disk (e.g. through a 3rd party plugin), this should now be better protected against abuse. See also #4339 --- src/octoprint/server/util/flask.py | 13 ++++++-- src/octoprint/util/safe_pickle.py | 48 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 src/octoprint/util/safe_pickle.py diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index e35062f9b..04170e445 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -175,11 +175,12 @@ def enable_additional_translations(default_locale="en", additional_folders=None) def fix_webassets_cache(): from webassets import cache + import octoprint.util.safe_pickle + error_logger = logging.getLogger(__name__ + ".fix_webassets_cache") def fixed_set(self, key, data): import os - import pickle import shutil import tempfile @@ -194,7 +195,7 @@ def fix_webassets_cache(): fd, temp_filename = tempfile.mkstemp(prefix="." + md5, dir=self.directory) try: with os.fdopen(fd, "wb") as f: - pickle.dump(data, f) + octoprint.util.safe_pickle.dump(data, f) f.flush() shutil.move(temp_filename, filename) except Exception: @@ -238,7 +239,13 @@ def fix_webassets_cache(): finally: f.close() - unpickled = webassets.cache.safe_unpickle(result) + try: + unpickled = octoprint.util.safe_pickle.safe_loads(result) + except Exception: + error_logger.exception( + "Got an exception while trying to load webasset file {}".format(filename) + ) + return None if unpickled is None: warnings.warn("Ignoring corrupted cache file %s" % filename) return unpickled diff --git a/src/octoprint/util/safe_pickle.py b/src/octoprint/util/safe_pickle.py new file mode 100644 index 000000000..de4b676ca --- /dev/null +++ b/src/octoprint/util/safe_pickle.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function, unicode_literals + +__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" +__copyright__ = "Copyright (C) 2021 The OctoPrint Project - Released under terms of the AGPLv3 License" + + +import io + + +def _validate(module, name, allowed=None): + if allowed is None: + return None + return allowed.get((module, name), None) + + +try: + import cPickle as pickle # Python 2 + + def safe_load(f, allowed=None): + unpickler = pickle.Unpickler(f) + unpickler.find_global = lambda m, n: _validate(m, n, allowed=allowed) + return unpickler.load() + + def safe_loads(s, allowed=None): + return safe_load(io.BytesIO(s), allowed=allowed) + + +except ImportError: + import pickle # Python 3 + + class RestrictedUnpickler(pickle.Unpickler): + def __init__(self, f, *args, **kwargs): + self.allowed = kwargs.get("allowed") + super(RestrictedUnpickler, self).__init__(f, *args, **kwargs) + + def find_class(self, module, name): + return _validate(module, name, allowed=self.allowed) + + def safe_load(f, allowed=None): + return RestrictedUnpickler(f, allowed=allowed).load() + + def safe_loads(s, allowed=None): + return safe_load(io.BytesIO(s), allowed=allowed) + + +dump = pickle.dump +dumps = pickle.dumps