diff --git a/test/congruence.py b/test/congruence.py index f349cd5..6f69827 100755 --- a/test/congruence.py +++ b/test/congruence.py @@ -27,7 +27,26 @@ def check_value(dfrow, colname, shouldbe): import controller as C with tarfile.open(os.path.join(repo_path, "test/congruence.tar.xz")) as fid: - fid.extractall(os.path.join(repo_path, "test/scratch")) + 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(fid, os.path.join(repo_path,"test/scratch")) dummydatadir = os.path.join(repo_path, "test/scratch/congruence/groundtruth-data/dummy-data") for ethogramfile in filter(lambda x: x.endswith("ethogram.log"), os.listdir(dummydatadir)):