From 0fcef79ea6b84cd6e8e164dd3d47852cc788312e Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:20:54 +0100 Subject: [PATCH] fix(cve): an alert keeps when it was first found when a scan reports it again (#1041) upsert_reported $set the whole alert a scan built, created_at included, so every rescan moved each still-reported CVE's 'found' time to its own: the target's history listed them as new again, and the CVE trend lost them for the days before. created_at is now written on insert only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Q9inRcu428A2iNjDHPEAT9 --- compliance-agent/src/pipeline/vex_lifecycle.rs | 12 ++++++++++-- compliance-agent/tests/vex_lifecycle.rs | 8 ++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/compliance-agent/src/pipeline/vex_lifecycle.rs b/compliance-agent/src/pipeline/vex_lifecycle.rs index cb0442a..b30f68e 100644 --- a/compliance-agent/src/pipeline/vex_lifecycle.rs +++ b/compliance-agent/src/pipeline/vex_lifecycle.rs @@ -110,8 +110,12 @@ pub async fn upsert_reported(db: &Database, alert: &CveAlert) -> Result<(), Agen let filter = doc! { "cve_id": &alert.cve_id, "repo_id": &alert.repo_id }; // The alert as found now; a VEX statement on it is left as it is (the // alert carries none), and it is reported again. - let found = mongodb::bson::to_document(alert) + let mut found = mongodb::bson::to_document(alert) .map_err(|e| AgentError::Other(format!("encode CVE alert: {e}")))?; + // When it was first found is written once (#1041): every scan builds its + // alerts anew, and a rescan must neither list it as new again nor move + // it later in the CVE history. + let first_found = found.remove("created_at"); // A purl is written when the scan knows one and otherwise kept (an alert // stored before purls were recorded gets one on its next scan); kept only // while it is the purl of the same package and version. @@ -134,8 +138,12 @@ pub async fn upsert_reported(db: &Database, alert: &CveAlert) -> Result<(), Agen if alert.supplier_vex.is_none() { unset.insert("supplier_vex", ""); } + let mut update = doc! { "$set": found, "$unset": unset }; + if let Some(first_found) = first_found { + update.insert("$setOnInsert", doc! { "created_at": first_found }); + } db.cve_alerts() - .update_one(filter.clone(), doc! { "$set": found, "$unset": unset }) + .update_one(filter.clone(), update) .upsert(true) .await?; // The first scan that finds it actively exploited starts the CRA's diff --git a/compliance-agent/tests/vex_lifecycle.rs b/compliance-agent/tests/vex_lifecycle.rs index 75dc865..88302a0 100644 --- a/compliance-agent/tests/vex_lifecycle.rs +++ b/compliance-agent/tests/vex_lifecycle.rs @@ -149,6 +149,14 @@ async fn vex_status_follows_the_scans_but_never_overrides_a_person() { upsert_reported(&db, &exploited).await.unwrap(); assert_eq!(stored(&db, "CVE-5").await.exploited_seen_at, Some(first)); + // When it was first found stays (#1041): a scan reporting it again + // builds it with a later `created_at`, which is not kept. + let first_found = stored(&db, "CVE-5").await.created_at; + let mut again = alert("CVE-5"); + again.created_at = first_found + chrono::Duration::hours(5); + upsert_reported(&db, &again).await.unwrap(); + assert_eq!(stored(&db, "CVE-5").await.created_at, first_found); + // 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. -- 2.54.1