Skip to content

Fetch gems that get built over https only; refuse paths that cannot be probed - #2112

Merged
Mikola Lysenko (mikolalysenko) merged 12 commits into
mainfrom
fix/gem-rebuild-https-and-probe
Nov 17, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 12 commits into
mainfrom
fix/gem-rebuild-https-and-probe

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

LLM Description written by Claude Code:claude-fable-5-2

Summary

Follow-up to #2098, which merged before the fixes for its two review-bot
findings were pushed. Both findings are confirmed, and both are closed here:

  • A gem that gets built or installed, and the checksum it is checked
    against, are fetched over https only.
    Rebuild native gem extensions from the source gem, precompiled gems included #2098 made apply download a
    source gem and run its build script. The checksum came from the same
    registry base, and that base (SOCKET_RUBYGEMS_URL) had no scheme check and
    the client followed any redirect, so on plain http someone on the network
    could serve both the gem and its checksum.
  • A stage, backup or extension path that cannot be probed is refused. The
    planted-link guard read any lstat error as "not a link".

main is not released with #2098 yet, so nothing shipped with either hole.

The change

  • utils/http.rs: one rule for what may be fetched on this path (https, or
    plain http to a loopback host, which the hermetic tests and a local mirror
    use), one redirect policy (nothing out of https, nothing to a non-loopback
    http host, ten hops), and one definition of a loopback host, now shared
    with patch/redirect/grant.rs and update/release.rs.
  • vendor/registry_fetch.rs SecureRegistryClient: refuses a URL before any
    request, carries that redirect policy, and sends a loopback request through
    a client that ignores system proxies, so "this machine" cannot leave the
    machine through HTTP_PROXY.
  • patch/gem_source.rs: the source gem, its build dependencies and both
    index reads use it. A refused registry behaves as "no source gem in reach":
    a gem RubyGems built falls back to its own tree, a precompiled gem is
    refused with nothing written, dry run included.
  • Hosted rollback had the same hole: it re-pinned a CHECKSUMS entry and
    reinstalled the upstream gem through the plain client. It uses the secure
    client too, and a copy it could not check is now reported under
    upstream_gem_stale_install with the reason, instead of being passed over.
  • utils/containment.rs: only "not found" reads as nothing there; any other
    probe error names the path. Used for the stage dirs (apply and hosted
    refresh), the backup, its marker, the extension dir and the own-source
    proof, where a file that could not be probed used to be skipped.
  • Registry credentials in SOCKET_RUBYGEMS_URL are redacted wherever the
    base is printed.
  • CLI_CONTRACT.md and docs/ecosystems.md describe the rule.

What a reviewer should weigh

  • With a non-loopback http:// SOCKET_RUBYGEMS_URL, hosted rollback no
    longer re-pins or refreshes from that registry; it restores the lock and
    reports the installed copy as unchecked.
  • The shared loopback rule is the strict one: localhost and loopback IPs.
    A patch-server grant URL on http://<name>.localhost is no longer treated
    as loopback (nothing makes a resolver answer such a name locally);
    --patch-server-url still admits such a server.
  • restore_original_build on a gem dir it cannot read now fails naming the
    path, where it used to report "no backup".

Code review

/code-review high raised ten findings on the first two commits; all ten were
confirmed and fixed here (the proxy bypass for loopback, the swallowed refusal
in upstream_archive, the backup-info probe, a test that reached DNS, the
redirect hop count, the duplicated loopback rule and stage check, unredacted
credentials, a client per URL, and a scheme check that disagreed with the
fetch). Left as is: rollback_files still reads an unreadable backup
description as none (it cannot signal an error, and the restore that follows
now fails loudly), and the source-gem path still builds its client per gem
copy that actually downloads.

Merge with main

main gained #2108 (in-place rebuild of a gem that builds a library into
its own tree) and #2103 while this was in review, and both touch the same
files. This branch merges main and keeps both sides. Two commits then
bring #2108's new code under the same rules as the rest of this PR:

  • Its tree snapshot and backup description are no longer read as "none" when
    they cannot be read. An unlistable snapshot used to be taken for empty, and
    the restore then removed every file of the gem's own tree.
  • A tree snapshot is not restored through a link inside the gem dir. A
    planted snapshot beside a parked build, plus such a link, made recovery
    write files outside the gem dir.

