Guard dataset tar extraction against path traversal (TarSlip) - #9590
Open
ousamabenyounes wants to merge 1 commit into
Open
Guard dataset tar extraction against path traversal (TarSlip)#9590ousamabenyounes wants to merge 1 commit into
ousamabenyounes wants to merge 1 commit into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9590
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Fixes pytorch#9517 torchvision.datasets.utils._extract_tar called tar.extractall() with no member validation, so a crafted archive (member name '../evil') could write files outside the extraction root (TarSlip / CVE-class path traversal). download_and_extract_archive feeds a caller-provided URL straight into this sink. Fix: extract tar archives through CPython's PEP 706 'data' filter, which rejects path traversal, absolute paths, and links escaping the destination. A manual containment guard covers Python patches predating the backported filter (<3.10.12/<3.11.4). Zip extraction is unaffected: zipfile.extractall already strips '..' and absolute components. Regression test: a tar member named '../escaped.txt' must not write outside to_path and must raise.
ousamabenyounes
force-pushed
the
fix/issue-9517
branch
from
August 16, 2026 16:53
1aea057 to
7452e06
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #9517
Problem
torchvision/datasets/utils.py::_extract_tarcalledtar.extractall(to_path)with no member validation (TarSlip / CVE-class path traversal). A crafted archive member named../evil— or a symlink/hardlink pointing outside — writes outside the extraction root.download_and_extract_archivefeeds a caller-provided URL straight into this sink, so a malicious dataset mirror can drop files anywhere the process can write.Fix (root cause, tar-scoped)
datafilter (extractall(to_path, filter="data")), which rejects traversal, absolute paths, and escaping links. It is the default on 3.14 and available since 3.12 (backported to 3.10.12+/3.11.4+)._reject_escaping_tar_membersguard validates every member (and link target) against the realpath'd root before extraction writes anything._extract_zipis left untouched:zipfile.extractallalready strips..and absolute components (verified).Test verification (RED → GREEN)
New parametrized test
test_extract_tar_rejects_path_traversal(Python 3.12.13, CPU):(Independently reconfirmed the mechanism on this machine:
extractall(filter="data")raisesOutsideDestinationErroron a../escaped.txtmember while a pre-3.12-defaultextractallwrites it outside the root.)