Skip to content

Commit a22b8b5

Browse files
Adding tarfile member sanitization to extractall() (#223)
1 parent 731d84c commit a22b8b5

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

genomepy/files.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,26 @@ def extract_tarball(fname, outfile=None, concat=True) -> Union[str, None]:
188188
# Extract files to temporary directory
189189
tmp_dir = mkdtemp(dir=os.path.dirname(outfile))
190190
with tarfile.open(fname) as tar:
191-
tar.extractall(path=tmp_dir)
191+
def is_within_directory(directory, target):
192+
193+
abs_directory = os.path.abspath(directory)
194+
abs_target = os.path.abspath(target)
195+
196+
prefix = os.path.commonprefix([abs_directory, abs_target])
197+
198+
return prefix == abs_directory
199+
200+
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
201+
202+
for member in tar.getmembers():
203+
member_path = os.path.join(path, member.name)
204+
if not is_within_directory(path, member_path):
205+
raise Exception("Attempted Path Traversal in Tar File")
206+
207+
tar.extractall(path, members, numeric_owner=numeric_owner)
208+
209+
210+
safe_extract(tar, path=tmp_dir)
192211
for root, _, files in os.walk(tmp_dir):
193212
fnames += [os.path.join(root, fname) for fname in files]
194213

0 commit comments

Comments
 (0)