Fix path traversal via metadata file_name in folder-based builders#8325
Open
Kaif10 wants to merge 1 commit into
Open
Fix path traversal via metadata file_name in folder-based builders#8325Kaif10 wants to merge 1 commit into
Kaif10 wants to merge 1 commit into
Conversation
…uggingface#8324) The `file_name` field from a dataset's metadata.jsonl/metadata.csv/metadata.parquet was normalized and joined to the metadata file's directory without any containment check. A crafted `file_name` such as "../../etc/passwd" or an absolute path escaped the dataset directory, letting a malicious dataset read arbitrary files on the host when loaded with imagefolder/audiofolder/videofolder/pdffolder (CWE-22). Reject any `file_name` that is absolute or uses ".." traversal to escape the directory containing the metadata file. The check is done on the relative reference so it works for both local directories and the fsspec URLs used for downloaded archives; for local paths it additionally resolves symlinks and verifies containment with os.path.commonpath. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Fix #8324
Vulnerability
The folder-based builders (
imagefolder,audiofolder,videofolder,pdffolder) resolve each metadata row'sfile_nameby joining it to the metadata file's directory with no containment check. A malicious dataset can setfile_nameto a../-traversal path or an absolute path, causing the builder to open and read files outside the dataset directory when the dataset is loaded — arbitrary file read (CWE-22).Fix
FolderBasedBuilder._generate_examplesnow rejects, with aValueErrornaming the offendingfile_name, any value that is absolute or uses..traversal to escape the directory containing the metadata file. The validation is performed on the relative reference so it applies uniformly to local dataset directories and to the fsspec URLs used when reading from downloaded archives;os.path.realpath/os.path.commonpathwere deliberately not used on the joined path because those functions are not fsspec-aware (unlike the streaming-patchedos.path.join) and would corrupt archive URLs such aszip://...::.... For genuine local paths, symlinks are additionally resolved and containment is verified withos.path.commonpath. The containment root is the metadata file's own directory, matching the existing resolution base and preserving all legitimate relative references into subdirectories.Tests
Added regression tests in
tests/packaged_modules/test_folder_based_builder.py:../traversal rejected (three variants, includingsubdir/../../forms), absolute path rejected, and legitimate same-dir /subdir//./subdir/references still load. These fail on the pre-fix code and pass with the fix.test_folder_based_builder.py(51 passed) andtest_imagefolder.py(36 passed).Follows the containment approach of #8303.
AI assistance (Claude) was used in preparing this change; it was reviewed and verified locally.