Skip to content

Fix path traversal via metadata file_name in folder-based builders#8325

Open
Kaif10 wants to merge 1 commit into
huggingface:mainfrom
Kaif10:fix/8324-metadata-path-traversal
Open

Fix path traversal via metadata file_name in folder-based builders#8325
Kaif10 wants to merge 1 commit into
huggingface:mainfrom
Kaif10:fix/8324-metadata-path-traversal

Conversation

@Kaif10

@Kaif10 Kaif10 commented Jul 12, 2026

Copy link
Copy Markdown

Fix #8324

Vulnerability

The folder-based builders (imagefolder, audiofolder, videofolder, pdffolder) resolve each metadata row's file_name by joining it to the metadata file's directory with no containment check. A malicious dataset can set file_name to 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_examples now rejects, with a ValueError naming the offending file_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.commonpath were deliberately not used on the joined path because those functions are not fsspec-aware (unlike the streaming-patched os.path.join) and would corrupt archive URLs such as zip://...::.... For genuine local paths, symlinks are additionally resolved and containment is verified with os.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, including subdir/../../ 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) and test_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.

…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Path traversal / arbitrary file read via the file_name metadata field in *folder dataset builders (no trust_remote_code)

1 participant