Skip to content
12 changes: 11 additions & 1 deletion crates/socket-patch-cli/src/commands/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,12 @@ async fn run_check(args: &VendorArgs) -> i32 {
failure = Some(unwired_check_failure(discovery, root, key, entry).await);
}
}
// Wired, and a NuGet lock pins other bytes: a restore takes those,
// whatever the config routes. The pin is read as the writer reads
// it, after the liveness rule, which names a wiring that is gone.
if failure.is_none() && entry.ecosystem == "nuget" {
failure = vendor::nuget_feed::lock_pin_mismatch(entry, root).await;
}
// The committed copy and its `replace` can be intact while the
// project no longer builds: vendor/modules.txt or the go.mod
// requirements out of step with the `replace` (#343, #618).
Expand Down Expand Up @@ -4962,10 +4968,14 @@ pub(crate) async fn vendor_records_reusing(
// A dry run previews an in-sync package as `verified`
// (the backends cannot tell without writing); the
// ledger recording this exact patch is the tell.
// NuGet's backend can tell: it reports an in-sync
// package as `already_vendored` on a dry run too, lock
// pin included, so its `verified` preview is always a
// package the wet run writes.
let dry_previewed_in_sync = common.dry_run
&& event.action == PatchAction::Verified
&& lookup_entry(&state.entries, candidate)
.is_some_and(|e| e.uuid == record.uuid);
.is_some_and(|e| e.uuid == record.uuid && e.ecosystem != "nuget");
if dry_previewed_in_sync {
dry_in_sync += 1;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/socket-patch-cli/src/commands/vex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2875,7 +2875,7 @@ fn omission_phrase(reason: &str) -> &'static str {
name, or a link), so what installs is not what was checked"
}
"vendor_lock_pin_mismatch" => {
"the lockfile pins the vendored tarball to a sha512 that is not that file's, so \
"the lockfile pins the vendored artifact to a sha512 that is not that file's, so \
an install does not deliver it; re-run socket-patch scan --mode vendored"
}
"vendor_path_unsafe" => "the vendored artifact path is unsafe",
Expand Down
5 changes: 5 additions & 0 deletions crates/socket-patch-cli/tests/vendor/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ mod redirect_npm_allow_remote;
mod vendor_failure_paths;
mod vendor_gem_docker_layer;
mod vendor_gem_lockfile_only_e2e;
mod vendor_nuget_lock_pin;
mod vendor_render;
mod vendor_rerun_no_network_e2e;
mod vendor_silent_errors;

// It loads `prebuilt_common` for itself.
#[allow(clippy::duplicate_mod)]
#[path = "../vendor_ecosystem_fixtures/mod.rs"]
mod fx;
#[path = "../prebuilt_common/mod.rs"]
mod prebuilt_common;
#[path = "../vlt_hosted_common/mod.rs"]
Expand Down
213 changes: 213 additions & 0 deletions crates/socket-patch-cli/tests/vendor/vendor_nuget_lock_pin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
//! A vendored NuGet package keeps its id and version, so the lock's
//! `contentHash` is all that ties a restore to the vendored nupkg while the
//! global packages folder holds the upstream package: `vendor` pins it, and
//! `dotnet restore --force-evaluate` there (NuGet's remedy for the NU1403
//! the pin raises) writes the upstream hash back and restores the upstream
//! package. `vendor --check`, `vex` and `vendor --dry-run` must read the pin
//! the way the writer does.

use crate::fx::Fixture;

const LOCK: &str = "packages.lock.json";
const ALPHA: &str = "pkg:nuget/Fx.Alpha@1.0.0";
const BETA: &str = "pkg:nuget/Fx.Beta@1.0.0";

