Repository navigation
fix(security): stop Gerrit suggestions from writing into .git - #3960
TianHengZhuang wants to merge 1 commit into
Conversation
publish_code_suggestions checks containment with (repo_root / relevant_file).resolve().relative_to(repo_root), but resolve() follows symlinks, so a tracked symlink that points into .git/ still looks contained. add_suggestion then rewrites whatever it points at -- the obvious target being .git/hooks/pre-commit -- before the git diff a few lines later reads it. Reject a symlink outright, and reject any path that resolves through a .git directory, in the same try/except that already drops traversal. Fixes The-PR-Agent#3914
PR Summary by QodoPrevent Gerrit suggestions from modifying Git metadata
AI Description
Diagram
High-Level Assessment
Files changed (2)
|
Code Review by Qodo
1. Git metadata can still be rewritten on Mac
|
| if ".git" in target_path.parts: | ||
| raise ValueError("resolves into .git") |
There was a problem hiding this comment.
1. Git metadata can still be rewritten on mac 🐞 Bug ⛨ Security
publish_code_suggestions compares resolved path components to the case-sensitive string .git. On a case-insensitive macOS filesystem, .GIT/config can refer to .git/config while retaining the requested spelling in the path, so the check passes and add_suggestion opens the metadata file for writing.
Agent Prompt
## Issue description
The exact `.git` component comparison can miss differently cased paths to Git metadata on case-insensitive filesystems.
## Fix Focus Areas
- pr_agent/git_providers/gerrit_provider.py[459-462]
- tests/unittest/test_gerrit_provider.py[121-140]
## Recommended Fix
Compare path components using case-insensitive normalization where the filesystem treats names case-insensitively. Add a test that verifies a differently cased spelling of `.git/config` cannot reach `add_suggestion`.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
|
Thanks for the careful write-up and the directory-symlink test, which is a good case to cover. #3915 was opened a day earlier for #3914 with the same guard, so I'm closing this as a duplicate to keep the work in one place. Both versions still let |
Fixes #3914.
resolve()follows symlinks, so a tracked symlink inside the checkout made the containment check pass:(repo_root / evil_link).resolve()lands onrepo_root/.git/hooks/pre-commit, which is still underrepo_root, sorelative_to()was happy.add_suggestionthen rewrote that path before thediff()call a few lines later read the tree, which turns a code suggestion into a write into git's own metadata.Two checks now sit next to the existing
relative_to, in the sametryso the oneexcept ValueErrorstill reports and skips:.gitamong its partsThe second one covers the case the first misses: a symlinked directory (
gitdir -> .git) whose final component is an ordinary file, which resolves into.gitwithout the requested path itself being a symlink. A symlink that resolves inside the checkout and outside.gitstill works, which is what the existing symlink tests in this file rely on.Tests in
tests/unittest/test_gerrit_provider.py:test_publish_code_suggestions_refuses_paths_that_reach_into_git, parametrised overlink.py(symlink to.git/config) andgitdir/config(through a symlinked directory). Both assertadd_suggestionwas never called and that.git/configis byte-identical afterwards.test_publish_code_suggestions_still_applies_a_regular_fileguards the other direction, so the new rejection cannot swallow ordinary suggestions.No conflict with #3876 as far as I can tell: that one edits
add_suggestion, this edits its caller and leaves the function alone. Happy to rebase if you would rather see them land in the other order.ruff checkclean.pytest tests/unittest/test_gerrit_provider.py -k publish_code_suggestionspasses. Seven other tests in that file compare newline-sensitive bytes and fail on a Windows checkout (b'app.py\r\n' != b'app.py\n'), which is unrelated to this change and does not happen on CI.