Skip to content

Agent apply also patches gems in the system gem home that Bundler never loads when the project sets a Bundler path other than vendor/bundle, or has nothing installed in it yet #2313

Done

Description

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

Summary

When a project installs its gems into an explicit Bundler path other than vendor/bundle (bundle config set --local path vendor/gems, BUNDLE_PATH=vendor/gems, …), agent mode (apply, scan --mode agent) patches the project's copy and every copy of the same name-version in the gem homes gem env reports (the system gems, an rbenv or asdf home, ~/.gem). Under an explicit path Bundler installs every non-default gem into that path and never loads the system copy for this project, so the second write patches nothing the project runs. It changes a gem that belongs to other projects on the machine.

The same happens with path vendor/bundle (or deployment mode) while nothing is installed there yet: apply run before the first bundle install of a fresh clone patches the system copies, prints 5 of 5 targeted patches applied, and the bundle install that follows installs unpatched gems.

With path vendor/bundle and the gems installed, the system copy is left alone, which is the expected behavior in all three cases.

Impact

  • "Do no harm": a gem home outside the project is modified for a project that does not use it. Another project that loads that copy runs the patched code without having asked for it. For a gem with a native extension the extension is rebuilt in the shared home as well.
  • The copies stay patched if the project is deleted, or its .socket/ is, without a rollback. In the fresh-clone case even rollback leaves them: once vendor/bundle holds a store the system homes are no longer crawled, the manifest records are kept, and the user has to run one socket-patch rollback <purl> -g per gem (the warning does name it, and it works).
  • In the fresh-clone case apply reports success while the project has no patched gem. apply --check and vex do catch it after the install.

Repro

No patch API needed: the script writes the manifest and blobs by hand and runs apply --offline.

#!/bin/sh
# usage: repro.sh <socket-patch binary> <bundler version> <path setting: vendor/gems | vendor/bundle> [env]
SP=$1; BV=$2; BP=$3; HOW=${4:-config}
W=$(mktemp -d); export GEM_HOME=$W/home BUNDLER_VERSION=$BV SOCKET_TELEMETRY_DISABLED=1
gem install --no-document bundler -v $BV >/dev/null 2>&1
gem install --no-document rack -v 2.2.8 >/dev/null      # another project's copy in the gem home
mkdir $W/proj && cd $W/proj
printf 'source "https://rubygems.org"\ngem "rack", "2.2.8"\n' > Gemfile
if [ $HOW = env ]; then export BUNDLE_PATH=$BP; else bundle config set --local path $BP >/dev/null; fi
bundle install >/dev/null 2>&1
ruby -rdigest -rjson -rfileutils -e '
  f = Dir["#{ARGV[0]}/ruby/*/gems/rack-2.2.8/lib/rack.rb"][0]
  before = File.binread(f); after = before + "\n# SOCKET-PATCHED\n"
  h = ->(b) { Digest::SHA256.hexdigest("blob #{b.bytesize}\0".b + b) }
  FileUtils.mkdir_p(".socket/blobs")
  File.binwrite(".socket/blobs/#{h[after]}", after); File.binwrite(".socket/blobs/#{h[before]}", before)
  File.write(".socket/manifest.json", JSON.pretty_generate("patches" => { "pkg:gem/rack@2.2.8?platform=ruby" => {
    "uuid" => "00000000-0000-4000-8000-000000000001", "exportedAt" => "2026-02-01T00:00:00Z",
    "files" => { "lib/rack.rb" => { "beforeHash" => h[before], "afterHash" => h[after] } },
    "vulnerabilities" => { "GHSA-aaaa-bbbb-cccd" => { "cves" => [], "summary" => "t", "severity" => "high", "description" => "t" } },
    "description" => "t", "license" => "MIT", "tier" => "free" } }))' $BP
$SP apply --offline 2>&1 | tail -4
echo "project copy patched:        $(grep -c SOCKET-PATCHED $BP/ruby/*/gems/rack-2.2.8/lib/rack.rb)"
echo "system gem home copy patched: $(grep -c SOCKET-PATCHED $GEM_HOME/gems/rack-2.2.8/lib/rack.rb)   <- Bundler never loads this copy"
bundle exec ruby -e 'puts "bundler loads: #{$LOADED_FEATURES.grep(/rack/).first || Gem.loaded_specs["rack"].full_gem_path}"'
rm -rf $W

repro.sh target/release/socket-patch 4.1.1 vendor/gems prints:

  pkg:gem/rack@2.2.8?platform=ruby (vendor/gems/ruby/3.3.0/gems/rack-2.2.8, via blob)
  pkg:gem/rack@2.2.8?platform=ruby (/tmp/tmp.ZIJU9EAq7Q/home/gems/rack-2.2.8, via blob)

Summary: 1 of 1 targeted patch applied, 0 already patched, 0 not found on disk
project copy patched:        1
system gem home copy patched: 1   <- Bundler never loads this copy
bundler loads: /tmp/tmp.ZIJU9EAq7Q/proj/vendor/gems/ruby/3.3.0/gems/rack-2.2.8

With vendor/bundle as the third argument the system copy stays unpatched (0).

The fresh-clone form, on a project with a committed .socket/manifest.json for five gems and the same gem versions in the gem home:

$ export BUNDLE_PATH=vendor/bundle
$ socket-patch apply                 # before bundle install
Summary: 5 of 5 targeted patches applied, 0 already patched, 0 not found on disk
$ bundle install && ruby app.rb      # the app loads no patched gem
$ socket-patch apply && socket-patch rollback
Warning: pkg:gem/rack@2.2.8?platform=ruby: its copy in the global install (<gem home>/gems/rack-2.2.8) is still patched or could not be verified, so its manifest record was kept; run `socket-patch rollback pkg:gem/rack@2.2.8 -g` to restore it
(one warning per gem; the five system copies are still patched)

