🔒️ Use custom safe(r) pickler for webassets
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
This commit is contained in:
1 parent
dc4b57213c
commit
0f41a4219b
2 files changed
+58
-3
No files matched your search
@@ -175,11 +175,12 @@ def enable_additional_translations(default_locale="en", additional_folders=None)
|
|||||||
def fix_webassets_cache():
|
def fix_webassets_cache():
|
||||||
from webassets import cache
|
from webassets import cache
|
||||||
|
|
||||||
|
import octoprint.util.safe_pickle
|
||||||
|
|
||||||
error_logger = logging.getLogger(__name__ + ".fix_webassets_cache")
|
error_logger = logging.getLogger(__name__ + ".fix_webassets_cache")
|
||||||
|
|
||||||
def fixed_set(self, key, data):
|
def fixed_set(self, key, data):
|
||||||
import os
|
import os
|
||||||
import pickle
|
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
@@ -194,7 +195,7 @@ def fix_webassets_cache():
|
|||||||
fd, temp_filename = tempfile.mkstemp(prefix="." + md5, dir=self.directory)
|
fd, temp_filename = tempfile.mkstemp(prefix="." + md5, dir=self.directory)
|
||||||
try:
|
try:
|
||||||
with os.fdopen(fd, "wb") as f:
|
with os.fdopen(fd, "wb") as f:
|
||||||
pickle.dump(data, f)
|
octoprint.util.safe_pickle.dump(data, f)
|
||||||
f.flush()
|
f.flush()
|
||||||
shutil.move(temp_filename, filename)
|
shutil.move(temp_filename, filename)
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -238,7 +239,13 @@ def fix_webassets_cache():
|
|||||||
finally:
|
finally:
|
||||||
f.close()
|
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:
|
if unpickled is None:
|
||||||
warnings.warn("Ignoring corrupted cache file %s" % filename)
|
warnings.warn("Ignoring corrupted cache file %s" % filename)
|
||||||
return unpickled
|
return unpickled
|
||||||
|
|||||||
@@ -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
|
||||||
Reference in new issue
Block a user