From 50f27482de64756f40073796ddea6db79502f845 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 20 Nov 2026 17:22:50 +0000 Subject: [PATCH 1/3] Start fix for platform-gem left-to-patch count Empty commit that opens the draft pull request for #2181. Assisted-by: Claude Code:claude-fable-5-2 Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_011f9aeSWDdLUDci7zLLrvTj From ecd8b0c1e8c10204b167a8fc08b5a1ff4db6cdbf Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 20 Nov 2026 17:35:31 +0000 Subject: [PATCH 2/3] Count a refused platform-only gem as left to patch The closing totals of a hosted or vendored scan leave out a package the lockfile does not name. The set of locked packages came from the lockfile inventory, which omits a gem the Bundler lock holds only as precompiled platform builds. A refused gem of that kind was taken for a copy the project does not use, and a run that patched another gem ended with "Nothing left to patch." The lockfile supplement now carries those gems in every mode, and the totals count them as locked. Fixes #2181 Assisted-by: Claude Code:claude-fable-5-2 Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_011f9aeSWDdLUDci7zLLrvTj --- .../src/commands/scan/discovery.rs | 16 ++- .../socket-patch-cli/src/commands/scan/mod.rs | 23 ++-- .../tests/e2e_redirect_gem_stale_install.rs | 104 ++++++++++++++++++ 3 files changed, 127 insertions(+), 16 deletions(-) diff --git a/crates/socket-patch-cli/src/commands/scan/discovery.rs b/crates/socket-patch-cli/src/commands/scan/discovery.rs index 5247005fe..7c0b58bf1 100644 --- a/crates/socket-patch-cli/src/commands/scan/discovery.rs +++ b/crates/socket-patch-cli/src/commands/scan/discovery.rs @@ -38,6 +38,11 @@ pub(crate) struct LockfileSupplement { /// packages included), kept so the hosted-wiring probes reuse it instead /// of re-parsing every project lockfile. Empty for global scans. pub(crate) entries: Vec, + /// The gems the Bundler lock resolves only as precompiled platform + /// builds, as `pkg:gem/@`. `entries` leaves them out, + /// in every mode, and the lock names them all the same: with + /// `entries` they are what the project's lockfiles resolve. + pub(crate) platform_gems: Vec, /// npm layouts the lockfile inventory REFUSED (Plug'n'Play loaders). /// Scan surfaces these as refusal warnings: under PnP the installed-tree /// crawl is empty too, so otherwise the project scans as a silent no-op. @@ -76,20 +81,19 @@ pub(crate) async fn lockfile_supplement( let locks = ctx.locks().await; out.unsupported = locks.unsupported.clone(); let entries = &locks.entries; - let platform_gems = if hosted { - ctx.gem_platform_only_purls().await - } else { - Vec::new() - }; + let platform_gems = ctx.gem_platform_only_purls().await; if entries.is_empty() && platform_gems.is_empty() { return out; } + // Only a hosted run can patch a platform-only gem that has no + // installed copy, so only a hosted supplement fabricates one. let resolved = entries .iter() .map(|entry| entry.purl.as_str()) - .chain(platform_gems.iter().map(String::as_str)); + .chain(platform_gems.iter().filter(|_| hosted).map(String::as_str)); (out.packages, out.purls) = lockfile_only_packages(resolved, crawled, only, &common.cwd); out.entries = entries.clone(); + out.platform_gems = platform_gems; out } diff --git a/crates/socket-patch-cli/src/commands/scan/mod.rs b/crates/socket-patch-cli/src/commands/scan/mod.rs index 0fd37f39b..fb51a84cf 100644 --- a/crates/socket-patch-cli/src/commands/scan/mod.rs +++ b/crates/socket-patch-cli/src/commands/scan/mod.rs @@ -3796,16 +3796,19 @@ async fn scan_project( // not use (another project's gem in a shared gem home, an extraneous // `node_modules` entry): the mode reports it, and the report does not // count it as left to patch. A requirements file may pin only the - // direct dependencies, so PyPI packages always count. - let locked: HashSet = lockfile_only - .entries - .iter() - .map(|entry| PurlKey::new(&entry.purl)) - .collect(); - let complete_locks: HashSet = lockfile_only - .entries - .iter() - .filter_map(|entry| Ecosystem::from_purl(&entry.purl)) + // direct dependencies, so PyPI packages always count. The lockfile + // inventory leaves out a gem the Bundler lock holds only as + // precompiled platform builds; the lock names it too. + let locked_purls = || { + lockfile_only + .entries + .iter() + .map(|entry| entry.purl.as_str()) + .chain(lockfile_only.platform_gems.iter().map(String::as_str)) + }; + let locked: HashSet = locked_purls().map(PurlKey::new).collect(); + let complete_locks: HashSet = locked_purls() + .filter_map(Ecosystem::from_purl) .filter(|eco| *eco != Ecosystem::Pypi) .collect(); let in_project = |purl: &str| { diff --git a/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs b/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs index faffe8edf..80b76a663 100644 --- a/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs +++ b/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs @@ -2813,6 +2813,110 @@ async fn gem_hosted_scan_pins_platform_builds_from_the_lock_alone() { } } +/// A lock that names the gem only as platform builds, beside a plain +/// gem, with a prebuild missing for one locked platform: no mode can +/// patch the gem. The lock still resolves it, so the closing totals count +/// its advisory as left to patch, in hosted mode (which reads the gem +/// from the lock alone) and in vendored mode (which reads the installed +/// platform copy). +#[tokio::test(flavor = "multi_thread")] +async fn refused_platform_gem_is_counted_as_left_to_patch() { + const LEFT: &str = + "\n1 vulnerability left to patch: 1 is in packages this run did not patch.\n"; + let arm_sha = sha256_of(&real_gem_for(PATCHED_LIB, PLATFORM)); + for mode in ["hosted", "vendored"] { + let server = MockServer::start().await; + // The lookup names the advisory the patch fixes, as the service + // does: the totals count advisories. + Mock::given(method("POST")) + .and(path(format!("/v0/orgs/{ORG}/patches/batch"))) + .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({ + "packages": [{ + "purl": PURL, + "patches": [{ + "uuid": UUID, "purl": PURL, "tier": "free", + "cveIds": [], "ghsaIds": [GHSA], "severity": "high", + "title": "gem stale-install fixture" + }] + }], + "canAccessPaidPatches": false, + }))) + .with_priority(1) + .mount(&server) + .await; + mount_api_with( + &server, + None, + &sha256_of(&real_gem(PATCHED_LIB)), + &[(PLATFORM, arm_sha.clone())], + ) + .await; + let tmp = tempfile::tempdir().unwrap(); + let proj = tmp.path().join("proj"); + std::fs::create_dir_all(&proj).unwrap(); + let gemfile = format!( + "source \"https://rubygems.org\"\ngem \"{DEP}\", \"{DEP_VERSION}\"\ngem \ + \"plain-probe-gem\", \"1.0.0\"\n" + ); + // The plain gem makes the lock one that lists ruby-platform gems + // too, as every real lock does (nokogiri beside racc). + let lock = platform_lock(&"1".repeat(64), &"2".repeat(64)) + .replace(" specs:\n", " specs:\n plain-probe-gem (1.0.0)\n") + .replace( + "DEPENDENCIES\n", + "DEPENDENCIES\n plain-probe-gem (= 1.0.0)\n", + ); + assert!( + lock.contains(" plain-probe-gem (1.0.0)\n") + && !lock.contains(&format!(" {DEP} ({DEP_VERSION})\n")), + "the lock holds a plain gem, and {DEP} as platform builds only:\n{lock}" + ); + std::fs::write(proj.join("Gemfile"), &gemfile).unwrap(); + std::fs::write(proj.join("Gemfile.lock"), &lock).unwrap(); + if mode == "vendored" { + install_real_gem_for(&project_home(&proj), UPSTREAM_LIB, PLATFORM); + } + + let uri = server.uri(); + let (code, stdout, stderr) = common::run_with_env( + &proj, + &[ + "scan", + "--mode", + mode, + "--yes", + "--cwd", + proj.to_str().unwrap(), + "--api-url", + uri.as_str(), + "--patch-server-url", + uri.as_str(), + "--org", + ORG, + "--api-token", + "fake", + ], + &[], + ); + assert_eq!(code, 0, "{mode}\nstdout:\n{stdout}\nstderr:\n{stderr}"); + // The premise: the run refused the gem for its missing prebuild. + assert!( + format!("{stdout}{stderr}").contains(OTHER_PLATFORM), + "{mode}: the run names the platform it has no patched build for\nstdout:\n\ + {stdout}\nstderr:\n{stderr}" + ); + assert_eq!( + std::fs::read_to_string(proj.join("Gemfile.lock")).unwrap(), + lock, + "{mode}: the lock is untouched" + ); + assert!( + stdout.ends_with(LEFT), + "{mode}: the refused gem is left to patch\nstdout:\n{stdout}\nstderr:\n{stderr}" + ); + } +} + /// Human output of the run that refreshes: one progress line per kind, /// no warning, and the committed archive in the files to commit. #[tokio::test(flavor = "multi_thread")] From 1e26e1668323ae56cf8059103b9d309c0e34cdd8 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 20 Nov 2026 17:44:00 +0000 Subject: [PATCH 3/3] Tighten the platform-gem totals test premise The lookup mock now answers only a run that asks about the gem, so the vendored arm fails without the installed platform copy, and each arm asserts its own refusal message. The supplement's comment no longer says what vendored mode can patch. Assisted-by: Claude Code:claude-fable-5-2 Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_011f9aeSWDdLUDci7zLLrvTj --- .../src/commands/scan/discovery.rs | 4 +- .../tests/e2e_redirect_gem_stale_install.rs | 37 +++++++++++++------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/crates/socket-patch-cli/src/commands/scan/discovery.rs b/crates/socket-patch-cli/src/commands/scan/discovery.rs index 7c0b58bf1..c8a01e483 100644 --- a/crates/socket-patch-cli/src/commands/scan/discovery.rs +++ b/crates/socket-patch-cli/src/commands/scan/discovery.rs @@ -85,8 +85,8 @@ pub(crate) async fn lockfile_supplement( if entries.is_empty() && platform_gems.is_empty() { return out; } - // Only a hosted run can patch a platform-only gem that has no - // installed copy, so only a hosted supplement fabricates one. + // Only a hosted supplement names a platform-only gem that has no + // installed copy (see above). let resolved = entries .iter() .map(|entry| entry.purl.as_str()) diff --git a/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs b/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs index 80b76a663..d332644ab 100644 --- a/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs +++ b/crates/socket-patch-cli/tests/e2e_redirect_gem_stale_install.rs @@ -41,7 +41,7 @@ use std::path::{Path, PathBuf}; use socket_patch_core::hash::git_sha256::compute_git_sha256_from_bytes; -use wiremock::matchers::{method, path, path_regex}; +use wiremock::matchers::{body_string_contains, method, path, path_regex}; use wiremock::{Mock, MockServer, ResponseTemplate}; #[path = "common/mod.rs"] @@ -2814,11 +2814,11 @@ async fn gem_hosted_scan_pins_platform_builds_from_the_lock_alone() { } /// A lock that names the gem only as platform builds, beside a plain -/// gem, with a prebuild missing for one locked platform: no mode can -/// patch the gem. The lock still resolves it, so the closing totals count -/// its advisory as left to patch, in hosted mode (which reads the gem -/// from the lock alone) and in vendored mode (which reads the installed -/// platform copy). +/// gem, and a patch without a patched build for every locked platform: +/// the run refuses the gem. The lock still resolves it, so the closing +/// totals count its advisory as left to patch, in hosted mode (which +/// reads the gem from the lock alone) and in vendored mode (which reads +/// the installed platform copy). #[tokio::test(flavor = "multi_thread")] async fn refused_platform_gem_is_counted_as_left_to_patch() { const LEFT: &str = @@ -2827,9 +2827,20 @@ async fn refused_platform_gem_is_counted_as_left_to_patch() { for mode in ["hosted", "vendored"] { let server = MockServer::start().await; // The lookup names the advisory the patch fixes, as the service - // does: the totals count advisories. + // does (the totals count advisories), and only for a run that + // asks about the gem. Mock::given(method("POST")) .and(path(format!("/v0/orgs/{ORG}/patches/batch"))) + .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({ + "packages": [], + "canAccessPaidPatches": false, + }))) + .with_priority(2) + .mount(&server) + .await; + Mock::given(method("POST")) + .and(path(format!("/v0/orgs/{ORG}/patches/batch"))) + .and(body_string_contains(DEP)) .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({ "packages": [{ "purl": PURL, @@ -2899,11 +2910,15 @@ async fn refused_platform_gem_is_counted_as_left_to_patch() { &[], ); assert_eq!(code, 0, "{mode}\nstdout:\n{stdout}\nstderr:\n{stderr}"); - // The premise: the run refused the gem for its missing prebuild. + // The premise: the run refused the gem for a platform the patch + // has no build for. + let refusal = match mode { + "hosted" => format!("no patched build for `{DEP} ({DEP_VERSION}-{OTHER_PLATFORM})`"), + _ => format!("the patch service has no prebuilt platform gem of `{DEP}`"), + }; assert!( - format!("{stdout}{stderr}").contains(OTHER_PLATFORM), - "{mode}: the run names the platform it has no patched build for\nstdout:\n\ - {stdout}\nstderr:\n{stderr}" + stderr.contains(&refusal), + "{mode}: the run refuses the gem\nstdout:\n{stdout}\nstderr:\n{stderr}" ); assert_eq!( std::fs::read_to_string(proj.join("Gemfile.lock")).unwrap(),