diff --git a/src/ial_build/pygmkpack.py b/src/ial_build/pygmkpack.py index 620ad61..ac8e57d 100644 --- a/src/ial_build/pygmkpack.py +++ b/src/ial_build/pygmkpack.py @@ -490,7 +490,26 @@ def _ics_insert(self, program, pattern, lines, offset=1): def populate_from_tar(self, tar): """Populate the incremental pack with the contents of a **tar** file.""" with tarfile.open(tar, 'r') as t: - t.extractall(path=self._local) + 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(t, path=self._local) def populate_from_files_in_dir(self, list_of_files, directory): """