From ac8ef4d8146aa7fed4b27629c4a386a1034937f2 Mon Sep 17 00:00:00 2001 From: Tanmay Singla Date: Fri, 20 Nov 2026 11:36:30 -0500 Subject: [PATCH 1/3] Find installed gems that ship no lib/ dir A gem whose files sit at its root, with no lib/ and no .gemspec of its own (cassandra-web packs app.rb, app/ and bin/), was never found: the Ruby crawler took a gem dir as installed only when it held lib/ or a *.gemspec. RubyGems writes such a gem's spec only to /specifications/.gemspec, so apply exited 1 with "The targeted manifest patch matched no installed package", and scan, vex and rollback missed the gem too. A gem dir now also counts when its gem home has a gemspec for it (specifications/ or specifications/default/), the file RubyGems and Bundler load the gem by. A lib-less dir with no such spec is still not a gem. Assisted-by: Claude Code:claude-opus-5-5 --- .../tests/apply/gem_without_lib_dir.rs | 101 ++++++++++++++++++ crates/socket-patch-cli/tests/apply/main.rs | 1 + .../src/crawlers/ruby_crawler.rs | 58 +++++++++- 3 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 crates/socket-patch-cli/tests/apply/gem_without_lib_dir.rs diff --git a/crates/socket-patch-cli/tests/apply/gem_without_lib_dir.rs b/crates/socket-patch-cli/tests/apply/gem_without_lib_dir.rs new file mode 100644 index 000000000..946874d4f --- /dev/null +++ b/crates/socket-patch-cli/tests/apply/gem_without_lib_dir.rs @@ -0,0 +1,101 @@ +//! A gem that ships neither `lib/` nor a `.gemspec` in its own dir. +//! +//! `cassandra-web` packs `app.rb`, `app/` and `bin/` at its root; RubyGems +//! writes its spec only to `/specifications/.gemspec`. The +//! crawler judged an installed gem dir by `lib/` or an in-dir `.gemspec`, so +//! such a gem was never found and `apply` exited 1 with "The targeted +//! manifest patch matched no installed package", in agent mode and under +//! `--global --global-prefix /gems` alike. + +use std::path::{Path, PathBuf}; + +use serde_json::json; + +use crate::apply_fixture::{ + one_file, patch_record, run_gem, run_gem_apply, stage_blob, write_manifest, +}; +use crate::common::git_sha256; + +const PURL: &str = "pkg:gem/cassandra-web@0.5.0"; +const LEAF: &str = "cassandra-web-0.5.0"; +const ORIGINAL: &[u8] = b"class App < Sinatra::Base\n disable :protection\nend\n"; +const PATCHED: &[u8] = b"class App < Sinatra::Base\n set :protection, :except => [:json_csrf]\nend\n"; + +/// The gem home `/vendor/bundle/ruby/3.4.0` holding `gems//app.rb` +/// (and nothing else in the gem dir), with `specifications/.gemspec` +/// when `with_spec`, plus a manifest patching `app.rb`. Returns the home +/// and the installed file. +fn lib_less_gem(root: &Path, with_spec: bool) -> (PathBuf, PathBuf) { + let home = root.join("vendor/bundle/ruby/3.4.0"); + let gem_dir = home.join("gems").join(LEAF); + std::fs::create_dir_all(gem_dir.join("bin")).unwrap(); + std::fs::write(gem_dir.join("bin/cassandra-web"), b"#!/usr/bin/env ruby\n").unwrap(); + let file = gem_dir.join("app.rb"); + std::fs::write(&file, ORIGINAL).unwrap(); + let specs = home.join("specifications"); + std::fs::create_dir_all(&specs).unwrap(); + if with_spec { + std::fs::write(specs.join(format!("{LEAF}.gemspec")), b"# installed spec\n").unwrap(); + } + write_manifest( + root, + json!({ PURL: patch_record( + "63617373-616e-4472-a177-656200000000", + one_file("app.rb", ORIGINAL, PATCHED), + )}), + ); + stage_blob(root, &git_sha256(PATCHED), PATCHED); + (home, file) +} + +#[test] +fn a_gem_with_no_lib_dir_is_patched_by_its_installed_spec() { + let tmp = tempfile::tempdir().unwrap(); + let (_home, file) = lib_less_gem(tmp.path(), true); + + let (code, stdout, stderr) = run_gem_apply(tmp.path(), &[]); + assert_eq!(code, 0, "stdout={stdout} stderr={stderr}"); + assert_eq!(std::fs::read(&file).unwrap(), PATCHED); +} + +#[test] +fn a_gem_with_no_lib_dir_is_patched_under_a_global_prefix() { + let tmp = tempfile::tempdir().unwrap(); + let (home, file) = lib_less_gem(tmp.path(), true); + let cwd = tmp.path().to_str().unwrap(); + let prefix = home.join("gems"); + + let (code, stdout, stderr) = run_gem( + Path::new("."), + &[ + "apply", + "--offline", + "--ecosystems", + "gem", + "--cwd", + cwd, + "--global", + "--global-prefix", + prefix.to_str().unwrap(), + ], + &[], + ); + assert_eq!(code, 0, "stdout={stdout} stderr={stderr}"); + assert_eq!(std::fs::read(&file).unwrap(), PATCHED); +} + +/// Without its spec, a lib-less dir is still no installed gem: RubyGems +/// cannot load it, so `apply` finds nothing and writes nothing. +#[test] +fn a_lib_less_dir_with_no_installed_spec_is_not_a_gem() { + let tmp = tempfile::tempdir().unwrap(); + let (_home, file) = lib_less_gem(tmp.path(), false); + + let (code, stdout, stderr) = run_gem_apply(tmp.path(), &[]); + assert_eq!(code, 1, "stdout={stdout} stderr={stderr}"); + assert!( + stderr.contains("The targeted manifest patch matched no installed package"), + "stderr={stderr}" + ); + assert_eq!(std::fs::read(&file).unwrap(), ORIGINAL); +} diff --git a/crates/socket-patch-cli/tests/apply/main.rs b/crates/socket-patch-cli/tests/apply/main.rs index a9d9281ff..fad76bc59 100644 --- a/crates/socket-patch-cli/tests/apply/main.rs +++ b/crates/socket-patch-cli/tests/apply/main.rs @@ -28,6 +28,7 @@ mod check_verifies_installed_tree; mod cli_gem_variant_mismatch_policy; mod e2e_safety_advisories; mod gem_stale_bundler_plugin; +mod gem_without_lib_dir; mod in_process_gem_config_warning; mod in_process_gem_fallback_home; mod in_process_gem_multicopy; diff --git a/crates/socket-patch-core/src/crawlers/ruby_crawler.rs b/crates/socket-patch-core/src/crawlers/ruby_crawler.rs index 17093408a..f17c18ebd 100644 --- a/crates/socket-patch-core/src/crawlers/ruby_crawler.rs +++ b/crates/socket-patch-core/src/crawlers/ruby_crawler.rs @@ -1115,7 +1115,8 @@ impl RubyCrawler { } /// Verify that a directory looks like an installed gem. - /// Checks for a `.gemspec` file or a `lib/` directory. + /// Checks for a `lib/` directory, a `.gemspec` file, or the gemspec + /// RubyGems installed for the dir in its gem home. async fn verify_gem_at_path(&self, path: &Path) -> bool { if !is_dir(path).await { return false; @@ -1126,6 +1127,11 @@ impl RubyCrawler { return true; } + let copy = path.to_path_buf(); + if run_blocking(move || home_lists_copy(©) == Some(true)).await { + return true; + } + // Check for any .gemspec file for entry in list_dir_entries(path).await { if let Some(name) = entry.file_name().to_str() { @@ -1308,7 +1314,12 @@ fn locate_gem_dirs_sync( } /// Blocking twin of `RubyCrawler::verify_gem_at_path`: a directory holding -/// `lib/` or a `.gemspec`. +/// `lib/` or a `.gemspec`, or one its gem home has a gemspec for. +/// +/// A gem need not ship either: `cassandra-web` packs `app.rb`, `app/` and +/// `bin/` at its root, and RubyGems writes its spec only to +/// `/specifications/.gemspec`. That spec is what RubyGems and +/// Bundler load the gem by, so it proves the dir installed. fn verify_gem_at_path_sync(path: &Path) -> bool { use crate::utils::fs::is_dir_sync; if !is_dir_sync(path) { @@ -1317,6 +1328,9 @@ fn verify_gem_at_path_sync(path: &Path) -> bool { if is_dir_sync(&path.join("lib")) { return true; } + if home_lists_copy(path) == Some(true) { + return true; + } super::listing::list_dir_sync(path).iter().any(|entry| { entry .name @@ -6810,6 +6824,46 @@ mod tests { assert!(!crawler.verify_gem_at_path(&gem_dir).await); } + /// A gem that ships neither `lib/` nor a `.gemspec` (`cassandra-web`: + /// `app.rb`, `app/`, `bin/` at its root) is installed when its gem home + /// has `specifications/.gemspec`, for the PURL lookup and the + /// scan alike; without that spec the dir is still no gem. + #[tokio::test] + async fn verify_gem_lib_less_dir_listed_by_its_home_spec() { + let home = tempfile::tempdir().unwrap(); + let gems = home.path().join("gems"); + let gem_dir = gems.join("cassandra-web-0.5.0"); + tokio::fs::create_dir_all(gem_dir.join("app")).await.unwrap(); + tokio::fs::write(gem_dir.join("app.rb"), b"# app").await.unwrap(); + let specs = home.path().join("specifications"); + tokio::fs::create_dir_all(&specs).await.unwrap(); + + let crawler = RubyCrawler::new(); + let purls = vec!["pkg:gem/cassandra-web@0.5.0".to_string()]; + let options = CrawlerOptions { + cwd: home.path().to_path_buf(), + global: false, + global_prefix: Some(gems.clone()), + }; + assert!(!crawler.verify_gem_at_path(&gem_dir).await); + assert!(crawler.find_by_purls(&gems, &purls).await.unwrap().is_empty()); + assert!(crawler.crawl_all(&options).await.is_empty()); + + tokio::fs::write(specs.join("cassandra-web-0.5.0.gemspec"), b"# spec") + .await + .unwrap(); + assert!(crawler.verify_gem_at_path(&gem_dir).await); + let found = crawler.find_by_purls(&gems, &purls).await.unwrap(); + assert_eq!(found["pkg:gem/cassandra-web@0.5.0"].path, gem_dir); + let scanned: Vec<_> = crawler + .crawl_all(&options) + .await + .into_iter() + .map(|p| p.purl) + .collect(); + assert_eq!(scanned, ["pkg:gem/cassandra-web@0.5.0"]); + } + /// `locate_gem_dirs_sync`' platform-dir scan must skip a `--*` dir /// that matches the prefix but fails gem verification (a hollow /// platform dir — no `lib/`, no `.gemspec`): alone it resolves From 213bf0a5dc6ab4f516e875e7e5fee308f54ec8f7 Mon Sep 17 00:00:00 2001 From: Tanmay Singla Date: Fri, 20 Nov 2026 11:49:36 -0500 Subject: [PATCH 2/3] Skip default gems' stub dirs, share one gem check A default gem's spec sits in specifications/default/, and its gems// dir holds only its executables while its code lives in Ruby's own lib dir. Counting that dir as an installed copy would aim a patch at files Ruby never loads, so only the installed specifications/.gemspec makes a lib-less dir a gem. The scan's async check now runs the same function as the PURL lookup instead of a copy of it, and comments that named only lib/ and .gemspec say what the check accepts. Assisted-by: Claude Code:claude-opus-5-5 --- .../src/crawlers/ruby_crawler.rs | 90 ++++++++++--------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/crates/socket-patch-core/src/crawlers/ruby_crawler.rs b/crates/socket-patch-core/src/crawlers/ruby_crawler.rs index f17c18ebd..47ada1e12 100644 --- a/crates/socket-patch-core/src/crawlers/ruby_crawler.rs +++ b/crates/socket-patch-core/src/crawlers/ruby_crawler.rs @@ -262,7 +262,8 @@ impl RubyCrawler { // coordinate is a single path segment, so reject any that // could traverse out of the gem root (`..`/`.`, a separator, // an absolute path, NUL). `verify_gem_at_path` only checks - // for `lib/`/`.gemspec` and gems patch in place, so fail + // for `lib/`/`.gemspec` (in the dir, or the home's + // `specifications/`) and gems patch in place, so fail // closed here — same as the deno/go/maven/npm/nuget guards. if !path_safety::is_safe_name_version(name, version) { continue; @@ -1090,7 +1091,7 @@ impl RubyCrawler { // Parse name-version from directory name if let Some((name, version)) = Self::parse_dir_name_version(&dir_name_str) { - // Verify it looks like a gem (has .gemspec or lib/) + // Verify it looks like an installed gem if !self.verify_gem_at_path(&gem_dir).await { continue; } @@ -1114,34 +1115,11 @@ impl RubyCrawler { results } - /// Verify that a directory looks like an installed gem. - /// Checks for a `lib/` directory, a `.gemspec` file, or the gemspec - /// RubyGems installed for the dir in its gem home. + /// Verify that a directory looks like an installed gem + /// ([`verify_gem_at_path_sync`] on the blocking pool). async fn verify_gem_at_path(&self, path: &Path) -> bool { - if !is_dir(path).await { - return false; - } - - // Check for lib/ directory - if is_dir(&path.join("lib")).await { - return true; - } - - let copy = path.to_path_buf(); - if run_blocking(move || home_lists_copy(©) == Some(true)).await { - return true; - } - - // Check for any .gemspec file - for entry in list_dir_entries(path).await { - if let Some(name) = entry.file_name().to_str() { - if name.ends_with(".gemspec") { - return true; - } - } - } - - false + let path = path.to_path_buf(); + run_blocking(move || verify_gem_at_path_sync(&path)).await } /// Parse a gem directory name into its base `(name, version)`. @@ -1313,22 +1291,21 @@ fn locate_gem_dirs_sync( dirs } -/// Blocking twin of `RubyCrawler::verify_gem_at_path`: a directory holding -/// `lib/` or a `.gemspec`, or one its gem home has a gemspec for. +/// Whether `path` looks like an installed gem dir: it holds `lib/` or a +/// `.gemspec`, or its gem home has `specifications/.gemspec`. /// /// A gem need not ship either: `cassandra-web` packs `app.rb`, `app/` and /// `bin/` at its root, and RubyGems writes its spec only to -/// `/specifications/.gemspec`. That spec is what RubyGems and -/// Bundler load the gem by, so it proves the dir installed. +/// `/specifications/.gemspec`, the file RubyGems and Bundler +/// load the gem by. A spec under `specifications/default/` does not count: +/// a default gem's `gems//` dir holds only its executables, while its +/// code lives in Ruby's own lib dir. fn verify_gem_at_path_sync(path: &Path) -> bool { use crate::utils::fs::is_dir_sync; if !is_dir_sync(path) { return false; } - if is_dir_sync(&path.join("lib")) { - return true; - } - if home_lists_copy(path) == Some(true) { + if is_dir_sync(&path.join("lib")) || home_installed_spec(path) { return true; } super::listing::list_dir_sync(path).iter().any(|entry| { @@ -1611,6 +1588,23 @@ fn home_lists_copy(copy: &Path) -> Option { listed } +/// Whether the gem home of the gem dir `/gems/` has the +/// installed (non-default) gemspec `specifications/.gemspec`. +fn home_installed_spec(copy: &Path) -> bool { + let (Some(leaf), Some(gems)) = (copy.file_name(), copy.parent()) else { + return false; + }; + if gems.file_name() != Some(OsStr::new("gems")) { + return false; + } + let Some(home) = gems.parent() else { + return false; + }; + let mut spec = leaf.to_os_string(); + spec.push(".gemspec"); + home.join("specifications").join(spec).is_file() +} + /// Whether a standalone bundle's `bundler/setup.rb` names the gem dir /// `/gems/`, or cannot be read to tell. The file sits in the /// bundle path: `/bundler/` for a flat home, two levels up for the @@ -6607,9 +6601,10 @@ mod tests { /// A tampered manifest PURL whose name carries `..` must not resolve /// to a directory outside the gem root. `locate_gem_dirs_sync` joins /// `-` straight onto `gem_path`, and - /// `verify_gem_at_path` only checks for `lib/`/`.gemspec`, so without - /// a coordinate gate `pkg:gem/../outside@1.0.0` escapes the gem store - /// and the patch applies in place out of tree. + /// `verify_gem_at_path` only checks for `lib/`/`.gemspec` (in the dir, + /// or the home's `specifications/`), so without a coordinate gate + /// `pkg:gem/../outside@1.0.0` escapes the gem store and the patch + /// applies in place out of tree. #[tokio::test] async fn find_by_purls_rejects_traversal_coordinates() { let dir = tempfile::tempdir().unwrap(); @@ -6827,7 +6822,8 @@ mod tests { /// A gem that ships neither `lib/` nor a `.gemspec` (`cassandra-web`: /// `app.rb`, `app/`, `bin/` at its root) is installed when its gem home /// has `specifications/.gemspec`, for the PURL lookup and the - /// scan alike; without that spec the dir is still no gem. + /// scan alike; without that spec, or with only a + /// `specifications/default/` one, the dir is still no gem. #[tokio::test] async fn verify_gem_lib_less_dir_listed_by_its_home_spec() { let home = tempfile::tempdir().unwrap(); @@ -6849,6 +6845,18 @@ mod tests { assert!(crawler.find_by_purls(&gems, &purls).await.unwrap().is_empty()); assert!(crawler.crawl_all(&options).await.is_empty()); + // A default gem's spec does not make its executables-only dir a copy. + tokio::fs::create_dir_all(specs.join("default")).await.unwrap(); + tokio::fs::write( + specs.join("default").join("cassandra-web-0.5.0.gemspec"), + b"# default spec", + ) + .await + .unwrap(); + assert!(!crawler.verify_gem_at_path(&gem_dir).await); + assert!(crawler.find_by_purls(&gems, &purls).await.unwrap().is_empty()); + assert!(crawler.crawl_all(&options).await.is_empty()); + tokio::fs::write(specs.join("cassandra-web-0.5.0.gemspec"), b"# spec") .await .unwrap(); From 11db2f3099d647adecc47d418c502fbb8da2c724 Mon Sep 17 00:00:00 2001 From: Tanmay Singla Date: Fri, 20 Nov 2026 11:52:37 -0500 Subject: [PATCH 3/3] Format the lib-less gem tests Assisted-by: Claude Code:claude-opus-5-5 --- .../tests/apply/gem_without_lib_dir.rs | 3 ++- .../src/crawlers/ruby_crawler.rs | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/crates/socket-patch-cli/tests/apply/gem_without_lib_dir.rs b/crates/socket-patch-cli/tests/apply/gem_without_lib_dir.rs index 946874d4f..fd26d2765 100644 --- a/crates/socket-patch-cli/tests/apply/gem_without_lib_dir.rs +++ b/crates/socket-patch-cli/tests/apply/gem_without_lib_dir.rs @@ -19,7 +19,8 @@ use crate::common::git_sha256; const PURL: &str = "pkg:gem/cassandra-web@0.5.0"; const LEAF: &str = "cassandra-web-0.5.0"; const ORIGINAL: &[u8] = b"class App < Sinatra::Base\n disable :protection\nend\n"; -const PATCHED: &[u8] = b"class App < Sinatra::Base\n set :protection, :except => [:json_csrf]\nend\n"; +const PATCHED: &[u8] = + b"class App < Sinatra::Base\n set :protection, :except => [:json_csrf]\nend\n"; /// The gem home `/vendor/bundle/ruby/3.4.0` holding `gems//app.rb` /// (and nothing else in the gem dir), with `specifications/.gemspec` diff --git a/crates/socket-patch-core/src/crawlers/ruby_crawler.rs b/crates/socket-patch-core/src/crawlers/ruby_crawler.rs index 47ada1e12..c829bc7fd 100644 --- a/crates/socket-patch-core/src/crawlers/ruby_crawler.rs +++ b/crates/socket-patch-core/src/crawlers/ruby_crawler.rs @@ -6829,8 +6829,12 @@ mod tests { let home = tempfile::tempdir().unwrap(); let gems = home.path().join("gems"); let gem_dir = gems.join("cassandra-web-0.5.0"); - tokio::fs::create_dir_all(gem_dir.join("app")).await.unwrap(); - tokio::fs::write(gem_dir.join("app.rb"), b"# app").await.unwrap(); + tokio::fs::create_dir_all(gem_dir.join("app")) + .await + .unwrap(); + tokio::fs::write(gem_dir.join("app.rb"), b"# app") + .await + .unwrap(); let specs = home.path().join("specifications"); tokio::fs::create_dir_all(&specs).await.unwrap(); @@ -6842,11 +6846,17 @@ mod tests { global_prefix: Some(gems.clone()), }; assert!(!crawler.verify_gem_at_path(&gem_dir).await); - assert!(crawler.find_by_purls(&gems, &purls).await.unwrap().is_empty()); + assert!(crawler + .find_by_purls(&gems, &purls) + .await + .unwrap() + .is_empty()); assert!(crawler.crawl_all(&options).await.is_empty()); // A default gem's spec does not make its executables-only dir a copy. - tokio::fs::create_dir_all(specs.join("default")).await.unwrap(); + tokio::fs::create_dir_all(specs.join("default")) + .await + .unwrap(); tokio::fs::write( specs.join("default").join("cassandra-web-0.5.0.gemspec"), b"# default spec", @@ -6854,7 +6864,11 @@ mod tests { .await .unwrap(); assert!(!crawler.verify_gem_at_path(&gem_dir).await); - assert!(crawler.find_by_purls(&gems, &purls).await.unwrap().is_empty()); + assert!(crawler + .find_by_purls(&gems, &purls) + .await + .unwrap() + .is_empty()); assert!(crawler.crawl_all(&options).await.is_empty()); tokio::fs::write(specs.join("cassandra-web-0.5.0.gemspec"), b"# spec")