Skip to content

Commit f665796

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/npm-discovery-false-attestation
# Conflicts: # crates/socket-patch-cli/CLI_CONTRACT.md # crates/socket-patch-cli/src/commands/vex_sources.rs # crates/socket-patch-core/src/vex/discover/mod.rs
2 parents e8b0934 + c2be854 commit f665796

8 files changed

Lines changed: 564 additions & 90 deletions

File tree

‎crates/socket-patch-cli/CLI_CONTRACT.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,8 @@ A reverted lockfile plus a leftover ledger or artifact therefore stops attesting
579579
| `patched_ref_invalid` | A Socket-shaped reference failed validation or is not live wiring: unsafe coordinates, a non-canonical uuid, a leaf naming another package, a path escaping the root, an inert or orphaned entry. |
580580
| `patched_ref_unattributable` | A Socket patch uuid cannot be tied to exactly one artifact / version. Examples: a maven repository no pin names, a nuget source without a mapping, a lock contested by another lock. |
581581
| `sbt_owned_file_modified` | `socket-patch.sbt` / `socket-patch-vendor.sbt` is not byte-for-byte what socket-patch generates; none of its pins is discovered. |
582-
| `sbt_resolution_unverified` | An sbt pin the build's local resolution evidence (`target/`, after `sbt update`) does not verify: the reference stays, without the lockfile basis (a hosted pin the build provably bypasses is also omitted, `vex_sbt_pin_bypassed`). |
583-
| `vendored_tree_missing` | A `socket-patch-vendor.sbt` pin whose `.socket/vendor/maven2/` file is missing or modified. |
582+
| `sbt_resolution_unverified` | An sbt pin the build's local resolution evidence (`target/`, after `sbt update`) does not verify, or a build-wide blocker applies (stale or partial evidence, project definitions that cannot be read statically, a `dependencyOverrides` / `onLoad` / `resolvers` reassignment, a `build.sbt.lock`); the detail names the cause and its remedy, or that it has none. A hosted reference stays, without the lockfile basis (a hosted pin the build provably bypasses is also omitted, `vex_sbt_pin_bypassed`); a vendored pin is not attested (`vex_sbt_resolution_unverified`). |
583+
| `vendored_tree_missing` | A `socket-patch-vendor.sbt` pin whose `.socket/vendor/maven2/` file is missing or modified; it is not attested (`vex_sbt_vendored_tree_missing`). |
584584

585585
Human mode also prints `Note:` lines: superseded records, fetch failures, `--offline` withholding fetches, conflict details, and why a claim is dead. While patch records are fetched, a terminal shows a transient `Fetching patch records... (n/N)` status line on stderr (never under `--json` / `--silent`).
586586

