feat(sbom): the stripped SBOM a supplier workspace releases (#1028) #1049

Merged
sharang merged 1 commits from feat/sbom-release-export into main 2026-11-19 07:55:09 +00:00
3 changed files with 208 additions and 1 deletions

No files matched your search

@@ -650,6 +650,47 @@ mod tests {
parse_with_statements(bytes, repo_id, label).map(|sbom| sbom.entries)
}
#[test]
fn a_released_sbom_is_read_back_as_a_supplier_sbom() {
// What a supplier workspace releases (#1028) reaches the customer
// through this reader: every component, with its purl, and the
// product itself left out.
let entries: Vec<SbomEntry> = (0..3)
.map(|i| {
let mut e = SbomEntry::new(
"supplier-target".into(),
format!("pkg{i}"),
"1.0.0".into(),
"npm".into(),
);
e.purl = Some(format!("pkg:npm/pkg{i}@1.0.0"));
e
})
.collect();
let product = compliance_core::sbom_docs::ReleasedProduct {
name: "DR-4 Firmware",
version: "1.4.0",
supplier: "Test Antriebstechnik",
};
let doc = compliance_core::sbom_docs::released_cyclonedx(&entries, &product);
let bytes = serde_json::to_vec(&doc).expect("json");
let read = parse_with_statements(&bytes, "customer-target", "").expect("readable");
assert_eq!(read.format, "CycloneDX 1.6");
assert!(read.statements.is_empty());
let purls: Vec<Option<&str>> = read.entries.iter().map(|e| e.purl.as_deref()).collect();
assert_eq!(
purls,
[
Some("pkg:npm/pkg0@1.0.0"),
Some("pkg:npm/pkg1@1.0.0"),
Some("pkg:npm/pkg2@1.0.0")
]
);
assert!(read.entries.iter().all(|e| e.repo_id == "customer-target"));
}
const CDX_JSON: &str = r#"{
"bomFormat": "CycloneDX",
"specVersion": "1.5",
+130 -1
View File
@@ -415,6 +415,75 @@ pub fn cyclonedx(entries: &[SbomEntry], subject: &str) -> serde_json::Value {
doc
}
/// The product a released SBOM describes (#1028): the target a supplier
/// workspace releases, the version it publishes, and the workspace's name as
/// its supplier.
#[derive(Debug, Clone, Copy)]
pub struct ReleasedProduct<'a> {
/// The target's name.
pub name: &'a str,
/// The version the supplier publishes, such as `1.4.0`.
pub version: &'a str,
/// The supplier: the releasing workspace's display name.
pub supplier: &'a str,
}
/// The keys of a component that a released SBOM keeps: what identifies the
/// component, its licences and its hashes. Everything else stays with the
/// supplier: properties (where it was found, the images it is in, the
/// licence review), copyright, references and the manufacturer. A list of
/// what is kept, so nothing added to the export later crosses unnoticed.
const RELEASED_COMPONENT_KEYS: [&str; 9] = [
"type", "bom-ref", "name", "group", "version", "purl", "cpe", "licenses", "hashes",
];
/// The SBOM a supplier workspace releases to a connected customer workspace
/// (#1028): a CycloneDX 1.6 document of `entries` about `product`, holding
/// only what identifies each component, its licences and its hashes.
///
/// No vulnerabilities, findings or provenance cross: the customer's own scan
/// finds the vulnerabilities. A confirmed custom licence keeps its name but
/// not its text, which may come from the supplier's own contract.
///
/// # Arguments
///
/// * `entries` - the SBOM entries of the target's latest scan
/// * `product` - the target, the version published and the supplier
pub fn released_cyclonedx(
entries: &[SbomEntry],
product: &ReleasedProduct<'_>,
) -> serde_json::Value {
let mut doc = cdx_frame(product.name, entries);
doc["metadata"]["component"]["version"] = serde_json::json!(product.version);
doc["metadata"]["component"]["supplier"] = serde_json::json!({ "name": product.supplier });
let components: Vec<serde_json::Value> = cdx_components(entries)
.into_iter()
.map(released_component)
.collect();
cdx_dependencies(&mut doc, &components);
doc["components"] = serde_json::Value::Array(components);
doc
}
/// `component` with only [`RELEASED_COMPONENT_KEYS`], and no licence text.
fn released_component(mut component: serde_json::Value) -> serde_json::Value {
if let Some(fields) = component.as_object_mut() {
fields.retain(|key, _| RELEASED_COMPONENT_KEYS.contains(&key.as_str()));
let licenses = fields
.get_mut("licenses")
.and_then(serde_json::Value::as_array_mut);
for choice in licenses.into_iter().flatten() {
if let Some(license) = choice
.get_mut("license")
.and_then(serde_json::Value::as_object_mut)
{
license.remove("text");
}
}
}
component
}
/// The known vulnerabilities of `entries` as the document's
/// `vulnerabilities`: one per CVE (or advisory without one), affecting every
/// component it was found in, by whichever scanner and id. None, none set.
@@ -677,7 +746,67 @@ pub fn spdx(entries: &[SbomEntry], subject: &str) -> serde_json::Value {
#[cfg(test)]
mod tests {
use super::*;
use crate::models::{InImage, InImageBasis};
use crate::models::{InImage, InImageBasis, VulnRef};
#[test]
fn a_released_sbom_keeps_what_identifies_each_component_and_nothing_else() {
let mut e = SbomEntry::new("t".into(), "pump-sdk".into(), "4.2".into(), "nuget".into());
e.purl = Some("pkg:nuget/pump-sdk@4.2".into());
e.cpe = Some("cpe:2.3:a:example:pump-sdk:4.2:*:*:*:*:*:*:*".into());
e.hashes = vec![ComponentHash {
alg: "SHA-256".into(),
content: "ab".repeat(32),
}];
e.download_url = Some("https://internal.example/feed/pump-sdk.4.2.nupkg".into());
e.manufacturer_url = Some("https://example.com".into());
e.concluded = Some(concluded("LicenseRef-Pump-EULA", Some("Pump EULA")));
e.in_image = Some(InImage {
basis: InImageBasis::Declared,
images: vec!["pump-image".into()],
});
e.known_vulnerabilities = vec![VulnRef {
id: "CVE-2026-0001".into(),
source: "osv".into(),
severity: Some("HIGH".into()),
url: None,
exploitation: None,
aliases: Vec::new(),
}];
let product = ReleasedProduct {
name: "DR-4 Firmware",
version: "1.4.0",
supplier: "Test Antriebstechnik",
};
let doc = released_cyclonedx(std::slice::from_ref(&e), &product);
let meta = &doc["metadata"]["component"];
assert_eq!(meta["name"], "DR-4 Firmware");
assert_eq!(meta["version"], "1.4.0");
assert_eq!(meta["supplier"]["name"], "Test Antriebstechnik");
assert!(doc.get("vulnerabilities").is_none(), "{doc}");
let component = doc["components"][0].as_object().unwrap();
let mut keys: Vec<&str> = component.keys().map(String::as_str).collect();
keys.sort_unstable();
assert_eq!(
keys,
["bom-ref", "cpe", "hashes", "licenses", "name", "purl", "type", "version"],
"nothing beyond identity, licences and hashes"
);
assert_eq!(component["licenses"][0]["license"]["name"], "Pump EULA");
assert!(component["licenses"][0]["license"].get("text").is_none());
let text = doc.to_string();
for leaked in [
"internal.example",
"pump-image",
"contract 4711",
"CVE-2026-0001",
] {
assert!(!text.contains(leaked), "{leaked} crossed: {text}");
}
// The product still depends on its component.
assert_eq!(doc["dependencies"][0]["dependsOn"][0], component["bom-ref"]);
}
#[test]
fn both_formats_list_every_component() {
+37
View File
@@ -0,0 +1,37 @@
//! The SBOM a supplier workspace releases to a customer workspace (#1028):
//! valid against CycloneDX 1.6.
#![allow(clippy::expect_used, clippy::unwrap_used)]
mod common;
use compliance_core::models::{ComponentHash, SbomEntry};
use compliance_core::sbom_docs::{self, ReleasedProduct};
#[test]
fn a_released_sbom_is_valid_cyclonedx() {
let mut sdk = SbomEntry::new("t".into(), "pump-sdk".into(), "4.2".into(), "nuget".into());
sdk.purl = Some("pkg:nuget/pump-sdk@4.2".into());
sdk.license = Some("MIT".into());
sdk.hashes = vec![ComponentHash {
alg: "SHA-256".into(),
content: "ab".repeat(32),
}];
// No purl: listed by its name and version.
let blob = SbomEntry::new(
"t".into(),
"vendor-blob".into(),
"7".into(),
"supplier".into(),
);
let product = ReleasedProduct {
name: "DR-4 Firmware",
version: "1.4.0",
supplier: "Test Antriebstechnik",
};
let doc = sbom_docs::released_cyclonedx(&[sdk, blob], &product);
assert_eq!(common::cyclonedx_errors(&doc), Vec::<String>::new());
assert_eq!(doc["components"].as_array().unwrap().len(), 2);
}