From 7d4004b7c3d0086d2650b5b1cc7f224f53444994 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:19:18 +0100 Subject: [PATCH] feat(pipeline): a DAST finding links the code finding with its CWE (#998) #998 point 4 asked to pass the target's open SAST findings to the DAST scan as hints. No DAST agent reads its hints, so that would change nothing. `DastFinding::linked_sast_finding_id` is read by the pentest report and the dashboard (file, line, snippet), but nothing set it. - New `pipeline::dast_findings::linked_code_finding`: the one finding of the target with the same CWE. No CWE, none or several with it: no link, since a CWE alone does not say which of several it is. - The DAST stage loads the target's open findings with a place in the code and a CWE (statuses open, triaged, needs_review) once per DAST target and links each DAST finding before storing it. - Docs: when a DAST finding is linked. Tests: - unit: the one match is linked; no CWE, another CWE or two candidates give no link. - `tests/dast_findings.rs`: with an open CWE-942 code finding seeded, the scan's CORS finding is stored linked to it. It fails without the link. Part 4 of #998 (point 4, agreed as linking instead of hints). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Q9inRcu428A2iNjDHPEAT9 --- .../src/pipeline/dast_findings.rs | 75 +++++++++++++++++++ compliance-agent/src/pipeline/graph_build.rs | 34 ++++++++- compliance-agent/tests/dast_findings.rs | 35 ++++++++- docs/features/dast.md | 2 +- 4 files changed, 141 insertions(+), 5 deletions(-) diff --git a/compliance-agent/src/pipeline/dast_findings.rs b/compliance-agent/src/pipeline/dast_findings.rs index e0144c4..80de93b 100644 --- a/compliance-agent/src/pipeline/dast_findings.rs +++ b/compliance-agent/src/pipeline/dast_findings.rs @@ -54,6 +54,31 @@ pub fn as_finding(repo_id: &str, dast: &DastFinding) -> Finding { finding } +/// The code finding `dast` is linked to (#998): the one finding in `code` +/// with the same CWE, so the DAST finding shows the file and line its +/// weakness is likely in. +/// +/// # Arguments +/// +/// * `dast` - the finding the DAST scan reported +/// * `code` - the target's open findings that have a place in the code +/// +/// # Returns +/// +/// That finding's id, or `None` when `dast` has no CWE, or no finding or +/// several share it: a CWE alone does not say which of several it is. +pub fn linked_code_finding(dast: &DastFinding, code: &[Finding]) -> Option { + let cwe = dast.cwe.as_deref().and_then(normalise_cwe)?; + let mut same = code + .iter() + .filter(|f| f.cwe.as_deref().and_then(normalise_cwe).as_deref() == Some(cwe.as_str())); + let only = same.next()?; + if same.next().is_some() { + return None; + } + only.id.map(|id| id.to_hex()) +} + #[cfg(test)] mod tests { use compliance_core::models::dast::DastVulnType; @@ -119,4 +144,54 @@ mod tests { } assert!(with_parameter.description.contains("parameter `q`")); } + + fn code_finding(cwe: &str) -> Finding { + let mut f = Finding::new( + "target-1".into(), + format!("fp-{cwe}"), + "semgrep".into(), + ScanType::Sast, + "Permissive CORS".into(), + "Any origin is allowed.".into(), + Severity::Medium, + ); + f.id = Some(mongodb::bson::oid::ObjectId::new()); + f.cwe = Some(cwe.into()); + f.file_path = Some("src/server.rs".into()); + f + } + + #[test] + fn a_dast_finding_links_the_one_code_finding_with_its_cwe() { + let cors = code_finding("CWE-942"); + let code = [code_finding("CWE-79"), cors.clone()]; + + assert_eq!( + linked_code_finding(&dast("https://app.example.com/", None), &code), + cors.id.map(|id| id.to_hex()) + ); + } + + #[test] + fn no_link_without_a_cwe_or_with_several_candidates() { + let finding = dast("https://app.example.com/", None); + let mut no_cwe = finding.clone(); + no_cwe.cwe = None; + + assert_eq!( + linked_code_finding(&finding, &[code_finding("CWE-79")]), + None + ); + assert_eq!( + linked_code_finding(&no_cwe, &[code_finding("CWE-942")]), + None + ); + assert_eq!( + linked_code_finding( + &finding, + &[code_finding("cwe-942"), code_finding("CWE-942")] + ), + None + ); + } } diff --git a/compliance-agent/src/pipeline/graph_build.rs b/compliance-agent/src/pipeline/graph_build.rs index a833076..6399e8f 100644 --- a/compliance-agent/src/pipeline/graph_build.rs +++ b/compliance-agent/src/pipeline/graph_build.rs @@ -179,7 +179,7 @@ impl PipelineOrchestrator { }; let orchestrator = compliance_dast::DastOrchestrator::new(100, self.net_guard); let scan = tokio::time::timeout(DAST_TIMEOUT, orchestrator.run_scan(target, Vec::new())); - let (mut run, findings) = match scan.await { + let (mut run, mut findings) = match scan.await { Ok(Ok(result)) => result, Ok(Err(e)) => { tracing::error!(target = %target.name, "DAST scan failed: {e}"); @@ -195,9 +195,12 @@ impl PipelineOrchestrator { tracing::error!("Failed to store DAST scan run: {e}"); return failed("DAST results could not be stored"); } + let code = self.open_code_findings(repo_id).await; let mut new_count = 0; - for finding in &findings { - if let Err(e) = self.db.dast_findings().insert_one(finding).await { + for finding in &mut findings { + finding.linked_sast_finding_id = + super::dast_findings::linked_code_finding(finding, &code); + if let Err(e) = self.db.dast_findings().insert_one(&*finding).await { tracing::error!("Failed to store DAST finding: {e}"); } match self.add_dast_finding(repo_id, scan_run_id, finding).await { @@ -215,6 +218,31 @@ impl PipelineOrchestrator { Ok(new_count) } + /// The open findings of target `repo_id` that have a place in the code and + /// a CWE: what a DAST finding can be linked to (#998). Empty when they + /// cannot be read (logged); the DAST findings are then stored unlinked. + async fn open_code_findings(&self, repo_id: &str) -> Vec { + use futures_util::TryStreamExt; + + let filter = mongodb::bson::doc! { + "repo_id": repo_id, + "scanner": { "$ne": super::dast_findings::SCANNER }, + "file_path": { "$ne": null }, + "cwe": { "$ne": null }, + "status": { "$in": ["open", "triaged", "needs_review"] }, + }; + match self.db.findings().find(filter).await { + Ok(cursor) => cursor.try_collect().await.unwrap_or_else(|e| { + tracing::warn!("Code findings for DAST links not read: {e}"); + Vec::new() + }), + Err(e) => { + tracing::warn!("Code findings for DAST links not read: {e}"); + Vec::new() + } + } + } + /// Store `dast` as a finding of target `repo_id`, unless the target has /// it already (the same fingerprint). /// diff --git a/compliance-agent/tests/dast_findings.rs b/compliance-agent/tests/dast_findings.rs index 16482d7..1c4a4ee 100644 --- a/compliance-agent/tests/dast_findings.rs +++ b/compliance-agent/tests/dast_findings.rs @@ -1,5 +1,6 @@ //! DAST findings join the findings of the target they were found on (#998), -//! with their control refs, and a second scan does not add them again. +//! with their control refs, and a second scan does not add them again. A DAST +//! finding is linked to the target's one open code finding with its CWE. //! Drives the real API and pipeline against a live Mongo (`TEST_MONGODB_URI`) //! and a local site, so this binary allows private targets for its process. @@ -10,6 +11,7 @@ mod common; use axum::http::header::ACCESS_CONTROL_ALLOW_ORIGIN; use axum::Router; use common::TestServer; +use compliance_core::models::{Finding, ScanType, Severity}; use mongodb::bson::{doc, Document}; use serde_json::json; @@ -79,6 +81,26 @@ async fn dast_findings_are_findings_of_the_target_once() { .database(&server.tenant_db_name()); let runs = db.collection::("scan_runs"); let findings = db.collection::("findings"); + let mut code = Finding::new( + id.clone(), + "fp-code-cors".into(), + "semgrep".into(), + ScanType::Sast, + "Permissive CORS".into(), + "The server allows any origin.".into(), + Severity::Medium, + ); + code.cwe = Some("CWE-942".into()); + code.file_path = Some("src/server.rs".into()); + let code_id = db + .collection::("findings") + .insert_one(&code) + .await + .unwrap() + .inserted_id + .as_object_id() + .unwrap() + .to_hex(); let first = scan(&server, &runs, &id).await; @@ -96,6 +118,17 @@ async fn dast_findings_are_findings_of_the_target_once() { .filter_map(|r| r.as_str()) .collect(); assert_eq!(refs, ["cra-ai-1"]); + let dast_cors = db + .collection::("dast_findings") + .find_one(doc! { "cwe": "CWE-942" }) + .await + .unwrap() + .expect("the DAST finding is stored with its evidence"); + assert_eq!( + dast_cors.get_str("linked_sast_finding_id").unwrap(), + code_id, + "linked to the code finding with the same CWE" + ); let stored = findings .count_documents(doc! { "repo_id": &id, "scanner": "dast" }) .await diff --git a/docs/features/dast.md b/docs/features/dast.md index f585e11..3809c8a 100644 --- a/docs/features/dast.md +++ b/docs/features/dast.md @@ -71,7 +71,7 @@ Click the **Scan** button on any target row. The scan progresses through: A target with a live URL gets a DAST scan in each of its scans, where the deployment runs DAST. The scan waits for it like for its other stages. When the DAST scan does not complete (the site cannot be reached, the login cannot be sent, or it takes longer than 20 minutes), the scan shows the DAST stage as not completed. The rest of the scan is not affected, and the DAST run keeps the reason. -Its findings are findings of the target too: they appear in the target's findings list with the other findings (scanner `dast`), count as new findings of the scan, and carry the CRA controls their CWE is evidence for. A later scan that finds the same weakness at the same endpoint, method and parameter does not add it again. The request and response evidence stays with the finding under **DAST > Findings**. +Its findings are findings of the target too: they appear in the target's findings list with the other findings (scanner `dast`), count as new findings of the scan, and carry the CRA controls their CWE is evidence for. A later scan that finds the same weakness at the same endpoint, method and parameter does not add it again. The request and response evidence stays with the finding under **DAST > Findings**. When exactly one open code finding of the target has the same CWE, the DAST finding is linked to it, so it shows the file and line where the weakness likely is. With several, the CWE alone does not say which one, so it stays unlinked. ## Viewing DAST Findings -- 2.54.1