@@ -2691,6 +2691,8 @@ match it, withholds the statement:
26912691
| `vex_maven_classifier_unpatched` | run warning and `failed[].reason` | A hosted Maven pin's GA also has a live executable `<classifier>` copy (not `sources` / `javadoc`) at a version other than the pin, which the classifier-less pin does not retarget and the grant does not serve; no statement while it does. `list`, `rollback` and `remove` still find the pin. |
26922692
| `vex_pnpm_git_branch_lockfile` | run warning and `failed[].reason` | An npm pin is wired in a pnpm lock, but a `pnpm-lock.<branch>.yaml` exists, which pnpm installs the branch from instead when `gitBranchLockfile` is on (#556); the branch lock's presence withholds the statement whatever the setting says, until the branch locks are folded in and deleted. `list`, `rollback` and `remove` still find the pin, and `vendor --check` and `scan` still treat the wiring as live. |
26932693
| `vex_npm_patched_dependency` | run warning and `failed[].reason` | An npm or Bun pin is wired, but the project patches that package itself (`patchedDependencies`, or npm's lock-entry `patched` record; #711, #367), which hosted and vendored mode never wire; no statement while it does. `list`, `rollback` and `remove` still find the pin. |
2694+
| `vex_sbt_resolution_unverified` | run warning and `failed[].reason` | A vendored sbt pin (`socket-patch-vendor.sbt`) whose resolution the build's local evidence does not verify, by the same check and build-wide blockers as a hosted sbt pin (no evidence or evidence older than a build source, a declared project with none, a `dependencyOverrides` / `resolvers` reassignment, a `build.sbt.lock`, project definitions that cannot be read statically, another version or other bytes resolved); no statement while it holds. The note names the cause and its remedy: `sbt update` clears stale or missing evidence, keeping the setting's existing entries (appending with `+=` / `++=` in place of a `:=`, `~=` or `--=`) clears a reassignment, removing what forces another version clears a version conflict, and a build whose projects cannot be read statically (a computed `Project(...)`, `projectMatrix`, `crossProject`) is never attested. `vendor --check` and `scan` still treat the wiring as live. |
2695+
| `vex_sbt_vendored_tree_missing` | run warning and `failed[].reason` | A vendored sbt pin whose committed `.socket/vendor/maven2/` file is missing or does not hash to the pinned sha256 (sbt's load-time check fails the build); no statement until the tree is restored. |
26942696
| `vex_nuget_lock_other_version` | run warning and `failed[].reason` | A NuGet Socket source maps an id that a governed `packages.lock.json` also locks at a version other than the patched one; the mapping routes every version to a source that serves only the patched one, so that target framework cannot restore, and both writers refuse such a lock. No statement until every target framework locks only the patched version. `list`, `rollback` and `remove` still find the wiring. |
26952697
| `vex_sbt_pin_bypassed` | run warning and `failed[].reason` | A hosted sbt pin (`socket-patch.sbt`) whose bytes the build provably does not use, and that no current evidence verifies: a build source reassigns `dependencyOverrides` (replacing the generated override) or reassigns `onLoad` without keeping its previous value (dropping the load-time verifier and resolver order), or a resolution record newer than the generated files and every build source shows the GA's base version, or other bytes at the pinned version from outside the pin repository. The copy downloaded into `.socket/sbt-hosted` then proves nothing, so no statement. `list`, `rollback` and `remove` still find the pin. |
26962698
| `vex_gradle_derived_cache_unchecked` | run warning | The derived-cache walk was cut short (a very large transforms cache); the statement is still emitted, the copies the walk did reach were checked. |

‎crates/socket-patch-cli/src/commands/vex_sources.rs‎

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,14 @@ const NOTE_PNPM_GIT_BRANCH_LOCKFILE: &str = "vex_pnpm_git_branch_lockfile";
239239
/// Omission tag and note: an npm or Bun pin's package is one the project
240240
/// patches itself (`patchedDependencies`, `vex::Unattested`).
241241
const NOTE_NPM_PATCHED_DEPENDENCY: &str = "vex_npm_patched_dependency";
242+
/// Omission tag and note: a vendored sbt pin the build's local resolution
243+
/// evidence does not verify, or under a build-wide blocker
244+
/// (`vex::Unattested`, `UnattestedKind::SbtResolutionUnverified`).
245+
const NOTE_SBT_RESOLUTION_UNVERIFIED: &str = "vex_sbt_resolution_unverified";
246+
/// Omission tag and note: a vendored sbt pin's committed tree file is
247+
/// missing or modified (`vex::Unattested`,
248+
/// `UnattestedKind::SbtVendoredTreeMissing`).
249+
const NOTE_SBT_VENDORED_TREE_MISSING: &str = "vex_sbt_vendored_tree_missing";
242250
/// Omission tag and note: a NuGet Socket source's id is also locked at a
243251
/// version other than the patched one, which that source cannot serve
244252
/// (`vex::Unattested`).
@@ -307,6 +315,17 @@ fn unattested_note(kind: UnattestedKind) -> (&'static str, &'static str) {
307315
npm pin an earlier hosted run wrote fails `npm ci` with `EPATCHFAILED` until \
308316
`socket-patch rollback <uuid>` restores the registry entry",
309317
),
318+
UnattestedKind::SbtResolutionUnverified => (
319+
NOTE_SBT_RESOLUTION_UNVERIFIED,
320+
"not attested while that holds (the detail names the remedy, or that there is \
321+
none); the wiring itself is intact, so `vendor --check` and `scan` still treat it \
322+
as live",
323+
),
324+
UnattestedKind::SbtVendoredTreeMissing => (
325+
NOTE_SBT_VENDORED_TREE_MISSING,
326+
"not attested while the committed tree differs from the pin (restore \
327+
`.socket/vendor/maven2` from version control, or re-run `socket-patch vendor`)",
328+
),
310329
UnattestedKind::NugetLockOtherVersion => (
311330
NOTE_NUGET_LOCK_OTHER_VERSION,
312331
"not attested until every target framework locks only the patched version (align \
@@ -326,6 +345,39 @@ fn unattested_note(kind: UnattestedKind) -> (&'static str, &'static str) {
326345
}
327346
}
328347

