fix: prevent cache poisoning by force-resetting worktree on failed PR remediation
Fixes #6367
Description
This fixes a cache poisoning bug in the PR remediator where failed remediations would leak uncommitted changes into the shared ingest cache, causing silent evaluation failures for subsequent rules.
When pull_request.go aborts a remediation and runs checkoutToOriginallyFetchedBranch, it previously used the default go-git checkout which preserves modifications to the worktree/index. Because the executor uses a single shared ingestCache.Fs per run, those left-behind dirty files would then be incorrectly evaluated by subsequent rules.
This PR aggressively resets the worktree during the checkout cleanup phase:
- Adds
Force: trueto the checkout options to discard modified tracked files. - Calls
wt.Clean(&git.CleanOptions{Dir: true})to remove any untracked leftover files.
I've also added a unit test (checkout_cleanup_test.go) that simulates a dirty worktree and ensures both tracked modifications and untracked files are correctly wiped out.
Checklist
- [x] Code compiles correctly
- [x] Added tests that fail without the change (if possible)
- [x] All tests passing
- [x] Extended the README / documentation, if necessary
coverage: 60.706% (-0.01%) from 60.717% — AftAb-25:fix/6367-remediation-cache-poisoning into mindersec:main
Hi @evankanderson, thank you for the review and the thoughtful suggestion!
I looked into relying solely on go-git's native Checkout logic to evaluate tracked vs untracked files, and I ran unit tests dropping the .Clean() call to verify. Unfortunately, it turns out that go-git's Checkout does not natively handle explicitly untracked files.
According to the official go-git [CheckoutOptions documentation](https://pkg.go.dev/github.com/go-git/go-git/v5/worktree#CheckoutOptions):
"Force, if true, any local modification to existing files will be dropped, and any untracked files will remain."
My local test assertions confirmed this limitation:
- If we remove
Force: true: Checkout cleanly falls back toMergeReset. However, because the remediator strictly operates by modifying files, the checkout immediately throws anErrUnstagedChangesexception. This leaves the modifications intact and leaves the cache fully poisoned. - If we use
Force: truebut remove.Clean(): The tracked files are successfully reverted, but any completely new, untracked files generated during the aborted remediation run (e.g..lockfiles or build artifacts) persistently remain in the directory.
Because we are checking out over a natively shared ingestcache where subsequent, unrelated rule evaluations heavily depend on a pure state, both Checkout(Force: true) (to wipe modified tracked files) and wt.Clean() (to strictly purge newly created untracked files) are structurally required to guarantee a fully hermetic state after an abort.
Let me know if this context makes sense, or if you'd like me to look into implementing this via a different go-git mechanism (like a manual HardReset combination) instead!
Hi @evankanderson, thank you for the review and the thoughtful suggestion!
I looked into relying solely on
go-git's native Checkout logic to evaluate tracked vs untracked files, and I ran unit tests dropping the.Clean()call to verify. Unfortunately, it turns out thatgo-git's Checkout does not natively handle explicitly untracked files.
Yes, I agreed with you in this comment: https://github.com/mindersec/minder/pull/6368#discussion_r3116237879
(We shouldn't have any untracked files except in very unusual circumstances, but it looks like even tracked file changes may persist in the worktree for future evaluations.)
However, I had two other requests as well:
This PR needs additional information before we can continue. It is now marked as stale because it has been open for 30 days with no activity. Please provide the necessary details to continue or it will be closed in 30 days.
Hi, @evankanderson Thanks for the feedback! I've updated the PR with both requests:
- Moved the tests into
pull_request_test.go - Updated the test file creation code to use
util.WriteFilefrom Billy's util module for simplicity