fix(path_utils): anchor is_safe_path containment on a separator boundary#35
Open
Osamaali313 wants to merge 1 commit into
Open
fix(path_utils): anchor is_safe_path containment on a separator boundary#35Osamaali313 wants to merge 1 commit into
Osamaali313 wants to merge 1 commit into
Conversation
is_safe_path tested repository containment with a bare
`resolved.startswith(self.repo_root)` (and the same for the not-yet-existing
branch). Since repo_root has no trailing separator, any path whose absolute
form merely begins with the repo-root string passed the check -- a sibling
directory sharing the name prefix ("myrepo_secrets" vs "myrepo") or a "../"
escape onto one. This is the security gate delegated to by AgentTools._is_
safe_path and used first in every agent read tool (list_directory,
search_codebase, read_file_content, ...), so it let the agent read/list
outside the repo. Require an exact match or a repo_root + os.sep boundary --
the same containment discipline the sibling file_path_to_module_path already
uses via os.path.commonpath.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
PathUtils.is_safe_path(fastcode/path_utils.py) — the repository-containment security check delegated to byAgentTools._is_safe_pathand used as the first gate in every agent read tool (list_directory,search_codebase,get_file_info,get_file_structure_summary,read_file_content) — tests containment with a barestartswith:self.repo_roothas no trailing separator, so any path whose absolute form merely begins with the repo-root string passes — including a sibling directory that shares the name prefix and../escapes onto it.Reproduction
Repo root
.../myrepo, sibling.../myrepo_secretsoutside it (verified against the realPathUtils):is_safe_path("../myrepo_secrets/passwords.txt")True❌Falseis_safe_path("../myrepo_secrets")True❌Falseis_safe_path("../myrepo2")True❌Falseis_safe_path(".")/is_safe_path("sub/f.py")TrueTrueA wrongly-permissive result lets the exploration agent read/list files outside the intended repository boundary.
Fix
Require an exact match or a path-separator boundary:
This is the same containment discipline the sibling function
file_path_to_module_pathin this file already applies (it was deliberately rewritten to useos.path.commonpath, per its comment "prevents path traversal attacks and handles edge cases like root directories") —is_safe_pathjust wasn't given the same treatment.Verified RED→GREEN against the real
PathUtils(module is stdlib-only);python -m py_compilepasses. (No test suite exists in the repo; the reproduction above is the RED→GREEN check.)