From 2bbcf4e405ffd8a6a7e714a1a9d468aaf89539ec Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Fri, 28 Oct 2022 22:29:08 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- plugin.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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)