From 109b7d581171e79f2fb59a3e36d54e5850d04feb Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Fri, 21 Oct 2022 09:58:20 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- taskcluster/taskgraph/util/docker.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/taskcluster/taskgraph/util/docker.py b/taskcluster/taskgraph/util/docker.py index df97e57bcf..9e37e94e22 100644 --- a/taskcluster/taskgraph/util/docker.py +++ b/taskcluster/taskgraph/util/docker.py @@ -136,7 +136,26 @@ def build_from_context(docker_bin, context_path, prefix, tag=None): d = tempfile.mkdtemp() try: with tarfile.open(context_path, 'r:gz') as tf: - tf.extractall(d) + 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, d) # If we wanted to do post-processing of the Dockerfile, this is # where we'd do it.