Skip to content

Fix base64 checksums in a gem compact index (#2285) - #2288

Open
Mikola Lysenko (mikolalysenko) wants to merge 5 commits into
mainfrom
agent/fix-gem-compact-index-base64-checksum
Open

Mikola Lysenko (mikolalysenko) wants to merge 5 commits into
mainfrom
agent/fix-gem-compact-index-base64-checksum

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

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

Fixes #2285

Summary

A gem server built with gem generate_index writes the sha256 in its compact index (info/<name>) as base64. rubygems.org writes hex. Bundler reads both and pins the hex digest in CHECKSUMS. socket-patch read only hex, so behind such a server:

  • rollback / remove of a hosted gem pin refused with "lists no ruby-platform 3.2.9 with a checksum", although SOCKET_RUBYGEMS_URL named the server and the index listed one;
  • the agent-mode native build could not download the source gem, or a gem the build lacks, from that server.

Both readers now take the digest the way Bundler's Checksum.to_hexdigest does: 64 hex digits, or 43 base64 digits (standard or URL-safe alphabet) and one =, converted to lowercase hex. Anything Bundler refuses as a digest is still "no checksum", so the refusal stays for an index that really has none. No lock, manifest or output format changes.

Root cause

Two functions parse a compact-index line, and both passed the checksum: value through sha256_hex, which accepts only 64 hex digits:

  • compact_index_checksum (patch/redirect/upstream/client.rs), used by the hosted unwind and by the source-gem download;
  • pick_release (patch/gem_source.rs), used when the native build needs a gem the home lacks.

The fix is one helper, utils::digest::sha256_hex_or_base64, called by both.

Evidence

Test Before After
compact_index_checksum_reads_a_base64_digest_as_bundler_does (core, the line gem generate_index wrote for rack 3.2.9, both alphabets, six malformed digests) fails passes
hosted_unwind_restores_a_mirror_gem_whose_index_checksum_is_base64 (e2e_redirect_gem_stale_install: scan, then rollback and remove with SOCKET_RUBYGEMS_URL on a base64 index; pair byte for byte, upstream gem back) fails with the issue's message, exit 1 passes
pick_release_takes_the_newest_release_that_fits (base64 case added) fails passes
a_compact_index_digest_is_read_as_hex_or_base64 (the new helper) new passes

The red commits come before the fix commits in the history. The accepted base64 shape was checked against Bundler 4.0.18's checksum.rb and Ruby 3.3.6's strict decoder (unpack1("m0")): no padding, ==, or bits set past the 32nd byte are refused there and here.

Run locally:

  • cargo clippy --locked --workspace --all-features -- -D warnings (the CI command): clean. With --all-targets there are lint errors in test code that are on main too; none is in a file this PR touches.
  • cargo test -p socket-patch-core and cargo test -p socket-patch-cli --lib --bins: pass.
  • cargo test -p socket-patch-cli --test e2e_redirect_gem_stale_install: 68 passed. e2e_gem_native_ext_build: 43 passed, 11 ignored.

Docs: the SOCKET_RUBYGEMS_URL row of CLI_CONTRACT.md and "Ruby gems: gems already installed" in docs/ecosystems.md now say both encodings are read.

Review

/code-review high found three things:

  1. pick_release was a second hex-only reader. Fixed, with a test.
  2. The e2e test's URL-safe leg served the same index as the standard leg (the fixture gem's digest has no + or /). The leg is removed; the unit tests cover the alphabet.
  3. Not fixed, and not new: a hex digest written in uppercase is lowercased before it is pinned, where Bundler keeps its case. No known server writes uppercase hex, and changing it would touch every hex reader, so it is left out of this PR.

Follow-ups

https://claude.ai/code/session_011f9aePYSz8M6vqhE3wfJ3t


Generated by Claude Code

Refs #2285

Assisted-by: Claude Code:claude-fable-5-2
A static index built with `gem generate_index` writes the sha256 of
a gem as base64. Bundler reads it and pins the hex digest. The unit
test and the hosted unwind test fail until the reader does the same.

Refs #2285

Assisted-by: Claude Code:claude-fable-5-2
A static index built with `gem generate_index` writes the sha256 of
a gem as base64 where rubygems.org writes hex. Bundler converts it
and pins the hex digest in CHECKSUMS. The reader took only hex, so
behind such a server SOCKET_RUBYGEMS_URL could not restore a hosted
pin, and the agent-mode source gem download found no checksum.

Read the digest as Bundler's Checksum.to_hexdigest does: 64 hex
digits, or 43 base64 digits of either alphabet and one `=`.
Anything else is still no checksum.

