feat(platform): count open vulnerabilities in the portal's target list
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 / Check (pull_request) Failing after 4m40s
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 / Check (pull_request) Failing after 4m40s
The target list the portal shows per project (GET /api/v1/platform/tenants/
{id}/targets) counted only open findings. A target whose only problems are
known vulnerabilities in its components (e.g. log4j-core 2.25.3 with three
later advisories) read "no open findings".
Each target now also carries open_vulnerabilities: its CVE alerts that are
not resolved and not assessed not_affected or fixed in VEX, counted in one
aggregation like the findings.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
This commit is contained in:
1 parent
5ce496303d
commit
4378730afb
1 file changed
+100
-14
@@ -472,6 +472,9 @@ pub struct PlatformTarget {
|
||||
pub last_scan_at: Option<chrono::DateTime<chrono::Utc>>,
|
||||
/// Findings that still need attention.
|
||||
pub open_findings: u64,
|
||||
/// Known vulnerabilities in its components that are still open: CVE
|
||||
/// alerts neither resolved nor assessed `not_affected` / `fixed`.
|
||||
pub open_vulnerabilities: u64,
|
||||
/// Its projects that the registry still lists.
|
||||
pub project_ids: Vec<String>,
|
||||
}
|
||||
@@ -481,23 +484,56 @@ async fn open_findings_counts(
|
||||
db: &crate::database::Database,
|
||||
target_ids: Vec<String>,
|
||||
) -> Result<std::collections::HashMap<String, u64>, StatusCode> {
|
||||
let pipeline = vec![
|
||||
doc! { "$match": {
|
||||
"repo_id": { "$in": target_ids },
|
||||
"status": { "$in": super::health::OPEN_STATUSES.to_vec() },
|
||||
} },
|
||||
doc! { "$group": { "_id": "$repo_id", "n": { "$sum": 1 } } },
|
||||
];
|
||||
let rows: Vec<Document> = db
|
||||
.findings()
|
||||
.aggregate(pipeline)
|
||||
.aggregate(count_by_target(doc! {
|
||||
"repo_id": { "$in": target_ids },
|
||||
"status": { "$in": super::health::OPEN_STATUSES.to_vec() },
|
||||
}))
|
||||
.await
|
||||
.map_err(internal)?
|
||||
.try_collect()
|
||||
.await
|
||||
.map_err(internal)?;
|
||||
Ok(rows
|
||||
.iter()
|
||||
Ok(counts_by_target(&rows))
|
||||
}
|
||||
|
||||
/// The CVE alerts that still need a decision: not resolved by a later scan
|
||||
/// and not assessed as `not_affected` or `fixed` in VEX.
|
||||
fn open_vulnerabilities_filter(target_ids: Vec<String>) -> Document {
|
||||
doc! {
|
||||
"repo_id": { "$in": target_ids },
|
||||
"resolved_at": null,
|
||||
"vex.status": { "$nin": ["not_affected", "fixed"] },
|
||||
}
|
||||
}
|
||||
|
||||
/// Open vulnerabilities (CVE alerts) of each target in `target_ids`, in one
|
||||
/// query.
|
||||
async fn open_vulnerabilities_counts(
|
||||
db: &crate::database::Database,
|
||||
target_ids: Vec<String>,
|
||||
) -> Result<std::collections::HashMap<String, u64>, StatusCode> {
|
||||
let rows: Vec<Document> = db
|
||||
.cve_alerts()
|
||||
.aggregate(count_by_target(open_vulnerabilities_filter(target_ids)))
|
||||
.await
|
||||
.map_err(internal)?
|
||||
.try_collect()
|
||||
.await
|
||||
.map_err(internal)?;
|
||||
Ok(counts_by_target(&rows))
|
||||
}
|
||||
|
||||
fn count_by_target(filter: Document) -> Vec<Document> {
|
||||
vec![
|
||||
doc! { "$match": filter },
|
||||
doc! { "$group": { "_id": "$repo_id", "n": { "$sum": 1 } } },
|
||||
]
|
||||
}
|
||||
|
||||
fn counts_by_target(rows: &[Document]) -> std::collections::HashMap<String, u64> {
|
||||
rows.iter()
|
||||
.filter_map(|row| {
|
||||
let id = row.get_str("_id").ok()?.to_string();
|
||||
let n = row
|
||||
@@ -507,7 +543,7 @@ async fn open_findings_counts(
|
||||
.ok()?;
|
||||
Some((id, u64::try_from(n).ok()?))
|
||||
})
|
||||
.collect())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `GET /api/v1/platform/tenants/{id}/targets?project=<id>` — the tenant's
|
||||
@@ -538,11 +574,12 @@ pub async fn list_targets(
|
||||
.filter_map(|t| t.id.map(|id| id.to_hex()))
|
||||
.collect();
|
||||
let runs = db.scan_runs();
|
||||
let (mut last_scans, open) = tokio::join!(
|
||||
let (mut last_scans, open, vulns) = tokio::join!(
|
||||
super::onboarding::latest_scan_runs(&runs, ids.clone()),
|
||||
open_findings_counts(&db, ids),
|
||||
open_findings_counts(&db, ids.clone()),
|
||||
open_vulnerabilities_counts(&db, ids),
|
||||
);
|
||||
let open = open?;
|
||||
let (open, vulns) = (open?, vulns?);
|
||||
let known = super::projects::tenant_projects(&st.agent, &tenant_id)
|
||||
.await
|
||||
.ok();
|
||||
@@ -553,6 +590,7 @@ pub async fn list_targets(
|
||||
PlatformTarget {
|
||||
last_scan_at: last_scans.remove(&id).map(|run| run.started_at),
|
||||
open_findings: open.get(&id).copied().unwrap_or(0),
|
||||
open_vulnerabilities: vulns.get(&id).copied().unwrap_or(0),
|
||||
project_ids: match known.as_deref() {
|
||||
Some(known) => super::projects::known_project_ids(&t.project_ids, known),
|
||||
None => t.project_ids,
|
||||
@@ -782,3 +820,51 @@ mod build_info_tests {
|
||||
assert!(!BUILD_COMMIT.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod target_count_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn open_vulnerabilities_leave_out_resolved_and_assessed_alerts() {
|
||||
let f = open_vulnerabilities_filter(vec!["a".into(), "b".into()]);
|
||||
assert_eq!(
|
||||
f.get_document("repo_id")
|
||||
.unwrap()
|
||||
.get_array("$in")
|
||||
.unwrap()
|
||||
.len(),
|
||||
2
|
||||
);
|
||||
assert_eq!(f.get("resolved_at"), Some(&mongodb::bson::Bson::Null));
|
||||
let nin = f
|
||||
.get_document("vex.status")
|
||||
.unwrap()
|
||||
.get_array("$nin")
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
nin,
|
||||
&vec![
|
||||
mongodb::bson::Bson::from("not_affected"),
|
||||
mongodb::bson::Bson::from("fixed")
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn counts_are_read_per_target_whatever_the_integer_width() {
|
||||
let rows = vec![
|
||||
doc! { "_id": "a", "n": 3_i32 },
|
||||
doc! { "_id": "b", "n": 5_i64 },
|
||||
doc! { "_id": "c", "n": -1_i64 },
|
||||
doc! { "n": 2_i32 },
|
||||
];
|
||||
let counts = counts_by_target(&rows);
|
||||
assert_eq!(counts.get("a"), Some(&3));
|
||||
assert_eq!(counts.get("b"), Some(&5));
|
||||
assert_eq!(counts.len(), 2);
|
||||
let stages = count_by_target(doc! { "x": 1 });
|
||||
assert_eq!(stages.len(), 2);
|
||||
assert!(stages[0].contains_key("$match") && stages[1].contains_key("$group"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user