fix(security): refuse file-tool writes to git-managed state under .git - #88747
Closed
andrexibiza wants to merge 1 commit into
Closed
fix(security): refuse file-tool writes to git-managed state under .git#88747andrexibiza wants to merge 1 commit into
andrexibiza wants to merge 1 commit into
Conversation
write_file/patch (and the other mutating file tools) could silently rewrite git's own control files inside a normal repository's .git directory — HEAD, index, refs/, objects/, logs/, packed-refs, ORIG_HEAD, FETCH_HEAD, MERGE_HEAD, CHERRY_PICK_HEAD, REBASE_HEAD, COMMIT_EDITMSG, shallow, and info/* except exclude. A single misdirected write to .git/HEAD replaces the branch identity and turns a healthy checkout into an apparently empty one (same silent-corruption shape as the NousResearch#78565 worktree-pointer guard, but inside the git dir). Add a git-state classifier to the shared write-deny path that refuses writes under any .git directory while keeping user-owned paths writable: .git/config, .git/hooks/*, .git/info/exclude, .git/description. Fixes NousResearch#78793
Collaborator
Contributor
Author
|
Closing into #78806 |
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.
What changed and why
write_file/patch(and the other mutating file tools) could silently rewrite git-managed state inside a normal repository's.gitdirectory —HEAD,index,refs/,objects/,logs/,packed-refs,ORIG_HEAD,FETCH_HEAD,MERGE_HEAD,CHERRY_PICK_HEAD,REBASE_HEAD,COMMIT_EDITMSG,shallow, andinfo/*exceptexclude.A single misdirected write to
.git/HEADreplaces the branch identity and turns a healthy checkout into an apparently empty one (git status→On branch evil/No commits yet). This is the same silent-corruption shape as the #78565 worktree-pointer guard, but inside the git directory itself — which that guard cannot reach.This PR adds a git-state classifier to the shared write-deny path (
agent/file_safety.py) that refuses writes under any.gitdirectory, while keeping the user-owned paths inside a git dir writable:.git/config(repository configuration).git/hooks/*(local hook scripts).git/info/exclude(per-repo ignore rules).git/descriptionEverything else under
.git/is git-managed state that a misdirected write silently corrupts. The guard runs onos.path.realpath()-expanded paths, so a symlink pointing into a git dir cannot bypass it.Reproduction (before this fix, on
main)After this change,
write_file('<repo>/.git/HEAD', ...)returns a write-denied error, and the repo's git state is untouched.How to test
The new suite asserts:
.git/HEAD,.git/index,.git/packed-refs,.git/ORIG_HEAD,.git/FETCH_HEAD,.git/MERGE_HEAD,.git/CHERRY_PICK_HEAD,.git/REBASE_HEAD,.git/COMMIT_EDITMSG,.git/shallow,.git/refs/*,.git/objects/*,.git/logs/*,.git/info/refs,.git/info/alternates, and the.gitdir itself..git/config,.git/description,.git/hooks/*,.git/info/exclude.Write denied: ... protected system/credential fileerror.Platforms tested
git diff --checkclean;scripts/check-windows-footguns.pyscans clean.tests/agent/test_file_safety_sandbox_mirror.pyhas 4 pre-existing failures that also occur on pristineorigin/main(an environment/cross-profile artifact unrelated to this change — verified on a clean main worktree).Why this matters to users
Today, one misdirected
write_filecall — or a prompt-injected path — can replace the contents of a repository's.git/HEAD, changing which branch the checkout reports and making a healthy repository look empty or corrupted. The working tree itself is untouched, so it appears as silent state corruption that's easy to miss untilgit statusgoes wrong.After this change, the file tools refuse to write git's own control files. Your repositories' git state (branches, refs, index, objects, logs) can no longer be silently clobbered by a stray file-write, while the git files you legitimately own and edit (config, hooks, info/exclude) stay fully writable. Nothing about normal file editing changes — only the dangerous, git-managed control surface is now protected.
Fixes #78793