From ee1c772b48b10655357787d3a5e41c5a4de5a028 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 19 Oct 2022 00:45:13 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- featuretools/entityset/deserialize.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/featuretools/entityset/deserialize.py b/featuretools/entityset/deserialize.py index 901c739c..0517fb00 100644 --- a/featuretools/entityset/deserialize.py +++ b/featuretools/entityset/deserialize.py @@ -238,7 +238,26 @@ def read_entityset(path, profile_name=None, **kwargs): use_smartopen_es(local_path, path, transport_params) with tarfile.open(str(local_path)) as tar: - tar.extractall(path=tmpdir) + 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=tmpdir) data_description = read_data_description(tmpdir) return description_to_entityset(data_description, **kwargs)