use reqwest::header::{ALLOW, CONTENT_TYPE};
use reqwest::Method;
use url::Url;
use crate::models::{
Category, CheckOutcome, Confidence, ExecutionStatus, Finding, Severity, Verdict,
};
use crate::transport::{AuthMode, HttpTransport, TransportError};
pub async fn check_http_methods(
transport: &HttpTransport,
url: &Url,
auth_context: &str,
) -> (Vec<Finding>, CheckOutcome) {
let mut findings = Vec::new();
let loc = transport.redactor.sanitize_url(url);
let mut any_error: Option<String> = None;
let mut cancelled = false;
match transport
.send(Method::OPTIONS, url, AuthMode::Anonymous, &[], Some(4096))
.await
{
Ok(resp) => {
if let Some(allow) = resp.headers.get(ALLOW).and_then(|v| v.to_str().ok()) {
let upper = allow.to_ascii_uppercase();
let risky: Vec<&str> = ["PUT", "DELETE", "TRACE", "CONNECT"]
.into_iter()
.filter(|m| upper.split(',').any(|tok| tok.trim() == *m))
.collect();
if !risky.is_empty() {
findings.push(Finding {
id: "HTTP-METHODS-ALLOW".into(),
category: Category::HttpProtocol,
severity: Severity::Low,
title: "OPTIONS Allow header advertises potentially unnecessary HTTP methods".into(),
summary: "Public web endpoints should generally restrict allowed methods to those explicitly required (GET, HEAD, POST).".into(),
evidence: Some(format!("Allow: {allow}")),
remediation: "Disable unused HTTP methods in web server or reverse proxy configuration.".into(),
standard_ref: Some("OWASP ASVS V14.5.1 / WSTG-CONF-06".into()),
confidence: Confidence::Medium,
});
}
}
}
Err(TransportError::Cancelled) => cancelled = true,
Err(e) => any_error = Some(e.to_string()),
}
if !cancelled {
if let Ok(trace_method) = Method::from_bytes(b"TRACE") {
match transport
.send(trace_method, url, AuthMode::Anonymous, &[], Some(4096))
.await
{
Ok(resp) => {
let ct = resp
.headers
.get(CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.unwrap_or("")
.to_ascii_lowercase();
if (200..300).contains(&resp.status) && ct.contains("message/http") {
findings.push(Finding {
id: "HTTP-TRACE-ENABLED".into(),
category: Category::HttpProtocol,
severity: Severity::Medium,
title: "HTTP TRACE method is enabled on the server".into(),
summary: "HTTP TRACE echoes request headers in the response body (Cross-Site Tracing / proxy header reflection risk).".into(),
evidence: Some(format!("TRACE {loc} -> {} ({ct})", resp.status)),
remediation: "Disable HTTP TRACE (e.g., `TraceEnable off` in Apache).".into(),
standard_ref: Some("OWASP WSTG-CONF-06 / ASVS V14.5.1".into()),
confidence: Confidence::High,
});
}
}
Err(TransportError::Cancelled) => cancelled = true,
Err(e) => {
if any_error.is_none() {
any_error = Some(e.to_string());
}
}
}
}
}
let outcome = if cancelled {
CheckOutcome {
check_id: "http-methods".into(),
category: Category::HttpProtocol,
location: loc,
auth_context: auth_context.to_string(),
status: ExecutionStatus::Cancelled,
verdict: Verdict::Inconclusive,
reason: Some("cancelled".into()),
}
} else if let Some(err) = any_error {
CheckOutcome {
check_id: "http-methods".into(),
category: Category::HttpProtocol,
location: loc,
auth_context: auth_context.to_string(),
status: ExecutionStatus::Error,
verdict: Verdict::Inconclusive,
reason: Some(err),
}
} else {
CheckOutcome {
check_id: "http-methods".into(),
category: Category::HttpProtocol,
location: loc,
auth_context: auth_context.to_string(),
status: ExecutionStatus::Completed,
verdict: if findings.is_empty() {
Verdict::Pass
} else {
Verdict::Fail
},
reason: None,
}
};
(findings, outcome)
}