Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix tarfile extraction #1868

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading