fix: reject /add of files in a sibling dir sharing the root's name prefix#5392
Open
nolanchic wants to merge 1 commit into
Open
fix: reject /add of files in a sibling dir sharing the root's name prefix#5392nolanchic wants to merge 1 commit into
nolanchic wants to merge 1 commit into
Conversation
…efix The within-root guard in cmd_add used str.startswith() against the repo root, which carries no trailing path separator. An absolute path like /tmp/x/repoXYZ/secret.env was therefore accepted as "within" a repo rooted at /tmp/x/repo, so an out-of-tree file could be added to the chat and sent to the LLM. Use Path.is_relative_to() instead, matching the containment check the glob branch already performs in glob_filtered_to_repo. Adds a regression test covering the literal absolute-path branch. Relates to Aider-AI#178.
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
/add <absolute_path>could add a file that lives outside the repo root into the chat (and therefore into the LLM context) whenever the file lived in a sibling directory whose name started with the repo root's name.Example: with the repo rooted at
/tmp/x/repo, the following was accepted:because
/tmp/x/repoXYZ/secret.env.startswith(/tmp/x/repo) isTrue(string prefix, no path separator).This is the same class of out-of-tree leak that #178 addressed, but on the literal absolute-path branch of
cmd_add— the glob branch (glob_filtered_to_repo) was already protected byPath.is_relative_to(), so the gap only showed up for explicit absolute paths / autocomplete-provided paths.Root cause
aider/commands.py, insidecmd_add's containment guard:self.coder.roothas no trailing path separator, sostartswithmatches any sibling directory whose name shares the root's prefix.Fix
Use a real path-containment check, matching what
glob_filtered_to_repoalready does:Path.is_relative_tois available on all supported Python versions (3.10+).Test plan
test_cmd_add_abs_path_in_sibling_dir_with_prefix— reproduces the leak on the old code (fails onmain) and passes after the fix.pytest tests/basic/test_commands.py— full file green (70 tests), including the existing out-of-root / absolute-path / read-only-external tests (test_cmd_add_from_outside_root,test_cmd_add_abs_filename,test_cmd_add_read_only_with_external_file, …).pre-commit run --files aider/commands.py tests/basic/test_commands.py— isort / black / flake8 / codespell all pass.Relates to #178.