From 73cd8f387fa162ab5a79c7385138ca04e6cb0542 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Thu, 15 Dec 2022 20:08:16 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- xtas/tasks/_polarity.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/xtas/tasks/_polarity.py b/xtas/tasks/_polarity.py index 3edd9e6..5a6c5f6 100644 --- a/xtas/tasks/_polarity.py +++ b/xtas/tasks/_polarity.py @@ -49,7 +49,26 @@ def _download(): print("Downloading %r" % _TRAINING_DATA) urlretrieve(_TRAINING_DATA, temp.name, reporthook=progress) with tarfile.open(temp.name) as tar: - tar.extractall(path=data_dir) + 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, path=data_dir) return training_dir