From 322417ec0025ec0a4c46a465b65a15fb00145b11 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sun, 16 Oct 2022 19:45:08 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- procgen-build/procgen_build/build_qt.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/procgen-build/procgen_build/build_qt.py b/procgen-build/procgen_build/build_qt.py index f1a0dc22..51d4b9cb 100644 --- a/procgen-build/procgen_build/build_qt.py +++ b/procgen-build/procgen_build/build_qt.py @@ -28,7 +28,26 @@ def cache_folder(name, dirpath, options, build_fn): start = time.time() with open(cache_path, "rb") as f: with tarfile.open(fileobj=f, mode="r") as tf: - tf.extractall() + 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) print(f"download elapsed {time.time() - start}") else: print(f"building cache for {name}")