Skip to content

Fix vendored mode failing for a gem patch that names the gem's own gemspec - #2485

Open
Mikola Lysenko (mikolalysenko) wants to merge 2 commits into
mainfrom
fix/vendored-gem-recorded-gemspec
Open

Mikola Lysenko (mikolalysenko) wants to merge 2 commits into
mainfrom
fix/vendored-gem-recorded-gemspec

Conversation

@mikolalysenko

Copy link
Copy Markdown
Collaborator

scan --mode vendored fails for a gem patch that names the gem's own <name>.gemspec. This fixes it, so the patch for octopoller 0.2.0 (GHSA-26qj-cr27-r5c4) can be vendored.

Why

  • The failure. With 5.2.0, socket-patch scan --mode vendored on a project that locks octopoller 0.2.0 exits 1 with vendor_prebuilt_required: "prebuilt .gem for octopoller extracted to an unexpected layout (patched files absent at their recorded paths)". Nothing is vendored. Agent mode and hosted mode apply the same patch correctly.
  • The cause. That patch fixes the gem's file permissions, so it names every file the gem ships, each with unchanged content. Octopoller ships octopoller.gemspec at its root, as most gems built from bundler's template do. A vendored copy carries the stub gemspec at exactly that path, because bundler evaluates <name>.gemspec of a path source. Vendored mode wrote the stub first and then compared that path with the hash the patch records.
  • Not only permissions patches. Any patch that names a gem's root <name>.gemspec hit the same check.

What changes

  • The download is checked before the stub is written. The served gem is held to every file of the patch while its tree is still the gem's own files. This applies to a gem's own tree and to each platform gem's tree. A served gem whose own gemspec is not the recorded one is still refused.
  • The vendored copy is not held to the patch's hash at the stub's path. This applies to the in-sync test of a re-run and to vendor --check, vex and repair. Without it the first vendor would pass, every later run would rebuild the copy, and vex would not attest it. The match is exact: <name>.gemspec at the root of the tree, nothing deeper.
  • The stub stays covered. Its bytes are in the ledger's inventory, which vendor --check and vex compare the whole tree with, and a re-run still requires a valid stub.
  • No silent pass. A copy whose patch names nothing but that gemspec has no patched file left to hash. vendor --check and vex do not attest it without an inventory (no_files), and a re-run holds it to the inventory and rebuilds it when a file changed or there is none.
  • The run's result lists the files the copy is held to, without the gemspec the stub replaced.

Other ecosystems are untouched: the exemption is applied only to gem entries.

How it was checked

  • Against the live patch for octopoller 0.2.0, with a build of this branch, on macOS:
    • scan --mode vendored under umask 000, on a project whose installed copy had 22 group- and world-writable files: 22 files vendored, none group- or world-writable (0644/0755), and octopoller.gemspec in the copy is the stub.
    • A second scan reports already_vendored, with no warning and no rebuild.
    • bundle install succeeds and require "octopoller" loads from the vendored copy.
    • vendor --check verifies the artifact and wiring. vex writes one statement: not_affected for GHSA-26qj-cr27-r5c4.
    • After a patched file is edited, vendor --check exits 1 with vendor_hash_mismatch, repair restores it and the check passes again. After the copy is deleted, scan restores it.
    • rollback restores the Gemfile and the lock.
  • New tests (vendor::gem):
    • A patch naming the gem's own gemspec vendors with the stub in place; a second run asks the service nothing and rebuilds nothing; the copy verifies; a tampered stub fails through the inventory and a tampered patched file fails by its hash.
    • A served gem whose own gemspec is not the recorded one is refused with nothing written.
    • A patch naming only that gemspec is not attested without an inventory, and a gemspec of the same name below the root is still held to its hash.
    • A re-run over such a copy is a no-op while it matches the inventory, and rebuilds it after a file is changed or when the ledger has no entry.
    • The platform-gem twin: vendors, stays in sync, verifies healthy, and refuses a platform gem whose own gemspec differs.
    • With the exemption switched off, the tests of the vendored copy fail; the test of the unchanged refusal still passes.
  • cargo test -p socket-patch-core --lib: 9,027 passed. cargo clippy --locked --workspace --all-features -- -D warnings and cargo fmt --check are clean.
  • Not run locally: the CLI's end-to-end suites and the Windows jobs. The platform-gem path was checked by its tests only, not against a live patch.

