From 3a458d6cce1d02f7b921c495a1df753dc0ff5733 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sun, 27 Nov 2022 17:10:17 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- rinse/core.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/rinse/core.py b/rinse/core.py index efd2216..5852d6d 100644 --- a/rinse/core.py +++ b/rinse/core.py @@ -235,7 +235,29 @@ def source_setup(self, src_file_path): # Extract the contents of the source tarball with tarfile.open(str(src_file_path)) as r_tar_file: self.logger.debug("Extracting source tarball.") - r_tar_file.extractall(path=str(self.tmp_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(r_tar_file, path=str(self.tmp_path)) # Configure rinse-bin for the configuration process rinse_bin = self.tmp_path / listdir(self.tmp_path)[0] / "rinse-bin"