//! 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)); }