348+
/// The [`Unattested`](socket_patch_core::vex::Unattested) mark discovery
349+
/// put on patch `uuid` for `purl`'s package, if any.
350+
fn unattested_mark<'d>(
351+
discovery: &'d Discovery,
352+
purl: &str,
353+
uuid: &str,
354+
) -> Option<&'d socket_patch_core::vex::Unattested> {
355+
let pkg = canonical_base_purl(purl);
356+
discovery
357+
.unattested
358+
.iter()
359+
.find(|u| u.uuid == uuid && PurlKey::same(&u.purl, &pkg))
360+
}
361+
362+
/// Omit `key` for the mark `u`: its `failed[]` entry and the note naming
363+
/// why and the remedy.
364+
fn gate_unattested(
365+
key: &str,
366+
u: &socket_patch_core::vex::Unattested,
367+
gated: &mut Vec<FailedPatch>,
368+
notes: &mut Vec<PlanNote>,
369+
) {
370+
let (code, remedy) = unattested_note(u.kind);
371+
gated.push(failed(key, code));
372+
notes.push(PlanNote {
373+
code,
374+
detail: format!(
375+
"{key}: patch {} is wired, but {}; {remedy}",
376+
u.uuid, u.detail
377+
),
378+
});
379+
}
380+
329381
/// One hosted-basis view purl's wiring (see [`Plan::hosted`]).
330382
#[derive(Debug, Clone, PartialEq, Eq)]
331383
pub(crate) struct HostedWiring {
@@ -521,15 +573,7 @@ pub(crate) async fn plan(
521573
first
522574
}
523575
};
524-
let (code, remedy) = unattested_note(u.kind);
525-
gated.push(failed(&c.key, code));
526-
notes.push(PlanNote {
527-
code,
528-
detail: format!(
529-
"{}: patch {} is wired, but {}; {remedy}",
530-
c.key, c.uuid, u.detail
531-
),
532-
});
576+
gate_unattested(&c.key, u, &mut gated, &mut notes);
533577
false
534578
});
535579
for (key, old_uuid, wired) in &superseded {
@@ -562,6 +606,7 @@ pub(crate) async fn plan(
562606
continue;
563607
}
564608
}
609+
let mut took_over = false;
565610
let basis = if let Some(vref) = cand
566611
.discovered
567612
.iter()
@@ -598,6 +643,7 @@ pub(crate) async fn plan(
598643
cand.uuid = hosted.uuid.clone();
599644
cand.record = Some(hosted);
600645
cand.vendor_entry = None;
646+
took_over = true;
601647
Basis::Hosted {
602648
lockfile_basis: false,
603649
}
@@ -633,6 +679,21 @@ pub(crate) async fn plan(
633679
} else {
634680
Basis::Installed
635681
};
682+
// The vendored basis attests the ENTRY's patch, which need not be
683+
// the candidate's uuid (a manifest record under another uuid keeps
684+
// the key; a vendored sbt pin has no ref to swap it in): its mark
685+
// is checked against what is actually attested.
686+
// Likewise the hosted-over-vendored takeover attests the redirect
687+
// ledger's uuid, swapped in after the early gate ran.
688+
let attested = match &basis {
689+
Basis::Vendored(entry) => Some((entry.base_purl.as_str(), entry.uuid.as_str())),
690+
Basis::Hosted { .. } if took_over => Some((cand.key.as_str(), cand.uuid.as_str())),
691+
_ => None,
692+
};
693+
if let Some(u) = attested.and_then(|(purl, uuid)| unattested_mark(&discovery, purl, uuid)) {
694+
gate_unattested(&cand.key, u, &mut gated, &mut notes);
695+
continue;
696+
}
636697
based.push((cand, basis));
637698
}
638699

@@ -1652,6 +1713,29 @@ mod tests {
16521713
let dead = plan(&common(tmp.path()), mk(), &[], &RunApiClient::new()).await;
16531714
assert!(dead.view.patches.is_empty());
16541715
assert_eq!(dead.gated, vec![failed(key, VENDOR_UNWIRED)]);
1716+
1717+
// The takeover attests the redirect ledger's uuid, swapped in after
1718+
// the early mark gate ran: a mark on it still gates.
1719+
let mut marked = mk();
1720+
marked
1721+
.discovery
1722+
.unattested
1723+
.push(socket_patch_core::vex::Unattested {
1724+
purl: key.into(),
1725+
uuid: U2.into(),
1726+
file: "package-lock.json".into(),
1727+
detail: "package-lock.json bypasses it".into(),
1728+
kind: UnattestedKind::LockAboveBase,
1729+
});
1730+
let gated = plan(
1731+
&common(tmp.path()),
1732+
marked,
1733+
&[key.to_string()],
1734+
&RunApiClient::new(),
1735+
)
1736+
.await;
1737+
assert!(gated.view.patches.is_empty(), "{:?}", gated.view.patches);
1738+
assert_eq!(gated.gated, vec![failed(key, NOTE_LOCK_ABOVE_BASE)]);
16551739
}
16561740

