fix(cve): the NVD monitor adds its CVEs to SBOM entries without overwriting a scan's
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) Successful in 11m9s
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) Successful in 11m9s
The pass reads the entries at its start and may run for 10 minutes; each new CVE reference is now pushed unless listed, instead of setting the whole list from the copy read earlier. Tests: an oversized page is asked for again with half as many CVEs; the one-request budget test gets room for a slow first connection. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
This commit is contained in:
1 parent
3e17163a6e
commit
0e3fccf674
1 file changed
+122
-15
@@ -529,36 +529,51 @@ async fn apply_repo(
|
||||
by_entry.entry(repo.watched[w].entry).or_default().push(cve);
|
||||
}
|
||||
let mut alerts = Vec::new();
|
||||
for (&entry, cves) in &mut by_entry {
|
||||
let mut found_ids: Vec<(usize, Vec<String>)> = Vec::with_capacity(by_entry.len());
|
||||
for (entry, cves) in by_entry {
|
||||
let (name, version) = (
|
||||
repo.entries[entry].name.clone(),
|
||||
repo.entries[entry].version.clone(),
|
||||
);
|
||||
found_ids.push((entry, cves.iter().map(|c| c.id.clone()).collect()));
|
||||
alerts.extend(record_nvd(
|
||||
&repo.repo_id,
|
||||
&mut repo.entries,
|
||||
&name,
|
||||
&version,
|
||||
std::mem::take(cves),
|
||||
cves,
|
||||
));
|
||||
}
|
||||
if let Some(enricher) = services.enricher {
|
||||
enricher.enrich(&mut alerts, &mut repo.entries).await;
|
||||
}
|
||||
for entry in by_entry.keys().filter_map(|&i| repo.entries.get(i)) {
|
||||
let Some(id) = entry.id else {
|
||||
// Each CVE is added to its stored entry unless listed already: the
|
||||
// entries were read at the start of the run, and a scan may have
|
||||
// rewritten them since.
|
||||
for (entry, ids) in &found_ids {
|
||||
let Some(entry) = repo.entries.get(*entry) else {
|
||||
continue;
|
||||
};
|
||||
db.sbom_entries()
|
||||
.update_one(
|
||||
doc! { "_id": id },
|
||||
doc! { "$set": {
|
||||
"known_vulnerabilities": mongodb::bson::to_bson(&entry.known_vulnerabilities)
|
||||
.map_err(|e| AgentError::Other(format!("encode SBOM entry: {e}")))?,
|
||||
"updated_at": mongodb::bson::DateTime::now(),
|
||||
}},
|
||||
)
|
||||
.await?;
|
||||
let Some(entry_id) = entry.id else {
|
||||
continue;
|
||||
};
|
||||
for vuln in entry
|
||||
.known_vulnerabilities
|
||||
.iter()
|
||||
.filter(|v| ids.contains(&v.id))
|
||||
{
|
||||
let vuln_ref = mongodb::bson::to_bson(vuln)
|
||||
.map_err(|e| AgentError::Other(format!("encode SBOM entry: {e}")))?;
|
||||
db.sbom_entries()
|
||||
.update_one(
|
||||
doc! { "_id": entry_id, "known_vulnerabilities.id": { "$ne": &vuln.id } },
|
||||
doc! {
|
||||
"$push": { "known_vulnerabilities": vuln_ref },
|
||||
"$set": { "updated_at": mongodb::bson::DateTime::now() },
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
let mut new = Vec::new();
|
||||
for (i, alert) in alerts.iter().enumerate() {
|
||||
@@ -1234,7 +1249,7 @@ mod tests {
|
||||
let tenants = || vec![("nvd-monitor".to_string(), db.clone())];
|
||||
|
||||
// Act 1: a budget for one request only.
|
||||
let one = feed(&url, Duration::from_secs(3600), Duration::from_secs(1));
|
||||
let one = feed(&url, Duration::from_secs(3600), Duration::from_secs(30));
|
||||
let first = run_pass(&admin, tenants(), &one, &services, now)
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -1381,4 +1396,96 @@ mod tests {
|
||||
db.inner().drop().await.unwrap();
|
||||
admin.drop().await.unwrap();
|
||||
}
|
||||
|
||||
/// A stand-in for NVD whose pages of more than 250 CVEs exceed the body
|
||||
/// cap, listing one CVE.
|
||||
async fn fake_nvd_too_large(listed: serde_json::Value) -> (String, Seen) {
|
||||
let seen: Seen = Arc::default();
|
||||
let log = Arc::clone(&seen);
|
||||
let oversized = Arc::new(" ".repeat(crate::pipeline::public_http::MAX_BODY_BYTES + 1));
|
||||
let app = axum::Router::new().route(
|
||||
"/",
|
||||
axum::routing::get(
|
||||
move |axum::extract::Query(q): axum::extract::Query<HashMap<String, String>>| {
|
||||
let log = Arc::clone(&log);
|
||||
let oversized = Arc::clone(&oversized);
|
||||
let listed = listed.clone();
|
||||
async move {
|
||||
let per_page: usize = q["resultsPerPage"].parse().unwrap();
|
||||
log.lock().unwrap().push(q);
|
||||
if per_page > 250 {
|
||||
return oversized.as_str().to_owned();
|
||||
}
|
||||
serde_json::json!({
|
||||
"totalResults": 1,
|
||||
"vulnerabilities": [listed],
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let url = format!("http://{}/", listener.local_addr().unwrap());
|
||||
tokio::spawn(async move {
|
||||
let _ = axum::serve(listener, app).await;
|
||||
});
|
||||
(url, seen)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_oversized_page_is_asked_for_again_with_fewer_cves() {
|
||||
let uri = std::env::var("TEST_MONGODB_URI")
|
||||
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
||||
let prefix = format!(
|
||||
"nvdbig_{}",
|
||||
&uuid::Uuid::new_v4().simple().to_string()[..10]
|
||||
);
|
||||
let Ok(pool) = crate::database::DatabasePool::connect(&uri, &prefix).await else {
|
||||
eprintln!("SKIP nvd_monitor: no MongoDB reachable at {uri}");
|
||||
return;
|
||||
};
|
||||
let db = pool.for_tenant_id("nvd-monitor").await.unwrap();
|
||||
let admin = pool.admin_db();
|
||||
db.sbom_entries()
|
||||
.insert_one(entry("t1", "lwIP", "2.1.2", EMBEDDED_MANAGER))
|
||||
.await
|
||||
.unwrap();
|
||||
let (url, seen) = fake_nvd_too_large(vuln(
|
||||
"CVE-2026-0003",
|
||||
"cpe:2.3:a:lwip_project:lwip:*:*:*:*:*:*:*:*",
|
||||
"2.2.0",
|
||||
))
|
||||
.await;
|
||||
let services = Services {
|
||||
enricher: None,
|
||||
issues: None,
|
||||
};
|
||||
let now = at("2026-10-09T12:00:00Z");
|
||||
|
||||
let report = run_pass(
|
||||
&admin,
|
||||
vec![("nvd-monitor".to_string(), db.clone())],
|
||||
&feed(&url, Duration::ZERO, Duration::from_secs(60)),
|
||||
&services,
|
||||
now,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let sizes: Vec<String> = seen
|
||||
.lock()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|q| q["resultsPerPage"].clone())
|
||||
.collect();
|
||||
assert_eq!(sizes, ["500", "250"]);
|
||||
assert_eq!(report.error, None);
|
||||
assert_eq!(report.tenants[0].1.new_alerts, 1);
|
||||
let cursor = load_cursor(&admin).await.unwrap().unwrap();
|
||||
assert_eq!(cursor.done_until, now - SETTLE);
|
||||
|
||||
db.inner().drop().await.unwrap();
|
||||
admin.drop().await.unwrap();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user