Not changed here: restore_backup and realign_lib_copies, which predate
#2108, still write through a link inside the gem dir. That is its own PR.

Testing

  • Unit tests for the scheme rule, the redirect rule (ten followed, eleven
    refused, no downgrade), the loopback rule, credential redaction, and the
    fail-closed probes (unreadable directories, skipped under root).
  • e2e: the source gem download with HTTP_PROXY/ALL_PROXY set asserts the
    proxy saw nothing; a plain-http registry refuses a precompiled gem and
    sends a built one to its own tree; rollback reports the copy a refused
    registry left unchecked.
  • Local runs, all passing: core lib 7795; e2e_gem_native_ext_build 30,
    e2e_redirect_gem_stale_install 32, in_process_rollback_hosted 41,
    e2e_redirect_gem_build 43, e2e_vendor_gem_build 14, e2e_gem 6 (with
    --include-ignored); upstream_restore_golden 62.
  • Real precompiled nokogiri 1.18.3 (macOS arm64, Ruby 3.4.10): over https,
    apply rebuilds in 34 s, 6 to 7 libxml2 patches, apply --check in sync,
    vex not_affected, rollback byte-identical. With
    SOCKET_RUBYGEMS_URL=http://user:secret@rubygems.org it is refused, nothing
    is written, and the credential does not appear in the output.

Not verified locally: Linux and Windows.

🤖 Generated with Claude Code

…only

