From ff3ba9cccedf3f39f212e0314846be647a7ef079 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Thu, 13 Oct 2022 11:16:05 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- applications/graph/GNN/data/PROTEINS/utils.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/applications/graph/GNN/data/PROTEINS/utils.py b/applications/graph/GNN/data/PROTEINS/utils.py index 9575d6875f5..284bdc92aef 100644 --- a/applications/graph/GNN/data/PROTEINS/utils.py +++ b/applications/graph/GNN/data/PROTEINS/utils.py @@ -17,7 +17,26 @@ def untar_file(data_dir, file_name): """ tar_name = os.path.join(data_dir, file_name) with tarfile.open(tar_name) 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) tar.close() def unzip_file(file_name, data_dir=None): """Helper function to unzip file