diff --git a/DeepLabV3Plus-Pytorch/datasets/voc.py b/DeepLabV3Plus-Pytorch/datasets/voc.py index c2a2668..3a315bb 100644 --- a/DeepLabV3Plus-Pytorch/datasets/voc.py +++ b/DeepLabV3Plus-Pytorch/datasets/voc.py @@ -164,4 +164,23 @@ def decode_target(cls, mask): def download_extract(url, root, filename, md5): download_url(url, root, filename, md5) with tarfile.open(os.path.join(root, filename), "r") as tar: - tar.extractall(path=root) \ No newline at end of file + 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=root) \ No newline at end of file