Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/socket-patch-cli/CLI_CONTRACT.md

Large diffs are not rendered by default.

130 changes: 129 additions & 1 deletion crates/socket-patch-cli/src/commands/vex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ async fn generate_vex(
let hosted = crate::commands::vex_consumed::hosted_consumed_copies(
common,
&plan.hosted,
&copies,
&with_gem_copies_by_build(&copies, &package_paths),
params.npm_prior.as_ref(),
&mut live_stores,
)
Expand Down Expand Up @@ -1100,12 +1100,17 @@ async fn generate_vex(
let composer_unread =
socket_patch_core::crawlers::composer_crawler::local_install_unread(&common.cwd);
let unread = |purl: &str| purl.starts_with("pkg:composer/") && composer_unread;
// Nor is the lock's pin evidence for one build of a gem while an
// installed build of the same gem fails its own patch's check: the
// copy this machine runs is then not the patched one.
let gem_build_fails = gems_with_a_failing_build(&outcome.failed);
let pin_excuses = |f: &FailedPatch| {
f.reason == "package_not_found"
&& plan.lockfile_basis.contains(&f.purl)
&& !mirror_refused(&f.purl)
&& crawled(&f.purl)
&& !hidden(&f.purl)
&& !gem_build_fails.contains(gem_of_build(&f.purl))
};
for f in outcome.failed.iter_mut() {
if pin_excuses(f) && unread(&f.purl) {
Expand Down Expand Up @@ -1776,6 +1781,50 @@ pub(crate) async fn vex_copy_sets(
sets
}

/// The installed copies a hosted purl is judged by: `copies` (every copy
/// of every purl), with each gem's taken from `by_build`, the sets the
/// record check uses ([`narrow_gem_variant_copies`]).
///
/// A gem fix has one patch per platform gem, each with bytes of its own,
/// and a lock can name several of them (`nokogiri (1.19.3)` beside
/// `nokogiri (1.19.3-x86_64-linux-gnu)`) where a machine installs one. The
/// installed copy is that one gem's: judged by the patch of another build
/// it matches neither the original nor the patched content.
fn with_gem_copies_by_build(
copies: &HashMap<String, Vec<PathBuf>>,
by_build: &HashMap<String, Vec<PathBuf>>,
) -> HashMap<String, Vec<PathBuf>> {
copies
.iter()
.map(|(purl, paths)| {
let paths = by_build
.get(purl)
.filter(|_| purl.starts_with("pkg:gem/"))
.unwrap_or(paths);
(purl.clone(), paths.clone())
})
.collect()
}

/// The gem a gem patch purl is a build of: the purl without its
/// qualifiers. Any other purl as it is.
fn gem_of_build(purl: &str) -> &str {
match purl.starts_with("pkg:gem/") {
true => socket_patch_core::utils::purl::strip_purl_qualifiers(purl),
false => purl,
}
}

/// The gems ([`gem_of_build`]) with a build in `failed` that an installed
/// copy failed on: any reason but `package_not_found`.
fn gems_with_a_failing_build(failed: &[FailedPatch]) -> HashSet<String> {
failed
.iter()
.filter(|f| f.purl.starts_with("pkg:gem/") && f.reason != "package_not_found")
.map(|f| gem_of_build(&f.purl).to_string())
.collect()
}

/// RubyGems: give each qualified variant key (`?platform=`…) only the
/// copies holding its distribution. One gem home can hold several builds of
/// a version (`x-1.0/` beside `x-1.0-x86_64-linux/`, #1092), each patched
Expand Down Expand Up @@ -3368,6 +3417,85 @@ mod tests {
}
}

/// A hosted gem purl is judged by the copies of its own build, every
/// other purl by every copy the crawl found.
#[test]
fn a_hosted_gem_is_judged_by_the_copies_of_its_own_build() {
let ruby = "pkg:gem/nokogiri@1.19.3?platform=ruby".to_string();
let linux = "pkg:gem/nokogiri@1.19.3?platform=x86_64-linux-gnu".to_string();
let npm = "pkg:npm/left-pad@1.3.0".to_string();
let copy = PathBuf::from("/gems/nokogiri-1.19.3-x86_64-linux-gnu");
let node = PathBuf::from("/node_modules/left-pad");
let copies = HashMap::from([
(ruby.clone(), vec![copy.clone()]),
(linux.clone(), vec![copy.clone()]),
(npm.clone(), vec![node.clone()]),
]);
let by_build = HashMap::from([
(ruby.clone(), Vec::new()),
(linux.clone(), vec![copy.clone()]),
(npm.clone(), Vec::new()),
]);
let judged = with_gem_copies_by_build(&copies, &by_build);
assert!(judged[&ruby].is_empty(), "no ruby gem is installed");
assert_eq!(judged[&linux], [copy]);
assert_eq!(judged[&npm], [node], "only a gem's copies are narrowed");
}

/// A build of a gem that an installed copy fails on marks the gem; a
/// build that is not installed, and any other ecosystem, marks nothing.
#[test]
fn a_gem_is_marked_by_a_build_an_installed_copy_fails_on() {
let fails = gems_with_a_failing_build(&[
failed(
"pkg:gem/nokogiri@1.19.3?platform=x86_64-linux-gnu",
"not_applied",
),
failed("pkg:gem/nokogiri@1.19.3?platform=ruby", "package_not_found"),
failed("pkg:gem/rack@3.1.8", "package_not_found"),
failed("pkg:npm/left-pad@1.3.0", "hash_mismatch"),
]);
assert_eq!(
fails,
HashSet::from(["pkg:gem/nokogiri@1.19.3".to_string()])
);
assert!(fails.contains(gem_of_build("pkg:gem/nokogiri@1.19.3?platform=ruby")));
assert_eq!(gem_of_build("pkg:npm/a@1?x=y"), "pkg:npm/a@1?x=y");
}

/// The run holds the ruby build's record alone: the patch of the build
/// this machine installed could not be fetched. The installed copy
/// holds neither the original nor the patched content of the ruby
/// build's record, so it stays on that build's key, where it fails the
/// check. The ruby build is then not "nowhere installed", and the
/// lock's pin does not attest it over a copy no record verified.
#[tokio::test]
async fn an_installed_gem_copy_no_record_matches_stays_on_the_build_that_has_one() {
use socket_patch_core::hash::git_sha256::compute_git_sha256_from_bytes as hash;
let tmp = tempfile::tempdir().unwrap();
let copy = tmp.path().join("gems/nokogiri-1.19.3-x86_64-linux-gnu");
std::fs::create_dir_all(copy.join("lib")).unwrap();
std::fs::write(copy.join("lib/nokogiri.rb"), b"the platform patch's bytes").unwrap();
let ruby = "pkg:gem/nokogiri@1.19.3?platform=ruby".to_string();
let record: socket_patch_core::manifest::schema::PatchRecord =
serde_json::from_value(serde_json::json!({
"uuid": "00000000-0000-4000-8000-000000000000",
"exportedAt": "2026-01-01T00:00:00Z",
"files": {"lib/nokogiri.rb": {
"beforeHash": hash(b"the original"),
"afterHash": hash(b"the ruby patch's bytes"),
}},
"vulnerabilities": {},
"description": "test", "license": "MIT", "tier": "free"
}))
.unwrap();
let mut manifest = PatchManifest::new();
manifest.patches.insert(ruby.clone(), record);
let mut sets = HashMap::from([(ruby.clone(), vec![copy.clone()])]);
narrow_gem_variant_copies(&manifest, &mut sets).await;
assert_eq!(sets[&ruby], [copy]);
}

/// The reported flood: a project that has not installed its patched
/// packages yet got one line per package. The patches left out are one
/// line per reason, largest first, with the count and the first few
Expand Down
5 changes: 3 additions & 2 deletions crates/socket-patch-cli/src/commands/vex_consumed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! | maven | `<repo>/<g>/<a>/<base>-socket.<hex8>/` (the version the pom or the hosted Gradle wiring pins) in `~/.m2` and every Gradle `files-2.1` holding it (hash dirs expanded), its artifact files matched under the suffixed name | the `<base>` version dir |
//! | npm | every `node_modules` copy the crawler finds (pnpm and vlt store copies included), every peer / modifier / registry variant of those in the same `.pnpm` / `.vlt` store, alias installs (`node_modules/<alias>` holding the package) in the root's and every workspace member's tree included | — each serves some dependent: ALL must verify |
//! | pypi | every copy in the crawler's environment set (the project's venvs when it has any, else the interpreters) | — any may be the one that runs the project: ALL must verify |
//! | gem | every copy in bundler's gem path; under an explicit or deployment `path` the `gem env` homes hold only default gems bundler loads | a non-default gem's `gem env` copy when bundler doesn't use system gems; otherwise bundler loads whichever `Gem.path` home it hits first: ALL must verify |
//! | gem | every copy in bundler's gem path; under an explicit or deployment `path` the `gem env` homes hold only default gems bundler loads. A purl that names one build of a gem (`?platform=`) is given the copies of that build alone, and a copy that matches no build's record stays on every build's purl (the caller narrows them, `vex`'s `narrow_gem_variant_copies`) | a non-default gem's `gem env` copy when bundler doesn't use system gems; otherwise bundler loads whichever `Gem.path` home it hits first: ALL must verify |
//!
//! composer and nuget have ONE install location shared by every source
//! (`vendor/`, the global packages folder — which restore reuses whatever
Expand Down Expand Up @@ -66,7 +66,8 @@ use crate::ecosystem_dispatch::{
/// the installed-tree lookup. `installed` is that lookup's every-copy
/// result ([`crate::ecosystem_dispatch::find_manifest_package_copies_reusing`] over
/// the record view, which holds every hosted purl), including npm store
/// variants, and `live` the judgement of the npm stores' live entries that
/// variants, with each gem build's purl narrowed to the copies of its own
/// build, and `live` the judgement of the npm stores' live entries that
/// lookup made, which this one shares: it drops no copy that lookup kept.
/// The shared-location ecosystems read it instead of crawling
/// the tree a second time. `prior`
Expand Down
Loading
Loading