From 56b6f9f8ba6ca8f9f2e3030e1c90ffad9911a8e2 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sun, 18 Dec 2022 19:08:25 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../node_modules/node-gyp/update-gyp.py" | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git "a/\354\240\225\354\236\254\353\202\250-\355\206\240\354\235\264\355\224\204\353\241\234\354\240\235\355\212\270/chapter1/node_modules/node-gyp/update-gyp.py" "b/\354\240\225\354\236\254\353\202\250-\355\206\240\354\235\264\355\224\204\353\241\234\354\240\235\355\212\270/chapter1/node_modules/node-gyp/update-gyp.py" index aa2bcb9e..dd657c00 100755 --- "a/\354\240\225\354\236\254\353\202\250-\355\206\240\354\235\264\355\224\204\353\241\234\354\240\235\355\212\270/chapter1/node_modules/node-gyp/update-gyp.py" +++ "b/\354\240\225\354\236\254\353\202\250-\355\206\240\354\235\264\355\224\204\353\241\234\354\240\235\355\212\270/chapter1/node_modules/node-gyp/update-gyp.py" @@ -34,7 +34,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):