From c0510f9bded2c1bda75141df02b8feb71f03b5e9 Mon Sep 17 00:00:00 2001 From: Mikola Lysenko Date: Thu, 3 Dec 2026 11:02:33 -0500 Subject: [PATCH 1/2] Fix vendoring a gem whose patch names its own gemspec A patch that fixes a gem's file permissions names every file the gem ships. Many gems ship `.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 --- crates/socket-patch-core/src/vendor/gem.rs | 268 ++++++++++++++++-- crates/socket-patch-core/src/vendor/verify.rs | 37 ++- 2 files changed, 278 insertions(+), 27 deletions(-) diff --git a/crates/socket-patch-core/src/vendor/gem.rs b/crates/socket-patch-core/src/vendor/gem.rs index f8652d939..47e7b608c 100644 --- a/crates/socket-patch-core/src/vendor/gem.rs +++ b/crates/socket-patch-core/src/vendor/gem.rs @@ -771,7 +771,8 @@ async fn gem_prelude( // runs second so a hash mismatch short-circuits it. copy_ok = match locked.platforms.as_slice() { [] => { - copy_matches_after_hashes(©_dir, &record.files).await + copy_matches_after_hashes(©_dir, &files_beside_stub(&record.files, name)) + .await && stub_gemspec_valid(©_dir, name).await } platforms => { @@ -1030,6 +1031,32 @@ pub(super) fn is_platform_dir_name(platform: &str) -> bool { is_safe_single_segment(platform) && is_plain_gem_token(platform) } +/// The files of a patch that a vendored tree of the gem `name` is held to: +/// `files` without the one at the stub gemspec's path, `.gemspec` at +/// the root of the tree. A gem may ship a gemspec of that name, and a patch +/// may name it (one that fixes the gem's file permissions names every file +/// it ships), but the vendored tree carries the stub there instead: bundler +/// evaluates that file of a path source. The archive the tree is unpacked +/// from is held to every file of the patch before the stub is written +/// ([`gem_service_copy`], [`stage_platform_builds`]); afterwards the stub's +/// own bytes are for [`stub_gemspec_valid`] and the ledger's inventory. +pub(super) fn files_beside_stub<'f>( + files: &'f HashMap, + name: &str, +) -> std::borrow::Cow<'f, HashMap> { + let is_stub = |key: &str| normalize_file_path(key).strip_suffix(".gemspec") == Some(name); + match files.keys().any(|key| is_stub(key)) { + false => std::borrow::Cow::Borrowed(files), + true => std::borrow::Cow::Owned( + files + .iter() + .filter(|(key, _)| !is_stub(key)) + .map(|(key, info)| (key.clone(), info.clone())) + .collect(), + ), + } +} + /// Whether `tree` holds the stub gemspec of the gem `name` /// (`.gemspec`, which a path source needs) with the attributes /// bundler requires of one. @@ -1076,7 +1103,8 @@ async fn platform_copy_intact( for (platform, files) in trees { let tree = copy_dir.join(platform); let voucher = super::verify::TreeVoucher::Ledger(inventory); - if super::verify::verify_platform_tree(&tree, files, voucher) + let files = files_beside_stub(files, name); + if super::verify::verify_platform_tree(&tree, &files, voucher) .await .is_err() || !stub_gemspec_valid(&tree, name).await @@ -1806,13 +1834,9 @@ pub(super) async fn gem_service_copy( )); } } - if let Err(e) = tokio::fs::write(stage.join(format!("{name}.gemspec")), &stub).await { - cleanup_failed_stage(&stage, uuid_dir, unwind_uuid_dir).await; - return Err(policy.hard( - "vendor_prebuilt_write_failed", - format!("cannot write the stub gemspec into the vendored dir: {e}"), - )); - } + // Checked while the tree is the gem's own files: a gem may ship a + // `.gemspec` the patch names, which the stub then replaces (see + // `files_beside_stub`). if !copy_matches_after_hashes(&stage, &record.files).await { cleanup_failed_stage(&stage, uuid_dir, unwind_uuid_dir).await; return Err(policy.miss(format!( @@ -1820,6 +1844,13 @@ pub(super) async fn gem_service_copy( (patched files absent at their recorded paths)" ))); } + if let Err(e) = tokio::fs::write(stage.join(format!("{name}.gemspec")), &stub).await { + cleanup_failed_stage(&stage, uuid_dir, unwind_uuid_dir).await; + return Err(policy.hard( + "vendor_prebuilt_write_failed", + format!("cannot write the stub gemspec into the vendored dir: {e}"), + )); + } let which = Shipped::Hiding { recorded }; let emptied = match empty_shipped_gitignores(&stage, &record.files, which).await { Ok(emptied) => emptied, @@ -2115,6 +2146,22 @@ async fn stage_platform_builds( format!("cannot extract the patched platform gem for {subject}: {e}"), )); } + // The gem passed the service's digest for this platform's patch + // above (`fetch_verified_archive`), which is what vouches for a + // tree that ships none of the patched files: only one it does ship + // can contradict the patch. Checked while the tree is the gem's own + // files, before the stub takes the place of a `.gemspec` it + // ships (see `files_beside_stub`). + let verified = super::verify::TreeVoucher::VerifiedDownload; + if super::verify::verify_platform_tree(&tree, &patch.record.files, verified) + .await + .is_err() + { + return Err(policy.miss(format!( + "patched platform gem for {subject} does not carry its patch (a patched file \ + it ships is not the patched one)" + ))); + } match empty_shipped_gitignores(&tree, &patch.record.files, Shipped::Every).await { Ok(files) => emptied.extend(files.into_iter().map(|file| format!("{platform}/{file}"))), Err(e) => { @@ -2130,20 +2177,6 @@ async fn stage_platform_builds( format!("cannot write the stub gemspec for {subject} into the vendored dir: {e}"), )); } - // The gem passed the service's digest for this platform's patch - // above (`fetch_verified_archive`), which is what vouches for a - // tree that ships none of the patched files: only one it does ship - // can contradict the patch. - let verified = super::verify::TreeVoucher::VerifiedDownload; - if super::verify::verify_platform_tree(&tree, &patch.record.files, verified) - .await - .is_err() - { - return Err(policy.miss(format!( - "patched platform gem for {subject} does not carry its patch (a patched file \ - it ships is not the patched one)" - ))); - } built.push(GemBuildEntry { platform: platform.to_string(), uuid: patch.record.uuid.clone(), @@ -10425,6 +10458,136 @@ mod tests { ); } + /// The gemspec a gem ships under its own name, as bundler's template + /// writes one. A patch that fixes a gem's file permissions names it, + /// like every other file the gem ships. + const SHIPPED_GEMSPEC: &[u8] = b"Gem::Specification.new do |spec|\n spec.name = \"rack\"\n spec.files = `git ls-files -z`.split(\"\\x0\")\nend\n"; + + /// `record`, also naming the gem's own `rack.gemspec` with the content + /// `shipped` and a mode, as a patch of the gem's file permissions does. + fn naming_own_gemspec(mut record: PatchRecord, shipped: &[u8]) -> PatchRecord { + let hash = compute_git_sha256_from_bytes(shipped); + record.files.insert( + "rack.gemspec".to_string(), + PatchFileInfo { + before_hash: hash.clone(), + after_hash: hash, + mode: crate::manifest::schema::FileMode::parse("0644"), + }, + ); + record + } + + /// A patch that names the gem's own `.gemspec` vendors: the served + /// gem is held to every file of the patch, and the copy then carries the + /// stub at that path. A second run finds the copy in sync, and the copy + /// verifies by its other patched files and its inventory. + #[tokio::test] + async fn a_patch_naming_the_gems_own_gemspec_vendors_with_the_stub() { + let (_tmp, root, installed, blobs, record) = fixture(GEMFILE_DIRECT, LOCK_DIRECT).await; + let record = naming_own_gemspec(record, SHIPPED_GEMSPEC); + let gem = make_gem(&[("lib/rack.rb", PATCHED), ("rack.gemspec", SHIPPED_GEMSPEC)]); + let (sri, stub_sri) = (sri_sha512(&gem), sri_sha512(SERVICE_STUB)); + let server = wiremock::MockServer::start().await; + mount_gem_granted(&server, &gem, &sri, Some((SERVICE_STUB, &stub_sri))).await; + let cfg = gem_service_cfg(&server.uri(), VendorSource::Service, false); + + let (result, entry, _) = + unwrap_done(run_vendor_service(&root, &blobs, &installed, &record, &cfg).await); + assert!(result.success, "{:?}", result.error); + assert_eq!( + tokio::fs::read(copy_gemspec(&root)).await.unwrap(), + SERVICE_STUB, + "the copy carries the stub, not the gemspec the gem ships" + ); + let entry = entry.expect("a ledger entry"); + crate::vendor::test_support::persist(&root, PURL, entry.clone()).await; + + // In sync: nothing is asked of the service and nothing is rebuilt. + let asked = crate::vendor::test_support::request_count(&server).await; + let (again, rebuilt, warnings) = + unwrap_done(run_vendor_service(&root, &blobs, &installed, &record, &cfg).await); + assert!(again.success, "{:?}", again.error); + assert!(rebuilt.is_none(), "no new ledger entry"); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!( + crate::vendor::test_support::request_count(&server).await, + asked + ); + + let verify = || crate::vendor::verify::verify_vendored_patch_record(&root, &entry, &record); + assert_eq!(verify().await, Ok(())); + // The stub's bytes are the inventory's to vouch for. + tokio::fs::write(copy_gemspec(&root), b"tampered\n") + .await + .unwrap(); + assert_eq!(verify().await, Err("vendor_inventory_mismatch".to_string())); + tokio::fs::write(copy_gemspec(&root), SERVICE_STUB) + .await + .unwrap(); + // Every other file of the patch is still held to its hash. + tokio::fs::write(copy_lib(&root), PRISTINE).await.unwrap(); + assert_eq!(verify().await, Err("vendor_hash_mismatch".to_string())); + } + + /// The exemption is of the stub's path alone, and only for the copy: + /// a served gem whose own `.gemspec` is not the one the patch + /// names is refused like any other file that is not the patched one. + #[tokio::test] + async fn a_served_gem_whose_own_gemspec_is_not_the_patched_one_is_refused() { + let (_tmp, root, installed, blobs, record) = fixture(GEMFILE_DIRECT, LOCK_DIRECT).await; + let record = naming_own_gemspec(record, SHIPPED_GEMSPEC); + let gem = make_gem(&[("lib/rack.rb", PATCHED), ("rack.gemspec", b"other\n")]); + let (sri, stub_sri) = (sri_sha512(&gem), sri_sha512(SERVICE_STUB)); + let server = wiremock::MockServer::start().await; + mount_gem_granted(&server, &gem, &sri, Some((SERVICE_STUB, &stub_sri))).await; + let cfg = gem_service_cfg(&server.uri(), VendorSource::Service, false); + + let (code, detail) = + unwrap_refused(run_vendor_service(&root, &blobs, &installed, &record, &cfg).await); + assert_eq!(code, "vendor_prebuilt_required"); + assert!(detail.contains("unexpected layout"), "{detail}"); + assert!(!root.join(format!(".socket/vendor/gem/{UUID}")).exists()); + } + + /// A patch that names nothing but the gem's own `.gemspec` leaves + /// no file of the copy to hash: without an inventory nothing vouches + /// for the copy, and it is not attested. A gemspec of the same name + /// below the root is no stub and stays held to its hash. + #[tokio::test] + async fn a_copy_with_only_its_stub_to_show_is_not_attested() { + let (_tmp, root, installed, blobs, record) = fixture(GEMFILE_DIRECT, LOCK_DIRECT).await; + let full = naming_own_gemspec(record.clone(), SHIPPED_GEMSPEC); + let gem = make_gem(&[("lib/rack.rb", PATCHED), ("rack.gemspec", SHIPPED_GEMSPEC)]); + let (sri, stub_sri) = (sri_sha512(&gem), sri_sha512(SERVICE_STUB)); + let server = wiremock::MockServer::start().await; + mount_gem_granted(&server, &gem, &sri, Some((SERVICE_STUB, &stub_sri))).await; + let cfg = gem_service_cfg(&server.uri(), VendorSource::Service, false); + let (result, entry, _) = + unwrap_done(run_vendor_service(&root, &blobs, &installed, &full, &cfg).await); + assert!(result.success, "{:?}", result.error); + let entry = entry.expect("a ledger entry"); + + let mut only_stub = full.clone(); + only_stub.files.retain(|file, _| file == "rack.gemspec"); + let verify = crate::vendor::verify::verify_vendored_patch_record; + assert_eq!(verify(&root, &entry, &only_stub).await, Ok(())); + let mut unrecorded = entry.clone(); + unrecorded.artifact.file_inventory = None; + assert_eq!( + verify(&root, &unrecorded, &only_stub).await, + Err("no_files".to_string()) + ); + + let mut nested = record.clone(); + let stub = full.files["rack.gemspec"].clone(); + nested.files.insert("spec/rack.gemspec".to_string(), stub); + assert_eq!( + verify(&root, &unrecorded, &nested).await, + Err("file_not_found".to_string()) + ); + } + /// `auto` + the same layout mismatch warns and falls back to the local /// build — the wrong-layout service bytes never land in the copy. #[tokio::test] @@ -14243,6 +14406,65 @@ mod tests { ); } + /// A patch that names the `nokogiri.gemspec` the platform gems + /// ship vendors: each served gem is held to it, each tree then + /// carries its stub there, a second run finds the copy in sync, + /// and the copy verifies. A served gem whose own gemspec is not + /// the one the patch names is refused. + #[tokio::test] + async fn a_patch_naming_the_gems_own_gemspec_vendors_platform_trees() { + const OWN: &str = "nokogiri.gemspec"; + const SHIPPED: &[u8] = + b"Gem::Specification.new do |spec|\n spec.name = \"nokogiri\"\nend\n"; + let shipping = |platform: &'static str, gemspec: &[u8]| Served { + gem: Some(make_gem(&[ + (RB, RB_PATCHED), + (SO, platform.as_bytes()), + (OWN, gemspec), + ])), + ..served(platform) + }; + let files = [(RB, RB_PRISTINE, RB_PATCHED), (OWN, SHIPPED, SHIPPED)]; + + let (_tmp, root, _) = project(&lock(None)).await; + let record = record(UUID, &files); + let (server, cfg) = + service(UUID, &[shipping(DARWIN, SHIPPED), shipping(LINUX, SHIPPED)]).await; + let entry = vendor_and_persist(&root, &record, &cfg).await; + for platform in [DARWIN, LINUX] { + assert_eq!( + tokio::fs::read(root.join(rel(UUID)).join(platform).join(OWN)) + .await + .unwrap(), + stub(platform) + ); + } + assert_eq!( + check_vendored_artifact(&root, &entry, &record).await, + ArtifactHealth::Healthy + ); + let asked = ts::request_count(&server).await; + let (again, rebuilt, warnings) = + unwrap_done(vendor(&root, ¬_installed(&root), &record, &cfg, false).await); + assert!(again.success, "{:?}", again.error); + assert!(rebuilt.is_none(), "no new ledger entry"); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(ts::request_count(&server).await, asked); + + let (_tmp, root, _) = project(&lock(None)).await; + let before = ts::tree_snapshot(&root); + let (_server, cfg) = service( + UUID, + &[shipping(DARWIN, SHIPPED), shipping(LINUX, b"other\n")], + ) + .await; + let (code, detail) = + unwrap_refused(vendor(&root, ¬_installed(&root), &record, &cfg, false).await); + assert_eq!(code, "vendor_prebuilt_required"); + assert!(detail.contains("does not carry its patch"), "{detail}"); + assert_eq!(ts::tree_snapshot(&root), before); + } + /// An installed ruby-platform copy of the gem (compiled on this /// machine) beside a lock that pins platform gems: the extensions /// its gemspec declares are not the prebuilt trees', so they are no diff --git a/crates/socket-patch-core/src/vendor/verify.rs b/crates/socket-patch-core/src/vendor/verify.rs index 93fdcd46b..a6ae72ade 100644 --- a/crates/socket-patch-core/src/vendor/verify.rs +++ b/crates/socket-patch-core/src/vendor/verify.rs @@ -221,6 +221,14 @@ pub async fn verify_vendored_patch_record( let is_zip = path_str.ends_with(".whl") || path_str.ends_with(".nupkg"); if !is_tarball && !is_zip { let cargo_uuid = (entry.ecosystem == "cargo").then_some(entry.uuid.as_str()); + // A vendored tree of a gem carries the stub gemspec in place of a + // `.gemspec` the gem ships: a patch that names that file is + // not held to it there (see `gem::files_beside_stub`). + let gem_name = match entry.ecosystem.as_str() { + "gem" => crate::utils::purl::parse_gem_purl(&entry.base_purl).map(|(name, _)| name), + _ => None, + }; + let held = |files| held_files(files, gem_name.as_deref()); match gem_platform_trees(&artifact, entry).await? { // A gem vendored as precompiled platform gems: bundler loads // the tree of whichever platform it runs on, so every one of @@ -246,10 +254,18 @@ pub async fn verify_vendored_patch_record( }, None => &record.files, }; - verify_platform_tree(tree, files, voucher).await?; + verify_platform_tree(tree, &held(files), voucher).await?; + } + } + None => { + let files = held(&record.files); + // Nothing left to hash and no inventory to vouch for the + // tree: never attested, like a record with no files. + if files.is_empty() && entry.artifact.file_inventory.is_none() { + return Err("no_files".to_string()); } + verify_dir_members(&artifact, &files, cargo_uuid).await? } - None => verify_dir_members(&artifact, record, cargo_uuid).await?, } // Whole-tree cross-check: a dir-shaped artifact's bytes are covered // by NO lockfile integrity (bundler path sources, cargo path deps, @@ -317,6 +333,19 @@ pub async fn verify_vendored_patch_record( super::bun_workspace::verify(project_root, entry).await } +/// The files of a patch a dir-shaped artifact is held to: all of `files`, +/// or for a vendored tree of the gem `gem_name` those beside its stub +/// gemspec. +fn held_files<'f>( + files: &'f HashMap, + gem_name: Option<&str>, +) -> std::borrow::Cow<'f, HashMap> { + match gem_name { + Some(name) => super::gem::files_beside_stub(files, name), + None => std::borrow::Cow::Borrowed(files), + } +} + /// Dir-shaped ecosystems (cargo/golang/composer/gem): hash files in place, /// reusing the hardened per-file verifier (it normalizes manifest keys and /// fail-closes on path-escaping keys). A cargo copy's `Cargo.toml` carries @@ -329,10 +358,10 @@ pub async fn verify_vendored_patch_record( /// refuses it. async fn verify_dir_members( dir: &Path, - record: &PatchRecord, + files: &HashMap, cargo_uuid: Option<&str>, ) -> Result<(), String> { - for (file_name, info) in &record.files { + for (file_name, info) in files { let result = verify_file_content(dir, file_name, info).await; match result.status { VerifyStatus::AlreadyPatched => continue, From 218db94d6fbfbaa0b84dd99927d2bd5680674f70 Mon Sep 17 00:00:00 2001 From: Mikola Lysenko Date: Thu, 3 Dec 2026 11:19:58 -0500 Subject: [PATCH 2/2] Hold a copy with only its stub to show to the inventory Review follow-ups for the vendored gem fix: - A re-run over a copy whose patch names nothing but the gem's own `.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 `.gemspec`, or that every file of the patch is hashed. Co-Authored-By: Claude Fable 5.2 --- crates/socket-patch-core/src/vendor/gem.rs | 85 +++++++++++++++++-- crates/socket-patch-core/src/vendor/verify.rs | 10 ++- 2 files changed, 83 insertions(+), 12 deletions(-) diff --git a/crates/socket-patch-core/src/vendor/gem.rs b/crates/socket-patch-core/src/vendor/gem.rs index 47e7b608c..9c3df3c79 100644 --- a/crates/socket-patch-core/src/vendor/gem.rs +++ b/crates/socket-patch-core/src/vendor/gem.rs @@ -771,9 +771,27 @@ async fn gem_prelude( // runs second so a hash mismatch short-circuits it. copy_ok = match locked.platforms.as_slice() { [] => { - copy_matches_after_hashes(©_dir, &files_beside_stub(&record.files, name)) - .await - && stub_gemspec_valid(©_dir, name).await + let held = files_beside_stub(&record.files, name); + let tree_ok = match held.is_empty() && !record.files.is_empty() { + // The patch names the stub's path alone, so no file + // of the copy is left to hash: the ledger's + // inventory vouches for the copy, or nothing does. + true => { + let inventory = recorded_copy(project_root, &record.uuid, ©_rel) + .await + .and_then(|entry| entry.artifact.file_inventory); + match inventory { + Some(inventory) => { + super::verify::verify_dir_inventory(©_dir, &inventory, None) + .await + .is_ok() + } + None => false, + } + } + false => copy_matches_after_hashes(©_dir, &held).await, + }; + tree_ok && stub_gemspec_valid(©_dir, name).await } platforms => { let recorded = recorded_copy(project_root, &record.uuid, ©_rel).await; @@ -1040,11 +1058,16 @@ pub(super) fn is_platform_dir_name(platform: &str) -> bool { /// from is held to every file of the patch before the stub is written /// ([`gem_service_copy`], [`stage_platform_builds`]); afterwards the stub's /// own bytes are for [`stub_gemspec_valid`] and the ledger's inventory. +/// The path is matched exactly, as the gem's name spells it. pub(super) fn files_beside_stub<'f>( files: &'f HashMap, name: &str, ) -> std::borrow::Cow<'f, HashMap> { - let is_stub = |key: &str| normalize_file_path(key).strip_suffix(".gemspec") == Some(name); + // A name that is no plain gem token (one with a `/` would name a file + // below the root) names no stub: nothing is left out. + let is_stub = |key: &str| { + is_plain_gem_token(name) && normalize_file_path(key).strip_suffix(".gemspec") == Some(name) + }; match files.keys().any(|key| is_stub(key)) { false => std::borrow::Cow::Borrowed(files), true => std::borrow::Cow::Owned( @@ -1389,7 +1412,7 @@ pub async fn vendor_gem_builds<'a>( )); } return done( - already_patched_result(purl, copy_dir, &record.files), + already_patched_result(purl, copy_dir, &files_beside_stub(&record.files, name)), None, warnings, ); @@ -1475,7 +1498,7 @@ pub async fn vendor_gem_builds<'a>( }; } return done( - super::common::preview_result(purl, copy_dir, &record.files), + super::common::preview_result(purl, copy_dir, &files_beside_stub(&record.files, name)), None, Vec::new(), ); @@ -1808,8 +1831,9 @@ pub(super) async fn gem_service_copy( let policy = ServicePolicy::Refused; // Extract the patched `.gem`'s data.tar.gz into a STAGE sibling, add the - // stub as `.gemspec` (a `.gem`'s data.tar.gz never carries one — - // the gemspec lives in metadata.gz), and swap it into the copy dir only + // stub as `.gemspec` (a path source needs one: a `.gem` carries + // its gemspec in metadata.gz, and a `.gemspec` among its files is + // whatever the gem's repository had), and swap it into the copy dir only // once fully verified — a failure then leaves any pre-existing (possibly // live-wired) copy untouched and no husk behind. let stage = stage_dir_for(copy_dir); @@ -2591,7 +2615,8 @@ async fn materialise_patched_copy( ), )); } - Ok((already_patched_result(purl, copy_dir, &record.files), built)) + let held = files_beside_stub(&record.files, name); + Ok((already_patched_result(purl, copy_dir, &held), built)) } /// Revert a gem vendor entry: restore the Gemfile line / delete the managed @@ -10586,6 +10611,48 @@ mod tests { verify(&root, &unrecorded, &nested).await, Err("file_not_found".to_string()) ); + // Nor does a name that is no plain gem token leave a file out. + assert_eq!(files_beside_stub(&nested.files, "spec/rack").len(), 2); + assert_eq!(files_beside_stub(&nested.files, "rack").len(), 2); + assert_eq!(files_beside_stub(&full.files, "rack").len(), 1); + } + + /// A re-run over a copy whose patch names nothing but the gem's own + /// `.gemspec` has no patched file to hash: the copy is in sync + /// while it matches the ledger's inventory, and is rebuilt when a file + /// of it changed or the ledger has no inventory. + #[tokio::test] + async fn a_rerun_holds_a_copy_with_only_its_stub_to_the_inventory() { + let (_tmp, root, installed, blobs, record) = fixture(GEMFILE_DIRECT, LOCK_DIRECT).await; + let mut record = naming_own_gemspec(record, SHIPPED_GEMSPEC); + record.files.retain(|file, _| file == "rack.gemspec"); + let gem = make_gem(&[("lib/rack.rb", PATCHED), ("rack.gemspec", SHIPPED_GEMSPEC)]); + let (sri, stub_sri) = (sri_sha512(&gem), sri_sha512(SERVICE_STUB)); + let server = wiremock::MockServer::start().await; + mount_gem_granted(&server, &gem, &sri, Some((SERVICE_STUB, &stub_sri))).await; + let cfg = gem_service_cfg(&server.uri(), VendorSource::Service, false); + let rerun = || run_vendor_service(&root, &blobs, &installed, &record, &cfg); + let rebuilt = |warnings: &[VendorWarning]| { + warnings.iter().any(|w| w.code == "vendor_artifact_rebuilt") + }; + + let (result, entry, _) = unwrap_done(rerun().await); + assert!(result.success, "{:?}", result.error); + let entry = entry.expect("a ledger entry"); + assert!(entry.artifact.file_inventory.is_some()); + + // No ledger entry yet: nothing vouches for the copy. + let (_, _, warnings) = unwrap_done(rerun().await); + assert!(rebuilt(&warnings), "{warnings:?}"); + + crate::vendor::test_support::persist(&root, PURL, entry).await; + let (_, again, warnings) = unwrap_done(rerun().await); + assert!(again.is_none() && warnings.is_empty(), "{warnings:?}"); + + tokio::fs::write(copy_lib(&root), PRISTINE).await.unwrap(); + let (_, _, warnings) = unwrap_done(rerun().await); + assert!(rebuilt(&warnings), "{warnings:?}"); + assert_eq!(tokio::fs::read(copy_lib(&root)).await.unwrap(), PATCHED); } /// `auto` + the same layout mismatch warns and falls back to the local diff --git a/crates/socket-patch-core/src/vendor/verify.rs b/crates/socket-patch-core/src/vendor/verify.rs index a6ae72ade..4d5332cf7 100644 --- a/crates/socket-patch-core/src/vendor/verify.rs +++ b/crates/socket-patch-core/src/vendor/verify.rs @@ -156,7 +156,10 @@ pub async fn vendored_jar_versioned_copies( } /// `Ok(())` iff every `record.files` entry hashes to its `afterHash` inside -/// the vendored artifact named by `entry`. The error is a stable routing tag +/// the vendored artifact named by `entry`. One entry is left out for a +/// vendored tree of a gem: the gem's own `.gemspec`, where the tree +/// carries the stub gemspec (`gem::files_beside_stub`); the ledger's +/// inventory covers that file. The error is a stable routing tag /// (see module docs) compatible with `vex::verify::FailedPatch.reason`. pub async fn verify_vendored_patch_record( project_root: &Path, @@ -1191,8 +1194,9 @@ async fn untagged_manifest_sha256(dir: &Path, uuid: &str) -> Option { /// routing tag. #[derive(Debug, Clone, PartialEq, Eq)] pub enum ArtifactHealth { - /// Exists and every record file hashes to its afterHash (and, for - /// file-shaped artifacts, the whole file matches the ledger sha256). + /// Exists and every record file hashes to its afterHash, as + /// [`verify_vendored_patch_record`] holds the artifact to them (and, + /// for file-shaped artifacts, the whole file matches the ledger sha256). Healthy, /// Nothing at the artifact path: rebuildable. Missing,