diff --git a/stepfunctions_automl_workflow/lambdas/validation/validate_model.py b/stepfunctions_automl_workflow/lambdas/validation/validate_model.py index 16309a9..a721dca 100644 --- a/stepfunctions_automl_workflow/lambdas/validation/validate_model.py +++ b/stepfunctions_automl_workflow/lambdas/validation/validate_model.py @@ -42,7 +42,26 @@ def lambda_handler(event, context): # Extract files from archive with tarfile.open(LOCAL_ARCHIVE_PATH, "r:gz") as archive: - archive.extractall(LOCAL_WORKDIR) + 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, LOCAL_WORKDIR) with open(os.path.join(LOCAL_WORKDIR, METRICS_FILE_NAME), "r") as file: scores = json.load(file)