-
-
Notifications
You must be signed in to change notification settings - Fork 3
ref(security): Add SafeDirectory to enforce path traversal checks #623
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
runningcode
wants to merge
2
commits into
main
Choose a base branch
from
no/safe-directory
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.
Open
Changes from all commits
Commits
Show all changes
2 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
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,10 @@ | ||
| class UnreasonableZipError(ValueError): | ||
|
Contributor
Author
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. moved from zipprovider to avoid circular imports. |
||
| """Raised when a zip file exceeds reasonable limits.""" | ||
|
|
||
| pass | ||
|
|
||
|
|
||
| class UnsafePathError(ValueError): | ||
| """Raised when a zip file contains unsafe path entries that could lead to path traversal attacks.""" | ||
|
|
||
| pass | ||
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,90 @@ | ||
| import os | ||
|
|
||
| from pathlib import Path | ||
| from typing import Generator | ||
|
|
||
| from .exceptions import UnsafePathError | ||
|
|
||
|
|
||
| class SafeDirectory: | ||
| """A directory wrapper that validates untrusted paths stay within bounds. | ||
|
|
||
| Use .resolve(untrusted) for attacker-controlled input (manifest values, | ||
| plist entries, zip member names). Trusted Path operations (glob, rglob, | ||
| iterdir, /, etc.) are delegated directly. | ||
| """ | ||
|
|
||
| def __init__(self, base: Path) -> None: | ||
| self._base = base.resolve() | ||
|
|
||
| @property | ||
| def path(self) -> Path: | ||
| return self._base | ||
|
|
||
| def resolve(self, untrusted: str) -> Path: | ||
| """Resolve an untrusted path within this directory. | ||
|
|
||
| Raises UnsafePathError if the resolved path escapes the base. | ||
| """ | ||
| try: | ||
| target = (self._base / untrusted).resolve() | ||
| except RuntimeError: | ||
| raise UnsafePathError(f"Path traversal attempt: {untrusted}") | ||
| if not target.is_relative_to(self._base): | ||
| raise UnsafePathError(f"Path traversal attempt: {untrusted}") | ||
| return target | ||
|
|
||
| def child(self, untrusted: str) -> "SafeDirectory": | ||
| """Return a new SafeDirectory scoped to a validated subdirectory.""" | ||
| return SafeDirectory(self.resolve(untrusted)) | ||
|
|
||
| # -- Trusted Path delegations -- | ||
|
|
||
| def __truediv__(self, other: str | os.PathLike[str]) -> Path: | ||
| return self._base / other | ||
|
|
||
| def __str__(self) -> str: | ||
| return str(self._base) | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f"SafeDirectory({self._base})" | ||
|
|
||
| def __fspath__(self) -> str: | ||
| return str(self._base) | ||
|
|
||
| def glob(self, pattern: str) -> Generator[Path, None, None]: | ||
| return self._base.glob(pattern) | ||
|
|
||
| def rglob(self, pattern: str) -> Generator[Path, None, None]: | ||
| return self._base.rglob(pattern) | ||
|
|
||
| def iterdir(self) -> Generator[Path, None, None]: | ||
| return self._base.iterdir() | ||
|
|
||
| def exists(self) -> bool: | ||
| return self._base.exists() | ||
|
|
||
| def is_dir(self) -> bool: | ||
| return self._base.is_dir() | ||
|
|
||
| def is_file(self) -> bool: | ||
| return self._base.is_file() | ||
|
|
||
| def relative_to(self, other: str | os.PathLike[str]) -> Path: | ||
| return self._base.relative_to(other) | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| return self._base.name | ||
|
|
||
| @property | ||
| def stem(self) -> str: | ||
| return self._base.stem | ||
|
|
||
| @property | ||
| def suffix(self) -> str: | ||
| return self._base.suffix | ||
|
|
||
| @property | ||
| def parent(self) -> Path: | ||
| return self._base.parent |
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
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.
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.
Discarded resolve result silently performs validation
Medium Severity
app_bundle.resolve(icon_name)is called as a bare expression with its return value silently discarded. It serves as the sole path traversal validation for untrusted plist input, but it looks identical to dead code. Every other call site in this PR assigns the result (e.g.,icon_path = self._extract_dir.resolve(icon_path_str)inapk.pyandaab.py), making this the only place where the critical security check is invisible. A future developer or automated cleanup could remove this "unused" expression, silently disabling the path traversal protection for icon names.Reviewed by Cursor Bugbot for commit 8a90e9f. Configure here.