Skip to content

Fix path traversal in dataset archive extraction - #9662

Open
Anuragcap wants to merge 1 commit into
pytorch:mainfrom
Anuragcap:fix/dataset-archive-path-traversal
Open

Fix path traversal in dataset archive extraction#9662
Anuragcap wants to merge 1 commit into
pytorch:mainfrom
Anuragcap:fix/dataset-archive-path-traversal

Conversation

@Anuragcap

Copy link
Copy Markdown

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.

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.
@pytorch-bot

pytorch-bot Bot commented Sep 9, 2026

Copy link
Copy Markdown

🔗 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.

@meta-cla meta-cla Bot added the cla signed label Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Path traversal in TorchVision dataset archive extraction

1 participant