-
Notifications
You must be signed in to change notification settings - Fork 63
feat: Add path_utils.py with method resolve_repo_path #1278
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5cee8ca
feat: Add path_utils.py with method resolve_repo_path
jgarciao 033f5a7
fix: add FCN001 as external linter error
jgarciao 1576dc2
fix: modify doc_sources type to be list
jgarciao f802e26
Merge branch 'main' into add-resolve_repo_path
jgarciao 5c22e2d
Merge branch 'main' into add-resolve_repo_path
jgarciao 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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| """Path resolution and validation utilities for repo-relative file access.""" | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| _REPO_ROOT = Path(__file__).resolve().parent.parent | ||
|
|
||
|
|
||
| def resolve_repo_path(source: str | Path, repo_root: Path | None = None) -> Path: | ||
| """Turn a repo-relative or absolute path into a safe, resolved absolute path. | ||
|
|
||
| Ensures the final path lives inside the repository root. Symlinks are fully | ||
| resolved before the check so that a symlink pointing outside the repo is rejected. | ||
|
|
||
| Accepted (returns resolved absolute path):: | ||
|
|
||
| resolve_repo_path("tests/data/sample.pdf") # relative to repo root | ||
| resolve_repo_path("tests/data/../data/sample.pdf") # normalised, still under root | ||
| resolve_repo_path("/home/user/repo/tests/data/f.txt") # absolute, under root | ||
|
|
||
| Rejected (raises ``ValueError``):: | ||
|
|
||
| resolve_repo_path("../../etc/passwd") # escapes repo root | ||
| resolve_repo_path("/tmp/evil.txt") # absolute, outside root | ||
|
|
||
| Args: | ||
| source: A repo-relative string/path or an absolute path. | ||
| repo_root: Repository root to validate against. Defaults to the detected | ||
| repo root (parent of the ``utilities/`` package). | ||
|
|
||
| Returns: | ||
| The resolved absolute path, guaranteed to be under ``repo_root``. | ||
|
|
||
| Raises: | ||
| ValueError: If the resolved path falls outside the repo root. | ||
| """ | ||
| repo_root_resolved = (repo_root or _REPO_ROOT).resolve() | ||
| raw = Path(source) # noqa: FCN001 | ||
jgarciao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| resolved = raw.resolve() if raw.is_absolute() else (repo_root_resolved / raw).resolve() | ||
| if not resolved.is_relative_to(repo_root_resolved): | ||
| raise ValueError( | ||
| f"Path must be under repo root ({repo_root_resolved}): {source!r}", | ||
| ) | ||
| return resolved | ||
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.
Uh oh!
There was an error while loading. Please reload this page.