CI / Check (push) Canceled after 0s
CI / Detect Changes (push) Canceled after 0s
CI / Deploy Agent (push) Canceled after 0s
CI / Deploy Dashboard (push) Canceled after 0s
CI / Deploy Docs (push) Canceled after 0s
CI / Deploy MCP (push) Canceled after 0s
275 lines
8.8 KiB
Rust
275 lines
8.8 KiB
Rust
//! Findings from a manual DAST run or penetration test: entered by hand and
|
|
//! imported from a report, next to the scan's findings on the target.
|
|
//!
|
|
//! Drives the real API against a live Mongo. Skips cleanly when no Mongo is
|
|
//! reachable.
|
|
|
|
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
|
|
|
mod common;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use axum::{middleware, Extension};
|
|
|
|
use compliance_agent::agent::ComplianceAgent;
|
|
use compliance_agent::api;
|
|
use compliance_agent::database::DatabasePool;
|
|
|
|
use common::dev_config;
|
|
|
|
/// The tenant `inject_dev_tenant` puts every request in.
|
|
const TENANT: &str = "dev";
|
|
|
|
const SARIF: &str = r#"{
|
|
"version": "2.1.0",
|
|
"runs": [{
|
|
"tool": { "driver": { "name": "Nuclei", "rules": [
|
|
{ "id": "default-login", "shortDescription": { "text": "Default credentials accepted" },
|
|
"properties": { "tags": ["external/cwe/cwe-798"], "security-severity": "9.1" } }
|
|
]}},
|
|
"results": [
|
|
{ "ruleId": "default-login", "message": { "text": "admin/admin works" },
|
|
"locations": [{ "physicalLocation": { "artifactLocation": { "uri": "https://192.0.2.10/login" } } }] },
|
|
{ "level": "note", "message": { "text": "Server banner discloses version" } }
|
|
]
|
|
}]
|
|
}"#;
|
|
|
|
struct Harness {
|
|
base_url: String,
|
|
client: reqwest::Client,
|
|
pool: DatabasePool,
|
|
}
|
|
|
|
async fn start() -> Option<Harness> {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let db_name = format!(
|
|
"manual_{}",
|
|
&uuid::Uuid::new_v4().simple().to_string()[..12]
|
|
);
|
|
let Ok(pool) = DatabasePool::connect(&uri, &db_name).await else {
|
|
eprintln!("SKIP manual_findings: no MongoDB reachable at {uri}");
|
|
return None;
|
|
};
|
|
let agent = Arc::new(ComplianceAgent::new(dev_config(uri, db_name), pool.clone()));
|
|
let app = api::routes::build_router()
|
|
.layer(Extension(agent))
|
|
.layer(middleware::from_fn(api::server::inject_dev_tenant));
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
let port = listener.local_addr().unwrap().port();
|
|
tokio::spawn(async move {
|
|
axum::serve(listener, app).await.ok();
|
|
});
|
|
Some(Harness {
|
|
base_url: format!("http://127.0.0.1:{port}"),
|
|
client: reqwest::Client::new(),
|
|
pool,
|
|
})
|
|
}
|
|
|
|
impl Harness {
|
|
async fn post_json(&self, path: &str, body: serde_json::Value) -> (u16, serde_json::Value) {
|
|
let resp = self
|
|
.client
|
|
.post(format!("{}{path}", self.base_url))
|
|
.json(&body)
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
let status = resp.status().as_u16();
|
|
(status, resp.json().await.unwrap_or_default())
|
|
}
|
|
|
|
async fn import(&self, path: &str, report: &str) -> (u16, serde_json::Value) {
|
|
let resp = self
|
|
.client
|
|
.post(format!("{}{path}", self.base_url))
|
|
.body(report.to_string())
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
let status = resp.status().as_u16();
|
|
(status, resp.json().await.unwrap_or_default())
|
|
}
|
|
|
|
async fn findings(&self, target: &str) -> Vec<serde_json::Value> {
|
|
let body: serde_json::Value = self
|
|
.client
|
|
.get(format!(
|
|
"{}/api/v1/findings?repo_id={target}&limit=50",
|
|
self.base_url
|
|
))
|
|
.send()
|
|
.await
|
|
.unwrap()
|
|
.json()
|
|
.await
|
|
.unwrap();
|
|
body["data"].as_array().cloned().unwrap_or_default()
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn pentest_findings_are_added_and_imported_once() {
|
|
let Some(h) = start().await else { return };
|
|
|
|
// Arrange: a target.
|
|
let (_, created) = h
|
|
.post_json(
|
|
"/api/v1/targets",
|
|
serde_json::json!({ "name": "sensor", "target_type": "firmware_rtos" }),
|
|
)
|
|
.await;
|
|
let id = created["data"]["_id"]["$oid"].as_str().unwrap().to_string();
|
|
|
|
// Act: one finding by hand, the same one again, then a report twice.
|
|
let finding = serde_json::json!({
|
|
"kind": "pentest",
|
|
"title": "Telnet open with a default password",
|
|
"severity": "critical",
|
|
"cwe": "798",
|
|
"location": "tcp://192.0.2.10:23",
|
|
"reproduction": "telnet 192.0.2.10; root/root",
|
|
});
|
|
let (added, body) = h
|
|
.post_json(&format!("/api/v1/targets/{id}/findings"), finding.clone())
|
|
.await;
|
|
let (again, _) = h
|
|
.post_json(&format!("/api/v1/targets/{id}/findings"), finding)
|
|
.await;
|
|
let import_path = format!("/api/v1/targets/{id}/findings/import?format=sarif&kind=pentest");
|
|
let (_, first) = h.import(&import_path, SARIF).await;
|
|
let (_, second) = h.import(&import_path, SARIF).await;
|
|
let (refused, _) = h.import(&import_path, "{\"not\": \"sarif\"}").await;
|
|
let (missing, _) = h
|
|
.post_json(
|
|
"/api/v1/targets/000000000000000000000000/findings",
|
|
serde_json::json!({ "kind": "dast", "title": "x", "severity": "low" }),
|
|
)
|
|
.await;
|
|
|
|
// Assert
|
|
assert_eq!(added, 201);
|
|
assert_eq!(body["data"]["scan_type"], "pentest");
|
|
assert_eq!(body["data"]["reported_by"], "dev-user");
|
|
assert!(body["data"]["control_refs"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.any(|c| c == "cra-ai-8"));
|
|
assert_eq!(again, 409, "the same finding is not added twice");
|
|
assert_eq!(first["data"]["imported"], 2);
|
|
assert_eq!(second["data"]["imported"], 0);
|
|
assert_eq!(second["data"]["duplicates"], 2);
|
|
assert_eq!(refused, 422);
|
|
assert_eq!(missing, 404);
|
|
|
|
let stored = h.findings(&id).await;
|
|
assert_eq!(stored.len(), 3, "{stored:#?}");
|
|
let nuclei = stored
|
|
.iter()
|
|
.find(|f| f["scanner"] == "Nuclei" && f["severity"] == "critical")
|
|
.expect("imported finding");
|
|
assert_eq!(nuclei["file_path"], "https://192.0.2.10/login");
|
|
|
|
// The targets list counts them, although no scan has run.
|
|
let list: serde_json::Value = h
|
|
.client
|
|
.get(format!("{}/api/v1/targets", h.base_url))
|
|
.send()
|
|
.await
|
|
.unwrap()
|
|
.json()
|
|
.await
|
|
.unwrap();
|
|
let listed = list["data"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.find(|t| t["_id"]["$oid"] == id.as_str())
|
|
.expect("target in the list");
|
|
assert_eq!(listed["findings_count"], 3);
|
|
|
|
h.pool.drop_tenant(TENANT).await.ok();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn status_changes_are_validated_and_recorded() {
|
|
let Some(h) = start().await else { return };
|
|
let (_, created) = h
|
|
.post_json(
|
|
"/api/v1/targets",
|
|
serde_json::json!({ "name": "gateway", "target_type": "firmware_rtos" }),
|
|
)
|
|
.await;
|
|
let target = created["data"]["_id"]["$oid"].as_str().unwrap().to_string();
|
|
let (_, added) = h
|
|
.post_json(
|
|
&format!("/api/v1/targets/{target}/findings"),
|
|
serde_json::json!({ "kind": "pentest", "title": "Debug UART open", "severity": "medium" }),
|
|
)
|
|
.await;
|
|
let id = added["data"]["_id"]["$oid"].as_str().unwrap().to_string();
|
|
let path = format!("{}/api/v1/findings/{id}/status", h.base_url);
|
|
let patch = |body: serde_json::Value| {
|
|
let client = h.client.clone();
|
|
let path = path.clone();
|
|
async move {
|
|
client
|
|
.patch(path)
|
|
.json(&body)
|
|
.send()
|
|
.await
|
|
.unwrap()
|
|
.status()
|
|
.as_u16()
|
|
}
|
|
};
|
|
|
|
// Act
|
|
let unknown = patch(serde_json::json!({ "status": "wontfix" })).await;
|
|
let no_reason = patch(serde_json::json!({ "status": "accepted_risk" })).await;
|
|
let triaged = patch(serde_json::json!({ "status": "triaged" })).await;
|
|
let accepted = patch(serde_json::json!({
|
|
"status": "accepted_risk",
|
|
"reason": "The UART is behind a sealed housing.",
|
|
}))
|
|
.await;
|
|
let missing = h
|
|
.client
|
|
.patch(format!(
|
|
"{}/api/v1/findings/000000000000000000000000/status",
|
|
h.base_url
|
|
))
|
|
.json(&serde_json::json!({ "status": "resolved" }))
|
|
.send()
|
|
.await
|
|
.unwrap()
|
|
.status()
|
|
.as_u16();
|
|
|
|
// Assert
|
|
assert_eq!(
|
|
(unknown, no_reason, triaged, accepted, missing),
|
|
(422, 422, 200, 200, 404)
|
|
);
|
|
let finding: serde_json::Value = h
|
|
.client
|
|
.get(format!("{}/api/v1/findings/{id}", h.base_url))
|
|
.send()
|
|
.await
|
|
.unwrap()
|
|
.json()
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(finding["data"]["status"], "accepted_risk");
|
|
let history = finding["data"]["status_history"].as_array().unwrap();
|
|
assert_eq!(history.len(), 2, "{history:#?}");
|
|
assert_eq!(history[1]["by"], "dev-user");
|
|
assert_eq!(history[1]["reason"], "The UART is behind a sealed housing.");
|
|
|
|
h.pool.drop_tenant(TENANT).await.ok();
|
|
}
|