Skip to content

Commit 33deab4

Browse files
TrottPatchTester
andauthored
Adding tarfile member sanitization to extractall() (#2741)
Co-authored-by: TrellixVulnTeam <[email protected]>
1 parent a26494f commit 33deab4

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

update-gyp.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,25 @@
3333

3434
print("Unzipping...")
3535
with tarfile.open(tar_file, "r:gz") as tar_ref:
36-
tar_ref.extractall(unzip_target)
36+
def is_within_directory(directory, target):
37+
38+
abs_directory = os.path.abspath(directory)
39+
abs_target = os.path.abspath(target)
40+
41+
prefix = os.path.commonprefix([abs_directory, abs_target])
42+
43+
return prefix == abs_directory
44+
45+
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
46+
47+
for member in tar.getmembers():
48+
member_path = os.path.join(path, member.name)
49+
if not is_within_directory(path, member_path):
50+
raise Exception("Attempted Path Traversal in Tar File")
51+
52+
tar.extractall(path, members, numeric_owner)
53+
54+
safe_extract(tar_ref, unzip_target)
3755

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

0 commit comments

Comments
 (0)