From 05496af7c0bfe3dbec13f02e257df2595c946da8 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Mon, 17 Oct 2022 20:48:30 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- algorithmia_ratchet.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/algorithmia_ratchet.py b/algorithmia_ratchet.py index 845a186..fdf9ed6 100644 --- a/algorithmia_ratchet.py +++ b/algorithmia_ratchet.py @@ -105,7 +105,29 @@ def create_workflows(workflows, source_client, environments, destination_client, local_code_zip = source_client.file(remote_code_path).getFile().name tar = tarfile.open(local_code_zip) with tar.open(local_code_zip) as f: - f.extractall(path=artifact_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(f, path=artifact_path) else: print("checking for local code...") find_algo(template_algorithm_name, artifact_path)