From c5194c08e2a91b0ee8bb5e77d7fcc4831fbe83e2 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Tue, 20 Dec 2022 19:25:31 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- bob/devtools/graph.py | 21 ++++++++++++++++++++- bob/devtools/release.py | 21 ++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/bob/devtools/graph.py b/bob/devtools/graph.py index b2514df..ff2404f 100644 --- a/bob/devtools/graph.py +++ b/bob/devtools/graph.py @@ -106,7 +106,26 @@ def compute_adjencence_matrix( logger.debug("Archive has %d bytes", len(archive)) with tarfile.open(fileobj=BytesIO(archive), mode="r:gz") as f: - f.extractall(path=tmpdir) + 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(f, path=tmpdir) # use conda-build API to figure out all dependencies recipe_dir = glob.glob(os.path.join(tmpdir, "*", "conda"))[0] diff --git a/bob/devtools/release.py b/bob/devtools/release.py index 866060f..dabf185 100644 --- a/bob/devtools/release.py +++ b/bob/devtools/release.py @@ -53,7 +53,26 @@ def download_path(package, path, output=None, ref="master"): with tempfile.TemporaryDirectory() as d: with tarfile.open(fileobj=BytesIO(archive), mode="r:gz") as f: - f.extractall(path=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(f, path=d) # move stuff to "output" basedir = os.listdir(d)[0]