From 9029d828643387600848a4c36d33a9bfc978dfe0 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Fri, 9 Dec 2022 19:34:12 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- pyccc/files/directory.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pyccc/files/directory.py b/pyccc/files/directory.py index 982af97..18ca5a3 100644 --- a/pyccc/files/directory.py +++ b/pyccc/files/directory.py @@ -92,7 +92,26 @@ def put(self, destination): if not members: raise ValueError("No files under path directory '%s' in this tarfile") - tf.extractall(target, members) + 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(tf, target, members) class DockerArchive(DirectoryArchive, LazyDockerCopy):