diff --git a/crates/socket-patch-cli/CLI_CONTRACT.md b/crates/socket-patch-cli/CLI_CONTRACT.md index bc390b2e6..d81ecaad7 100644 --- a/crates/socket-patch-cli/CLI_CONTRACT.md +++ b/crates/socket-patch-cli/CLI_CONTRACT.md @@ -750,6 +750,8 @@ Every VEX statement's impact string records which patch-application mode persist **Identifier overrides.** `--product` / `--vex-product` and `--doc-id` / `--vex-doc-id` are emitted with surrounding whitespace trimmed (a blank value means unset). +**Statement products.** The first product of every statement is the project (`--product` or the detected one), with the statement's packages as its subcomponents. Each package then follows as a product of its own with no subcomponents, under its PURL and under the forms Trivy, Grype and Syft report it by: the PURL without qualifiers or subpath, and for a gem whose `platform` qualifier is not `ruby`, `pkg:gem/@-`. A form is left out of every statement of a vulnerability when it would stand for packages of that vulnerability with different statuses (an `affected` build and a `not_affected` one), and when it equals the project's identifier. Readers that walk `products[0].subcomponents` see the same packages as before. + **No product to detect.** The product is read from the git `origin` remote, then from the name and version in `package.json`, `pyproject.toml`, `Cargo.toml`, `go.mod`, `composer.json`, `pom.xml`, the root's single `.csproj` or its single `.gemspec`. An application with none of these (a Bundler or pip project in a directory that has no git remote) has no product, and the run stops with `product_undetected` (`vex`: exit 2; embedded `--vex`: the host command's exit 1). No product is assumed: a directory name is not an identity (one checkout is `app` in a container and `workspace` in CI, and unrelated projects share those names), so a document issued under it could be read as a statement about another project. The message says what detection read, names any of those files that exists without a usable name and version, and ends with the run's own flag and a value built from the directory name, ready to paste: `… Pass a name that identifies this project, for example: --product pkg:generic/my-app` (`--vex-product …` on an embedded run). A run started in the project directory says `the current directory` instead of `.`. **Patch hosts (manifest-less VEX).** A hosted lockfile reference counts only when it points at Socket's patch server or the operator's `--patch-server-url` / `SOCKET_PATCH_SERVER_URL` origin or under the `--patch-mirror-url` / `SOCKET_PATCH_MIRROR_URL` base. A hosted URL on any OTHER host — a staging patch server used without `--patch-server-url`, or a look-alike host — is not a hosted pin at all (v5.0: there is no redirect ledger to vouch for it), so `vex` does not attest it, and `list`, `rollback`, `remove`, `vendor` and `repair` do not see it either. Pass `--patch-server-url` for a non-production patch server. A pre-v5 redirect ledger still on disk does not change this: a record whose lockfile or recorded wiring resolves the package from a hosted-shaped URL on an origin this run was not given (or one carrying userinfo) is never attested from the ledger, whatever is installed and even under `--no-verify` / `--vex-no-verify`: `vex` reports it as `redirect_unwired`, with a `vex_claim_unwired` note naming the cause. (A patch that `apply`'s manifest also records is still verified as an agent-mode patch, on that record.) `scan` does not treat such wiring as gone: its takeover and hosted-wiring advisories still see it. diff --git a/crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs b/crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs index dea1f4b94..a73499602 100644 --- a/crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs +++ b/crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs @@ -1270,8 +1270,14 @@ impl Cell { let named = named.unwrap_or_default(); let purl = normalize_purl(claim.purl); assert_eq!(normalize_purl(named), purl, "{test}: {text}"); - let products = json!([{ "@id": PRODUCT, "subcomponents": [{ "@id": named }] }]); - assert_eq!(statement["products"], products, "{test}: {text}"); + let project = json!({ "@id": PRODUCT, "subcomponents": [{ "@id": named }] }); + assert_eq!(statement["products"][0], project, "{test}: {text}"); + let products = statement["products"].as_array().map(Vec::as_slice); + let packages = products.unwrap_or_default().get(1..).unwrap_or_default(); + assert!( + packages.iter().any(|p| p["@id"] == named), + "{test}: the package is a product of its own: {text}" + ); let (uuid, how) = (claim.uuid, claim.how.words()); if claim.affected() { assert_eq!(statement["status"], "affected", "{test}: {text}"); diff --git a/crates/socket-patch-cli/tests/e2e_embedded_vex.rs b/crates/socket-patch-cli/tests/e2e_embedded_vex.rs index 6159f8c2a..058482b82 100644 --- a/crates/socket-patch-cli/tests/e2e_embedded_vex.rs +++ b/crates/socket-patch-cli/tests/e2e_embedded_vex.rs @@ -176,7 +176,12 @@ fn assert_not_affected_statement( ); let products = stmt["products"].as_array().expect("statement.products"); - assert_eq!(products.len(), 1, "exactly one product per statement"); + assert_eq!( + products.len(), + 2, + "the project, then the patched package as a product of its own" + ); + assert_eq!(products[1]["@id"], expect_subcomponent); assert_eq!( products[0]["@id"], expect_product, "product comes from --vex-product" diff --git a/crates/socket-patch-cli/tests/e2e_vex.rs b/crates/socket-patch-cli/tests/e2e_vex.rs index 0d0b0c7ad..be00d1339 100644 --- a/crates/socket-patch-cli/tests/e2e_vex.rs +++ b/crates/socket-patch-cli/tests/e2e_vex.rs @@ -181,11 +181,13 @@ fn no_verify_emits_valid_openvex() { assert_eq!(s0["justification"], "inline_mitigations_already_exist"); let products = s0["products"].as_array().unwrap(); - assert_eq!(products.len(), 1); + assert_eq!(products.len(), 2); assert_eq!(products[0]["@id"], "pkg:npm/test-app@1.0.0"); let subs = products[0]["subcomponents"].as_array().unwrap(); assert_eq!(subs.len(), 1); assert_eq!(subs[0]["@id"], "pkg:npm/lodash@4.17.20"); + assert_eq!(products[1]["@id"], "pkg:npm/lodash@4.17.20"); + assert!(products[1].get("subcomponents").is_none()); maybe_validate_with_vexctl(&stdout); } diff --git a/crates/socket-patch-core/src/vex/build.rs b/crates/socket-patch-core/src/vex/build.rs index 5dac36818..ae1e12080 100644 --- a/crates/socket-patch-core/src/vex/build.rs +++ b/crates/socket-patch-core/src/vex/build.rs @@ -17,8 +17,17 @@ //! vulnerability ID, of its own, after the `not_affected` one of the same //! ID. Such statements are not merged, so that the action statement of //! each is about one subcomponent. +//! +//! Each statement also names every one of its packages as a product of +//! its own, after the project. Trivy and Grype look a finding up by the +//! package's PURL and never match the project, so without these a +//! scanner suppresses nothing. A package is named by its PURL and by the +//! forms those scanners report it under ([`scanner_forms`]). A form that +//! stands for packages with different statuses for one vulnerability (a +//! patched and an unpatched build of one gem) is left out, so the +//! scanner keeps reporting the finding. -use std::collections::{BTreeMap, BTreeSet, HashSet}; +use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use crate::manifest::schema::PatchManifest; use crate::vex::schema::{ @@ -230,8 +239,47 @@ pub fn build_document_with( return None; } + // (vuln-id, package form) -> the statuses the form stands for: + // `true` for `affected`, `false` for `not_affected`. + let mut form_statuses: HashMap<(&str, String), BTreeSet> = HashMap::new(); + for ((vuln_id, affected), group) in &grouped { + for purl in &group.subcomponents { + for form in package_forms(purl) { + form_statuses + .entry((vuln_id.as_str(), form)) + .or_default() + .insert(affected.is_some()); + } + } + } + let package_products: Vec> = grouped + .iter() + .map(|((vuln_id, _), group)| { + let forms: BTreeSet = group + .subcomponents + .iter() + .flat_map(|purl| package_forms(purl)) + .filter(|form| { + *form != opts.product_id + && form_statuses + .get(&(vuln_id.as_str(), form.clone())) + .is_some_and(|statuses| statuses.len() == 1) + }) + .collect(); + forms + .into_iter() + .map(|id| Product { + id, + identifiers: None, + hashes: None, + subcomponents: Vec::new(), + }) + .collect() + }) + .collect(); + let mut statements = Vec::with_capacity(grouped.len()); - for ((vuln_id, affected), group) in grouped { + for (((vuln_id, affected), group), packages) in grouped.into_iter().zip(package_products) { let subcomponents = group .subcomponents .into_iter() @@ -273,12 +321,14 @@ pub fn build_document_with( }, timestamp: Some(timestamp.clone()), last_updated: None, - products: vec![Product { + products: std::iter::once(Product { id: opts.product_id.clone(), identifiers: None, hashes: None, subcomponents, - }], + }) + .chain(packages) + .collect(), status, supplier: None, justification, @@ -300,6 +350,43 @@ pub fn build_document_with( }) } +/// `purl` and the forms scanners report the same package under. +fn package_forms(purl: &str) -> impl Iterator { + std::iter::once(purl.to_string()).chain(scanner_forms(purl)) +} + +/// The forms other than `purl` itself that Trivy, Grype and Syft report +/// the package under: the PURL without qualifiers and subpath, which +/// they never carry, and for a gem built for a platform other than +/// `ruby`, `name@version-platform`, which is how Grype and Syft name it +/// (Trivy names it without the platform). +fn scanner_forms(purl: &str) -> Vec { + let head = purl.split('#').next().unwrap_or(purl); + let (base, query) = match head.split_once('?') { + Some((base, query)) => (base, Some(query)), + None => (head, None), + }; + let mut forms = Vec::new(); + if base != purl { + forms.push(base.to_string()); + } + let platform = query.and_then(|query| { + query + .split('&') + .find_map(|pair| pair.strip_prefix("platform=")) + }); + if let Some(platform) = platform { + if base.starts_with("pkg:gem/") + && base.contains('@') + && !platform.is_empty() + && platform != "ruby" + { + forms.push(format!("{base}-{platform}")); + } + } + forms +} + #[derive(Default)] struct VulnGroup { aliases: BTreeSet, @@ -416,13 +503,125 @@ mod tests { st.justification, Some(Justification::InlineMitigationsAlreadyExist) ); - assert_eq!(st.products.len(), 1); + assert_eq!(st.products.len(), 2); assert_eq!(st.products[0].id, "pkg:npm/app@1.0.0"); assert_eq!(st.products[0].subcomponents.len(), 1); assert_eq!(st.products[0].subcomponents[0].id, "pkg:npm/lodash@4.0.0"); + assert_eq!(st.products[1].id, "pkg:npm/lodash@4.0.0"); + assert!(st.products[1].subcomponents.is_empty()); assert!(st.impact_statement.as_ref().unwrap().contains("u1")); } + fn product_ids(st: &Statement) -> Vec<&str> { + st.products.iter().map(|p| p.id.as_str()).collect() + } + + #[test] + fn native_gem_is_named_in_every_scanner_form() { + let purl = "pkg:gem/nokogiri@1.13.9?platform=x86_64-linux"; + let mut manifest = PatchManifest::new(); + manifest + .patches + .insert(purl.to_string(), record("u1", vec![("GHSA-aaaa", vec![])])); + let doc = build_plain(&manifest, &[purl.to_string()]).unwrap(); + assert_eq!( + product_ids(&doc.statements[0]), + vec![ + "pkg:npm/app@1.0.0", + "pkg:gem/nokogiri@1.13.9", + "pkg:gem/nokogiri@1.13.9-x86_64-linux", + purl, + ] + ); + } + + #[test] + fn ruby_platform_gem_gets_no_platform_suffix() { + let purl = "pkg:gem/nokogiri@1.13.9?platform=ruby"; + let mut manifest = PatchManifest::new(); + manifest + .patches + .insert(purl.to_string(), record("u1", vec![("GHSA-aaaa", vec![])])); + let doc = build_plain(&manifest, &[purl.to_string()]).unwrap(); + assert_eq!( + product_ids(&doc.statements[0]), + vec!["pkg:npm/app@1.0.0", "pkg:gem/nokogiri@1.13.9", purl] + ); + } + + #[test] + fn shared_form_is_left_out_when_one_build_is_affected() { + let linux = "pkg:gem/nokogiri@1.13.9?platform=x86_64-linux"; + let darwin = "pkg:gem/nokogiri@1.13.9?platform=arm64-darwin"; + let mut manifest = PatchManifest::new(); + for purl in [linux, darwin] { + manifest + .patches + .insert(purl.to_string(), record("u1", vec![("GHSA-aaaa", vec![])])); + } + let affected = vec![(darwin.to_string(), "Rebuild it.".to_string())]; + let doc = build_affected(&manifest, &[linux.to_string()], &affected).unwrap(); + assert_eq!(doc.statements.len(), 2); + for st in &doc.statements { + assert!( + !product_ids(st).contains(&"pkg:gem/nokogiri@1.13.9"), + "{:?} must not name the form both builds share", + st.status + ); + } + let patched = doc + .statements + .iter() + .find(|st| st.status == Status::NotAffected) + .unwrap(); + assert_eq!( + product_ids(patched), + vec![ + "pkg:npm/app@1.0.0", + "pkg:gem/nokogiri@1.13.9-x86_64-linux", + linux, + ] + ); + } + + #[test] + fn shared_form_is_kept_when_every_build_is_patched() { + let linux = "pkg:gem/nokogiri@1.13.9?platform=x86_64-linux"; + let darwin = "pkg:gem/nokogiri@1.13.9?platform=arm64-darwin"; + let mut manifest = PatchManifest::new(); + for purl in [linux, darwin] { + manifest + .patches + .insert(purl.to_string(), record("u1", vec![("GHSA-aaaa", vec![])])); + } + let doc = build_plain(&manifest, &[linux.to_string(), darwin.to_string()]).unwrap(); + assert_eq!(doc.statements.len(), 1); + assert!(product_ids(&doc.statements[0]).contains(&"pkg:gem/nokogiri@1.13.9")); + } + + #[test] + fn product_override_naming_the_package_is_not_repeated() { + let mut manifest = PatchManifest::new(); + manifest.patches.insert( + "pkg:npm/lodash@4.0.0".to_string(), + record("u1", vec![("GHSA-aaaa", vec![])]), + ); + let mut o = opts(); + o.product_id = "pkg:npm/lodash@4.0.0".to_string(); + let doc = build_document( + &manifest, + &["pkg:npm/lodash@4.0.0".to_string()], + &[], + &[], + &o, + ) + .unwrap(); + assert_eq!( + product_ids(&doc.statements[0]), + vec!["pkg:npm/lodash@4.0.0"] + ); + } + #[test] fn cves_flatten_into_aliases() { let mut manifest = PatchManifest::new(); diff --git a/crates/socket-patch-core/src/vex/conformance_tests.rs b/crates/socket-patch-core/src/vex/conformance_tests.rs index 9cbb9154f..b8e6d9552 100644 --- a/crates/socket-patch-core/src/vex/conformance_tests.rs +++ b/crates/socket-patch-core/src/vex/conformance_tests.rs @@ -701,12 +701,10 @@ fn fixtures_carry_subcomponents_so_at_id_walks_have_teeth() { // produces subcomponents. If a future fixture edit dropped them, // those tests would pass vacuously. Pin the precondition directly. for st in &sample_doc().statements { - for p in &st.products { - assert!( - !p.subcomponents.is_empty(), - "sample_doc product must carry >=1 subcomponent" - ); - } + assert!( + !st.products[0].subcomponents.is_empty(), + "sample_doc project product must carry >=1 subcomponent" + ); } let merged = merged_doc(); assert!( @@ -794,12 +792,21 @@ fn an_affected_statement_carries_an_action_statement_and_no_justification() { } assert_eq!(st.timestamp.as_deref(), Some(doc.timestamp.as_str())); assert!(!st.vulnerability.name.is_empty()); - let [product] = &st.products[..] else { - panic!("not one product: {json}"); + let [product, packages @ ..] = &st.products[..] else { + panic!("no product: {json}"); }; assert_eq!(product.id, "pkg:npm/test-app@1.0.0"); assert!(!product.subcomponents.is_empty()); assert!(product.subcomponents.iter().all(|sub| !sub.id.is_empty())); + let named: Vec<&str> = packages.iter().map(|p| p.id.as_str()).collect(); + for sub in &product.subcomponents { + assert!( + named.contains(&sub.id.as_str()), + "package {} is named as a product of its own: {json}", + sub.id + ); + } + assert!(packages.iter().all(|p| p.subcomponents.is_empty())); let reserved = ["justification", "impact_statement"]; match st.status { Status::Affected => { diff --git a/docs/ecosystems.md b/docs/ecosystems.md index 1388da628..52b98d6ad 100644 --- a/docs/ecosystems.md +++ b/docs/ecosystems.md @@ -4304,7 +4304,7 @@ is patched: said of it. Each advisory of such a patch gets a statement of the package's own with the status -`affected`. It names that package alone under the product, and its `action_statement` says +`affected`. It names that package alone under the product (and as a product of its own), and its `action_statement` says what Bazel builds the package from and what to run so that the build holds the patch. It has no `justification` and no `impact_statement`. Packages that share an advisory are not merged into one such statement, so each action statement is about one package; the @@ -4315,7 +4315,8 @@ merged into one such statement, so each action statement is about one package; t "vulnerability": { "name": "GHSA-xxxx-xxxx-xxxx", "aliases": ["CVE-2024-0001"] }, "timestamp": "2026-11-19T15:14:47Z", "products": [ - { "@id": "pkg:npm/shop@1.0.0", "subcomponents": [{ "@id": "pkg:npm/left-pad@1.3.0" }] } + { "@id": "pkg:npm/shop@1.0.0", "subcomponents": [{ "@id": "pkg:npm/left-pad@1.3.0" }] }, + { "@id": "pkg:npm/left-pad@1.3.0" } ], "status": "affected", "action_statement": "Bazel builds this package from pnpm-lock.yaml (`npm.npm_translate_lock`, MODULE.bazel:6), which does not name Socket patch 7a11e000-...: the patch is only in the installed copy. Run `socket-patch scan --mode hosted` in the directory of that lock to pin the patch there. If what you ship is the installed tree and not what Bazel builds, pass `--no-bazel` (`--vex-no-bazel` on scan, apply and vendor)." diff --git a/docs/usage.md b/docs/usage.md index d3f846549..1b121bbaa 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -263,7 +263,20 @@ socket-patch vex --json --output socket.vex.json The document marks vulnerabilities covered by verified Socket patches as `not_affected`. It does not assess other vulnerabilities. A VEX-aware scanner must -be configured to consume the resulting file. +be configured to consume the resulting file: + +```sh +trivy fs --vex socket.vex.json --show-suppressed . +grype dir:. --vex socket.vex.json --show-suppressed +``` + +Each statement names the project as its product, with the patched packages as its +subcomponents, and names each patched package again as a product of its own, which +is how Trivy and Grype look a finding up. A package is named by its PURL and by the +forms those scanners report it under: without qualifiers, and for a gem built for a +platform, also as `name@version-platform`. A form that would also stand for an +unpatched copy of the package in the same document is left out, so the scanner +keeps reporting that copy. | Patch mode | Evidence used by VEX | | --- | --- |