diff --git a/reproducible_builds/repacker.py b/reproducible_builds/repacker.py index efb2ff0..ae21a3f 100644 --- a/reproducible_builds/repacker.py +++ b/reproducible_builds/repacker.py @@ -106,7 +106,29 @@ def as_source(self): if self.path.name.endswith(".tar.gz"): with tarfile.open(self.path, "r:*") as archive: logger.info(f"Extracting `{self.path.name}` to `{tmpdir}`") - archive.extractall(tmpdir) + + 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(archive, tmpdir) for x in tmp_path.glob("*/PKG-INFO"): yield x.parent