diff --git a/compliance-dast/src/checks.rs b/compliance-dast/src/checks.rs new file mode 100644 index 0000000..8d1e505 --- /dev/null +++ b/compliance-dast/src/checks.rs @@ -0,0 +1,62 @@ +//! The deterministic checks every DAST scan runs (#998): security headers, +//! Content-Security-Policy, cookies and TLS. They read what the target sends +//! and judge it by fixed rules, so the same site gives the same findings in +//! every scan. Until now only the LLM pentest could call them. +//! +//! CORS is checked by the API fuzzer agent, which every scan runs already. + +use compliance_core::models::dast::{DastFinding, DastTarget}; +use compliance_core::traits::pentest_tool::{PentestTool, PentestToolContext}; +use compliance_core::NetGuard; +use serde_json::json; +use tracing::warn; + +use crate::tools::cookie_analyzer::CookieAnalyzerTool; +use crate::tools::csp_analyzer::CspAnalyzerTool; +use crate::tools::security_headers::SecurityHeadersTool; +use crate::tools::tls_analyzer::TlsAnalyzerTool; + +/// Run every check against `target`'s base URL. +/// +/// # Arguments +/// +/// * `target` - the DAST target; its findings name it +/// * `http` - the scan's client, with the target's login, so the checks see +/// the pages a logged-in visitor gets +/// * `guard` - where the TLS and cookie checks may connect +/// +/// # Returns +/// +/// The findings of all checks. A check that cannot run (the site does not +/// answer it) is logged and adds nothing; the others still run. +pub async fn run_checks( + target: &DastTarget, + http: &reqwest::Client, + guard: NetGuard, +) -> Vec { + let checks: [Box; 4] = [ + Box::new(SecurityHeadersTool::new(http.clone())), + Box::new(CspAnalyzerTool::new(http.clone())), + Box::new(CookieAnalyzerTool::new(http.clone(), guard)), + Box::new(TlsAnalyzerTool::new(http.clone(), guard)), + ]; + let context = PentestToolContext { + target: target.clone(), + session_id: String::new(), + sast_findings: Vec::new(), + sbom_entries: Vec::new(), + code_context: Vec::new(), + rate_limit: target.rate_limit, + allow_destructive: target.allow_destructive, + }; + let input = json!({ "url": target.base_url }); + + let mut findings = Vec::new(); + for check in &checks { + match check.execute(input.clone(), &context).await { + Ok(result) => findings.extend(result.findings), + Err(e) => warn!(check = check.name(), error = %e, "DAST check did not run"), + } + } + findings +} diff --git a/compliance-dast/src/lib.rs b/compliance-dast/src/lib.rs index ec7a6c0..d26439c 100644 --- a/compliance-dast/src/lib.rs +++ b/compliance-dast/src/lib.rs @@ -1,6 +1,7 @@ pub mod agents; pub mod auth; mod cdp; +pub mod checks; pub mod crawler; pub mod orchestrator; pub mod recon; diff --git a/compliance-dast/src/orchestrator/state_machine.rs b/compliance-dast/src/orchestrator/state_machine.rs index 44fab28..cd616ac 100644 --- a/compliance-dast/src/orchestrator/state_machine.rs +++ b/compliance-dast/src/orchestrator/state_machine.rs @@ -133,8 +133,9 @@ impl DastOrchestrator { sast_hints, }; - // Phase 3: Vulnerability Analysis + // Phase 3: Vulnerability Analysis: the fixed checks, then the agents scan_run.current_phase = DastScanPhase::VulnerabilityAnalysis; + all_findings.extend(crate::checks::run_checks(target, &http, self.guard).await); let vuln_findings = self .run_vulnerability_agents(target, &context, http) .await?; diff --git a/compliance-dast/tests/checks.rs b/compliance-dast/tests/checks.rs new file mode 100644 index 0000000..1b5e74d --- /dev/null +++ b/compliance-dast/tests/checks.rs @@ -0,0 +1,72 @@ +//! Every DAST scan runs the fixed checks (#998): a site without a +//! Content-Security-Policy and with a session cookie lacking its flags gets +//! those findings, and a site that sends a policy does not get the missing +//! one. + +#![allow(clippy::unwrap_used)] + +use axum::http::header::{CONTENT_SECURITY_POLICY, SET_COOKIE}; +use axum::response::IntoResponse; +use axum::Router; +use compliance_core::models::dast::{DastFinding, DastTarget, DastTargetType, DastVulnType}; +use compliance_core::NetGuard; +use compliance_dast::DastOrchestrator; + +async fn serve(app: Router) -> String { + 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}") +} + +async fn scan(base: &str) -> Vec { + let mut target = DastTarget::new("site".into(), base.into(), DastTargetType::WebApp); + target.max_crawl_depth = 1; + let (_run, findings) = DastOrchestrator::new(0, NetGuard::allow_private()) + .run_scan(&target, Vec::new()) + .await + .unwrap(); + findings +} + +fn missing_csp(findings: &[DastFinding]) -> bool { + findings + .iter() + .any(|f| f.vuln_type == DastVulnType::CspIssue && f.title.contains("Missing")) +} + +#[tokio::test] +async fn a_site_without_a_policy_gets_the_header_and_cookie_findings() { + let base = serve(Router::new().fallback(|| async { + ( + [(SET_COOKIE, "session=abc")], + "shop", + ) + .into_response() + })) + .await; + + let findings = scan(&base).await; + + assert!(missing_csp(&findings), "{findings:#?}"); + assert!( + findings + .iter() + .any(|f| f.vuln_type == DastVulnType::CookieSecurity), + "{findings:#?}" + ); +} + +#[tokio::test] +async fn a_site_with_a_policy_does_not_get_the_missing_one() { + let base = serve(Router::new().fallback(|| async { + ( + [(CONTENT_SECURITY_POLICY, "default-src 'self'")], + "shop", + ) + .into_response() + })) + .await; + + assert!(!missing_csp(&scan(&base).await)); +} diff --git a/docs/features/dast.md b/docs/features/dast.md index 3809c8a..7454c6d 100644 --- a/docs/features/dast.md +++ b/docs/features/dast.md @@ -64,8 +64,9 @@ Authenticated scans access more of the application surface. Only test applicatio Click the **Scan** button on any target row. The scan progresses through: 1. **Crawl** -- discovers pages, forms, and API endpoints by following links and analyzing JavaScript -2. **Test** -- sends attack payloads to discovered parameters -3. **Report** -- collects results and generates findings +2. **Check** -- fixed checks of what the base URL sends: security headers, Content-Security-Policy, cookie flags (`Secure`, `HttpOnly`, `SameSite`) and TLS. They follow fixed rules, so the same site gives the same findings in every scan. +3. **Test** -- sends attack payloads to discovered parameters +4. **Report** -- collects results and generates findings ### DAST in a target's scan