diff --git a/Spam-Email-Classifier/Lambda/sagemaker/local/image.py b/Spam-Email-Classifier/Lambda/sagemaker/local/image.py index 8fe3550..a38e829 100644 --- a/Spam-Email-Classifier/Lambda/sagemaker/local/image.py +++ b/Spam-Email-Classifier/Lambda/sagemaker/local/image.py @@ -413,7 +413,26 @@ def _prepare_serving_volumes(self, model_location): for filename in model_data_source.get_file_list(): if tarfile.is_tarfile(filename): with tarfile.open(filename) as tar: - tar.extractall(path=model_data_source.get_root_dir()) + 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=model_data_source.get_root_dir()) volumes.append(_Volume(model_data_source.get_root_dir(), "/opt/ml/model")) diff --git a/Spam-Email-Classifier/Lambda/sagemaker/utils.py b/Spam-Email-Classifier/Lambda/sagemaker/utils.py index 5b25b61..22a0846 100644 --- a/Spam-Email-Classifier/Lambda/sagemaker/utils.py +++ b/Spam-Email-Classifier/Lambda/sagemaker/utils.py @@ -543,7 +543,26 @@ def _create_or_update_code_dir( download_file_from_url(source_directory, local_code_path, sagemaker_session) with tarfile.open(name=local_code_path, mode="r:gz") as t: - t.extractall(path=code_dir) + 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(t, path=code_dir) elif source_directory: if os.path.exists(code_dir): @@ -579,7 +598,26 @@ def _extract_model(model_uri, sagemaker_session, tmp): else: local_model_path = model_uri.replace("file://", "") with tarfile.open(name=local_model_path, mode="r:gz") as t: - t.extractall(path=tmp_model_dir) + 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(t, path=tmp_model_dir) return tmp_model_dir