diff --git a/ilmbase/2.2.0/rezbuild.py b/ilmbase/2.2.0/rezbuild.py index 6938e06..4393c14 100644 --- a/ilmbase/2.2.0/rezbuild.py +++ b/ilmbase/2.2.0/rezbuild.py @@ -22,7 +22,26 @@ def build(source_path, build_path, install_path, targets): # Unzip the source print("Unzipping to: %s" % build_path) with tarfile.TarFile.open(archive, mode='r:gz') as tar: - tar.extractall(build_path) + 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, build_path) folder_name = filename.rsplit(".tar.gz")[0] source_root = os.path.join(build_path, folder_name) diff --git a/openexr/2.2.0/rezbuild.py b/openexr/2.2.0/rezbuild.py index 9c45157..c58f444 100644 --- a/openexr/2.2.0/rezbuild.py +++ b/openexr/2.2.0/rezbuild.py @@ -23,7 +23,26 @@ def build(source_path, build_path, install_path, targets): # Unzip the source print("Unzipping to: %s" % build_path) with tarfile.TarFile.open(archive, mode='r:gz') as tar: - tar.extractall(build_path) + 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, build_path) folder_name = filename.rsplit(".tar.gz")[0] source_root = os.path.join(build_path, folder_name) diff --git a/zlib/1.2.11/rezbuild.py b/zlib/1.2.11/rezbuild.py index ec912e9..7f45d61 100644 --- a/zlib/1.2.11/rezbuild.py +++ b/zlib/1.2.11/rezbuild.py @@ -22,7 +22,26 @@ def build(source_path, build_path, install_path, targets): # Unzip the source print("Unzipping to: %s" % build_path) with tarfile.TarFile.open(archive, mode='r:gz') as tar: - tar.extractall(build_path) + 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, build_path) folder_name = filename.rsplit(".tar.gz")[0] source_root = os.path.join(build_path, folder_name)