use reqwest::header::HeaderMap;
use reqwest::Method;
use url::Url;
use crate::models::{
Category, CheckOutcome, Confidence, ExecutionStatus, Finding, Severity, Verdict,
};
use crate::transport::{AuthMode, HttpTransport, TransportError};
fn header_str(headers: &HeaderMap, name: &str) -> Option<String> {
headers
.get(name)
.and_then(|v| v.to_str().ok())
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
}
pub async fn check_cors(
transport: &HttpTransport,
url: &Url,
base_headers: &HeaderMap,
auth_context: &str,
) -> (Vec<Finding>, CheckOutcome) {
let mut findings = Vec::new();
let loc = transport.redactor.sanitize_url(url);
let acao_base = header_str(base_headers, "access-control-allow-origin");
let acac_base = header_str(base_headers, "access-control-allow-credentials")
.map(|v| v.eq_ignore_ascii_case("true"))
.unwrap_or(false);
if let Some(acao) = &acao_base {
if acao == "*" && acac_base {
findings.push(Finding {
id: "CORS-WILDCARD-CREDENTIALS".into(),
category: Category::Cors,
severity: Severity::High,
title: "Dangerous CORS configuration: '*' with Access-Control-Allow-Credentials: true".into(),
summary: "Combining Access-Control-Allow-Origin: * with Allow-Credentials: true is invalid and signals an overly permissive CORS policy.".into(),
evidence: Some(format!("Access-Control-Allow-Origin: {acao}; Access-Control-Allow-Credentials: true")),
remediation: "Use an explicit allowlist of trusted origins instead of '*' when credentials are enabled.".into(),
standard_ref: Some("OWASP ASVS V14.5.3 / WSTG-CLNT-07".into()),
confidence: Confidence::High,
});
return (
findings,
CheckOutcome {
check_id: "cors-policy".into(),
category: Category::Cors,
location: loc,
auth_context: auth_context.to_string(),
status: ExecutionStatus::Completed,
verdict: Verdict::Fail,
reason: None,
},
);
} else if acao == "*" {
findings.push(Finding {
id: "CORS-WILDCARD".into(),
category: Category::Cors,
severity: Severity::Info,
title: "Open CORS: Access-Control-Allow-Origin: *".into(),
summary: "Any external origin can read responses from this endpoint (acceptable for public data, unsafe for private endpoints).".into(),
evidence: Some("Access-Control-Allow-Origin: *".into()),
remediation: "Verify this endpoint does not serve sensitive or user-specific data.".into(),
standard_ref: Some("OWASP ASVS V14.5.3 / WSTG-CLNT-07".into()),
confidence: Confidence::High,
});
return (
findings,
CheckOutcome {
check_id: "cors-policy".into(),
category: Category::Cors,
location: loc,
auth_context: auth_context.to_string(),
status: ExecutionStatus::Completed,
verdict: Verdict::Pass,
reason: None,
},
);
}
}
let probe_origin = "https://cors-audit.invalid";
match transport
.send(
Method::GET,
url,
AuthMode::Anonymous,
&[("Origin", probe_origin)],
Some(4096),
)
.await
{
Ok(r) => {
let acao = header_str(&r.headers, "access-control-allow-origin");
let acac = header_str(&r.headers, "access-control-allow-credentials")
.map(|v| v.eq_ignore_ascii_case("true"))
.unwrap_or(false);
let mut failed = false;
if let Some(origin_val) = acao {
if origin_val == probe_origin {
failed = true;
if acac {
findings.push(Finding {
id: "CORS-REFLECT-CREDS".into(),
category: Category::Cors,
severity: Severity::High,
title: "Server reflects arbitrary Origin with Allow-Credentials: true".into(),
summary: "Any third-party origin can make credentialed cross-origin requests and read the responses.".into(),
evidence: Some(format!(
"Origin: {probe_origin} -> Access-Control-Allow-Origin: {origin_val}, Allow-Credentials: true"
)),
remediation: "Validate the Origin header against a strict server-side allowlist of trusted origins.".into(),
standard_ref: Some("OWASP ASVS V14.5.3 / WSTG-CLNT-07".into()),
confidence: Confidence::High,
});
} else {
findings.push(Finding {
id: "CORS-REFLECT-NOCREDS".into(),
category: Category::Cors,
severity: Severity::Medium,
title: "Server reflects arbitrary Origin in CORS response".into(),
summary: format!("Server echoed back probe Origin: {probe_origin} in Access-Control-Allow-Origin."),
evidence: Some(format!("Access-Control-Allow-Origin: {origin_val}")),
remediation: "Restrict allowed origins via an allowlist instead of echoing the request Origin.".into(),
standard_ref: Some("OWASP ASVS V14.5.3 / WSTG-CLNT-07".into()),
confidence: Confidence::High,
});
}
}
}
(
findings,
CheckOutcome {
check_id: "cors-policy".into(),
category: Category::Cors,
location: loc,
auth_context: auth_context.to_string(),
status: ExecutionStatus::Completed,
verdict: if failed { Verdict::Fail } else { Verdict::Pass },
reason: None,
},
)
}
Err(TransportError::Cancelled) => (
findings,
CheckOutcome {
check_id: "cors-policy".into(),
category: Category::Cors,
location: loc,
auth_context: auth_context.to_string(),
status: ExecutionStatus::Cancelled,
verdict: Verdict::Inconclusive,
reason: Some("cancelled".into()),
},
),
Err(e) => (
findings,
CheckOutcome {
check_id: "cors-policy".into(),
category: Category::Cors,
location: loc,
auth_context: auth_context.to_string(),
status: ExecutionStatus::Error,
verdict: Verdict::Inconclusive,
reason: Some(e.to_string()),
},
),
}
}