diff --git a/scripts/get_rust_pkg.py b/scripts/get_rust_pkg.py index 6b402aa19..f10ca419a 100755 --- a/scripts/get_rust_pkg.py +++ b/scripts/get_rust_pkg.py @@ -146,7 +146,26 @@ def fetch_pkg(args, pkg, dl_path): tar_file, _ = urllib.request.urlretrieve(url) with tarfile.open(tar_file, mode="r") as tfile: echo(args, "extract tar file {} into {}".format(tar_file, tmp_dir)) - tfile.extractall(tmp_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(tfile, tmp_dir) files = os.listdir(tmp_dir) # There should be only one directory in the tar file, # but it might not be (name + "-" + version)