Files
pruefwerk/compliance-agent/tests/integration/api/dast.rs
T
sharang 4f5d713bbc
CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 4s
CI / Deploy Docs (push) Successful in 1m6s
CI / Deploy MCP (push) Successful in 3m43s
CI / Deploy Dashboard (push) Successful in 4m45s
CI / Deploy Agent (push) Successful in 5m42s
feat(pipeline): the DAST stage waits for its scans and records a failure (#998) (#1007)
2026-11-16 10:30:45 +00:00

201 lines
6.5 KiB
Rust

use crate::common::TestServer;
use mongodb::bson::doc;
use serde_json::json;
#[tokio::test]
async fn add_and_list_dast_targets() {
let server = TestServer::start().await;
// Initially empty
let resp = server.get("/api/v1/dast/targets").await;
assert_eq!(resp.status(), 200);
let body: serde_json::Value = resp.json().await.unwrap();
assert_eq!(body["data"].as_array().unwrap().len(), 0);
// Add a target
let resp = server
.post(
"/api/v1/dast/targets",
&json!({
"name": "test-app",
"base_url": "https://test-app.example.com",
"target_type": "web_app",
}),
)
.await;
assert_eq!(resp.status(), 200);
// List should return 1
let resp = server.get("/api/v1/dast/targets").await;
let body: serde_json::Value = resp.json().await.unwrap();
let targets = body["data"].as_array().unwrap();
assert_eq!(targets.len(), 1);
assert_eq!(targets[0]["name"], "test-app");
assert_eq!(targets[0]["base_url"], "https://test-app.example.com");
server.cleanup().await;
}
#[tokio::test]
async fn list_dast_findings_empty() {
let server = TestServer::start().await;
let resp = server.get("/api/v1/dast/findings").await;
assert_eq!(resp.status(), 200);
let body: serde_json::Value = resp.json().await.unwrap();
assert_eq!(body["data"].as_array().unwrap().len(), 0);
server.cleanup().await;
}
#[tokio::test]
async fn a_targets_login_is_stored_sealed_and_never_returned() {
let server = TestServer::start().await;
let resp = server
.post(
"/api/v1/dast/targets",
&json!({
"name": "members",
"base_url": "https://members.example.com",
"auth": {
"method": "bearer",
"token": "tok-secret",
"headers": { "X-Api-Key": "key-secret" },
},
}),
)
.await;
assert_eq!(resp.status(), 200);
let body: serde_json::Value = resp.json().await.unwrap();
let id = body["data"]["_id"]["$oid"].as_str().expect("the new id");
let returned = body.to_string();
assert!(
!returned.contains("tok-secret") && !returned.contains("key-secret"),
"{returned}"
);
let mongodb_uri = std::env::var("TEST_MONGODB_URI")
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
let client = mongodb::Client::with_uri_str(&mongodb_uri).await.unwrap();
let stored = client
.database(&server.tenant_db_name())
.collection::<mongodb::bson::Document>("dast_targets")
.find_one(mongodb::bson::doc! {
"_id": mongodb::bson::oid::ObjectId::parse_str(id).unwrap()
})
.await
.unwrap()
.expect("stored under the returned id");
let auth = stored.get_document("auth_config").unwrap();
let token = auth.get_str("token").unwrap();
let header = auth
.get_document("headers")
.unwrap()
.get_str("X-Api-Key")
.unwrap();
assert!(token != "tok-secret" && header != "key-secret", "{auth}");
server.cleanup().await;
}
#[tokio::test]
async fn a_login_the_scan_cannot_send_is_refused() {
let server = TestServer::start().await;
for auth in [
json!({ "method": "form", "login_url": "https://members.example.com/login" }),
json!({ "method": "bearer" }),
json!({ "method": "ntlm", "token": "t" }),
] {
let resp = server
.post(
"/api/v1/dast/targets",
&json!({ "name": "m", "base_url": "https://members.example.com", "auth": auth }),
)
.await;
assert_eq!(resp.status(), 400, "{auth}");
}
let resp = server.get("/api/v1/dast/targets").await;
let body: serde_json::Value = resp.json().await.unwrap();
assert!(body["data"].as_array().unwrap().is_empty());
server.cleanup().await;
}
/// A DAST scan that does not complete is a failed `dast` stage of the scan
/// run, recorded once the stage has waited for it; the run itself completes
/// (#998). The live URL does not resolve, so reconnaissance fails.
#[tokio::test]
async fn a_dast_scan_that_does_not_complete_is_a_failed_stage_of_the_run() {
let server = TestServer::start().await;
let created: serde_json::Value = server
.post(
"/api/v1/targets",
&json!({
"name": "web",
"target_type": "web_app",
"artifacts": [
{ "kind": "live_url", "source_ref": "https://no-such-host.example.com" }
]
}),
)
.await
.json()
.await
.unwrap();
let id = created["data"]["_id"]["$oid"].as_str().unwrap().to_string();
server
.post(&format!("/api/v1/targets/{id}/scan"), &json!({}))
.await;
let client = mongodb::Client::with_uri_str(&server_mongodb_uri())
.await
.unwrap();
let runs = client
.database(&server.tenant_db_name())
.collection::<mongodb::bson::Document>("scan_runs");
let mut run = None;
for _ in 0..120 {
let found = runs
.find_one(doc! { "repo_id": &id, "status": { "$in": ["completed", "failed"] } })
.await
.unwrap();
if found.is_some() {
run = found;
break;
}
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
}
let run = run.expect("the scan run finished");
assert_eq!(run.get_str("status").unwrap(), "completed");
let dast: Vec<_> = run
.get_array("skipped_stages")
.unwrap()
.iter()
.filter_map(|s| s.as_document())
.filter(|s| s.get_str("stage") == Ok("dast"))
.collect();
assert_eq!(dast.len(), 1, "{run:?}");
assert_eq!(dast[0].get_str("code").unwrap(), "tool_failed");
assert_eq!(dast[0].get_str("subject").unwrap(), "DAST");
// The DAST run itself is stored, with the scanner's reason.
let dast_run = client
.database(&server.tenant_db_name())
.collection::<mongodb::bson::Document>("dast_scan_runs")
.find_one(doc! {})
.await
.unwrap()
.expect("the DAST run is stored");
assert_eq!(dast_run.get_str("status").unwrap(), "failed");
server.cleanup().await;
}
fn server_mongodb_uri() -> String {
std::env::var("TEST_MONGODB_URI")
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into())
}