-
Notifications
You must be signed in to change notification settings - Fork 52
fix: harden dependency path validation #364
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
63f1945
fix: harden dependency path validation
danielmeppiel 934da74
Merge branch 'main' into fix/validate-dependency-paths
danielmeppiel b387fcc
fix: address review — local path guard, cross-platform backslash norm…
danielmeppiel b675356
fix: validate repo_url and virtual_path segments in get_install_path
danielmeppiel 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
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,58 @@ | ||
| """Centralised path-security helpers for APM CLI. | ||
|
|
||
| Every filesystem operation whose target is derived from user-controlled | ||
| input (dependency strings, ``virtual_path``, ``apm.yml`` fields) **must** | ||
| pass through one of these guards before touching the disk. | ||
|
|
||
| Design | ||
| ------ | ||
| * ``ensure_path_within`` is the single predicate — resolves both paths and | ||
| asserts containment via ``Path.is_relative_to``. | ||
| * ``safe_rmtree`` wraps ``shutil.rmtree`` with an ``ensure_path_within`` | ||
| check so callers get a drop-in replacement. | ||
| * ``PathTraversalError`` is a ``ValueError`` subclass for clear error | ||
| semantics and easy ``except`` targeting. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import shutil | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| class PathTraversalError(ValueError): | ||
| """Raised when a computed path escapes its expected base directory.""" | ||
|
|
||
|
|
||
| def ensure_path_within(path: Path, base_dir: Path) -> Path: | ||
| """Resolve *path* and assert it lives inside *base_dir*. | ||
|
|
||
| Returns the resolved path on success. Raises | ||
| :class:`PathTraversalError` if the resolved path escapes *base_dir*. | ||
|
|
||
| This is intentionally strict: symlinks are resolved so that a link | ||
| pointing outside the base is caught as well. | ||
| """ | ||
| resolved = path.resolve() | ||
| resolved_base = base_dir.resolve() | ||
| try: | ||
danielmeppiel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if not resolved.is_relative_to(resolved_base): | ||
| raise PathTraversalError( | ||
| f"Path '{path}' resolves to '{resolved}' which is outside " | ||
| f"the allowed base directory '{resolved_base}'" | ||
| ) | ||
| except (TypeError, ValueError) as exc: | ||
| raise PathTraversalError( | ||
| f"Cannot verify containment of '{path}' within '{base_dir}': {exc}" | ||
| ) from exc | ||
| return resolved | ||
|
|
||
|
|
||
| def safe_rmtree(path: Path, base_dir: Path) -> None: | ||
| """Remove *path* only if it resolves within *base_dir*. | ||
|
|
||
| Drop-in replacement for ``shutil.rmtree(path)`` at sites where the | ||
| target is derived from user-controlled input. | ||
| """ | ||
| ensure_path_within(path, base_dir) | ||
| shutil.rmtree(path) | ||
danielmeppiel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
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.