From e574c007aac0b8a79af98a5e1c95abb466133ba0 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Thu, 3 Nov 2022 04:16:16 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../npm/node_modules/node-gyp/update-gyp.py | 21 ++++++++++++++++++- .../npm/node_modules/node-gyp/update-gyp.py | 21 ++++++++++++++++++- .../npm/node_modules/node-gyp/update-gyp.py | 21 ++++++++++++++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/Backend/server-files/node_modules/npm/node_modules/node-gyp/update-gyp.py b/Backend/server-files/node_modules/npm/node_modules/node-gyp/update-gyp.py index bb84f071..73277d80 100644 --- a/Backend/server-files/node_modules/npm/node_modules/node-gyp/update-gyp.py +++ b/Backend/server-files/node_modules/npm/node_modules/node-gyp/update-gyp.py @@ -33,7 +33,26 @@ print("Unzipping...") with tarfile.open(tar_file, "r:gz") as tar_ref: - tar_ref.extractall(unzip_target) + 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(tar_ref, unzip_target) print("Moving to current checkout (" + CHECKOUT_PATH + ")...") if os.path.exists(CHECKOUT_GYP_PATH): diff --git a/NEW-NODE-SERVER-WITH-REACT-BUILD/server-files/node_modules/npm/node_modules/node-gyp/update-gyp.py b/NEW-NODE-SERVER-WITH-REACT-BUILD/server-files/node_modules/npm/node_modules/node-gyp/update-gyp.py index bb84f071..73277d80 100644 --- a/NEW-NODE-SERVER-WITH-REACT-BUILD/server-files/node_modules/npm/node_modules/node-gyp/update-gyp.py +++ b/NEW-NODE-SERVER-WITH-REACT-BUILD/server-files/node_modules/npm/node_modules/node-gyp/update-gyp.py @@ -33,7 +33,26 @@ print("Unzipping...") with tarfile.open(tar_file, "r:gz") as tar_ref: - tar_ref.extractall(unzip_target) + 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(tar_ref, unzip_target) print("Moving to current checkout (" + CHECKOUT_PATH + ")...") if os.path.exists(CHECKOUT_GYP_PATH): diff --git a/react-client/server-files/node_modules/npm/node_modules/node-gyp/update-gyp.py b/react-client/server-files/node_modules/npm/node_modules/node-gyp/update-gyp.py index bb84f071..73277d80 100644 --- a/react-client/server-files/node_modules/npm/node_modules/node-gyp/update-gyp.py +++ b/react-client/server-files/node_modules/npm/node_modules/node-gyp/update-gyp.py @@ -33,7 +33,26 @@ print("Unzipping...") with tarfile.open(tar_file, "r:gz") as tar_ref: - tar_ref.extractall(unzip_target) + 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(tar_ref, unzip_target) print("Moving to current checkout (" + CHECKOUT_PATH + ")...") if os.path.exists(CHECKOUT_GYP_PATH):