Expected

apply patches the copies the project loads and no other. The read-only side already follows that rule: crates/socket-patch-cli/CLI_CONTRACT.md ("Gem stale-install guard") says the gem env homes count only when Bundler uses system gems, "since with such a path bundle install fetches non-default gems into it and never reuses a system copy", and that vex and apply --check judge gem copies by the same rule: "a copy in a gem env home is not one the project loads and is not verified, unless it is a default gem". The contract keeps the gem env homes as write targets "for default gems like rexml/json", which only ever live there. docs/ecosystems.md ("Ruby gems: gems already installed") states the principle for hosted mode: gems in a gem home outside the project "are not touched: another project on the machine may use the same copy".

So under an explicit or deployment path, a copy in a gem env home should be written only when it is a default gem (its spec under specifications/default/). With nothing installed in the project's path yet, apply should report the gems as not installed instead of patching the system copies.

Actual

Every non-default copy of the patched name-version in the gem env homes is patched too, whenever the explicit path is not the default vendor/bundle root, or holds no store yet.

Versions

socket-patch Bundler Ruby Path setting System copy patched
main cfc870f 4.1.1 3.3.6 .bundle/config path vendor/gems yes
main cfc870f 2.6.9 3.3.6 .bundle/config path vendor/gems yes
main cfc870f 2.5.22 3.3.6 BUNDLE_PATH=vendor/gems yes
main cfc870f 4.1.1 3.3.6 BUNDLE_PATH=vendor/bundle, nothing installed yet yes
5.1.0 4.1.1 3.3.6 .bundle/config path vendor/gems yes
main cfc870f 4.1.1 3.3.6 path vendor/bundle, gems installed no (correct)

Not a regression: 5.1.0 behaves the same. Linux only; the crawler code is not platform-specific, so macOS and Windows should behave the same.

Suspect code

crates/socket-patch-core/src/crawlers/ruby_crawler.rs:140: gem_paths_and_discovery appends every gem env home unless the default vendor/bundle root holds a store, and the comment above it says stores found through an env or config root deliberately do not suppress that (for default gems). Nothing then limits the appended homes to default gems for the writers. bundler_unused_system_gem_homes (ruby_crawler.rs:843) already computes the homes Bundler does not load from, and is_default_gem_copy (ruby_crawler.rs:1511) the exception; only the verifiers use them.


Generated by Claude Code

Activity

  1. added
    bug
    bughunt
    pm:bundler
    on Nov 27, 2026
  2. mikolalysenko commented on Dec 1, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Claiming this issue. Branch: agent/fix-gem-apply-unused-system-homes.


    Generated by Claude Code

  3. mikolalysenko commented on Dec 1, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Blocked on a maintainer decision before any code changes. I claimed this, read the code, and stopped: the behavior the issue calls wrong is the documented and tested contract for apply, so the fix is a contract change and not a bug fix I should make alone.

    What the issue asks for: under an explicit or deployment Bundler path, apply writes a copy in a gem env home only when it is a default gem, and reports the gem as not installed when the project's path holds nothing yet.

    What is pinned today:

    • CLI_CONTRACT.md, "Copy classes": a copy in a gem env fallback home "is patched too when it matches and is writable", and with no bundle-store copy it "IS the primary install".
    • crates/socket-patch-cli/tests/apply/in_process_gem_fallback_home.rs: writable_fallback_home_copy_still_patched sets BUNDLE_PATH to a store (an explicit path), stages a healthy copy in the gem env home and asserts both are patched (applied: 2). The file header gives the reason: "Patching a shared home's copy is fine (vulnerable bytes are vulnerable bytes)".
    • apply.rs, the copy-class comment: "Copy CLASS decides FAILURE semantics (never write scope …)".

    The fix would invert that test and rewrite that contract paragraph. The four other tests in the file (best-effort skips of a mismatched or unwritable home copy) would no longer have a home copy to skip, except for default gems.

    The question: should agent apply stop writing non-default gem copies in gem env homes when Bundler does not use system gems for the project (the rule vex, apply --check and the hosted stale-install guard already follow)?

    • Yes: I will filter the forward writer only (apply, and the apply step of scan --mode agent), keep rollback and remove finding those copies so that copies an earlier release patched can still be restored, invert the test above, and update the contract and the Ruby docs. A gem with no copy left reports as not installed. Open sub-question: should the agent-mode scan's discovery crawl follow the same rule, so that a fresh clone does not download patches for gems only the system home holds?
    • No: the shared-home write stays, and this issue narrows to the fresh-clone report (5 of 5 applied while the project has no patched gem), which could become a warning.

    Reply here and the next run picks it up. Draft PR #2402 holds the claim and has no code yet.


    Generated by Claude Code

  4. mikolalysenko commented on Dec 1, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] The maintainer answered the question above on 2026-12-01: yes. Agent apply stops writing non-default gem copies in gem env homes that Bundler does not load for the project; rollback and remove keep finding them.

    It is implemented in #2402, which is out of draft and ready for review. Two things in it go past the question as asked, and are listed in the PR under "For a maintainer": an agent-mode scan and get by name or advisory select by the same rule (leaving them alone turned a scan that exits 0 on main into exit 1), and a record an earlier release wrote for another project's gem is skipped with a warning that names socket-patch rollback <purl>.

    No further work is needed on this issue from another run.


    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

    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