Skip to content

Agent re-scan that replaces a gem patch with a newer one leaves the old patch's other files patched: apply --check and vex pass, and after rollback the app still loads patched code that no command lists #2096

Closed

Description

[agent] Found by the scheduled Bundler (RubyGems) bug-hunt routine (ledger #316).

Summary

In agent mode, when a re-scan replaces the recorded patch of a gem with a newer one ([update] pkg:gem/rack@2.2.8 (replacing 44444444)), the files that only the old patch changed stay patched in the installed gem. The manifest record is overwritten with the new patch's file list, so nothing knows about those files any more:

  • apply --check says "Patches are in sync", and vex attests the gem, while the gem holds edits from two patches.
  • rollback (and remove) restore only the new patch's files, exit 0, and empty the manifest. The old patch's edit is still in vendor/bundle, list says "No patches in this project", and the app keeps loading the patched file.
  • A plain bundle install keeps the copy ("Using rack 2.2.8"). Only bundle pristine rack or bundle install --redownload gets the original file back, and no message says so.

A newer patch that changes the same file as the old one is handled correctly (the file ends on the new patch's content and rollback restores the original). Only files that dropped out of the patch are left behind.

Impact

After rollback the user believes the gem is back to what RubyGems installed, but it runs code from a patch that no command lists, checks or can undo. This is the same end state as #2093, reached with agent mode alone and an ordinary re-scan. Before the rollback, vex attests a tree that matches no single patch.

Repro

Needs a patch API that serves patch A for pkg:gem/rack@2.2.8 first and patch B (a different uuid, for the same purl) later. A changes lib/rack.rb; B changes only lib/rack/version.rb. The ledger's harness notes describe the mock (state/bundler.md on bughunt/ledger).

printf 'source "https://rubygems.org"\ngem "rack", "2.2.8"\n' > Gemfile
bundle config set --local path vendor/bundle && bundle install
G=vendor/bundle/ruby/3.3.0/gems/rack-2.2.8
sha256sum $G/lib/rack.rb $G/lib/rack/version.rb > .before

socket-patch scan --mode agent --yes        # the API serves patch A
# the API now serves patch B for the same purl
socket-patch scan --mode agent --yes
#   pkg:gem/rack@2.2.8 [FREE] (replaces 44444444)
#   [update] pkg:gem/rack@2.2.8 (replacing 44444444)
#   Summary: 1 of 1 targeted patch applied, 0 already patched, 0 not found on disk

grep -c SOCKET_PATCHED $G/lib/rack.rb $G/lib/rack/version.rb
#   lib/rack.rb:1            <- patch A's edit, still there
#   lib/rack/version.rb:1    <- patch B's edit
socket-patch apply --check                  # Patches are in sync (1 patch checked).   exit 0
socket-patch vex --product pkg:generic/app@1 -O vex.json   # Wrote OpenVEX document with 1 statement

socket-patch rollback --yes                 # Rolled back packages: pkg:gem/rack@2.2.8   exit 0
sha256sum -c .before
#   lib/rack.rb: FAILED
#   lib/rack/version.rb: OK
socket-patch list                           # No patches in this project. Run `socket-patch scan`.
bundle install                              # Bundle complete!  (keeps the copy)
bundle exec ruby -e 'require "rack"; puts defined?(SOCKET_PATCHED_RACK) ? "PATCHED" : "plain"'
#   PATCHED

Expected

docs/ecosystems.md and the CLI contract treat an agent patch as fully reversible: rollback restores the files the patch changed, and apply --check / vex report a gem only when its installed files match the recorded patch. Replacing a recorded patch with a newer one should first restore the files the old record lists (its before-blobs are in .socket/blobs/ at that point) and then apply the new patch, or keep enough of the old record to do so at rollback. If that is not possible the update should be refused with a remedy rather than leave unrecorded edits behind.

Actual

The re-scan overwrites the manifest record and applies only the new patch's files. Patch A's edit to lib/rack.rb is kept, unrecorded, through apply --check, vex, rollback, remove, list and bundle install. The old patch's blobs are then deleted as unused ("Freed 12.11 KB of unused blobs and archives"), so the original bytes are no longer in the project either.

Versions

Bundler Ruby Result
4.0.18 3.3.6 reproduces
2.6.9 3.1.6 reproduces
2.5.22 3.3.6 reproduces
4.0.18, newer patch changes the same file as the old one 3.3.6 does not reproduce (file updated, rollback restores the original)

socket-patch main 63f791d (5.1.0), Linux. Nothing here depends on the platform, and nothing in the suspect code is specific to gems, so other ecosystems' agent mode probably behaves the same way. I tested Bundler only. A first bad release was not looked for.

Suspect code

crates/socket-patch-cli/src/commands/agent_download.rs:1737: manifest.patches.insert(patch.purl.clone(), build_patch_record(&patch, files)) replaces the record of a PatchAction::Updated patch (decided at agent_download.rs:1024) without restoring the files that are in the old record and not in the new one. get reports the same replacement (commands/get.rs:654, "Updated: 1 (replacing …)") and likely shares the path.


Generated by Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent:triagedbugbughuntpm:bundlerpriority:p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions