CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Docs (push) Successful in 1m11s
CI / Deploy MCP (push) Successful in 4m57s
CI / Deploy Dashboard (push) Successful in 6m45s
CI / Deploy Agent (push) Successful in 8m53s
519 lines
19 KiB
Rust
519 lines
19 KiB
Rust
//! #498: a CVE alert's VEX status follows the scans. Gone after a complete
|
|
//! lookup, an unassessed or affected alert is `fixed` by the scan, a
|
|
//! person's `not_affected` stays; reported again, the scan's `fixed` is
|
|
//! withdrawn. A stored alert gets its component's purl on a later scan. An
|
|
//! alert `not_affected` or `fixed` raises no notification until the
|
|
//! statement is withdrawn.
|
|
//! Runs against a live Mongo (`TEST_MONGODB_URI`) and skips
|
|
//! cleanly without one.
|
|
|
|
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
|
|
|
use compliance_agent::database::{Database, DatabasePool};
|
|
use compliance_agent::pipeline::cve_notify::notify_new_cves;
|
|
use compliance_agent::pipeline::vex_lifecycle::{
|
|
apply_scan_statements, attach_stored_vex, backfill_purls, resolve_gone, upsert_reported,
|
|
};
|
|
use compliance_agent::pipeline::yocto_inventory::{self, CveStatus, Patch, Recipe};
|
|
use compliance_core::models::{
|
|
CveAlert, CveSource, SbomEntry, VexAssessment, VexJustification, VexStatus,
|
|
};
|
|
use compliance_core::{csaf_docs, vex_docs};
|
|
use mongodb::bson::doc;
|
|
|
|
const TARGET: &str = "target-1";
|
|
|
|
fn alert(id: &str) -> CveAlert {
|
|
CveAlert::new(
|
|
id.into(),
|
|
TARGET.into(),
|
|
"lib".into(),
|
|
"1.0".into(),
|
|
CveSource::Osv,
|
|
)
|
|
}
|
|
|
|
fn by_person(status: VexStatus) -> VexAssessment {
|
|
VexAssessment {
|
|
status,
|
|
justification: Some(VexJustification::VulnerableCodeNotPresent),
|
|
impact_statement: None,
|
|
action_statement: Some("Update.".into()),
|
|
by: "u1".into(),
|
|
by_name: Some("Erika".into()),
|
|
at: chrono::Utc::now(),
|
|
by_scan: false,
|
|
}
|
|
}
|
|
|
|
async fn stored(db: &Database, id: &str) -> CveAlert {
|
|
db.cve_alerts()
|
|
.find_one(doc! { "repo_id": TARGET, "cve_id": id })
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn vex_status_follows_the_scans_but_never_overrides_a_person() {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let prefix = format!(
|
|
"vexlife_{}",
|
|
&uuid::Uuid::new_v4().simple().to_string()[..10]
|
|
);
|
|
let Ok(pool) = DatabasePool::connect(&uri, &prefix).await else {
|
|
eprintln!("SKIP vex_lifecycle: no MongoDB reachable at {uri}");
|
|
return;
|
|
};
|
|
let tenant = "vex-lifecycle";
|
|
let Ok(db) = pool.for_tenant_id(tenant).await else {
|
|
eprintln!("SKIP vex_lifecycle: MongoDB at {uri} not usable");
|
|
return;
|
|
};
|
|
|
|
// Arrange: four alerts reported; a person assessed two of them.
|
|
for id in ["CVE-1", "CVE-2", "CVE-3", "CVE-4"] {
|
|
upsert_reported(&db, &alert(id)).await.unwrap();
|
|
}
|
|
for (id, status) in [
|
|
("CVE-2", VexStatus::NotAffected),
|
|
("CVE-3", VexStatus::Affected),
|
|
] {
|
|
let vex = mongodb::bson::to_bson(&by_person(status)).unwrap();
|
|
db.cve_alerts()
|
|
.update_one(doc! { "cve_id": id }, doc! { "$set": { "vex": vex } })
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
// An incomplete lookup resolves nothing.
|
|
assert_eq!(
|
|
resolve_gone(&db, TARGET, &[alert("CVE-4")], false)
|
|
.await
|
|
.unwrap(),
|
|
0
|
|
);
|
|
|
|
// Act: a complete scan reports only CVE-4.
|
|
let resolved = resolve_gone(&db, TARGET, &[alert("CVE-4")], true)
|
|
.await
|
|
.unwrap();
|
|
|
|
// Assert: three resolved; the unassessed and the affected one are fixed
|
|
// by the scan, the person's not_affected stays.
|
|
assert_eq!(resolved, 3);
|
|
let one = stored(&db, "CVE-1").await;
|
|
assert!(one.resolved_at.is_some());
|
|
assert_eq!(
|
|
one.vex.as_ref().map(|v| (v.status, v.by_scan)),
|
|
Some((VexStatus::Fixed, true))
|
|
);
|
|
let two = stored(&db, "CVE-2").await;
|
|
assert!(two.resolved_at.is_some());
|
|
assert_eq!(
|
|
two.vex.as_ref().map(|v| v.status),
|
|
Some(VexStatus::NotAffected)
|
|
);
|
|
assert_eq!(
|
|
stored(&db, "CVE-3").await.vex.map(|v| v.status),
|
|
Some(VexStatus::Fixed)
|
|
);
|
|
assert!(stored(&db, "CVE-4").await.resolved_at.is_none());
|
|
|
|
// Reported again: open, and the scan's `fixed` is withdrawn; the
|
|
// person's statement is kept.
|
|
upsert_reported(&db, &alert("CVE-1")).await.unwrap();
|
|
upsert_reported(&db, &alert("CVE-2")).await.unwrap();
|
|
let one = stored(&db, "CVE-1").await;
|
|
assert!(one.resolved_at.is_none());
|
|
assert!(one.vex.is_none());
|
|
let two = stored(&db, "CVE-2").await;
|
|
assert!(two.resolved_at.is_none());
|
|
assert_eq!(two.vex.map(|v| v.status), Some(VexStatus::NotAffected));
|
|
|
|
// The first scan that finds it actively exploited starts the reporting
|
|
// clock; a later scan keeps that time.
|
|
let mut exploited = alert("CVE-5");
|
|
exploited.exploitation = Some(compliance_core::models::Exploitation {
|
|
kev: Some(compliance_core::models::KevEntry {
|
|
date_added: "2021-12-10".into(),
|
|
known_ransomware: false,
|
|
}),
|
|
..Default::default()
|
|
});
|
|
upsert_reported(&db, &alert("CVE-5")).await.unwrap();
|
|
assert!(stored(&db, "CVE-5").await.exploited_seen_at.is_none());
|
|
upsert_reported(&db, &exploited).await.unwrap();
|
|
let first = stored(&db, "CVE-5").await.exploited_seen_at.unwrap();
|
|
upsert_reported(&db, &exploited).await.unwrap();
|
|
assert_eq!(stored(&db, "CVE-5").await.exploited_seen_at, Some(first));
|
|
|
|
// An alert stored without a purl gets its component's on the next scan;
|
|
// a scan that knows none keeps it, unless the alert is now about another
|
|
// component.
|
|
let maven = "pkg:maven/org.example/lib@1.0";
|
|
let mut with_purl = alert("CVE-1");
|
|
with_purl.purl = Some(maven.into());
|
|
assert_eq!(stored(&db, "CVE-1").await.purl, None);
|
|
upsert_reported(&db, &with_purl).await.unwrap();
|
|
assert_eq!(stored(&db, "CVE-1").await.purl.as_deref(), Some(maven));
|
|
upsert_reported(&db, &alert("CVE-1")).await.unwrap();
|
|
assert_eq!(stored(&db, "CVE-1").await.purl.as_deref(), Some(maven));
|
|
let mut other = alert("CVE-1");
|
|
other.affected_version = "2.0".into();
|
|
upsert_reported(&db, &other).await.unwrap();
|
|
assert_eq!(stored(&db, "CVE-1").await.purl, None);
|
|
|
|
pool.drop_tenant(tenant).await.ok();
|
|
}
|
|
|
|
/// #532: an alert stored before purls were recorded gets its component's
|
|
/// purl from the stored SBOM before a scan removes the component, so once
|
|
/// resolved it still names the real package in every VEX export.
|
|
#[tokio::test]
|
|
async fn an_alert_from_before_keeps_its_purl_after_the_component_is_gone() {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let prefix = format!(
|
|
"vexpurl_{}",
|
|
&uuid::Uuid::new_v4().simple().to_string()[..10]
|
|
);
|
|
let Ok(pool) = DatabasePool::connect(&uri, &prefix).await else {
|
|
eprintln!("SKIP vex_lifecycle: no MongoDB reachable at {uri}");
|
|
return;
|
|
};
|
|
let tenant = "vex-purl";
|
|
let Ok(db) = pool.for_tenant_id(tenant).await else {
|
|
eprintln!("SKIP vex_lifecycle: MongoDB at {uri} not usable");
|
|
return;
|
|
};
|
|
let purl = "pkg:maven/com.google.code.gson/gson@2.8.8";
|
|
|
|
// Arrange: an open alert stored without a purl, its component in the
|
|
// stored SBOM (name in another case), and one already resolved.
|
|
let gson = CveAlert::new(
|
|
"GHSA-4jrv-ppp4-jm57".into(),
|
|
TARGET.into(),
|
|
"gson".into(),
|
|
"2.8.8".into(),
|
|
CveSource::Osv,
|
|
);
|
|
upsert_reported(&db, &gson).await.unwrap();
|
|
upsert_reported(&db, &alert("CVE-OLD")).await.unwrap();
|
|
resolve_gone(&db, TARGET, std::slice::from_ref(&gson), true)
|
|
.await
|
|
.unwrap();
|
|
let mut entry = SbomEntry::new(TARGET.into(), "Gson".into(), "2.8.8".into(), "maven".into());
|
|
entry.purl = Some(purl.into());
|
|
db.sbom_entries().insert_one(&entry).await.unwrap();
|
|
assert_eq!(stored(&db, "GHSA-4jrv-ppp4-jm57").await.purl, None);
|
|
|
|
// Act: the backfill before the next scan, then that scan: gson updated,
|
|
// its old version gone from the SBOM and the alert resolved.
|
|
assert_eq!(backfill_purls(&db, Some(TARGET)).await.unwrap(), 1);
|
|
assert_eq!(backfill_purls(&db, Some(TARGET)).await.unwrap(), 0, "once");
|
|
db.sbom_entries()
|
|
.delete_many(doc! { "repo_id": TARGET })
|
|
.await
|
|
.unwrap();
|
|
resolve_gone(&db, TARGET, &[], true).await.unwrap();
|
|
|
|
// Assert: resolved, with the purl, and every export names it.
|
|
let resolved = stored(&db, "GHSA-4jrv-ppp4-jm57").await;
|
|
assert!(resolved.resolved_at.is_some());
|
|
assert_eq!(resolved.purl.as_deref(), Some(purl));
|
|
assert_eq!(stored(&db, "CVE-OLD").await.purl, None, "already resolved");
|
|
let alerts = [resolved];
|
|
let input = vex_docs::VexInput {
|
|
subject: "app",
|
|
author: "ws",
|
|
tooling: "Prüfwerk test",
|
|
alerts: &alerts,
|
|
sbom: &[],
|
|
now: chrono::Utc::now(),
|
|
};
|
|
for doc in [
|
|
vex_docs::openvex(&input),
|
|
vex_docs::cyclonedx_vex(&input),
|
|
csaf_docs::csaf_vex(&input, "https://example.invalid/ws", "de"),
|
|
] {
|
|
let text = doc.to_string();
|
|
assert!(text.contains(purl), "{text}");
|
|
// The product itself (the target) is a generic purl; the component is not.
|
|
assert!(!text.contains("pkg:generic/gson"), "{text}");
|
|
}
|
|
|
|
pool.drop_tenant(tenant).await.ok();
|
|
}
|
|
|
|
/// A Yocto recipe's CVE_STATUS and `CVE:` patch become the alerts' VEX
|
|
/// statements on every scan, reach the exports, are withdrawn when the recipe
|
|
/// stops saying so, and never replace a person's statement.
|
|
#[tokio::test]
|
|
async fn a_recipe_states_vex_each_scan_but_never_over_a_person() {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let prefix = format!(
|
|
"vexyocto_{}",
|
|
&uuid::Uuid::new_v4().simple().to_string()[..10]
|
|
);
|
|
let Ok(pool) = DatabasePool::connect(&uri, &prefix).await else {
|
|
eprintln!("SKIP vex_lifecycle: no MongoDB reachable at {uri}");
|
|
return;
|
|
};
|
|
let tenant = "vex-yocto";
|
|
let Ok(db) = pool.for_tenant_id(tenant).await else {
|
|
eprintln!("SKIP vex_lifecycle: MongoDB at {uri} not usable");
|
|
return;
|
|
};
|
|
|
|
// Arrange: busybox's recipe assesses one CVE and patches another; a
|
|
// person assessed a third one the recipe also names.
|
|
let mut recipe = Recipe {
|
|
file: "recipes-core/busybox/busybox_1.35.0.bb".into(),
|
|
pn: "busybox".into(),
|
|
bpn: "busybox".into(),
|
|
pv: Some("1.35.0".into()),
|
|
cve_status: vec![
|
|
CveStatus {
|
|
cve: "CVE-2022-28391".into(),
|
|
status: "not-applicable-config".into(),
|
|
detail: "netstat is disabled".into(),
|
|
},
|
|
CveStatus {
|
|
cve: "CVE-2022-48174".into(),
|
|
status: "ignored".into(),
|
|
detail: String::new(),
|
|
},
|
|
],
|
|
patches: vec![Patch {
|
|
file: "recipes-core/busybox/busybox/fix.patch".into(),
|
|
cves: vec!["CVE-2022-30065".into()],
|
|
}],
|
|
..Recipe::default()
|
|
};
|
|
let busybox = |id: &str| {
|
|
CveAlert::new(
|
|
id.into(),
|
|
TARGET.into(),
|
|
"busybox".into(),
|
|
"1.35.0".into(),
|
|
CveSource::Nvd,
|
|
)
|
|
};
|
|
let alerts: Vec<CveAlert> = ["CVE-2022-28391", "CVE-2022-30065", "CVE-2022-48174"]
|
|
.into_iter()
|
|
.map(busybox)
|
|
.collect();
|
|
for alert in &alerts {
|
|
upsert_reported(&db, alert).await.unwrap();
|
|
}
|
|
let person = mongodb::bson::to_bson(&by_person(VexStatus::Affected)).unwrap();
|
|
db.cve_alerts()
|
|
.update_one(
|
|
doc! { "cve_id": "CVE-2022-48174" },
|
|
doc! { "$set": { "vex": person } },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
// Act: the scan states what the recipe says.
|
|
let statements = yocto_inventory::vex_statements(std::slice::from_ref(&recipe), &alerts);
|
|
assert_eq!(statements.len(), 3);
|
|
assert_eq!(
|
|
apply_scan_statements(&db, TARGET, &statements)
|
|
.await
|
|
.unwrap(),
|
|
2
|
|
);
|
|
|
|
// Assert: not_affected and fixed by the scan; the person's stays.
|
|
let status = stored(&db, "CVE-2022-28391").await.vex.unwrap();
|
|
assert_eq!(
|
|
(status.status, status.justification, status.by_scan),
|
|
(
|
|
VexStatus::NotAffected,
|
|
Some(VexJustification::VulnerableCodeNotPresent),
|
|
true
|
|
)
|
|
);
|
|
let patched = stored(&db, "CVE-2022-30065").await.vex.unwrap();
|
|
assert_eq!(patched.status, VexStatus::Fixed);
|
|
let kept = stored(&db, "CVE-2022-48174").await.vex.unwrap();
|
|
assert_eq!((kept.status, kept.by_scan), (VexStatus::Affected, false));
|
|
|
|
// The exports carry the recipe's reason.
|
|
let current = vec![stored(&db, "CVE-2022-28391").await];
|
|
let input = vex_docs::VexInput {
|
|
subject: "pump",
|
|
author: "ws",
|
|
tooling: "Prüfwerk test",
|
|
alerts: ¤t,
|
|
sbom: &[],
|
|
now: chrono::Utc::now(),
|
|
};
|
|
let openvex = vex_docs::openvex(&input).to_string();
|
|
assert!(openvex.contains("not_affected"), "{openvex}");
|
|
assert!(openvex.contains("netstat is disabled"), "{openvex}");
|
|
|
|
// The next scan: the recipe no longer assesses CVE-2022-28391, so its
|
|
// statement is withdrawn; the patch still fixes CVE-2022-30065.
|
|
recipe.cve_status.remove(0);
|
|
for alert in &alerts {
|
|
upsert_reported(&db, alert).await.unwrap();
|
|
}
|
|
let statements = yocto_inventory::vex_statements(std::slice::from_ref(&recipe), &alerts);
|
|
apply_scan_statements(&db, TARGET, &statements)
|
|
.await
|
|
.unwrap();
|
|
assert!(stored(&db, "CVE-2022-28391").await.vex.is_none());
|
|
assert_eq!(
|
|
stored(&db, "CVE-2022-30065").await.vex.map(|v| v.status),
|
|
Some(VexStatus::Fixed)
|
|
);
|
|
assert_eq!(
|
|
stored(&db, "CVE-2022-48174").await.vex.map(|v| v.status),
|
|
Some(VexStatus::Affected)
|
|
);
|
|
|
|
pool.drop_tenant(tenant).await.ok();
|
|
}
|
|
|
|
/// What a scan does: store the alerts, set its own statements, then give the
|
|
/// alerts their stored statements and notify.
|
|
async fn scan_and_notify(
|
|
db: &Database,
|
|
found: &[CveAlert],
|
|
statements: &[(String, VexAssessment)],
|
|
) -> u32 {
|
|
for alert in found {
|
|
upsert_reported(db, alert).await.unwrap();
|
|
}
|
|
apply_scan_statements(db, TARGET, statements).await.unwrap();
|
|
let mut found = found.to_vec();
|
|
attach_stored_vex(db, TARGET, &mut found).await.unwrap();
|
|
notify_new_cves(db, TARGET, "app", &found).await
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn not_affected_and_fixed_raise_no_notification_until_withdrawn() {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let prefix = format!(
|
|
"vexnotif_{}",
|
|
&uuid::Uuid::new_v4().simple().to_string()[..10]
|
|
);
|
|
let Ok(pool) = DatabasePool::connect(&uri, &prefix).await else {
|
|
eprintln!("SKIP vex_lifecycle: no MongoDB reachable at {uri}");
|
|
return;
|
|
};
|
|
let tenant = "vex-notifications";
|
|
let Ok(db) = pool.for_tenant_id(tenant).await else {
|
|
eprintln!("SKIP vex_lifecycle: MongoDB at {uri} not usable");
|
|
return;
|
|
};
|
|
let found: Vec<CveAlert> = ["CVE-1", "CVE-2", "CVE-3", "CVE-4", "CVE-5"]
|
|
.into_iter()
|
|
.map(alert)
|
|
.collect();
|
|
for alert in &found {
|
|
upsert_reported(&db, alert).await.unwrap();
|
|
}
|
|
// A person settles CVE-2 and CVE-3 and confirms CVE-4; a recipe says
|
|
// CVE-5 is not affected.
|
|
for (id, status) in [
|
|
("CVE-2", VexStatus::NotAffected),
|
|
("CVE-3", VexStatus::Fixed),
|
|
("CVE-4", VexStatus::Affected),
|
|
] {
|
|
let vex = mongodb::bson::to_bson(&by_person(status)).unwrap();
|
|
db.cve_alerts()
|
|
.update_one(doc! { "cve_id": id }, doc! { "$set": { "vex": vex } })
|
|
.await
|
|
.unwrap();
|
|
}
|
|
let mut recipe_says = by_person(VexStatus::NotAffected);
|
|
recipe_says.by_scan = true;
|
|
let recipe = vec![("CVE-5".to_string(), recipe_says)];
|
|
|
|
// Only the unsettled CVEs notify, and only once.
|
|
assert_eq!(scan_and_notify(&db, &found, &recipe).await, 2);
|
|
assert_eq!(scan_and_notify(&db, &found, &recipe).await, 0);
|
|
let mut notified: Vec<String> = db
|
|
.cve_notifications()
|
|
.distinct("cve_id", doc! {})
|
|
.await
|
|
.unwrap()
|
|
.into_iter()
|
|
.filter_map(|id| id.as_str().map(str::to_owned))
|
|
.collect();
|
|
notified.sort();
|
|
assert_eq!(notified, ["CVE-1", "CVE-4"]);
|
|
|
|
// The person withdraws CVE-2's statement and the recipe no longer
|
|
// assesses CVE-5: both notify now; CVE-3 stays fixed and silent.
|
|
db.cve_alerts()
|
|
.update_one(doc! { "cve_id": "CVE-2" }, doc! { "$unset": { "vex": "" } })
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(scan_and_notify(&db, &found, &[]).await, 2);
|
|
assert_eq!(
|
|
db.cve_notifications()
|
|
.count_documents(doc! { "cve_id": "CVE-3" })
|
|
.await
|
|
.unwrap(),
|
|
0
|
|
);
|
|
|
|
pool.drop_tenant(tenant).await.ok();
|
|
}
|
|
|
|
/// #1025: a supplier's statement is stored with the alert as the scan read
|
|
/// it, beside (never in) the alert's own VEX, and goes once a scan's SBOM no
|
|
/// longer states it.
|
|
#[tokio::test]
|
|
async fn a_suppliers_statement_is_restated_by_each_scan() {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let prefix = format!(
|
|
"vexsup_{}",
|
|
&uuid::Uuid::new_v4().simple().to_string()[..10]
|
|
);
|
|
let Ok(pool) = DatabasePool::connect(&uri, &prefix).await else {
|
|
eprintln!("SKIP vex_lifecycle: no MongoDB reachable at {uri}");
|
|
return;
|
|
};
|
|
let tenant = "vex-supplier";
|
|
let Ok(db) = pool.for_tenant_id(tenant).await else {
|
|
eprintln!("SKIP vex_lifecycle: MongoDB at {uri} not usable");
|
|
return;
|
|
};
|
|
let mut stated = alert("CVE-7");
|
|
stated.supplier_vex = Some(compliance_core::models::SupplierVex {
|
|
status: VexStatus::NotAffected,
|
|
state: "not_affected".into(),
|
|
justification: Some("code_not_reachable".into()),
|
|
detail: None,
|
|
response: Vec::new(),
|
|
source: "pump.cdx.json".into(),
|
|
});
|
|
|
|
upsert_reported(&db, &stated).await.unwrap();
|
|
let first = stored(&db, "CVE-7").await;
|
|
assert_eq!(
|
|
first.supplier_vex.as_ref().map(|v| v.status),
|
|
Some(VexStatus::NotAffected)
|
|
);
|
|
assert!(
|
|
first.vex.is_none(),
|
|
"it is not the manufacturer's statement"
|
|
);
|
|
assert!(!first.is_resolved_by_vex(), "and settles nothing");
|
|
|
|
upsert_reported(&db, &alert("CVE-7")).await.unwrap();
|
|
assert!(stored(&db, "CVE-7").await.supplier_vex.is_none());
|
|
|
|
pool.drop_tenant(tenant).await.ok();
|
|
}
|