use crate::common::metrics_shim::counter;
use crate::domain::services::webhook_sender::WebhookSender;
use crate::infrastructure::security::ssrf::validate_url;
use crate::utils::http_client::create_http_client;
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use log::{error, warn};
use reqwest::Client;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
const WEBHOOK_TIMEOUT_SECS: u64 = 10;
#[derive(Clone)]
pub struct WebhookSenderImpl {
client: Arc<Client>,
timeout: Duration,
ssrf_guard: bool,
}
impl WebhookSenderImpl {
pub fn new(client: Arc<Client>, timeout: Duration) -> Self {
Self {
client,
timeout,
ssrf_guard: true,
}
}
#[cfg(test)]
pub fn new_without_ssrf_guard(client: Arc<Client>, timeout: Duration) -> Self {
Self {
client,
timeout,
ssrf_guard: false,
}
}
pub fn with_default_config() -> Self {
Self::new(
create_http_client(),
Duration::from_secs(WEBHOOK_TIMEOUT_SECS),
)
}
async fn build_request<'a>(
&self,
url: &str,
payload: &'a Value,
headers: Option<&'a HashMap<String, String>>,
) -> Result<reqwest::RequestBuilder> {
let payload_str = serde_json::to_string(payload)
.map_err(|e| anyhow!("Failed to serialize payload: {}", e))?;
let mut request_builder = self.client.post(url).body(payload_str);
request_builder = request_builder.header(
"Content-Type",
reqwest::header::HeaderValue::from_static("application/json"),
);
if let Some(custom_headers) = headers {
for (key, value) in custom_headers {
if let Ok(header_value) = reqwest::header::HeaderValue::from_str(value) {
request_builder = request_builder.header(key.as_str(), header_value);
} else {
warn!("Invalid header value for '{}': {}", key, value);
}
}
}
Ok(request_builder)
}
fn is_success_status(status: u16) -> bool {
(200..300).contains(&status)
}
async fn ssrf_check(&self, url: &str) -> Result<()> {
if !self.ssrf_guard {
return Ok(());
}
if let Err(e) = validate_url(url).await {
warn!(
"SSRF protection blocked webhook delivery to {}: {}",
crate::utils::url_redaction::redact_webhook_url_for_log(url),
e
);
counter!(
"crawlrs_webhook_ssrf_blocked_total",
"reason" => e.kind().to_string()
)
.increment(1);
return Err(anyhow!(
"SSRF protection: webhook URL rejected: {}",
e.kind()
));
}
Ok(())
}
}
#[async_trait]
impl WebhookSender for WebhookSenderImpl {
async fn send(
&self,
url: &str,
payload: &Value,
headers: Option<&HashMap<String, String>>,
) -> Result<()> {
self.send_with_status(url, payload, headers).await?;
Ok(())
}
async fn send_with_status(
&self,
url: &str,
payload: &Value,
headers: Option<&HashMap<String, String>>,
) -> Result<u16> {
self.ssrf_check(url).await?;
let request_builder = self.build_request(url, payload, headers).await?;
let request_builder = request_builder.timeout(self.timeout);
let response = match request_builder.send().await {
Ok(resp) => resp,
Err(e) => {
counter!(
"crawlrs_webhook_delivery_total",
"result" => "failure",
"status_code" => "0"
)
.increment(1);
return Err(anyhow!(
"Failed to send webhook request: {}",
e.without_url()
));
}
};
let status = response.status().as_u16();
if Self::is_success_status(status) {
counter!(
"crawlrs_webhook_delivery_total",
"result" => "success",
"status_code" => status.to_string()
)
.increment(1);
Ok(status)
} else {
let body = response
.text()
.await
.unwrap_or_else(|_| "Unable to read response body".to_string());
let truncated_body = if body.chars().count() > 200 {
format!(
"{}... (truncated)",
crate::common::text_slice::truncate_chars(&body, 200)
)
} else {
body
};
error!(
"Webhook delivery failed with status {}: {}",
status, truncated_body
);
counter!(
"crawlrs_webhook_delivery_total",
"result" => "failure",
"status_code" => status.to_string()
)
.increment(1);
Err(anyhow!(
"Webhook delivery failed with status {}: {}",
status,
truncated_body
))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use wiremock::matchers::{header, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
#[tokio::test]
async fn test_send_success() {
let mock_server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/webhook"))
.and(header("Content-Type", "application/json"))
.respond_with(ResponseTemplate::new(200))
.mount(&mock_server)
.await;
let client = Arc::new(
Client::builder()
.timeout(Duration::from_secs(5))
.build()
.unwrap(),
);
let sender = WebhookSenderImpl::new_without_ssrf_guard(client, Duration::from_secs(5));
let payload = json!({"test": "data"});
let headers = HashMap::new();
let webhook_url = format!("{}/webhook", mock_server.uri());
let result = sender.send(&webhook_url, &payload, Some(&headers)).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_send_failure() {
let mock_server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/webhook"))
.respond_with(ResponseTemplate::new(500))
.mount(&mock_server)
.await;
let client = Arc::new(
Client::builder()
.timeout(Duration::from_secs(5))
.build()
.unwrap(),
);
let sender = WebhookSenderImpl::new_without_ssrf_guard(client, Duration::from_secs(5));
let payload = json!({"test": "data"});
let webhook_url = format!("{}/webhook", mock_server.uri());
let result = sender.send(&webhook_url, &payload, None).await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_send_with_custom_headers() {
let mock_server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/webhook"))
.and(header("X-Custom-Header", "custom-value"))
.and(header("X-Crawlrs-Signature", "sig-123"))
.respond_with(ResponseTemplate::new(200))
.mount(&mock_server)
.await;
let client = Arc::new(
Client::builder()
.timeout(Duration::from_secs(5))
.build()
.unwrap(),
);
let sender = WebhookSenderImpl::new_without_ssrf_guard(client, Duration::from_secs(5));
let payload = json!({"test": "data"});
let mut headers = HashMap::new();
headers.insert("X-Custom-Header".to_string(), "custom-value".to_string());
headers.insert("X-Crawlrs-Signature".to_string(), "sig-123".to_string());
let webhook_url = format!("{}/webhook", mock_server.uri());
let result = sender.send(&webhook_url, &payload, Some(&headers)).await;
assert!(result.is_ok());
}
#[test]
fn test_is_success_status_2xx_range() {
assert!(WebhookSenderImpl::is_success_status(200));
assert!(WebhookSenderImpl::is_success_status(201));
assert!(WebhookSenderImpl::is_success_status(204));
assert!(WebhookSenderImpl::is_success_status(299));
}
#[test]
fn test_is_success_status_non_2xx() {
assert!(!WebhookSenderImpl::is_success_status(199));
assert!(!WebhookSenderImpl::is_success_status(300));
assert!(!WebhookSenderImpl::is_success_status(301));
assert!(!WebhookSenderImpl::is_success_status(404));
assert!(!WebhookSenderImpl::is_success_status(500));
assert!(!WebhookSenderImpl::is_success_status(503));
}
#[test]
fn test_with_default_config_creates_sender() {
let sender = WebhookSenderImpl::with_default_config();
assert_eq!(sender.timeout, Duration::from_secs(WEBHOOK_TIMEOUT_SECS));
}
#[test]
fn test_new_enables_ssrf_guard_by_default() {
let client = Arc::new(Client::new());
assert!(WebhookSenderImpl::new(client, Duration::from_secs(1)).ssrf_guard);
let client = Arc::new(Client::new());
assert!(
!WebhookSenderImpl::new_without_ssrf_guard(client, Duration::from_secs(1)).ssrf_guard
);
}
#[tokio::test]
async fn test_ssrf_guard_blocks_loopback_wiremock_url() {
let mock_server = MockServer::start().await;
Mock::given(method("POST"))
.respond_with(ResponseTemplate::new(200))
.mount(&mock_server)
.await;
let client = Arc::new(Client::builder().build().unwrap());
let sender = WebhookSenderImpl::new(client, Duration::from_secs(5));
let webhook_url = format!("{}/webhook", mock_server.uri());
let result = sender
.send(&webhook_url, &serde_json::json!({"k": "v"}), None)
.await;
assert!(result.is_err(), "loopback webhook URL must be rejected");
let err_msg = result.unwrap_err().to_string();
assert!(
err_msg.contains("SSRF protection"),
"error should be attributed to SSRF guard, got: {}",
err_msg
);
}
#[tokio::test]
async fn test_ssrf_guard_blocks_private_and_metadata_targets() {
let client = Arc::new(Client::builder().build().unwrap());
let sender = WebhookSenderImpl::new(client, Duration::from_secs(5));
for url in [
"http://10.0.0.1/hook",
"http://192.168.1.1/hook",
"http://172.16.0.1/hook",
"http://169.254.169.254/latest/meta-data/",
"http://localhost:9000/hook",
"file:///etc/passwd",
] {
let result = sender.send(url, &serde_json::json!({}), None).await;
assert!(result.is_err(), "webhook to {} must be rejected", url);
assert!(
result.unwrap_err().to_string().contains("SSRF protection"),
"rejection for {} must come from SSRF guard",
url
);
}
}
#[tokio::test]
async fn test_ssrf_guard_error_message_is_sanitized() {
let client = Arc::new(Client::builder().build().unwrap());
let sender = WebhookSenderImpl::new(client, Duration::from_secs(5));
let result = sender
.send("http://10.0.0.1/hook", &serde_json::json!({}), None)
.await;
let err_msg = result.unwrap_err().to_string();
assert!(!err_msg.contains("10.0.0.1"), "got: {}", err_msg);
assert!(!err_msg.contains("/hook"), "got: {}", err_msg);
}
#[tokio::test]
async fn test_send_with_invalid_header_value_still_succeeds() {
let mock_server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/webhook"))
.and(header("Content-Type", "application/json"))
.respond_with(ResponseTemplate::new(200))
.mount(&mock_server)
.await;
let client = Arc::new(
Client::builder()
.timeout(Duration::from_secs(5))
.build()
.unwrap(),
);
let sender = WebhookSenderImpl::new_without_ssrf_guard(client, Duration::from_secs(5));
let payload = json!({"test": "data"});
let mut headers = HashMap::new();
headers.insert("X-Invalid".to_string(), "bad\nvalue".to_string());
let webhook_url = format!("{}/webhook", mock_server.uri());
let result = sender.send(&webhook_url, &payload, Some(&headers)).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_send_failure_truncates_long_response_body() {
let mock_server = MockServer::start().await;
let long_body = "x".repeat(300);
Mock::given(method("POST"))
.and(path("/webhook"))
.respond_with(ResponseTemplate::new(500).set_body_string(long_body.clone()))
.mount(&mock_server)
.await;
let client = Arc::new(
Client::builder()
.timeout(Duration::from_secs(5))
.build()
.unwrap(),
);
let sender = WebhookSenderImpl::new_without_ssrf_guard(client, Duration::from_secs(5));
let payload = json!({"test": "data"});
let webhook_url = format!("{}/webhook", mock_server.uri());
let result = sender.send(&webhook_url, &payload, None).await;
assert!(result.is_err());
let error_msg = result.unwrap_err().to_string();
assert!(
error_msg.contains("... (truncated)"),
"Error should contain truncated marker"
);
assert!(error_msg.contains("500"));
}
}