From 966c3e182f43b0660747fd360e3e9fecc22cad96 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sat, 1 Oct 2022 00:11:37 +0000 Subject: [PATCH 1/2] Adding tarfile member sanitization to extractall() --- install-gcam.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/install-gcam.py b/install-gcam.py index 36ebc039..31c474fa 100755 --- a/install-gcam.py +++ b/install-gcam.py @@ -80,7 +80,26 @@ def untar(filename, directory='.', printOnly=False): return with tarfile.open(filename, mode='r:gz') as tar: - tar.extractall(path=directory) + 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, path=directory) # # Comment taken from exe/run-gcam.command: From 2ad45515db08aa646fcbc922c2c373e5d57c6fa4 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Tue, 4 Oct 2022 01:24:40 +0000 Subject: [PATCH 2/2] Adding numeric_owner as keyword arguement --- install-gcam.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install-gcam.py b/install-gcam.py index 31c474fa..32de9379 100755 --- a/install-gcam.py +++ b/install-gcam.py @@ -96,7 +96,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, path=directory)