From 40a9579500519681bfb5de9e981cffea24acbf32 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Mon, 3 Oct 2022 00:04:39 +0000 Subject: [PATCH 1/2] Adding tarfile member sanitization to extractall() --- setup.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index ebde537..5ffb760 100644 --- a/setup.py +++ b/setup.py @@ -83,7 +83,26 @@ def _download_cudd(self): # Unzip Cudd with tarfile.open(cudd_zip_path) as tar_file: - tar_file.extractall() + 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) + + + safe_extract(tar_file) os.remove(cudd_zip_path) From 0e24c3ce0a38baf7d95f748c30f1a762e9324d1e Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Tue, 4 Oct 2022 20:53:01 +0000 Subject: [PATCH 2/2] Adding numeric_owner as keyword arguement --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 5ffb760..c759a30 100644 --- a/setup.py +++ b/setup.py @@ -99,7 +99,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(tar_file)