Skip to content

Commit

Permalink
Adding tarfile member sanitization to extractall() (#2741)
Browse files Browse the repository at this point in the history
Co-authored-by: TrellixVulnTeam <kasimir.schulz@trellix.com>
  • Loading branch information
Trott and PatchTester committed Oct 2, 2022
1 parent a26494f commit 33deab4
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion update-gyp.py
Expand Up @@ -33,7 +33,25 @@

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)

safe_extract(tar_ref, unzip_target)

print("Moving to current checkout (" + CHECKOUT_PATH + ")...")
if os.path.exists(CHECKOUT_GYP_PATH):
Expand Down

0 comments on commit 33deab4

Please sign in to comment.