Skip to content

Rolling back a gem that went from hosted to vendored leaves the patched installed copy in vendor/bundle: the app keeps loading patched code and bundle pristine / --redownload fail on the restored checksum #2083

Open

Description

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

Summary

A gem that was patched in hosted mode and then switched to vendored mode keeps its patched installed copy and .gem archive in the project's own vendor/bundle (the hosted scan put them there). When the vendored gem is later rolled back, Gemfile and Gemfile.lock return to the originals byte for byte, but nothing puts the installed copy back. The project is left in a state the original never had:

  • the app still loads the patched code after rollback and after bundle install ("Using rack 2.2.8");
  • on a lock with a CHECKSUMS section (Bundler 2.6+ with checksums, the default on 4.x), bundle pristine and bundle install --redownload fail with exit 37 ("Bundler found mismatched checksums"), and after the failed --redownload the gem is gone and the app no longer boots;
  • on a lock without CHECKSUMS (2.5.22, 2.6.9), bundle pristine and --redownload reinstall the patched archive, so the patch can't be removed with Bundler at all;
  • rollback's own note says the opposite: "1 unwired package keeps its patched bytes in installed trees until the next package-manager install". The next install changes nothing.

A plain hosted rollback handles the same end state correctly ("Replaced the installed copy of 1 gem with the upstream build"); the vendored unwind doesn't.

Impact

Do no harm: a project that ran bundle pristine / bundle install --redownload cleanly before socket-patch touched it fails both after scan --mode hosted, scan --mode vendored, rollback, although the Gemfile pair is the original. The user followed only socket-patch commands and the remedy in the note doesn't work. Trying hosted first and then moving to vendored (for example because CI can't reach the patch server) is an ordinary path.

Repro

sp is socket-patch pointed at a patch API that has a patch for rack 2.2.8 (I used a local mock of the API and registry serving a repacked rack-2.2.8.gem; the real rubygems.org served everything else).

mkdir r1 && cd r1 && git init -q .
printf '**/vendor/bundle/\n.bundle/\n' > .gitignore
printf 'source "https://rubygems.org"\n\ngem "rack", "2.2.8"\n' > Gemfile
bundle config set --local path vendor/bundle
bundle install
git add -A && git commit -qm init

sp scan --mode hosted      # "Replaced the installed copy of 1 gem with the patched build: rack 2.2.8."
bundle install
sp scan --mode vendored    # "was hosted; restored its upstream registry entry ... (mode takeover)"
bundle install
sp rollback                # "Reverted vendoring for pkg:gem/rack@2.2.8"
git diff --exit-code HEAD -- Gemfile Gemfile.lock   # identical to the original

bundle install             # Bundle complete!
bundle exec ruby -e 'require "rack"; p defined?(SOCKET_PATCHED_RACK)'   # still the patched code
bundle pristine            # exit 37 on Bundler 4.0.x
bundle install --redownload   # exit 37; afterwards `require "rack"` raises LoadError

Output on Bundler 4.0.24 / Ruby 3.3.6:

$ rollback
Reverted vendoring for pkg:gem/rack@2.2.8

Note: 1 unwired package keeps its patched bytes in installed trees until the next package-manager install.
  Gemfile + Gemfile.lock byte-identical to the original
$ bundle install
Bundle complete! 1 Gemfile dependency, 1 gem now installed.
  app loads patched rack: true
$ bundle pristine
Bundler found mismatched checksums. This is a potential security risk.
rack (2.2.8)
sha256=7b83a1f1304a8f5554c67bc83632d29ecd2ed1daeb88d276b7898533fde22d97
    from the lockfile CHECKSUMS at Gemfile.lock:15:16
  exit=37
$ bundle install --redownload
Bundler found mismatched checksums. This is a potential security risk.
  exit=37
bundled_gems.rb:69:in `require': cannot load such file -- rack (LoadError)

vendor/bundle/ruby/3.3.0/cache/rack-2.2.8.gem is the patched archive (sha256 bab891e7…), while the restored lock pins the upstream 7b83a1f1….

Expected

docs/ecosystems.md, "Ruby gems: gems already installed": "rollback and remove do the same with the upstream gem", and the task's bar that a project which installed cleanly before must still do so afterwards.

CLI_CONTRACT.md ("Gem stale-install guard", The unwind direction) describes exactly this end state as the problem the upstream refresh exists for: "its installed copies and archives hold the PATCHED gem ... the patched code keeps running, and on bundler ≥ 2.6 bundle pristine fails on the restored upstream checksum". What never refreshes then says a hosted → vendored takeover leaves the copies alone because they are "unused leftovers" while the gem is vendored, and that a vendored unwind "likewise only names that archive". The two rules together leave the gap: once the vendored gem is unwound the leftovers are used again, and no command refreshes or even names them (the run prints no upstream_gem_stale_install warning, only the generic note whose remedy is wrong).

Either the vendored unwind should refresh a project-local installed copy whose archive is not the upstream one (as the hosted unwind does), or it should at least warn upstream_gem_stale_install with a remedy that works (remove the gem's specifications entry and cached .gem, then bundle install).

Actual

No refresh and no warning. The project keeps running patched code after rollback, and Bundler's reinstall commands fail or reinstall the patch.

Versions

Bundler Ruby Result
4.0.24 3.3.6 reproduces, twice (a one-gem project and a 10-gem project with git and path gems): patched code after rollback, pristine and --redownload exit 37
2.6.9 (lock without CHECKSUMS) 3.1.6 reproduces: patched code after rollback, after pristine and after --redownload (all exit 0)
2.5.22 3.3.6 same as 2.6.9
any, hosted rollback without the vendored step does not reproduce: "Replaced the installed copy ... with the upstream build"
any, vendored only (never hosted) does not reproduce: the installed copy was never replaced

socket-patch main 63f791d (5.1.0). Linux only; nothing here looks platform-specific. remove <gem> and vendor --revert after the same takeover were not run, but the contract gives them the same rule.

Suspect code

  • crates/socket-patch-cli/src/commands/rollback.rs:466 prints the "until the next package-manager install" note for the vendored unwind without checking the project's installed gem copies.
  • crates/socket-patch-cli/src/commands/gem_refresh.rs:56 (Direction::Upstream, upstream_gem_stale_install): the upstream refresh that the hosted unwind runs is not run by the vendored unwind (CLI_CONTRACT.md line 463, What never refreshes).

Generated by Claude Code

Activity

  1. mikolalysenko commented on Nov 17, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Claiming this issue. Branch: agent/fix-vendored-unwind-stale-hosted-gem.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions