Skip to main content

graphrt_http/
client.rs

1//! Policy-enforcing HTTP client service.
2
3use std::net::SocketAddr;
4use std::sync::{Arc, Once};
5use std::time::Duration;
6
7use reqwest::dns::{Addrs, Name, Resolve, Resolving};
8use reqwest::redirect;
9
10use crate::policy::{EgressError, EgressPolicy};
11
12/// Error building an [`HttpClient`].
13#[derive(Debug, thiserror::Error)]
14#[non_exhaustive]
15pub enum ClientError {
16    #[error("invalid proxy `{0}`: {1}")]
17    Proxy(String, String),
18    #[error("cannot build HTTP client: {0}")]
19    Build(String),
20}
21
22/// HTTP client shared by HTTP-calling nodes. Register it in
23/// [`graphrt_core::Services`]; when absent, nodes use a client with the default
24/// (restrictive) [`EgressPolicy`].
25///
26/// Every connection goes through the policy:
27/// - literal-IP URLs and every redirect hop are validated before connecting;
28/// - host names are resolved by a guarded resolver that refuses non-public
29///   addresses, and the connection is made to exactly the addresses it returned
30///   (so DNS rebinding cannot swap in a private address afterwards);
31/// - environment proxy variables are ignored.
32#[derive(Clone, Debug)]
33pub struct HttpClient {
34    policy: Arc<EgressPolicy>,
35    verified: reqwest::Client,
36    unverified: reqwest::Client,
37    /// Clients that never follow redirects; the `http-request` node follows them
38    /// itself so it can strip credentials on cross-origin hops.
39    manual_verified: reqwest::Client,
40    manual_unverified: reqwest::Client,
41}
42
43impl HttpClient {
44    /// Build a client enforcing `policy`.
45    pub fn new(policy: EgressPolicy) -> Result<Self, ClientError> {
46        install_crypto_provider();
47        let policy = Arc::new(policy);
48        let verified = build(&policy, true, true)?;
49        let unverified = build(&policy, false, true)?;
50        let manual_verified = build(&policy, true, false)?;
51        let manual_unverified = build(&policy, false, false)?;
52        Ok(Self { policy, verified, unverified, manual_verified, manual_unverified })
53    }
54
55    /// Client with [`EgressPolicy::trusted`], for operator-configured endpoints.
56    pub fn trusted() -> Result<Self, ClientError> {
57        Self::new(EgressPolicy::trusted())
58    }
59
60    pub fn policy(&self) -> &EgressPolicy {
61        &self.policy
62    }
63
64    /// Underlying client. `verify_tls = false` disables certificate verification.
65    ///
66    /// Redirects are followed automatically (each hop re-validated). reqwest strips only
67    /// `authorization`, `cookie` and `proxy-authorization` on cross-host hops, so callers
68    /// sending other credential headers should use [`HttpClient::manual_redirect_client`].
69    pub fn client(&self, verify_tls: bool) -> &reqwest::Client {
70        if verify_tls { &self.verified } else { &self.unverified }
71    }
72
73    /// Client that never follows redirects: 3xx responses are returned to the caller.
74    pub fn manual_redirect_client(&self, verify_tls: bool) -> &reqwest::Client {
75        if verify_tls { &self.manual_verified } else { &self.manual_unverified }
76    }
77
78    /// Validate a URL against the policy (scheme, host lists, literal addresses).
79    pub fn check_url(&self, url: &reqwest::Url) -> Result<(), EgressError> {
80        self.policy.check_url(url)
81    }
82}
83
84/// Use the small `ring` crypto backend. Installing fails harmlessly if the
85/// process already chose a provider.
86fn install_crypto_provider() {
87    static ONCE: Once = Once::new();
88    ONCE.call_once(|| {
89        let _already_installed = rustls::crypto::ring::default_provider().install_default();
90    });
91}
92
93fn build(policy: &Arc<EgressPolicy>, verify_tls: bool, follow_redirects: bool) -> Result<reqwest::Client, ClientError> {
94    let redirect_policy = if !follow_redirects {
95        redirect::Policy::none()
96    } else {
97        let policy = Arc::clone(policy);
98        redirect::Policy::custom(move |attempt| {
99            if attempt.previous().len() >= policy.max_redirects {
100                return attempt.error(format!("too many redirects (limit {})", policy.max_redirects));
101            }
102            match policy.check_url(attempt.url()) {
103                Ok(()) => attempt.follow(),
104                Err(e) => attempt.error(format!("redirect blocked: {e}")),
105            }
106        })
107    };
108    let mut builder = reqwest::Client::builder()
109        .no_proxy()
110        .redirect(redirect_policy)
111        .dns_resolver(Arc::new(GuardedResolver { policy: Arc::clone(policy) }))
112        .connect_timeout(policy.connect_timeout)
113        .read_timeout(policy.read_timeout)
114        .pool_idle_timeout(Duration::from_secs(90))
115        .tls_danger_accept_invalid_certs(!verify_tls);
116    if let Some(proxy_url) = &policy.proxy {
117        // The proxy resolves target hosts itself; it is expected to enforce its own egress rules.
118        let proxy = reqwest::Proxy::all(proxy_url).map_err(|e| ClientError::Proxy(proxy_url.clone(), e.to_string()))?;
119        builder = builder.proxy(proxy);
120    }
121    builder.build().map_err(|e| ClientError::Build(e.to_string()))
122}
123
124/// DNS resolver that refuses names resolving to non-public addresses.
125#[derive(Debug)]
126pub(crate) struct GuardedResolver {
127    policy: Arc<EgressPolicy>,
128}
129
130impl Resolve for GuardedResolver {
131    fn resolve(&self, name: Name) -> Resolving {
132        let policy = Arc::clone(&self.policy);
133        let host = name.as_str().to_owned();
134        Box::pin(async move {
135            policy.check_host_name(&host)?;
136            let resolved: Vec<SocketAddr> = tokio::net::lookup_host((host.as_str(), 0)).await?.collect();
137            let allowed = filter_addresses(&policy, &host, resolved)?;
138            Ok(Box::new(allowed.into_iter()) as Addrs)
139        })
140    }
141}
142
143/// Keep only permitted addresses. If any resolved address is blocked the whole
144/// name is refused: a name mixing public and private records is suspicious.
145pub(crate) fn filter_addresses(
146    policy: &EgressPolicy,
147    host: &str,
148    resolved: Vec<SocketAddr>,
149) -> Result<Vec<SocketAddr>, EgressError> {
150    if resolved.is_empty() {
151        return Err(EgressError::InvalidUrl(format!("`{host}` did not resolve to any address")));
152    }
153    for addr in &resolved {
154        policy.check_ip(addr.ip())?;
155    }
156    Ok(resolved)
157}
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162
163    fn addr(s: &str) -> SocketAddr {
164        SocketAddr::new(s.parse().unwrap(), 0)
165    }
166
167    #[test]
168    fn resolver_filter_blocks_private_records() {
169        let p = EgressPolicy::default();
170        assert!(filter_addresses(&p, "h", vec![addr("93.184.216.34")]).is_ok());
171        assert!(filter_addresses(&p, "h", vec![addr("127.0.0.1")]).is_err());
172        assert!(filter_addresses(&p, "h", vec![addr("93.184.216.34"), addr("10.0.0.1")]).is_err());
173        assert!(filter_addresses(&p, "h", vec![addr("::ffff:192.168.0.1")]).is_err());
174        assert!(filter_addresses(&p, "h", vec![]).is_err());
175        assert!(filter_addresses(&EgressPolicy::trusted(), "h", vec![addr("127.0.0.1")]).is_ok());
176    }
177
178    #[tokio::test]
179    async fn resolver_refuses_localhost_name() {
180        let r = GuardedResolver { policy: Arc::new(EgressPolicy::default()) };
181        let name: Name = "localhost".parse().unwrap();
182        assert!(r.resolve(name).await.is_err());
183    }
184
185    #[tokio::test]
186    async fn resolver_respects_denied_hosts() {
187        let policy = EgressPolicy { denied_hosts: vec!["localhost".into()], ..EgressPolicy::trusted() };
188        let r = GuardedResolver { policy: Arc::new(policy) };
189        let name: Name = "localhost".parse().unwrap();
190        assert!(r.resolve(name).await.is_err());
191    }
192
193    #[test]
194    fn builds() {
195        assert!(HttpClient::new(EgressPolicy::default()).is_ok());
196    }
197}