refactor: modularize codebase and add 404 unit tests (#13)
All checks were successful
CI / Format (push) Successful in 4s
CI / Clippy (push) Successful in 4m19s
CI / Security Audit (push) Successful in 1m44s
CI / Detect Changes (push) Successful in 5s
CI / Tests (push) Successful in 5m15s
CI / Deploy Agent (push) Successful in 2s
CI / Deploy Dashboard (push) Successful in 2s
CI / Deploy Docs (push) Has been skipped
CI / Deploy MCP (push) Successful in 2s
All checks were successful
CI / Format (push) Successful in 4s
CI / Clippy (push) Successful in 4m19s
CI / Security Audit (push) Successful in 1m44s
CI / Detect Changes (push) Successful in 5s
CI / Tests (push) Successful in 5m15s
CI / Deploy Agent (push) Successful in 2s
CI / Deploy Dashboard (push) Successful in 2s
CI / Deploy Docs (push) Has been skipped
CI / Deploy MCP (push) Successful in 2s
This commit was merged in pull request #13.
This commit is contained in:
@@ -111,57 +111,107 @@ impl PentestTool for SecurityHeadersTool {
|
||||
&'a self,
|
||||
input: serde_json::Value,
|
||||
context: &'a PentestToolContext,
|
||||
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<PentestToolResult, CoreError>> + Send + 'a>> {
|
||||
) -> std::pin::Pin<
|
||||
Box<dyn std::future::Future<Output = Result<PentestToolResult, CoreError>> + Send + 'a>,
|
||||
> {
|
||||
Box::pin(async move {
|
||||
let url = input
|
||||
.get("url")
|
||||
.and_then(|v| v.as_str())
|
||||
.ok_or_else(|| CoreError::Dast("Missing required 'url' parameter".to_string()))?;
|
||||
let url = input
|
||||
.get("url")
|
||||
.and_then(|v| v.as_str())
|
||||
.ok_or_else(|| CoreError::Dast("Missing required 'url' parameter".to_string()))?;
|
||||
|
||||
let target_id = context
|
||||
.target
|
||||
.id
|
||||
.map(|oid| oid.to_hex())
|
||||
.unwrap_or_else(|| "unknown".to_string());
|
||||
let target_id = context
|
||||
.target
|
||||
.id
|
||||
.map(|oid| oid.to_hex())
|
||||
.unwrap_or_else(|| "unknown".to_string());
|
||||
|
||||
let response = self
|
||||
.http
|
||||
.get(url)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| CoreError::Dast(format!("Failed to fetch {url}: {e}")))?;
|
||||
let response = self
|
||||
.http
|
||||
.get(url)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| CoreError::Dast(format!("Failed to fetch {url}: {e}")))?;
|
||||
|
||||
let status = response.status().as_u16();
|
||||
let response_headers: HashMap<String, String> = response
|
||||
.headers()
|
||||
.iter()
|
||||
.map(|(k, v)| (k.to_string().to_lowercase(), v.to_str().unwrap_or("").to_string()))
|
||||
.collect();
|
||||
let status = response.status().as_u16();
|
||||
let response_headers: HashMap<String, String> = response
|
||||
.headers()
|
||||
.iter()
|
||||
.map(|(k, v)| {
|
||||
(
|
||||
k.to_string().to_lowercase(),
|
||||
v.to_str().unwrap_or("").to_string(),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut findings = Vec::new();
|
||||
let mut header_results: HashMap<String, serde_json::Value> = HashMap::new();
|
||||
let mut findings = Vec::new();
|
||||
let mut header_results: HashMap<String, serde_json::Value> = HashMap::new();
|
||||
|
||||
for expected in Self::expected_headers() {
|
||||
let header_value = response_headers.get(expected.name);
|
||||
for expected in Self::expected_headers() {
|
||||
let header_value = response_headers.get(expected.name);
|
||||
|
||||
match header_value {
|
||||
Some(value) => {
|
||||
let mut is_valid = true;
|
||||
if let Some(ref valid) = expected.valid_values {
|
||||
let lower = value.to_lowercase();
|
||||
is_valid = valid.iter().any(|v| lower.contains(v));
|
||||
match header_value {
|
||||
Some(value) => {
|
||||
let mut is_valid = true;
|
||||
if let Some(ref valid) = expected.valid_values {
|
||||
let lower = value.to_lowercase();
|
||||
is_valid = valid.iter().any(|v| lower.contains(v));
|
||||
}
|
||||
|
||||
header_results.insert(
|
||||
expected.name.to_string(),
|
||||
json!({
|
||||
"present": true,
|
||||
"value": value,
|
||||
"valid": is_valid,
|
||||
}),
|
||||
);
|
||||
|
||||
if !is_valid {
|
||||
let evidence = DastEvidence {
|
||||
request_method: "GET".to_string(),
|
||||
request_url: url.to_string(),
|
||||
request_headers: None,
|
||||
request_body: None,
|
||||
response_status: status,
|
||||
response_headers: Some(response_headers.clone()),
|
||||
response_snippet: Some(format!("{}: {}", expected.name, value)),
|
||||
screenshot_path: None,
|
||||
payload: None,
|
||||
response_time_ms: None,
|
||||
};
|
||||
|
||||
let mut finding = DastFinding::new(
|
||||
String::new(),
|
||||
target_id.clone(),
|
||||
DastVulnType::SecurityHeaderMissing,
|
||||
format!("Invalid {} header value", expected.name),
|
||||
format!(
|
||||
"The {} header is present but has an invalid or weak value: '{}'. \
|
||||
{}",
|
||||
expected.name, value, expected.description
|
||||
),
|
||||
expected.severity.clone(),
|
||||
url.to_string(),
|
||||
"GET".to_string(),
|
||||
);
|
||||
finding.cwe = Some(expected.cwe.to_string());
|
||||
finding.evidence = vec![evidence];
|
||||
finding.remediation = Some(expected.remediation.to_string());
|
||||
findings.push(finding);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
header_results.insert(
|
||||
expected.name.to_string(),
|
||||
json!({
|
||||
"present": false,
|
||||
"value": null,
|
||||
"valid": false,
|
||||
}),
|
||||
);
|
||||
|
||||
header_results.insert(
|
||||
expected.name.to_string(),
|
||||
json!({
|
||||
"present": true,
|
||||
"value": value,
|
||||
"valid": is_valid,
|
||||
}),
|
||||
);
|
||||
|
||||
if !is_valid {
|
||||
let evidence = DastEvidence {
|
||||
request_method: "GET".to_string(),
|
||||
request_url: url.to_string(),
|
||||
@@ -169,7 +219,7 @@ impl PentestTool for SecurityHeadersTool {
|
||||
request_body: None,
|
||||
response_status: status,
|
||||
response_headers: Some(response_headers.clone()),
|
||||
response_snippet: Some(format!("{}: {}", expected.name, value)),
|
||||
response_snippet: Some(format!("{} header is missing", expected.name)),
|
||||
screenshot_path: None,
|
||||
payload: None,
|
||||
response_time_ms: None,
|
||||
@@ -179,11 +229,10 @@ impl PentestTool for SecurityHeadersTool {
|
||||
String::new(),
|
||||
target_id.clone(),
|
||||
DastVulnType::SecurityHeaderMissing,
|
||||
format!("Invalid {} header value", expected.name),
|
||||
format!("Missing {} header", expected.name),
|
||||
format!(
|
||||
"The {} header is present but has an invalid or weak value: '{}'. \
|
||||
{}",
|
||||
expected.name, value, expected.description
|
||||
"The {} header is not present in the response. {}",
|
||||
expected.name, expected.description
|
||||
),
|
||||
expected.severity.clone(),
|
||||
url.to_string(),
|
||||
@@ -195,14 +244,20 @@ impl PentestTool for SecurityHeadersTool {
|
||||
findings.push(finding);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
}
|
||||
|
||||
// Also check for information disclosure headers
|
||||
let disclosure_headers = [
|
||||
"server",
|
||||
"x-powered-by",
|
||||
"x-aspnet-version",
|
||||
"x-aspnetmvc-version",
|
||||
];
|
||||
for h in &disclosure_headers {
|
||||
if let Some(value) = response_headers.get(*h) {
|
||||
header_results.insert(
|
||||
expected.name.to_string(),
|
||||
json!({
|
||||
"present": false,
|
||||
"value": null,
|
||||
"valid": false,
|
||||
}),
|
||||
format!("{h}_disclosure"),
|
||||
json!({ "present": true, "value": value }),
|
||||
);
|
||||
|
||||
let evidence = DastEvidence {
|
||||
@@ -212,56 +267,13 @@ impl PentestTool for SecurityHeadersTool {
|
||||
request_body: None,
|
||||
response_status: status,
|
||||
response_headers: Some(response_headers.clone()),
|
||||
response_snippet: Some(format!("{} header is missing", expected.name)),
|
||||
response_snippet: Some(format!("{h}: {value}")),
|
||||
screenshot_path: None,
|
||||
payload: None,
|
||||
response_time_ms: None,
|
||||
};
|
||||
|
||||
let mut finding = DastFinding::new(
|
||||
String::new(),
|
||||
target_id.clone(),
|
||||
DastVulnType::SecurityHeaderMissing,
|
||||
format!("Missing {} header", expected.name),
|
||||
format!(
|
||||
"The {} header is not present in the response. {}",
|
||||
expected.name, expected.description
|
||||
),
|
||||
expected.severity.clone(),
|
||||
url.to_string(),
|
||||
"GET".to_string(),
|
||||
);
|
||||
finding.cwe = Some(expected.cwe.to_string());
|
||||
finding.evidence = vec![evidence];
|
||||
finding.remediation = Some(expected.remediation.to_string());
|
||||
findings.push(finding);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Also check for information disclosure headers
|
||||
let disclosure_headers = ["server", "x-powered-by", "x-aspnet-version", "x-aspnetmvc-version"];
|
||||
for h in &disclosure_headers {
|
||||
if let Some(value) = response_headers.get(*h) {
|
||||
header_results.insert(
|
||||
format!("{h}_disclosure"),
|
||||
json!({ "present": true, "value": value }),
|
||||
);
|
||||
|
||||
let evidence = DastEvidence {
|
||||
request_method: "GET".to_string(),
|
||||
request_url: url.to_string(),
|
||||
request_headers: None,
|
||||
request_body: None,
|
||||
response_status: status,
|
||||
response_headers: Some(response_headers.clone()),
|
||||
response_snippet: Some(format!("{h}: {value}")),
|
||||
screenshot_path: None,
|
||||
payload: None,
|
||||
response_time_ms: None,
|
||||
};
|
||||
|
||||
let mut finding = DastFinding::new(
|
||||
String::new(),
|
||||
target_id.clone(),
|
||||
DastVulnType::SecurityHeaderMissing,
|
||||
@@ -274,27 +286,27 @@ impl PentestTool for SecurityHeadersTool {
|
||||
url.to_string(),
|
||||
"GET".to_string(),
|
||||
);
|
||||
finding.cwe = Some("CWE-200".to_string());
|
||||
finding.evidence = vec![evidence];
|
||||
finding.remediation = Some(format!(
|
||||
"Remove or suppress the {h} header in your server configuration."
|
||||
));
|
||||
findings.push(finding);
|
||||
finding.cwe = Some("CWE-200".to_string());
|
||||
finding.evidence = vec![evidence];
|
||||
finding.remediation = Some(format!(
|
||||
"Remove or suppress the {h} header in your server configuration."
|
||||
));
|
||||
findings.push(finding);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let count = findings.len();
|
||||
info!(url, findings = count, "Security headers check complete");
|
||||
let count = findings.len();
|
||||
info!(url, findings = count, "Security headers check complete");
|
||||
|
||||
Ok(PentestToolResult {
|
||||
summary: if count > 0 {
|
||||
format!("Found {count} security header issues for {url}.")
|
||||
} else {
|
||||
format!("All checked security headers are present and valid for {url}.")
|
||||
},
|
||||
findings,
|
||||
data: json!(header_results),
|
||||
})
|
||||
Ok(PentestToolResult {
|
||||
summary: if count > 0 {
|
||||
format!("Found {count} security header issues for {url}.")
|
||||
} else {
|
||||
format!("All checked security headers are present and valid for {url}.")
|
||||
},
|
||||
findings,
|
||||
data: json!(header_results),
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user