/// The fixture, vendored, with the lock as it was before (every pin the
/// upstream nupkg's) and as `vendor` left it (every pin the vendored one's).
fn vendored() -> (Fixture, String, String) {
let f = Fixture::new("nuget");
let upstream = std::fs::read_to_string(f.root.join(LOCK)).unwrap();
let (code, stdout, stderr) = f.vendor(&[], &[]);
assert_eq!(code, 0, "{stdout}\n{stderr}");
let pinned = std::fs::read_to_string(f.root.join(LOCK)).unwrap();
assert_ne!(pinned, upstream, "vendor pins the lock");
(f, upstream, pinned)
}

/// `vendor --check`: the exit code and both output streams.
fn check(f: &Fixture) -> (i32, String) {
let (code, stdout, stderr) = f.run(&["vendor", "--check"], &[]);
(code, format!("{stdout}\n{stderr}"))
}

/// `vex`: the purls it attests `not_affected`, and both output streams.
fn vex(f: &Fixture) -> (Vec<String>, String) {
let (_, stdout, stderr) = f.run(&["vex", "--product", "pkg:nuget/app@1.0.0"], &[]);
let attested = [ALPHA, BETA]
.into_iter()
.filter(|purl| {
serde_json::from_str::<serde_json::Value>(&stdout)
.ok()
.and_then(|doc| doc["statements"].as_array().cloned())
.unwrap_or_default()
.iter()
.any(|statement| {
statement["status"] == "not_affected"
&& statement["products"]
.to_string()
.to_lowercase()
.contains(&purl.to_lowercase())
})
})
.map(str::to_string)
.collect();
(attested, format!("{stdout}\n{stderr}"))
}

/// `vendor --dry-run`: both output streams.
fn dry_run(f: &Fixture) -> String {
let (code, stdout, stderr) = f.run(&["vendor", "--dry-run"], &[]);
assert_eq!(code, 0, "{stdout}\n{stderr}");
format!("{stdout}\n{stderr}")
}

const NOTHING_TO_DO: &str = "All 2 packages are already vendored; nothing to do.";

/// The control: the lock `vendor` wrote is verified and attested, and a
/// dry run has nothing to do.
#[test]
fn nuget_lock_pinned_to_the_vendored_nupkg_is_verified() {
let (f, ..) = vendored();
let (code, out) = check(&f);
assert_eq!(code, 0, "{out}");
let (attested, out) = vex(&f);
assert_eq!(attested, [ALPHA, BETA], "{out}");
let out = dry_run(&f);
assert!(out.contains(NOTHING_TO_DO), "{out}");
}

/// What `dotnet restore --force-evaluate` leaves over a global packages
/// folder that holds the upstream packages: the config still maps each id
/// to its vendored feed, and the lock pins the upstream `contentHash` again.
#[test]
fn nuget_lock_pinned_to_other_bytes_is_not_verified() {
let (f, upstream, pinned) = vendored();
std::fs::write(f.root.join(LOCK), &upstream).unwrap();

let (code, out) = check(&f);
let (attested, vex_out) = vex(&f);
assert_eq!(
(code, attested.len()),
(1, 0),
"(vendor --check exit code, not_affected statements)\n{out}\n{vex_out}"
);
for purl in [ALPHA, BETA] {
assert!(
out.contains(&format!("{purl}: {LOCK} pins ")),
"the failure names the lock: {out}"
);
}
assert!(
vex_out.contains("(vendor_lock_pin_mismatch): Fx.Alpha@1.0.0, Fx.Beta@1.0.0")
&& vex_out.contains("pins the vendored artifact to a sha512")
&& !vex_out.contains("tarball"),
"{vex_out}"
);

// The remedy the failure names: a wet run pins the lock again. A dry
// run says so, and writes nothing.
assert!(
out.contains("re-run `socket-patch scan --mode vendored` (or `socket-patch vendor`"),
"{out}"
);
let out = dry_run(&f);
assert!(
out.contains("Would vendor 2 packages") && !out.contains("nothing to do"),
"{out}"
);
assert_eq!(
std::fs::read_to_string(f.root.join(LOCK)).unwrap(),
upstream
);
// NuGet's record of the bytes it extracted the global copy from.
let global = f.store.join("nuget/fx.alpha/1.0.0");
std::fs::write(
global.join(".nupkg.metadata"),
r#"{"version":2,"contentHash":"UPSTREAM=="}"#,
)
.unwrap();
let (code, stdout, stderr) = f.vendor(&[], &[]);
assert_eq!(code, 0, "{stdout}\n{stderr}");
assert!(
stdout.contains("a lock pinned Fx.Alpha@1.0.0 to other bytes")
&& !stdout.contains("missing or stale"),
"the run says what was stale: {stdout}"
);
assert!(
stdout.contains("vendor_nuget_stale_global_package")
&& stdout.contains(&global.display().to_string()),
"the run names the copy a restore still takes: {stdout}"
);
assert_eq!(std::fs::read_to_string(f.root.join(LOCK)).unwrap(), pinned);
let (code, out) = check(&f);
assert_eq!(code, 0, "{out}");
let (attested, out) = vex(&f);
assert_eq!(attested, [ALPHA, BETA], "{out}");
}