Review

/code-review high on this branch found no fault in the two download checks and five edge cases. Four are fixed in the second commit: the re-run over a copy whose patch names only the gemspec, a ledger name with a / in it, the result's file list, and three stale comments. The fifth is the case-insensitive filesystem gap below. I left it because the copy of such a gem already differs between filesystems today (one file on macOS and Windows, two on Linux), and making the trees agree changes what existing vendored copies are restored to. Before this change such a gem could not be vendored at all when its patch names the gemspec, so this is not a regression.

Known gaps, not changed here

  • A .gitignore the patch names. The vendored copy empties a .gitignore the gem ships when its rules would hide a file of the copy, unless the patch names that .gitignore. A permissions patch names it, so for a gem whose .gitignore hides one of its own shipped files the copy is refused as gitignored. Octopoller is not affected.
  • rollback wording. For this patch, rollback prints both "1 gem still holds the patched build on disk" and "Replaced the installed copy of 1 gem with the upstream build". The patched and upstream files have the same content, so the first message is misleading. It predates this change.
  • Case-insensitive filesystems. The exemption matches the gem's name exactly. For a gem that ships its gemspec under another spelling of its name (redcloth.gemspec for RedCloth), on macOS or Windows the stub overwrites that file, so a patch that names it vendors once and then fails vendor --check and is rebuilt on every run.

🤖 Generated with Claude Code

A patch that fixes a gem's file permissions names every file the gem
ships. Many gems ship `<name>.gemspec` at their root, the path where a
vendored copy carries the stub gemspec bundler evaluates for a path
source. Vendored mode wrote the stub and then compared that path with
the hash the patch records, so `scan --mode vendored` failed with
`vendor_prebuilt_required` ("extracted to an unexpected layout") for
octopoller 0.2.0 (GHSA-26qj-cr27-r5c4), and left nothing vendored.

- The downloaded gem is now held to every file of the patch before the
  stub is written, for a gem's own tree and for each platform gem's
  tree. A served gem whose own gemspec is not the recorded one is still
  refused.
- The vendored copy is no longer held to the patch's hash at the stub's
  path: in the in-sync test of a re-run, and in `vendor --check`, `vex`
  and `repair`. The stub's bytes stay covered by the ledger's inventory.
  Without that exemption every later run would rebuild the copy and
  `vex` would not attest it.
- A copy whose patch names nothing but that gemspec, and whose ledger
  entry has no inventory, is not attested.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Review follow-ups for the vendored gem fix:

- A re-run over a copy whose patch names nothing but the gem's own
  `<name>.gemspec` had no file left to hash and called any existing
  copy in sync. It now holds such a copy to the ledger's inventory and
  rebuilds it when a file changed or the ledger has none.
- Only a plain gem name names a stub path, so a ledger entry whose
  name holds a `/` cannot exempt a file below the root.
- The run's result lists the files the copy is held to, without the
  gemspec the stub replaced.
- Reworded three comments that said a gem never ships a
  `<name>.gemspec`, or that every file of the patch is hashed.

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

cursor Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches gem vendoring, verification, and attestation paths; behavior changes for patches that include root gemspec entries, though scoped to the gem ecosystem.

Overview
Fixes vendored gem flow when a patch lists the gem’s root <name>.gemspec (common for permission-only patches). The served tree is validated against patch hashes before the stub is written, and vendored copies are no longer required to match the patch at that path.

Adds files_beside_stub and wires it through in-sync checks, platform trees, preview/result file lists, and verify_vendored_patch_record. Stub bytes remain covered by the ledger inventory; copies with only that gemspec in the patch are not attested without an inventory (no_files), and re-runs can still reconcile via inventory.

New vendor::gem tests cover service vendoring, refusal when the shipped gemspec differs, inventory-only patches, re-run behavior, and platform-gem trees.

Reviewed by Cursor Bugbot for commit 218db94. Configure here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant