diff --git a/prism_metric.py b/prism_metric.py index ad0efae..b80a8a9 100644 --- a/prism_metric.py +++ b/prism_metric.py @@ -26,7 +26,29 @@ def __init__(self, tgt_lang: str, src_lang: str, reference_free=False, urlretrieve(url, tarfile_path) print(f'Extracting Prism model to {model_path}') with tarfile.open(tarfile_path, 'r') as tar: - tar.extractall(path=model_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(tar, path=model_path) tarfile_path.unlink() print(f'{self}: Initializing {model_dir}/m39v1')