From 71c071d265d019587497e24e784f19455b8d569c Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sun, 9 Oct 2022 00:24:29 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- gpu_inference.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/gpu_inference.py b/gpu_inference.py index da14a34..efd9f0a 100644 --- a/gpu_inference.py +++ b/gpu_inference.py @@ -364,7 +364,26 @@ def gpu_cah_interface(i:int, incomingqueue: JoinableQueue, outgoingqueue: Joinab invalidURL (job, engine, jobset) if resp == 0: with tarfile.open(f"{job}.tar.gz", "r:gz") as tar: - tar.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=numeric_owner) + + + safe_extract(tar) break # test for csv and for images folder