From e9fef70bda93dce38624c5a06ce5b047510d0d13 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Fri, 9 Dec 2022 21:29:35 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- libnode/scripts/download.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/libnode/scripts/download.py b/libnode/scripts/download.py index 40b17bc..0529d1f 100755 --- a/libnode/scripts/download.py +++ b/libnode/scripts/download.py @@ -15,6 +15,25 @@ print("Extracting...") with tarfile.open('node_src.tar.gz') as srcTarball: - srcTarball.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(srcTarball) os.remove('node_src.tar.gz')