minder icon indicating copy to clipboard operation
minder copied to clipboard

fix: prevent cache poisoning by force-resetting worktree on failed PR remediation

Open AftAb-25 opened this issue 7 months ago • 4 comments

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: true to 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

AftAb-25 avatar Apr 14 '26 19:04 AftAb-25

Coverage Status

coverage: 60.706% (-0.01%) from 60.717% — AftAb-25:fix/6367-remediation-cache-poisoning into mindersec:main

coveralls avatar Apr 14 '26 19:04 coveralls

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:

  1. If we remove Force: true: Checkout cleanly falls back to MergeReset. However, because the remediator strictly operates by modifying files, the checkout immediately throws an ErrUnstagedChanges exception. This leaves the modifications intact and leaves the cache fully poisoned.
  2. If we use Force: true but remove .Clean(): The tracked files are successfully reverted, but any completely new, untracked files generated during the aborted remediation run (e.g. .lock files 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!

AftAb-25 avatar Apr 23 '26 06:04 AftAb-25

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.

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:

evankanderson avatar Apr 23 '26 13:04 evankanderson

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.

github-actions[bot] avatar May 24 '26 05:05 github-actions[bot]

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.WriteFile from Billy's util module for simplicity

AftAb-25 avatar Jun 21 '26 17:06 AftAb-25