Fixes #2285

Assisted-by: Claude Code:claude-fable-5-2
The agent-mode build picks a missing gem's release from the compact
index with a second reader, which also takes only a hex digest. The
hosted unwind test drops its URL-safe leg: the fixture gem's digest
has no `+` or `/`, so that leg served the same index. The unit
tests cover the alphabet.

Refs #2285

Assisted-by: Claude Code:claude-fable-5-2
pick_release, the reader the agent-mode build uses for a gem the
home lacks, took only a hex `checksum:`, so behind a
`gem generate_index` server every release was skipped. It reads the
digest as compact_index_checksum does now.

Refs #2285

Assisted-by: Claude Code:claude-fable-5-2
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 26, 2026 17:49
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes upstream gem checksum parsing used for rollback/remove and native builds; behavior is aligned with Bundler but affects restore paths when SOCKET_RUBYGEMS_URL is set.

Overview
Fixes #2285 by teaching compact-index parsing to accept RubyGems checksum: values as either lowercase hex (rubygems.org) or the 44-character base64 form produced by gem generate_index, normalizing both to hex the way Bundler’s Checksum.to_hexdigest does.

A new sha256_hex_or_base64 helper in digest.rs replaces hex-only parsing in compact_index_checksum (hosted unwind / CHECKSUMS restore) and pick_release (native-extension source resolution). Invalid digests are still rejected with the same semantics as before.

Unit tests cover standard and URL-safe base64, malformed digests, and pick_release with a base64 index line; an e2e case verifies hosted rollback/remove restores lockfiles and the installed gem when SOCKET_RUBYGEMS_URL points at a mirror with base64 checksums. CLI_CONTRACT and ecosystems docs note both encodings.

Reviewed by Cursor Bugbot for commit 8284fa9. 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.

✅ Bugbot reviewed your changes and found no new issues!

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

Reviewed by Cursor Bugbot for commit 8284fa9. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] hosted-e2e is red on 8284fa9 and it is not this PR's: the only failing test is gem_bundler_hosted_install_proof (15 passed, 1 failed, on all three in-job attempts), with "the redirect wired patch 146ef6aa-…, which is not in the pinned any-of set". That is #2270, which fails the same way on main's latest CI run and on every other open PR. This PR does not touch that test or the hosted scan path. Once #2270 is fixed on main, a re-run of hosted-e2e here should be all that is needed.


Generated by Claude Code

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does
A gem server built with gem generate_index writes the sha256 in its compact index as base64, not hex. socket-patch read only hex, so rollback / remove of a hosted gem pin behind such a server refused with "lists no ... with a checksum", and the agent-mode native build couldn't fetch gems from it. Both compact-index readers now accept hex or Bundler-style base64 (43 digits plus one =, standard or URL-safe) and turn it into lowercase hex.

Risk: low. It only widens what's accepted as a digest. Hex input takes the same path as before. Malformed base64 (missing or doubled padding, trailing bits set, wrong length) still counts as "no checksum". The digest is still checked against the downloaded bytes, so a wrong decode can't let an unverified gem through. No lock, manifest or output format changes.

Look here

  • digest.rs:49: sha256_hex_or_base64, the new helper. The strict STANDARD engine at L67 refuses missing padding and trailing bits.
  • [client.rs:177](https://github.com/SocketDev/socket-patch/blob/8284fa98a7fb839f9821e8590a6cec06e539075a/crates/socket-patch-core/src/patch/redirect/upstream/client.rs#L177): compact_index_checksum (hosted unwind and source-gem download).
  • gem_source.rs:436: pick_release, the second reader (a gem the native build lacks).
  • [client.rs:1113](https://github.com/SocketDev/socket-patch/blob/8284fa98a7fb839f9821e8590a6cec06e539075a/crates/socket-patch-core/src/patch/redirect/upstream/client.rs#L1113) and [e2e_redirect_gem_stale_install.rs:4264](https://github.com/SocketDev/socket-patch/blob/8284fa98a7fb839f9821e8590a6cec06e539075a/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs#L4264): unit test with the real gem generate_index line for rack 3.2.9, and an end-to-end unwind through the binary.

Verified

Changes I made: none.

Open questions: none. The description notes that uppercase hex is lowercased where Bundler keeps its case. That isn't new, and no known server writes uppercase hex.


Generated by Claude Code

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hosted gem rollback behind a gem generate_index mirror still refuses with SOCKET_RUBYGEMS_URL set: a base64 checksum: in the compact index is not read

2 participants