From 1900af99c98a2c422a3fa1a8782aae2dad6f092f Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Fri, 9 Dec 2022 16:01:16 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- src/slurm_exporter_ops.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/slurm_exporter_ops.py b/src/slurm_exporter_ops.py index 07323a7..8c63a49 100644 --- a/src/slurm_exporter_ops.py +++ b/src/slurm_exporter_ops.py @@ -37,7 +37,29 @@ def install(self, resource_path: Path): with TemporaryDirectory(prefix="omni") as tmp_dir: logger.debug(f"## extracting {resource_path} to {tmp_dir}") with tarfile.open(resource_path, 'r') as tar: - tar.extractall(path=tmp_dir) + + 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(tar, path=tmp_dir) logger.debug(f"## installing binary in {self._binary_path}")