systemprompt_models/
net.rs1use std::time::Duration;
13use thiserror::Error;
14
15#[derive(Debug, Error)]
17pub enum OutboundUrlError {
18 #[error("invalid url: {0}")]
19 Parse(String),
20 #[error("unsupported url scheme: {0}")]
21 Scheme(String),
22 #[error("http url only permitted for loopback hosts")]
23 NonLoopbackHttp,
24 #[error("host {0} is in a blocked private range")]
25 BlockedHost(String),
26}
27
28pub const HTTP_CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
29
30pub const HTTP_DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
31
32pub const HTTP_HEALTH_CHECK_TIMEOUT: Duration = Duration::from_secs(5);
33
34pub const HTTP_AUTH_VERIFY_TIMEOUT: Duration = Duration::from_secs(10);
35
36pub const HTTP_SYNC_DEPLOY_TIMEOUT: Duration = Duration::from_secs(60);
37
38pub const HTTP_STREAM_CONNECT_TIMEOUT: Duration = Duration::from_secs(30);
39
40pub const HTTP_KEEPALIVE: Duration = Duration::from_secs(60);
41
42pub const HTTP_POOL_IDLE_TIMEOUT: Duration = Duration::from_secs(90);
43
44pub const AGENT_MONITOR_TCP_TIMEOUT: Duration = Duration::from_secs(15);
45
46pub const AGENT_READINESS_TCP_TIMEOUT: Duration = Duration::from_secs(2);
47
48pub const IMAGE_GEN_LONG_POLL_TIMEOUT: Duration = Duration::from_secs(300);
49
50pub const IMAGE_GEN_OPENAI_TIMEOUT: Duration = Duration::from_secs(120);
51
52pub const AI_PROVIDER_REQUEST_TIMEOUT: Duration = Duration::from_secs(60);
53
54pub const MCP_TOOL_EXECUTION_TIMEOUT: Duration = Duration::from_secs(30);
55
56pub const TRUSTED_HTTP_HOSTS_ENV: &str = "SYSTEMPROMPT_TRUSTED_HTTP_HOSTS";
57
58#[must_use]
59pub fn trusted_http_hosts_from_env() -> Vec<String> {
60 std::env::var(TRUSTED_HTTP_HOSTS_ENV)
61 .ok()
62 .map(|raw| {
63 raw.split(',')
64 .map(|s| s.trim().to_ascii_lowercase())
65 .filter(|s| !s.is_empty())
66 .collect()
67 })
68 .unwrap_or_default()
69}
70
71pub fn validate_outbound_url(url: &str) -> Result<url::Url, OutboundUrlError> {
72 let no_trust: [&str; 0] = [];
73 validate_outbound_url_with_trust(url, &no_trust)
74}
75
76pub fn validate_outbound_url_with_trust(
77 url: &str,
78 trusted_http_hosts: &[impl AsRef<str>],
79) -> Result<url::Url, OutboundUrlError> {
80 let parsed = url::Url::parse(url).map_err(|e| OutboundUrlError::Parse(e.to_string()))?;
81 let host = parsed
82 .host()
83 .ok_or_else(|| OutboundUrlError::Parse("missing host".to_owned()))?;
84
85 let is_loopback_host = match &host {
86 url::Host::Domain(d) => d.eq_ignore_ascii_case("localhost"),
87 url::Host::Ipv4(ip) => ip.is_loopback(),
88 url::Host::Ipv6(ip) => ip.is_loopback(),
89 };
90
91 let host_str = parsed.host_str().unwrap_or_default().to_ascii_lowercase();
92 let is_trusted = !host_str.is_empty()
93 && trusted_http_hosts
94 .iter()
95 .any(|h| h.as_ref().eq_ignore_ascii_case(&host_str));
96
97 match parsed.scheme() {
98 "https" => {},
99 "http" if is_loopback_host || is_trusted => {},
100 "http" => return Err(OutboundUrlError::NonLoopbackHttp),
101 scheme => return Err(OutboundUrlError::Scheme(scheme.to_owned())),
102 }
103
104 if is_loopback_host || is_trusted {
105 return Ok(parsed);
106 }
107
108 let blocked = match host {
109 url::Host::Domain(_) => false,
110 url::Host::Ipv4(ip) => is_blocked_v4(ip),
111 url::Host::Ipv6(ip) => {
112 ip.to_ipv4_mapped().map_or_else(
116 || {
117 let segments = ip.segments();
118 let is_unique_local = (segments[0] & 0xfe00) == 0xfc00;
119 let is_link_local = (segments[0] & 0xffc0) == 0xfe80;
120 ip.is_loopback() || ip.is_unspecified() || is_unique_local || is_link_local
121 },
122 is_blocked_v4,
123 )
124 },
125 };
126 if blocked {
127 return Err(OutboundUrlError::BlockedHost(
128 parsed.host_str().unwrap_or_default().to_owned(),
129 ));
130 }
131 Ok(parsed)
132}
133
134fn is_cgnat_shared_v4(ip: std::net::Ipv4Addr) -> bool {
137 let [a, b, _, _] = ip.octets();
138 a == 100 && (64..=127).contains(&b)
139}
140
141fn is_blocked_v4(ip: std::net::Ipv4Addr) -> bool {
142 ip.is_private()
143 || ip.is_loopback()
144 || ip.is_link_local()
145 || ip.is_unspecified()
146 || ip.is_broadcast()
147 || is_cgnat_shared_v4(ip)
148}