From 3791abbb87311a3ccd2301dff9240877514b6f2c Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sun, 2 Oct 2022 04:51:01 +0000 Subject: [PATCH 1/2] Adding tarfile member sanitization to extractall() --- .../pytorch_pretrained_bert/modeling.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/BertFlowDelta/pytorch_pretrained_bert/modeling.py b/BertFlowDelta/pytorch_pretrained_bert/modeling.py index 7241469..9e4c455 100644 --- a/BertFlowDelta/pytorch_pretrained_bert/modeling.py +++ b/BertFlowDelta/pytorch_pretrained_bert/modeling.py @@ -521,7 +521,26 @@ def from_pretrained(cls, pretrained_model_name, state_dict=None, cache_dir=None, logger.info("extracting archive file {} to temp dir {}".format( resolved_archive_file, tempdir)) with tarfile.open(resolved_archive_file, 'r:gz') as archive: - archive.extractall(tempdir) + 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(archive, tempdir) serialization_dir = tempdir # Load config config_file = os.path.join(serialization_dir, CONFIG_NAME) From 05c951bb2ebb846a95f48c82deae28ae291be8dd Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Tue, 4 Oct 2022 15:33:14 +0000 Subject: [PATCH 2/2] Adding numeric_owner as keyword arguement --- BertFlowDelta/pytorch_pretrained_bert/modeling.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BertFlowDelta/pytorch_pretrained_bert/modeling.py b/BertFlowDelta/pytorch_pretrained_bert/modeling.py index 9e4c455..1995e3e 100644 --- a/BertFlowDelta/pytorch_pretrained_bert/modeling.py +++ b/BertFlowDelta/pytorch_pretrained_bert/modeling.py @@ -537,7 +537,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(archive, tempdir)