feat(security): path traversal protection — PathRefusalError, realpath confinement, MCP + guard integration (closes #58 phase-1)#131
Merged
Conversation
…h confinement, MCP + guard integration (closes #58 phase-1)
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
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.



Summary
Implements Phase 1 of issue #58 — path traversal protection for CodeLens.
CodeLens reads source files from a workspace directory. When the workspace path — or any file path derived from agent-supplied input — is naively joined with
os.path.joinand opened, a malicious or buggy caller can escape the workspace via:../../etc/passwd/etc/passwd/home/proj-evilvs/home/projThis PR implements symlink-aware path confinement to the project root using
os.path.realpathon both sides, defeating symlink-based escapes by comparing real on-disk locations.Files
New:
scripts/security/__init__.py— package init, re-exports public APIscripts/security/path_traversal.py—PathRefusalError,resolve_path_within_project(),is_path_within_project(),safe_resolve_path()tests/test_path_traversal.py— 41 tests covering symlink escapes, sibling-prefix collision, dotdot traversal, MCP-side validationModified:
scripts/utils.py— addedsafe_read_file_within_project()wrapper (degrades gracefully if security package unavailable)scripts/commands/guard.py—_analyze_filenow usessafe_read_file_within_projectscripts/mcp_server.py—_execute_commandcalls_validate_path_argsbefore dispatching; refusesfile/path/file_pathargs that escape workspace, returns structuredpath_refusalerror so agents can self-correctKey design decisions
os.path.realpathon both project root and candidate path — defeats symlink-based escapes by comparing real on-disk locations._normalize_project_rootappendsos.sepsostartswith()respects path-segment boundaries (defeats/home/proj-evilvs/home/projcollision).PathRefusalErrorinheritsPermissionError— existingexcept OSError/except PermissionErrorhandlers in callers continue to catch it without modification.scripts/security/is unavailable (older install),safe_read_file_within_projectfalls back to legacysafe_read_filerather than crashing.{"status":"error","error":"path_refusal","message":...,"suggestion":...}so AI agents get actionable feedback.Acceptance criteria
..traversal that escapes is refusedproj-evilvsproj) is refusedPathRefusalErrorexception class exists and isPermissionErrorsubclass(Phases 2–7 — config secret redaction, git safety guard, Secretlint, file system MCP tools, LLM schema validation, PoC generation — are out of scope for this Phase 1 PR.)
Test results
tests/test_path_traversal.py— all passtests/test_command_registry.py+tests/test_command_count.py— all pass (no command count change in this PR)Verified
git applycleanly againstWolfvin/CodeLens@main.Closes #58 (phase-1).