16571741
/// Core discover rule 11 through the plan: a vendor ledger entry and a

‎crates/socket-patch-cli/tests/e2e_vex_lockfile/sbt_vendored.rs‎

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@
55
//!
66
//! The suffixed tree path carries no full uuid, so the generated file is no
77
//! `.socket/vendor/<eco>/<uuid>/` reference: a vendored sbt pin is attested
8-
//! through the vendor ledger `vendor` writes, and the extractor diagnoses
9-
//! what would make that attestation a lie. Each cell vendors for real
8+
//! through the vendor ledger `vendor` writes, and only when the extractor
9+
//! finds nothing that would make that attestation a lie (the hosted sbt
10+
//! evidence gate). Each cell vendors for real
1011
//! (`vendor` over the sbt 1.13.0 probe build, the installed jar in an
1112
//! isolated Coursier cache), drops `.socket/manifest.json`, and runs
1213
//! `vex --offline` (the ledger embeds the record):
1314
//!
1415
//! | cell | state | expected |
1516
//! |---|---|---|
1617
//! | a | after `sbt update` (the evidence resolves the suffixed version from the tree) | attested, no sbt diagnostic |
17-
//! | b | before `sbt update` (the evidence still resolves the base version) | `sbt_resolution_unverified` |
18+
//! | b | before `sbt update` (the evidence still resolves the base version) | `sbt_resolution_unverified`, `vex_sbt_resolution_unverified`, not attested |
1819
//! | c | a tree jar tampered | `vendored_tree_missing`, not attested |
1920
//! | d | the generated file edited | `sbt_owned_file_modified` |
2021
//! | e | no ledger | nothing attested; the tree check still runs |
22+
//! | f | after `sbt update`, then a `dependencyOverrides :=` | `vex_sbt_resolution_unverified` naming it, not attested |
23+
//! | g | before `sbt update`, the manifest back (same or another uuid) | `vex_sbt_resolution_unverified`, not attested |
2124
22-
use crate::common::envelope::all_codes;
25+
use crate::common::envelope::{all_codes, warning_codes};
2326
use crate::common::git_sha256;
2427

2528
use std::path::{Path, PathBuf};
@@ -42,6 +45,8 @@ const CENTRAL: &str = "repo1.maven.org/maven2";
4245
const MEMBER: &str = "META-INF/NOTICE.txt";
4346
const VULN: &str = "GHSA-sbtv-test-xxxx";
4447
const VENDORED_FILE: &str = "socket-patch-vendor.sbt";
48+
/// A manifest uuid other than the vendored one.
49+
const OTHER_UUID: &str = "9e8d7c6b-5a49-4382-9f0e-2a3b4c5d6e7f";
4550

