Closes #464. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
140 lines
5.1 KiB
Rust
140 lines
5.1 KiB
Rust
//! #464: one rule's many low findings become one; the findings stored one
|
|
//! per place are closed as grouped, and the group stays the same finding
|
|
//! from scan to scan. 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::dedup::compute_fingerprint;
|
|
use compliance_agent::pipeline::finding_store::RunFindings;
|
|
use compliance_agent::pipeline::grouping::{group_fingerprint, group_repetitive, GroupingPolicy};
|
|
use compliance_core::models::{
|
|
Finding, FindingStatus, ScanChangeKind, ScanType, Severity, StatusChange,
|
|
};
|
|
use mongodb::bson::doc;
|
|
|
|
const TENANT: &str = "group-findings";
|
|
const TARGET: &str = "target-1";
|
|
|
|
fn logging(line: u32) -> Finding {
|
|
let file = "app/src/main/java/Main.java";
|
|
let mut f = Finding::new(
|
|
TARGET.into(),
|
|
compute_fingerprint(&[TARGET, "android_logging", file, &line.to_string()]),
|
|
"mobsfscan".into(),
|
|
ScanType::Sast,
|
|
"The app logs information".into(),
|
|
"The app logs information.".into(),
|
|
Severity::Low,
|
|
);
|
|
f.rule_id = Some("android_logging".into());
|
|
f.file_path = Some(file.into());
|
|
f.line_number = Some(line);
|
|
f
|
|
}
|
|
|
|
/// What a scan reports for `lines`, grouped as the pipeline does.
|
|
fn scan(lines: impl Iterator<Item = u32>) -> Vec<Finding> {
|
|
let mut findings: Vec<Finding> = lines.map(logging).collect();
|
|
group_repetitive(&mut findings, &GroupingPolicy::default());
|
|
findings
|
|
}
|
|
|
|
async fn row(db: &Database, fingerprint: &str) -> Finding {
|
|
db.findings()
|
|
.find_one(doc! { "fingerprint": fingerprint })
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_rule_firing_everywhere_is_one_finding_across_scans() {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let prefix = format!("group_{}", &uuid::Uuid::new_v4().simple().to_string()[..10]);
|
|
let Ok(pool) = DatabasePool::connect(&uri, &prefix).await else {
|
|
eprintln!("SKIP group_findings: no MongoDB reachable at {uri}");
|
|
return;
|
|
};
|
|
let Ok(db) = pool.for_tenant_id(TENANT).await else {
|
|
eprintln!("SKIP group_findings: MongoDB at {uri} not usable");
|
|
return;
|
|
};
|
|
|
|
// Arrange: a scan before grouping stored 38 findings, one per place; a
|
|
// person decided one of them.
|
|
let mut stored: Vec<Finding> = (1..=38).map(logging).collect();
|
|
stored[0].status = FindingStatus::FalsePositive;
|
|
db.findings().insert_many(&stored).await.unwrap();
|
|
|
|
// Act: a scan with grouping.
|
|
let mut first = scan(1..=38);
|
|
assert_eq!(first.len(), 1);
|
|
let mut run = RunFindings::new("run-1");
|
|
run.completed("mobsfscan");
|
|
assert_eq!(run.save_new(&db, &mut first).await.unwrap(), 1);
|
|
run.write_back(&db, &mut first).await.unwrap();
|
|
let closed = run.resolve_missing(&db, TARGET, &[]).await.unwrap();
|
|
|
|
// Assert: the 37 undecided ones are closed as grouped (not as fixed),
|
|
// the decided one is left alone.
|
|
assert_eq!(closed, 37);
|
|
let single = row(&db, &stored[1].fingerprint).await;
|
|
assert_eq!(single.status, FindingStatus::Resolved);
|
|
let change = single
|
|
.status_history
|
|
.last()
|
|
.filter(|c| c.by == StatusChange::SCAN_ACTOR)
|
|
.and_then(|c| c.scan.as_ref())
|
|
.map(|s| s.kind);
|
|
assert_eq!(change, Some(ScanChangeKind::Grouped));
|
|
assert_eq!(
|
|
row(&db, &stored[0].fingerprint).await.status,
|
|
FindingStatus::FalsePositive
|
|
);
|
|
let fp = group_fingerprint(TARGET, "mobsfscan", "android_logging");
|
|
let group = row(&db, &fp).await;
|
|
assert_eq!(group.occurrences.len(), 38);
|
|
|
|
// A person triages the group.
|
|
db.findings()
|
|
.update_one(
|
|
doc! { "fingerprint": &fp },
|
|
doc! { "$set": { "status": "accepted_risk" } },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
// Act: a rescan of the same tree finds nothing new.
|
|
let mut again = scan(1..=38);
|
|
let mut run = RunFindings::new("run-2");
|
|
run.completed("mobsfscan");
|
|
assert_eq!(run.save_new(&db, &mut again).await.unwrap(), 0);
|
|
|
|
// Act: the places change (two more, one gone): still the same finding,
|
|
// its decision kept, its places updated.
|
|
let mut moved = scan(2..=40);
|
|
let mut run = RunFindings::new("run-3");
|
|
run.completed("mobsfscan");
|
|
assert_eq!(run.save_new(&db, &mut moved).await.unwrap(), 0);
|
|
run.write_back(&db, &mut moved).await.unwrap();
|
|
run.resolve_missing(&db, TARGET, &[]).await.unwrap();
|
|
let group = row(&db, &fp).await;
|
|
assert_eq!(group.status, FindingStatus::AcceptedRisk);
|
|
assert_eq!(group.occurrences.len(), 39);
|
|
assert_eq!(group.line_number, Some(2));
|
|
assert!(group.description.contains("39 places"));
|
|
assert_eq!(
|
|
db.findings()
|
|
.count_documents(doc! { "repo_id": TARGET })
|
|
.await
|
|
.unwrap(),
|
|
39,
|
|
"38 single findings and one group, nothing else"
|
|
);
|
|
|
|
pool.drop_tenant(TENANT).await.ok();
|
|
}
|