diff --git a/algorithms/GA/hnsw/benchmarks/benchmark_script.py b/algorithms/GA/hnsw/benchmarks/benchmark_script.py index 3241e13..ef03677 100644 --- a/algorithms/GA/hnsw/benchmarks/benchmark_script.py +++ b/algorithms/GA/hnsw/benchmarks/benchmark_script.py @@ -303,7 +303,26 @@ def download_file(url, dst): if args.dataset == 'sift' and not os.path.exists(SIFT_DIR): download_file("ftp://ftp.irisa.fr/local/texmex/corpus/sift.tar.gz", "datasets") with tarfile.open("datasets/sift.tar.gz") as t: - t.extractall(path="datasets") + 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="datasets") subprocess.call("python datasets/convert_texmex_fvec.py datasets/sift/sift_base.fvecs >> datasets/sift.txt", shell=True) if args.dataset == 'youtube' and not os.path.exists(YOUTUBE_DIR): diff --git a/algorithms/TOGG-KDT/hnsw/benchmarks/benchmark_script.py b/algorithms/TOGG-KDT/hnsw/benchmarks/benchmark_script.py index 3241e13..ef03677 100644 --- a/algorithms/TOGG-KDT/hnsw/benchmarks/benchmark_script.py +++ b/algorithms/TOGG-KDT/hnsw/benchmarks/benchmark_script.py @@ -303,7 +303,26 @@ def download_file(url, dst): if args.dataset == 'sift' and not os.path.exists(SIFT_DIR): download_file("ftp://ftp.irisa.fr/local/texmex/corpus/sift.tar.gz", "datasets") with tarfile.open("datasets/sift.tar.gz") as t: - t.extractall(path="datasets") + 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="datasets") subprocess.call("python datasets/convert_texmex_fvec.py datasets/sift/sift_base.fvecs >> datasets/sift.txt", shell=True) if args.dataset == 'youtube' and not os.path.exists(YOUTUBE_DIR): diff --git a/algorithms/TOGG-KMC/hnsw/benchmarks/benchmark_script.py b/algorithms/TOGG-KMC/hnsw/benchmarks/benchmark_script.py index 3241e13..ef03677 100644 --- a/algorithms/TOGG-KMC/hnsw/benchmarks/benchmark_script.py +++ b/algorithms/TOGG-KMC/hnsw/benchmarks/benchmark_script.py @@ -303,7 +303,26 @@ def download_file(url, dst): if args.dataset == 'sift' and not os.path.exists(SIFT_DIR): download_file("ftp://ftp.irisa.fr/local/texmex/corpus/sift.tar.gz", "datasets") with tarfile.open("datasets/sift.tar.gz") as t: - t.extractall(path="datasets") + 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="datasets") subprocess.call("python datasets/convert_texmex_fvec.py datasets/sift/sift_base.fvecs >> datasets/sift.txt", shell=True) if args.dataset == 'youtube' and not os.path.exists(YOUTUBE_DIR):