Skip to content

Commit 80b9067

Browse files
committed
Merge branch 'master' of github.com:PaddlePaddle/InterpretDL
2 parents 6c785ed + c53f18a commit 80b9067

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

interpretdl/common/file_utils.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,26 @@ def decompress(fname):
133133

134134
if fname.find('tar') >= 0 or fname.find('tgz') >= 0:
135135
with tarfile.open(fname) as tf:
136-
tf.extractall(path=fpath_tmp)
136+
def is_within_directory(directory, target):
137+
138+
abs_directory = os.path.abspath(directory)
139+
abs_target = os.path.abspath(target)
140+
141+
prefix = os.path.commonprefix([abs_directory, abs_target])
142+
143+
return prefix == abs_directory
144+
145+
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
146+
147+
for member in tar.getmembers():
148+
member_path = os.path.join(path, member.name)
149+
if not is_within_directory(path, member_path):
150+
raise Exception("Attempted Path Traversal in Tar File")
151+
152+
tar.extractall(path, members, numeric_owner=numeric_owner)
153+
154+
155+
safe_extract(tf, path=fpath_tmp)
137156
elif fname.find('zip') >= 0:
138157
with zipfile.ZipFile(fname) as zf:
139158
zf.extractall(path=fpath_tmp)

0 commit comments

Comments
 (0)