From 9226c1e5c24f4d358abf10ffc06462b99ae294c2 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 2 Nov 2022 19:08:37 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- neuralcoref/__init__.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/neuralcoref/__init__.py b/neuralcoref/__init__.py index f4158d2..ad70e31 100644 --- a/neuralcoref/__init__.py +++ b/neuralcoref/__init__.py @@ -35,7 +35,26 @@ f"extracting archive file {downloaded_model} to dir {NEURALCOREF_MODEL_PATH}" ) with tarfile.open(downloaded_model, "r:gz") as archive: - archive.extractall(NEURALCOREF_CACHE) + 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(archive, NEURALCOREF_CACHE) def add_to_pipe(nlp, **kwargs):