/// One package's pin is not another's: only the package whose entry pins
/// other bytes fails the check and loses its statement.
#[test]
fn nuget_lock_pin_is_judged_per_package() {
let (f, upstream, pinned) = vendored();
let upstream: serde_json::Value = serde_json::from_str(&upstream).unwrap();
let mut lock: serde_json::Value = serde_json::from_str(&pinned).unwrap();
let hash =
|doc: &serde_json::Value| doc["dependencies"]["net8.0"]["Fx.Alpha"]["contentHash"].clone();
assert_ne!(hash(&lock), hash(&upstream));
lock["dependencies"]["net8.0"]["Fx.Alpha"]["contentHash"] = hash(&upstream);
std::fs::write(
f.root.join(LOCK),
serde_json::to_string_pretty(&lock).unwrap(),
)
.unwrap();

let (code, out) = check(&f);
assert_eq!(code, 1, "{out}");
assert!(out.contains(&format!("{ALPHA}: {LOCK} pins ")), "{out}");
assert!(
out.contains(&format!("{BETA}: committed artifact and wiring verified")),
"{out}"
);
let (attested, out) = vex(&f);
assert_eq!(attested, [BETA], "{out}");
}

/// The pin is judged on a package that is wired. With the config put back
/// nothing routes the package to the vendored feed, whatever the lock pins:
/// the check names the wiring that is gone, as `vex` does, and a dry run
/// would vendor both again. The same once the lock is put back too.
#[test]
fn nuget_package_with_no_wiring_left_is_reported_as_unwired() {
let f = Fixture::new("nuget");
let config = std::fs::read_to_string(f.root.join("nuget.config")).unwrap();
let lock = std::fs::read_to_string(f.root.join(LOCK)).unwrap();
let (code, stdout, stderr) = f.vendor(&[], &[]);
assert_eq!(code, 0, "{stdout}\n{stderr}");

let unwired = |state: &str| {
let (code, out) = check(&f);
assert_eq!(code, 1, "{state}: {out}");
for purl in [ALPHA, BETA] {
assert!(
out.contains(&format!("{purl}: wiring missing")),
"{state}: the failure is the liveness rule's: {out}"
);
}
assert!(!out.contains("contentHash"), "{state}: {out}");
let (attested, out) = vex(&f);
assert!(
attested.is_empty() && out.contains("(vendor_unwired)"),
"{state}: {out}"
);
let out = dry_run(&f);
assert!(
out.contains("Would vendor 2 packages") && !out.contains("nothing to do"),
"{state}: {out}"
);
};
std::fs::write(f.root.join("nuget.config"), &config).unwrap();
unwired("the config put back, the lock still pinned");
std::fs::write(f.root.join(LOCK), &lock).unwrap();
unwired("the config and the lock put back");
}
Loading
Loading