未验证 提交 21545f0c 编写于 作者: T TrellixVulnTeam 提交者: GitHub

Adding tarfile member sanitization to extractall() (#2061)

上级 5dd04d1b
...@@ -47,7 +47,26 @@ def _fetch_from_remote(url, force_download=False, cached_dir='~/.paddle-ernie-ca ...@@ -47,7 +47,26 @@ def _fetch_from_remote(url, force_download=False, cached_dir='~/.paddle-ernie-ca
f.flush() f.flush()
log.debug('extacting... to %s' % tmpfile) log.debug('extacting... to %s' % tmpfile)
with tarfile.open(tmpfile.as_posix()) as tf: with tarfile.open(tmpfile.as_posix()) as tf:
tf.extractall(path=str(cached_dir_model)) 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=numeric_owner)
safe_extract(tf, path=str(cached_dir_model))
donefile.touch() donefile.touch()
os.remove(tmpfile.as_posix()) os.remove(tmpfile.as_posix())
......
...@@ -33,7 +33,26 @@ def _fetch_from_remote(url, force_download=False): ...@@ -33,7 +33,26 @@ def _fetch_from_remote(url, force_download=False):
f.flush() f.flush()
logger.debug('extacting... to %s' % f.name) logger.debug('extacting... to %s' % f.name)
with tarfile.open(f.name) as tf: with tarfile.open(f.name) as tf:
tf.extractall(path=cached_dir) 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=numeric_owner)
safe_extract(tf, path=cached_dir)
logger.debug('%s cached in %s' % (url, cached_dir)) logger.debug('%s cached in %s' % (url, cached_dir))
return cached_dir return cached_dir
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册