Skip to content

Commit

Permalink
fix tarfile extraction (#1868)
Browse files Browse the repository at this point in the history
* fix tarfile extraction

* fix onnxruntime version
  • Loading branch information
ofrimasad authored Feb 26, 2024
1 parent e166460 commit 9385f93
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ sphinx-rtd-theme
torchmetrics==0.8
hydra-core>=1.2.0
omegaconf
onnxruntime==1.16.0
onnxruntime==1.15.0
onnx==1.15.0
pillow>=5.3.0,!=8.3
pip-tools>=6.12.1
Expand Down
17 changes: 15 additions & 2 deletions src/super_gradients/training/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,20 @@ def download_one(url, dir):
download_one(u, dir)


def safe_untar(tar_file, extract_path):
"""
Protect against Tar Slip vulnerability.
Calling extractall to extract all files from a tar file without sanitization
may result files outside destination directory to be overwritten, resulting in an arbitrary file write.
CVE-2007-4559 https://nvd.nist.gov/vuln/detail/CVE-2007-4559
"""
with tarfile.TarFile(tar_file, "r") as tf:
for member in tf:
file_path = os.path.realpath(os.path.join(extract_path, member.name))
if file_path.startswith(os.path.realpath(extract_path)):
tf.extract(member, extract_path)


def download_and_untar_from_url(urls: List[str], dir: Union[str, Path] = "."):
"""
Download a file from url and untar.
Expand All @@ -533,8 +547,7 @@ def download_and_untar_from_url(urls: List[str], dir: Union[str, Path] = "."):
assert filepath.suffix in modes.keys(), f"{filepath} has {filepath.suffix} suffix which is not supported"

logger.info(f"Extracting to {dir}...")
with tarfile.open(filepath, mode=modes[filepath.suffix]) as f:
f.extractall(dir)
safe_untar(filepath, dir)
filepath.unlink()


Expand Down

0 comments on commit 9385f93

Please sign in to comment.