The source gem of a native-extension rebuild and the gems its build lacks
are downloaded from `SOCKET_RUBYGEMS_URL` (default https://rubygems.org)
and used when their sha256 is the one that registry's compact index
records. Nothing required the registry to be https, and the client
followed a redirect from https to http. Over plain http whoever sits on
the network path serves both the archive and the checksum, and the
archive's `extconf.rb` then runs. The own-source proof does not cover
files the installed copy lacks (a precompiled nokogiri ships no
`patches/**`).

These downloads now go through `SecureRegistryClient`: https, or plain
http to a loopback host (`localhost`, `127.0.0.1`, `[::1]`: a local
mirror, the hermetic tests), and nothing else. Any other URL is refused
before a request is made, with a message that says to use an https URL. A
redirect that leaves https, or leads to an http host that is not this
machine, is not followed. The outcome is that of a source gem out of
reach: a gem RubyGems built is rebuilt in place, a precompiled gem is
refused with nothing written, in a dry run too.

The hosted unwind had the same hole. After `rollback` / `remove`, the
installed copy of a restored gem is replaced by the registry's archive
(and built, for a native gem), verified against the sha256 the restore
read from the same registry's compact index and pinned into `CHECKSUMS`.
`UpstreamClient::rubygems_sha256` and the refresh's registry download go
through the same client, so that checksum is never read over plain http.
For an https or loopback registry nothing changes. With any other
`SOCKET_RUBYGEMS_URL` a pin whose `CHECKSUMS` entry must be re-pinned is
refused with the https message, as it is when the registry cannot be
reached.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The link guards read any `lstat` failure as "not a link": the private
`is_symlink`, and `stage_dirs` with the refresh's copy of it. A path that
could not be probed for another reason than "not found" (permission
denied, a parent that is no directory, an I/O error) was then created,
emptied or deleted by path like a real directory. Nothing said it was not
a planted link.

Only "not found" now reads as nothing there (`containment::try_entry_type`).
Any other probe error is refused like a link, with a message naming the
path:

- the stage of a rebuild and of a hosted refresh is neither swept nor
  opened; a gem RubyGems built is rebuilt in place, a precompiled gem is
  refused;
- the extension dir a recovery moves, the extension dir a restore empties
  and the backup a rollback restores from are refused, so a rollback no
  longer reports a copy whose gem dir cannot be read as carrying no
  backup;
- a new backup of the original build is made only when no backup and no
  marker are there, not when they cannot be seen;
- a file the installed copy holds but the own-source proof cannot look at
  fails the proof instead of being skipped, which let a differing file
  behind an unsearchable directory pass.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ne loopback rule

Review of the https-only gem registry client:

- A plain-http request to a loopback registry is accepted because it
  crosses no network, but the client kept the system proxy. With
  `HTTP_PROXY` or `ALL_PROXY` set and no `NO_PROXY`, the request went to
  the proxy host in clear text. `SecureRegistryClient` now sends plain
  http through a client built without a proxy; https keeps the system
  proxy, where it is tunnelled and verified end to end.
- The gate parsed the URL while the fetch compared its text, so
  `HTTPS://mirror` passed the first and was refused by the second. The
  request is now made to the URL the gate parsed (`secure_url`).
- `no_downgrade_redirects` repeated the self-update's policy and stopped
  one redirect short of it. Both now use `redirects_unless` and
  `MAX_REDIRECTS`, which count as `Policy::limited(10)` does.
- "This machine" was defined twice, differently. `is_loopback_host` is the
  one rule, for the gem registry and for a grant's patch server URL:
  `localhost` or a loopback address. A name under `.localhost` is no
  longer taken for a grant URL either, since nothing makes the resolver
  answer it locally; `--patch-server-url` still admits such a server.
- Messages named the registry with the credentials of
  `SOCKET_RUBYGEMS_URL` in them: the remedy, "lists no ...", a sha256
  mismatch, an index that is not text, and the errors of the upstream
  checksum lookup. All are redacted now.
- The client test followed a redirect to a host name with an unguarded
  client. Every URL in these tests now names this machine, the refused
  ones too.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…stry client per run

After `rollback` / `remove` puts a gem back on its upstream entry, the
installed copy is judged against the upstream sha256. When the lock pins
none and `SOCKET_RUBYGEMS_URL` is refused (plain http, not this machine),
the lookup's refusal was dropped with `.ok()?`: the copy was neither
refreshed nor mentioned, while it still held the patched build.

`upstream_archive` now returns that refusal (`UpstreamArchive::Unverified`)
and the unwind reports each installed copy it could not judge under
`upstream_gem_stale_install`: "could not be checked against the upstream
gem", the reason, and the files to remove. It is not said to hold the
patched build, which nothing proves. The human warning counts such copies
apart from the ones known to be stale. A lookup that fails for another
reason (offline, a registry error) is passed over as before.

The refresh built a registry client for every URL of every stale gem. The
unwind now builds one for the run (`Source::Registry(&client)`), and none
when nothing is stale.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ctory check

`restore_original_build` refused a backup dir it could not probe, but
read any error on the backup's description file, one level below, as "no
backup", and an unreadable marker as "no marker". Rollback could then
report a copy as carrying nothing, or rebuild in place and delete the
marker, while the original build sat unrestored in its backup.

Only "not there" now says there is no backup and no marker. A description
file that cannot be probed, and a marker file that is there but cannot be
read or understood (another schema), fail the restore with the path and
the `gem pristine` remedy. A backup that is there is restored whatever
the marker says, as before.

The stage-directory guard of a rebuild and of a hosted refresh was the
same four-arm match written twice. Both call
`containment::ensure_dir_or_absent`.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…report

A request to a loopback gem registry never goes through a proxy; a name
under `.localhost` is not a loopback host; credentials in
`SOCKET_RUBYGEMS_URL` are sent and never printed; ten redirects at most.
After an unwind under a refused registry, an installed copy that could
not be checked is reported under `upstream_gem_stale_install`.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cursor

cursor Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

PR Summary

High Risk
Changes authentication-adjacent supply-chain behavior: gem downloads and checksum verification, redirect/proxy handling, and rollback refresh when SOCKET_RUBYGEMS_URL is insecure; misconfiguration can block refresh or leave installs unchecked rather than silently wrong.

Overview
This PR tightens how RubyGems traffic is fetched when gems are built or installed, and how the CLI reports stale installs when that trust chain breaks.

Registry transport. Gem source downloads, native-extension rebuilds, hosted refresh, and upstream checksum lookup now go through SecureRegistryClient: only https or plain http to loopback hosts are allowed, redirects cannot downgrade off https or jump to non-loopback http (10-hop cap), and loopback http bypasses proxies so traffic cannot be sent elsewhere in cleartext. SOCKET_RUBYGEMS_URL that fails this check is refused up front; errors and docs redact credentials.

Upstream unwind / rollback. When the registry cannot be used to learn the upstream sha256, installed copies are surfaced as unchecked (upstream_gem_stale_install / Why::Unverified) with explicit human and JSON wording, instead of being silently skipped or assumed stale.

Filesystem safety. Stage dirs, backups, markers, and extension paths use try_entry_type / ensure_dir_or_absent: probe failures (permissions, ENOTDIR) are treated like unsafe links—operations refuse rather than assuming “nothing there,” including rollback and own-source proof paths.

Contract docs (CLI_CONTRACT.md, docs/ecosystems.md) and e2e tests cover refused http registries, proxy bypass, and unchecked install reporting.

Reviewed by Cursor Bugbot for commit f8e8054. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit f8e8054. Configure here.

After a hosted rollback whose registry was refused, an installed copy
that could not be checked was listed only when its archive was still
readable in the gem home's cache. With the cache pruned, as slim
images have it, the copy was passed over and the patched build stayed
in use with no warning.

A copy that cannot be judged is now listed whether or not its archive
is cached. A copy with a known upstream checksum is judged as before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: final review of 701acfa found one product decision for a person.

Question: should rollback / remove of a hosted gem pin still re-pin its CHECKSUMS sha256 when SOCKET_RUBYGEMS_URL is a plain-http mirror on another host (e.g. http://gemstash:9292)?

  • On main (and in the released v5.0 upstream restore), rubygems_sha256 reads {rubygems_base()}/info/<name> over any scheme. Here it goes through rubygems_secure_base() ([client.rs:119](https://github.com/SocketDev/socket-patch/blob/701acfa653a2256fbce02b9e78eb659429cf1c27/crates/socket-patch-core/src/patch/redirect/upstream/client.rs#L119), [client.rs:616](https://github.com/SocketDev/socket-patch/blob/701acfa653a2256fbce02b9e78eb659429cf1c27/crates/socket-patch-core/src/patch/redirect/upstream/client.rs#L616)), so the lookup errors and restore_one refuses the pin ([gem.rs:1246](https://github.com/SocketDev/socket-patch/blob/701acfa653a2256fbce02b9e78eb659429cf1c27/crates/socket-patch-core/src/patch/redirect/upstream/gem.rs#L1246)). CLI_CONTRACT.md says so ("a pin whose CHECKSUMS entry must be re-pinned is not restored"); the PR description's "What a reviewer should weigh" says rollback "restores the lock", which is true only when no re-pin is needed.
  • That checksum is only written into the lock. Bundler then downloads from the lock's own remote: (rubygems.org over https) and verifies against it, so a tampered checksum makes install fail; it doesn't get attacker code built. The bytes that are installed after an unwind already go through SecureRegistryClient.
  • Options: (a) keep https-only for anything built or installed (gem_source, gem_refresh), but let the lock re-pin read the checksum from the configured base as before (a small change at the two lines above); or (b) keep the refusal on purpose, and say so in the description as a behaviour change for released rollback, not only for unreleased Rebuild native gem extensions from the source gem, precompiled gems included #2098.

Everything else looks sound: the scheme and redirect rules, the proxy split for loopback, credential redaction and the fail-closed probes are tested. CHANGELOG.md is untouched. ci-ok and clippy are green on 701acfa. Smaller notes, none blocking:

  • With a plain-http mirror and a lock without CHECKSUMS (bundler < 2.6), every gem rollback now warns upstream_gem_stale_install ("could not be checked"), even for a copy installed after the unwind. The exit code is still 0.
  • grant.rs no longer treats http://<name>.localhost as loopback. The description flags this.
  • A marker with an unknown schema (written by a newer socket-patch) now fails restore_original_build, where before it returned "nothing to restore".

Labelled agent:needs-human. Once the question is answered, the shepherd will apply (a) or note (b) and post the approver brief.


Generated by Claude Code

Main gained the in-tree build products fix (#2108) and the hosted scan's
refresh of the archive beside an agent-patched gem (#2103). Both touch
files this branch changes; three hunks conflicted, each resolved by
keeping both sides.

crates/socket-patch-core/src/patch/gem_ext.rs,
`recover_interrupted_rebuild_sync`: this branch's `refuse_link(&ext, ..)`
(a link, or an extension dir that cannot be probed, is refused) in place
of main's unchanged `is_symlink` check, followed by main's new restore of
the gem's tree from the snapshot of an interrupted in-place build.

crates/socket-patch-cli/src/commands/gem_refresh.rs, `report`: main's two
headlines for gems patched in place become arms of this branch's `known`
headline, which the count of copies that could not be checked is then
added to. The words "the switch to hosted patches" come from `switch`.

crates/socket-patch-cli/src/commands/gem_refresh.rs,
`stale_after_restore`: this branch's listing of a copy that cannot be
judged whether or not its archive is cached, with main's new
`in_place: false` field.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The in-tree build products fix (#2108) acts on what a listing lacks: a
file the tree snapshot does not show is removed when a build is undone,
and a rollback removes what the marker lists and the backup does not
hold. Its probes read an error as "nothing there":

- `restore_tree` listed the snapshot's copies with a walk that skips
  what it cannot read. A snapshot whose copies could not be listed was
  read as holding none, and every file of the gem's own tree was removed.
  `snapshot_tree` listed the gem's tree the same way, so a directory that
  could not be read was left out and its files were the next undo's to
  remove. Both now use `try_tree_files`, which fails instead.
- Recovery took a snapshot dir it could not probe for no snapshot and
  settled the extension dir without putting the tree back; so did the
  failed-snapshot path of `rebuild`. `build_committed_since` took a marker
  it could not probe for no marker. Each is an error now.
- `back_up_tree_changes` and `note_absent_files` took a backup
  description that is there and cannot be read for no backup, recorded
  nothing, and let the build be committed. `try_read_backup_info` fails
  instead, and the build is undone.

The marker and backup formats are as #2108 left them, and
`read_marker_strict` and the restore read them unchanged.

The contract still said an in-place rebuild leaves a library in the gem's
own tree as last built, next to #2108's account of backing it up. That
sentence now says what the code does.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Recovery of an interrupted in-place build puts the gem's own tree back
from the snapshot beside the parked build, file by file, by path. It runs
first on every native apply, gem rollback and hosted refresh, and nothing
vouches for a snapshot it finds: a gem home can come from the repository
being scanned. A snapshot planted there, naming a file under a link in
the gem dir, had that file created or replaced wherever the link points.

A snapshot this module takes holds no file under a link, since links are
not walked when it is taken. `restore_tree` now checks every path it is
about to write for a linked level below the gem dir before it removes or
writes anything, and refuses the snapshot when there is one.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Nov 17, 2026
Main gained the vendored unwind's refresh of an install a hosted scan
patched (#2113). It reworks the unwind refresh this branch changed, and
two files conflicted. Each hunk keeps both sides.

crates/socket-patch-cli/src/commands/gem_refresh.rs:

- `stale_after_restore` takes main's `installed_with_archives` walk and
  its `Scope`, and keeps this branch's `Option<sha256>` map: a copy with
  no upstream sha256 to judge it by is listed whether or not its archive
  is cached, one with a sha256 is judged by the archive as before.
- `after_restore` and main's new `after_vendored_unwind` both run
  `refresh_restored`, which keeps this branch's handling of
  `UpstreamArchive` (a refused registry is reported, not dropped) and its
  one `SecureRegistryClient` per run, with main's rule that a vendored
  unwind judges only a gem the lock resolves from rubygems.org. A refused
  lookup only comes back for such a gem, so that rule holds for the
  unchecked copies too.

crates/socket-patch-cli/CLI_CONTRACT.md:

- The unwind direction paragraph keeps this branch's account of a copy
  that could not be checked and ends with main's sentence on a copy left
  by `--no-gem-install-refresh`.
- The `upstream_gem_stale_install` row takes main's vendored unwind
  columns and keeps this branch's "could not be checked" meaning.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…used registry

The vendored unwind (#2113) now runs the same refresh as the hosted one,
so it reports an installed copy it could not judge when the lock pins no
sha256 and `SOCKET_RUBYGEMS_URL` is refused. An e2e holds that for
`rollback`, `remove` and `vendor --revert`: the gem home is unchanged, no
registry is asked, and the copy is named with the reason. The contract's
paragraph on a vendored unwind says so.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants