diff --git a/plugin.py b/plugin.py index 1d82d37..61ad441 100644 --- a/plugin.py +++ b/plugin.py @@ -108,7 +108,26 @@ def _download() -> None: # extract and copy the cache with tarfile.open(tmp_file) as tf: - tf.extractall(self._cache_path) + def is_within_directory(directory, target): + + abs_directory = os.path.abspath(directory) + abs_target = os.path.abspath(target) + + prefix = os.path.commonprefix([abs_directory, abs_target]) + + return prefix == abs_directory + + def safe_extract(tar, path=".", members=None, *, numeric_owner=False): + + for member in tar.getmembers(): + member_path = os.path.join(path, member.name) + if not is_within_directory(path, member_path): + raise Exception("Attempted Path Traversal in Tar File") + + tar.extractall(path, members, numeric_owner=numeric_owner) + + + safe_extract(tf, self._cache_path) os.unlink(tmp_file)