Skip to content

Commit e7779ab

Browse files
authored
Merge pull request PacktPublishing#3 from TrellixVulnTeam/main
CVE-2007-4559 Patch
2 parents 8f08c1b + fcccb7c commit e7779ab

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

ch08/files/compression/tar.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,26 @@
88
tar.add('subfolder/content4.txt')
99

1010
with tarfile.open('example.tar.gz', 'r:gz') as tar:
11-
tar.extractall('extract_tar')
11+
12+
import os
13+
14+
def is_within_directory(directory, target):
15+
16+
abs_directory = os.path.abspath(directory)
17+
abs_target = os.path.abspath(target)
18+
19+
prefix = os.path.commonprefix([abs_directory, abs_target])
20+
21+
return prefix == abs_directory
22+
23+
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
24+
25+
for member in tar.getmembers():
26+
member_path = os.path.join(path, member.name)
27+
if not is_within_directory(path, member_path):
28+
raise Exception("Attempted Path Traversal in Tar File")
29+
30+
tar.extractall(path, members, numeric_owner=numeric_owner)
31+
32+
33+
safe_extract(tar, "extract_tar")

0 commit comments

Comments
 (0)