From 1de51081080b4ba3d24c77255d27f3f4ecab9c52 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 12 Oct 2022 09:20:15 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- pipelines/abalone/evaluate.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/pipelines/abalone/evaluate.py b/pipelines/abalone/evaluate.py index 6ddf06d..bb19e8e 100644 --- a/pipelines/abalone/evaluate.py +++ b/pipelines/abalone/evaluate.py @@ -21,7 +21,29 @@ logger.debug("Starting evaluation.") model_path = "/opt/ml/processing/model/model.tar.gz" with tarfile.open(model_path) as tar: - tar.extractall(path=".") + + import os + + 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=".") logger.debug("Loading xgboost model.") model = pickle.load(open("xgboost-model", "rb"))