4651
fn tree_jar(proj: &Path) -> PathBuf {
4752
proj.join(".socket/vendor/maven2/com/google/code/gson/gson")
@@ -69,6 +74,8 @@ struct Fx {
6974
_tmp: tempfile::TempDir,
7075
proj: PathBuf,
7176
home: SbtHome,
77+
/// The manifest `vendor` ran with.
78+
manifest: Value,
7279
}
7380

7481
impl Fx {
@@ -111,6 +118,7 @@ impl Fx {
111118
_tmp: tmp,
112119
proj,
113120
home,
121+
manifest,
114122
};
115123
let (code, env) = fx.run(&["vendor"]);
116124
assert_eq!(code, Some(0), "vendor: {env}");
@@ -120,6 +128,17 @@ impl Fx {
120128
fx
121129
}
122130

131+
/// Put the manifest back, its gson record filed under `uuid`.
132+
fn restore_manifest(&self, uuid: &str) {
133+
let mut manifest = self.manifest.clone();
134+
manifest["patches"][GSON.purl()]["uuid"] = uuid.into();
135+
std::fs::write(
136+
self.proj.join(".socket/manifest.json"),
137+
serde_json::to_vec_pretty(&manifest).unwrap(),
138+
)
139+
.unwrap();
140+
}
141+
123142
/// What `sbt update` records after vendoring.
124143
fn sbt_update(&self) {
125144
let changed = record_pinned_resolution(
@@ -197,6 +216,17 @@ fn attested(doc: Option<&Value>) -> bool {
197216
})
198217
}
199218

219+
/// The details of the top-level warnings coded `code`.
220+
fn warning_details(env: &Value, code: &str) -> Vec<String> {
221+
env["warnings"]
222+
.as_array()
223+
.into_iter()
224+
.flatten()
225+
.filter(|w| w["code"] == code)
226+
.filter_map(|w| w["detail"].as_str().map(str::to_string))
227+
.collect()
228+
}
229+
200230
const SBT_DIAGS: &[&str] = &[
201231
"sbt_resolution_unverified",
202232
"vendored_tree_missing",
@@ -220,11 +250,66 @@ fn sbt_vendored_vex_attests_after_sbt_update() {
220250
#[test]
221251
fn sbt_vendored_vex_before_sbt_update_is_unverified() {
222252
let fx = Fx::vendored();
223-
let (_, env, _) = fx.vex();
253+
let (_, env, doc) = fx.vex();
254+
let got = all_codes(&env);
255+
assert!(
256+
got.contains(&"sbt_resolution_unverified".to_string()),
257+
"{env}"
258+
);
259+
assert!(
260+
got.contains(&"vex_sbt_resolution_unverified".to_string()),
261+
"{env}"
262+
);
263+
assert!(
264+
!attested(doc.as_ref()),
265+
"an unverified resolution is never attested: {doc:?}"
266+
);
267+
}
268+
269+
#[test]
270+
fn sbt_vendored_vex_withholds_under_an_overrides_reassignment() {
271+
let fx = Fx::vendored();
272+
fx.sbt_update();
273+
// Dated back to its old time, the edit stays older than the evidence
274+
// however slow the run (staleness would withhold the pin too).
275+
let build = fx.proj.join("build.sbt");
276+
let dated = std::fs::metadata(&build).unwrap().modified().unwrap();
277+
let mut text = std::fs::read_to_string(&build).unwrap();
278+
text.push_str("ThisBuild / dependencyOverrides := Seq()\n");
279+
std::fs::write(&build, text).unwrap();
280+
std::fs::File::options()
281+
.write(true)
282+
.open(&build)
283+
.unwrap()
284+
.set_modified(dated)
285+
.unwrap();
286+
let (_, env, doc) = fx.vex();
287+
let details = warning_details(&env, "vex_sbt_resolution_unverified");
224288
assert!(
225-
all_codes(&env).contains(&"sbt_resolution_unverified".to_string()),
289+
details.iter().any(|d| d.contains("`dependencyOverrides`")),
226290
"{env}"
227291
);
292+
assert!(
293+
!attested(doc.as_ref()),
294+
"an overrides reassignment replaces the pin: {doc:?}"
295+
);
296+
}
297+
298+
/// #2008 review: the manifest still records gson, under the vendored uuid
299+
/// or another one (the vendor entry, not the candidate's uuid, is what the
300+
/// vendored basis attests). Before `sbt update` neither attests.
301+
#[test]
302+
fn sbt_vendored_vex_with_a_manifest_still_needs_evidence() {
303+
let fx = Fx::vendored();
304+
for uuid in [UUID, OTHER_UUID] {
305+
fx.restore_manifest(uuid);
306+
let (_, env, doc) = fx.vex();
307+
assert!(
308+
warning_codes(&env).contains(&"vex_sbt_resolution_unverified".to_string()),
309+
"{uuid}: {env}"
310+
);
311+
assert!(!attested(doc.as_ref()), "{uuid}: {doc:?}");
312+
}
228313
}
229314

230315
#[test]

‎crates/socket-patch-core/src/crawlers/sbt_evidence.rs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,36 @@ pub(crate) fn stage_fixture(from: &Path, to: &Path) {
665665

666666
#[cfg(test)]
667667
mod tests;
668+
669+
/// Helpers for tests that stage sbt evidence on disk (this module's and
670+
/// `vex::discover::sbt`'s).
671+
#[cfg(test)]
672+
pub(crate) mod testing {
673+
use std::path::Path;
674+
use std::time::SystemTime;
675+
676+
/// Date `path` at `t`.
677+
pub(crate) fn set_mtime(path: &Path, t: SystemTime) {
678+
std::fs::OpenOptions::new()
679+
.write(true)
680+
.open(path)
681+
.unwrap()
682+
.set_modified(t)
683+
.unwrap();
684+
}
685+
686+
/// The `file:` URI sbt writes for `path`: `file:///C:/x` on Windows (a
687+
/// backslash is no URI separator, and an invalid JSON escape). `path`
688+
/// must not be a verbatim (`\\?\`) path, which sbt never records.
689+
pub(crate) fn file_uri(path: &Path) -> String {
690+
let text = path
691+
.to_string_lossy()
692+
.replace('\\', "/")
693+
.replace(' ', "%20");
694+
if text.starts_with('/') {
695+
format!("file://{text}")
696+
} else {
697+
format!("file:///{text}")
698+
}
699+
}
700+
}

0 commit comments

Comments
 (0)