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
152 lines
5.1 KiB
Rust
152 lines
5.1 KiB
Rust
//! 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. 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.
|
|
|
|
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
|
|
|
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;
|
|
|
|
/// A site that lets any origin read its answers.
|
|
async fn open_cors_site() -> String {
|
|
let app = Router::new().fallback(|| async {
|
|
(
|
|
[(ACCESS_CONTROL_ALLOW_ORIGIN, "*")],
|
|
"<html><body>shop</body></html>",
|
|
)
|
|
});
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
let addr = listener.local_addr().unwrap();
|
|
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
|
|
format!("http://{addr}")
|
|
}
|
|
|
|
/// Scan target `id` and return the finished scan run.
|
|
async fn scan(server: &TestServer, runs: &mongodb::Collection<Document>, id: &str) -> Document {
|
|
let before = runs.count_documents(doc! { "repo_id": id }).await.unwrap();
|
|
server
|
|
.post(&format!("/api/v1/targets/{id}/scan"), &json!({}))
|
|
.await;
|
|
for _ in 0..240 {
|
|
let done = runs
|
|
.count_documents(doc! { "repo_id": id, "status": { "$in": ["completed", "failed"] } })
|
|
.await
|
|
.unwrap();
|
|
if done > before {
|
|
return runs
|
|
.find_one(doc! { "repo_id": id })
|
|
.sort(doc! { "started_at": -1 })
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
}
|
|
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
|
|
}
|
|
panic!("the scan of {id} did not finish");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn dast_findings_are_findings_of_the_target_once() {
|
|
// This binary holds only this test, so the setting reaches no other one.
|
|
std::env::set_var("PW_ALLOW_PRIVATE_TARGETS", "1");
|
|
let site = open_cors_site().await;
|
|
let server = TestServer::start().await;
|
|
let created: serde_json::Value = server
|
|
.post(
|
|
"/api/v1/targets",
|
|
&json!({
|
|
"name": "shop",
|
|
"target_type": "web_app",
|
|
"artifacts": [{ "kind": "live_url", "source_ref": site }]
|
|
}),
|
|
)
|
|
.await
|
|
.json()
|
|
.await
|
|
.unwrap();
|
|
let id = created["data"]["_id"]["$oid"].as_str().unwrap().to_string();
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let db = mongodb::Client::with_uri_str(&uri)
|
|
.await
|
|
.unwrap()
|
|
.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;
|
|
|
|
assert_eq!(first.get_str("status").unwrap(), "completed", "{first:?}");
|
|
let cors = findings
|
|
.find_one(doc! { "repo_id": &id, "scanner": "dast", "cwe": "CWE-942" })
|
|
.await
|
|
.unwrap()
|
|
.expect("the CORS finding is a finding of the target");
|
|
assert_eq!(cors.get_str("scan_type").unwrap(), "dast");
|
|
let refs: Vec<&str> = cors
|
|
.get_array("control_refs")
|
|
.unwrap()
|
|
.iter()
|
|
.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
|
|
.unwrap();
|
|
assert!(first.get_i64("new_findings_count").unwrap() >= i64::try_from(stored).unwrap());
|
|
|
|
let second = scan(&server, &runs, &id).await;
|
|
|
|
assert_eq!(
|
|
findings
|
|
.count_documents(doc! { "repo_id": &id, "scanner": "dast" })
|
|
.await
|
|
.unwrap(),
|
|
stored,
|
|
"a second scan finds the same weaknesses, it does not add them again"
|
|
);
|
|
assert_eq!(second.get_i64("new_findings_count").unwrap(), 0);
|
|
|
|
server.cleanup().await;
|
|
}
|