feat(pipeline): a DAST finding links the code finding with its CWE (#998) (#1010)
CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Dashboard (push) Skipped
CI / Deploy MCP (push) Skipped
CI / Deploy Docs (push) Successful in 1m7s
CI / Deploy Agent (push) Successful in 6m22s
CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Dashboard (push) Skipped
CI / Deploy MCP (push) Skipped
CI / Deploy Docs (push) Successful in 1m7s
CI / Deploy Agent (push) Successful in 6m22s
This commit was merged in pull request #1010.
This commit is contained in:
1 parent
0ad8bbc3c9
commit
98be82c0d0
4 files changed
+141
-5
No files matched your search
@@ -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<String> {
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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<compliance_core::models::Finding> {
|
||||
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).
|
||||
///
|
||||
|
||||
@@ -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::<Document>("scan_runs");
|
||||
let findings = db.collection::<Document>("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::<Finding>("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::<Document>("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
|
||||
|
||||
@@ -72,7 +72,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
|
||||
|
||||
|
||||
Reference in new issue
Block a user