fix(sbom): a released SBOM's purls name no download or VCS address (#1028)
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Agent integration (pull_request) Successful in 5m25s
CI / Check (pull_request) Successful in 21m49s

Yocto recipes give pkg:generic purls a download_url or vcs_url
qualifier, which can name the supplier's own git server or feed. The
released document passed them on in purl and bom-ref. They are now left
out (download_url, vcs_url, repository_url); other qualifiers and the
subpath stay. A bom-ref that becomes a duplicate gets " (2)" appended.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q9inRcu428A2iNjDHPEAT9
This commit is contained in:
Sharang ParnerkarandClaude Opus 5.5 committed 2026-11-19 18:05:08 +01:00
1 parent b05154adce
commit 1ca85ca859
1 file changed
+130 -2
+130 -2
View File
@@ -456,19 +456,84 @@ pub fn released_cyclonedx(
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)
let mut components: Vec<serde_json::Value> = cdx_components(entries)
.into_iter()
.map(released_component)
.collect();
unique_refs(&mut components);
cdx_dependencies(&mut doc, &components);
doc["components"] = serde_json::Value::Array(components);
doc
}
/// `component` with only [`RELEASED_COMPONENT_KEYS`], and no licence text.
/// Purl qualifiers that say where a component was fetched from. A released
/// SBOM leaves them out: they can name the supplier's own servers (a Yocto
/// recipe's `vcs_url`, an internal feed).
const ADDRESS_QUALIFIERS: [&str; 3] = ["download_url", "vcs_url", "repository_url"];
/// `purl` without its [`ADDRESS_QUALIFIERS`]; anything that is not a purl
/// as it is.
fn without_addresses(purl: &str) -> String {
if !purl.starts_with("pkg:") {
return purl.to_string();
}
let (body, subpath) = match purl.split_once('#') {
Some((body, subpath)) => (body, Some(subpath)),
None => (purl, None),
};
let Some((base, qualifiers)) = body.split_once('?') else {
return purl.to_string();
};
let kept: Vec<&str> = qualifiers
.split('&')
.filter(|q| {
let key = q.split_once('=').map_or(*q, |(key, _)| key);
!ADDRESS_QUALIFIERS.contains(&key)
})
.collect();
let mut out = base.to_string();
if !kept.is_empty() {
out.push('?');
out.push_str(&kept.join("&"));
}
if let Some(subpath) = subpath {
out.push('#');
out.push_str(subpath);
}
out
}
/// Make each component's `bom-ref` unique again: two components can differ
/// only in a qualifier [`without_addresses`] removed. A repeated one gets
/// ` (2)`, ` (3)`, ... appended.
fn unique_refs(components: &mut [serde_json::Value]) {
let mut seen: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
for component in components {
let Some(reference) = component["bom-ref"].as_str().map(str::to_string) else {
continue;
};
let count = seen.entry(reference.clone()).or_insert(0);
*count += 1;
if *count > 1 {
component["bom-ref"] = serde_json::json!(format!("{reference} ({count})"));
}
}
}
/// `component` with only [`RELEASED_COMPONENT_KEYS`], no licence text and
/// no addresses in its purl.
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()));
for key in ["purl", "bom-ref"] {
if let Some(stripped) = fields
.get(key)
.and_then(|v| v.as_str())
.map(without_addresses)
{
fields.insert(key.to_string(), serde_json::Value::String(stripped));
}
}
let licenses = fields
.get_mut("licenses")
.and_then(serde_json::Value::as_array_mut);
@@ -808,6 +873,69 @@ mod tests {
assert_eq!(doc["dependencies"][0]["dependsOn"][0], component["bom-ref"]);
}
#[test]
fn a_released_purl_names_no_address() {
let entry = |purl: &str| {
let mut e = SbomEntry::new("t".into(), "pump-fw".into(), "1.0".into(), "yocto".into());
e.purl = Some(purl.into());
e
};
let entries = [
entry("pkg:generic/pump-fw@1.0?vcs_url=git%2Bhttps%3A%2F%2Fgit.internal.example%2Fpump-fw.git"),
entry("pkg:generic/pump-fw@1.0?download_url=http%3A%2F%2Fdl.internal.example%2Fpump.tgz&arch=arm"),
];
let product = ReleasedProduct {
name: "DR-4 Firmware",
version: "1.4.0",
supplier: "Test Antriebstechnik",
};
let doc = released_cyclonedx(&entries, &product);
assert!(!doc.to_string().contains("internal.example"), "{doc}");
let purls: Vec<&str> = doc["components"]
.as_array()
.unwrap()
.iter()
.map(|c| c["purl"].as_str().unwrap())
.collect();
assert_eq!(
purls,
[
"pkg:generic/pump-fw@1.0",
"pkg:generic/pump-fw@1.0?arch=arm"
]
);
assert_eq!(
without_addresses("pkg:npm/lodash@4.17.21"),
"pkg:npm/lodash@4.17.21"
);
assert_eq!(
without_addresses("pkg:generic/a@1?vcs_url=x#src/lib"),
"pkg:generic/a@1#src/lib"
);
}
#[test]
fn released_bom_refs_stay_unique() {
let mut components = vec![
serde_json::json!({ "bom-ref": "pkg:generic/a@1" }),
serde_json::json!({ "bom-ref": "pkg:generic/a@1" }),
serde_json::json!({ "bom-ref": "pkg:generic/b@1" }),
];
unique_refs(&mut components);
let refs: Vec<&str> = components
.iter()
.map(|c| c["bom-ref"].as_str().unwrap())
.collect();
assert_eq!(
refs,
["pkg:generic/a@1", "pkg:generic/a@1 (2)", "pkg:generic/b@1"]
);
}
#[test]
fn both_formats_list_every_component() {
let mut e = SbomEntry::new("t".into(), "lwIP".into(), "2.1.2".into(), "embedded".into());