Fix path traversal in dataset archive extraction - #9662
Open
Anuragcap wants to merge 1 commit into
Open
Conversation
Fixes pytorch#9517. _extract_tar() and _extract_zip() called extractall() without validating that archive member paths stay within the destination directory, allowing a malicious archive (e.g. containing a member path like '../evil.txt') to write files outside the intended extraction location. For tar archives, this uses Python's built-in filter="data" safe extraction mode (the standard fix for this class of bug, see CVE-2007-4559), with a manual path-containment fallback for Python builds that lack the filter keyword. For zip archives, which has no equivalent built-in filter, this adds manual validation of every member's resolved path before extraction. Both extractors share a new _validate_extract_target() helper.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9662
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. |
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.
_extract_tar() and _extract_zip() in torchvision/datasets/utils.py called extractall() without validating that archive member paths stay within the destination directory. A malicious archive (e.g. containing a member with a path like ../evil.txt) could write files outside the intended extraction directory (CWE-22, path traversal / "tar slip").
This applies the same fix pattern used for a very similar recent Keras CVE (2025-12060):
For tar archives, uses Python's built-in filter="data" safe-extraction mode (the standard mitigation for this bug class, originally CVE-2007-4559), with a manual path-containment fallback for Python builds without the filter keyword.
For zip archives, which has no equivalent built-in filter, adds manual validation of every member's resolved path before extraction.
Both paths share a new _validate_extract_target() helper to avoid duplicating the containment check.
Test
Added test_extract_tar_path_traversal and test_extract_zip_path_traversal in test/test_datasets_utils.py, each building a malicious archive with a ../-escaping member and asserting extraction is refused and no file lands outside the destination.