Files
pruefwerk/compliance-core/tests/csaf_ingest.rs
T
sharang 2d3311bd09
CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 4s
CI / Deploy Docs (push) Successful in 1m5s
CI / Deploy MCP (push) Successful in 2m54s
CI / Deploy Dashboard (push) Successful in 6m29s
CI / Deploy Agent (push) Successful in 8m16s
feat(devices): a target's Siemens controllers on a DIN rail (SPS 4a part 2) (#1078)
2026-11-28 22:09:06 +00:00

162 lines
6.1 KiB
Rust

//! #515: vendor CSAF advisories read for matching: products with their
//! versions, ranges and identifiers, per vulnerability the affected and fixed
//! products and the vendor's remediation. Fixtures: a CISA ICS advisory, a
//! Siemens ProductCERT advisory (see the fixture README for the sources) and
//! a synthetic advisory shaped like CERT@VDE's.
#![allow(clippy::expect_used, clippy::unwrap_used)]
use compliance_core::csaf_ingest::{normalised_name, parse, Advisory};
fn fixture(name: &str) -> Advisory {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/csaf")
.join(name);
let doc: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
parse(&doc).unwrap()
}
#[test]
fn a_vendor_advisory_gives_ranges_fixes_and_remediations() {
let a = fixture("vde-synthetic.json");
assert_eq!(a.id, "VDE-2026-9001");
assert_eq!(a.tlp.as_deref(), Some("WHITE"));
// The readable page, not the JSON.
assert_eq!(
a.url.as_deref(),
Some("https://certvde.com/en/advisories/VDE-2026-9001")
);
let affected = a.product("CSAFPID-51001").unwrap();
assert_eq!(affected.vendor.as_deref(), Some("Example"));
assert_eq!(affected.family.as_deref(), Some("Software"));
assert_eq!(
normalised_name(&affected.name),
"example control for linux sl"
);
assert!(affected.cpe.is_some());
// A runtime below the fix is covered, the fix itself and newer are not.
assert!(affected.covers("Example Control for Linux SL", "3.5.16.0"));
assert!(!affected.covers("Example Control for Linux SL", "3.5.22.40"));
assert!(!affected.covers("Example Gateway", "3.5.16.0"));
let v = &a.vulnerabilities[0];
assert_eq!(v.cve.as_deref(), Some("CVE-2026-90001"));
assert_eq!(v.affected, ["CSAFPID-51001"]);
assert_eq!(v.severity.as_deref(), Some("HIGH"));
assert_eq!(v.cvss_score, Some(7.5));
assert_eq!(
v.summary.as_deref(),
Some("A crafted request can crash the runtime.")
);
assert_eq!(v.remediations[0].category, "vendor_fix");
// Named through its product group, as CERT@VDE's vendors do.
assert_eq!(v.remediations[0].product_ids, ["CSAFPID-51001"]);
assert_eq!(
a.fixed_versions(v, "Example Control for Linux SL"),
["3.5.22.40"]
);
}
#[test]
fn a_siemens_advisory_covers_every_version_of_its_family() {
let a = fixture("ssa-240718.json");
assert_eq!(a.id, "SSA-240718");
assert_eq!(
a.url.as_deref(),
Some("https://cert-portal.siemens.com/productcert/html/ssa-240718.html")
);
let p = &a.products[0];
assert_eq!(p.vendor.as_deref(), Some("Siemens"));
assert_eq!(p.version_range.as_deref(), Some("vers:all/*"));
assert!(p.covers(&p.name.clone(), "2.2"));
let v = &a.vulnerabilities[0];
assert_eq!(v.cve.as_deref(), Some("CVE-2012-3037"));
let categories: Vec<&str> = v.remediations.iter().map(|r| r.category.as_str()).collect();
assert_eq!(categories, ["mitigation", "no_fix_planned"]);
}
#[test]
fn a_cisa_advisory_names_products_without_identifiers() {
let a = fixture("icsa-26-260-07.json");
assert_eq!(a.id, "ICSA-26-260-07");
assert_eq!(
a.url.as_deref(),
Some("https://www.cisa.gov/news-events/ics-advisories/icsa-26-260-07")
);
let powerchute: Vec<_> = a
.products
.iter()
.filter(|p| p.vendor.as_deref() == Some("Schneider Electric"))
.collect();
assert!(powerchute
.iter()
.any(|p| p.version_range.as_deref() == Some("vers:intdot/<=1.5")));
assert!(powerchute
.iter()
.any(|p| p.version.as_deref() == Some("1.6")));
assert!(powerchute
.iter()
.all(|p| p.cpe.is_none() && p.model_numbers.is_empty()));
assert!(!a.vulnerabilities.is_empty());
assert!(a.vulnerabilities.iter().all(|v| v.cve.is_some()));
}
#[test]
fn what_is_not_csaf_is_refused() {
assert!(parse(&serde_json::json!({ "document": { "csaf_version": "2.1" } })).is_err());
assert!(parse(&serde_json::json!({})).is_err());
}
/// The newest advisory of each default provider, fetched live and read:
/// `cargo test -p compliance-core --test csaf_ingest -- --ignored`.
#[test]
#[ignore = "fetches from the providers"]
fn live_advisories_of_the_default_providers_are_read() {
let get = |url: &str| -> serde_json::Value {
let out = std::process::Command::new("curl")
.args(["-sSfL", "--max-time", "60", url])
.output()
.expect("curl runs");
assert!(out.status.success(), "{url}");
serde_json::from_slice(&out.stdout).unwrap()
};
for feed in [
"https://codesys.csaf-tp.certvde.com/.well-known/csaf/white/csaf-feed-tlp-white.json",
"https://cert-portal.siemens.com/productcert/csaf/ssa-feed-tlp-white.json",
"https://raw.githubusercontent.com/cisagov/CSAF/develop/csaf_files/OT/white/cisa-csaf-ot-feed-tlp-white.json",
] {
let feed = get(feed);
let entry = &feed["feed"]["entry"][0];
let url = entry["link"]
.as_array()
.unwrap()
.iter()
.find(|l| l["rel"] == "self")
.and_then(|l| l["href"].as_str())
.unwrap()
.to_string();
let advisory = parse(&get(&url)).unwrap();
println!(
"{} {}: {} products, {} vulnerabilities, {:?}",
advisory.publisher,
advisory.id,
advisory.products.len(),
advisory.vulnerabilities.len(),
advisory.url
);
assert!(!advisory.products.is_empty());
assert!(!advisory.vulnerabilities.is_empty());
}
}
#[test]
fn a_siemens_fix_is_read_from_its_vendor_fix_text_for_the_device_only() {
let a = fixture("siemens-synthetic.json");
let v = &a.vulnerabilities[0];
// Product 2 is fixed at 3.1.5, and the vendor fix for product 1 says
// "Update to V3.1.5 or later version": one version.
assert_eq!(a.fixed_device_versions(v, "6es7511-1ak02-0ab0"), ["3.1.5"]);
// The fix names product 1 only, not the other CPU.
assert!(a.fixed_device_versions(v, "6ES7513-1AL02-0AB0").is_empty());
}