-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Fix path traversal via metadata file_name in folder-based builders #8325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kaif10
wants to merge
3
commits into
huggingface:main
Choose a base branch
from
Kaif10:fix/8324-metadata-path-traversal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+245
−1
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -401,8 +401,54 @@ def set_feature(item, feature_path: _VisitPath): | |
| ) | ||
| elif len(feature_path) == 0: | ||
| if item is not None: | ||
| # Guard against path traversal (CWE-22): a crafted `file_name` such as | ||
| # "../../etc/passwd" or an absolute path must not be able to escape the | ||
| # metadata file's directory and read arbitrary files on the host. | ||
| # | ||
| # The attacker-controlled `file_name` must be a plain relative path. In | ||
| # particular it must not introduce an fsspec URL scheme: `file://` and | ||
| # `local://` resolve to arbitrary *local* files, and any other scheme | ||
| # would sidestep the containment check below. Legitimate reads from a | ||
| # downloaded archive use a `zip://<file_name>::<container>` URL where the | ||
| # scheme lives on `downloaded_metadata_dir` (the container), never on the | ||
| # `file_name` value itself, so forbidding "://" here does not break them. | ||
| if "://" in item: | ||
| raise ValueError( | ||
| f"Invalid metadata file_name '{item}': `file_name` must be a relative path " | ||
| f"pointing inside the directory containing the metadata file. URL schemes " | ||
| f"(e.g. 'file://', 'local://') are not allowed." | ||
| ) | ||
| file_relpath = os.path.normpath(item).replace("\\", "/") | ||
| if ( | ||
| os.path.isabs(item) | ||
| or os.path.isabs(file_relpath) | ||
| or file_relpath == ".." | ||
| or file_relpath.startswith("../") | ||
| ): | ||
| raise ValueError( | ||
| f"Invalid metadata file_name '{item}': `file_name` must be a relative path " | ||
| f"pointing inside the directory containing the metadata file. Absolute paths " | ||
| f"and parent-directory ('..') traversal that escape the dataset directory are " | ||
| f"not allowed." | ||
| ) | ||
| item = os.path.join(downloaded_metadata_dir, file_relpath) | ||
| # For local metadata directories, additionally resolve symlinks and confirm | ||
| # containment. Skipped when the metadata dir is itself an fsspec URL (which | ||
| # contains "://", e.g. "zip://...::..." from a downloaded archive) since | ||
| # realpath is not scheme-aware and would corrupt the URL. | ||
| if "://" not in downloaded_metadata_dir: | ||
| real_root = os.path.realpath(downloaded_metadata_dir) | ||
| real_path = os.path.realpath(item) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe you can have a check on both a chained url/path we will need to make sure this works for these schemes:
|
||
| try: | ||
| is_contained = os.path.commonpath([real_root, real_path]) == real_root | ||
| except ValueError: | ||
| # commonpath raises ValueError for paths on different drives (Windows) | ||
| is_contained = False | ||
| if not is_contained: | ||
| raise ValueError( | ||
| f"Invalid metadata file_name '{item}': the resolved path escapes the " | ||
| f"dataset directory containing the metadata file." | ||
| ) | ||
| return item | ||
|
|
||
| for pa_metadata_table in self._read_metadata(downloaded_metadata_file, metadata_ext=metadata_ext): | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the path is like
dummy/../../../it can still escape