From 6cff12446c74e9f37942fac0e315e2001b6cc035 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 26 Oct 2022 21:55:04 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- nyx/package.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/nyx/package.py b/nyx/package.py index 2fcf042..3ef03d7 100644 --- a/nyx/package.py +++ b/nyx/package.py @@ -144,7 +144,26 @@ def install(self, config) -> bool: self.util_createpath(sysroot_dir) with tarfile.open(self.pkg_path(config), mode="r") as zip_ref: - zip_ref.extractall(sysroot_dir) + 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(zip_ref, sysroot_dir) return True def print_info(self):