Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix: unify HTTP proxy route selection

ldm00abe983cc1f3
diff --git a/moli-fetch/src/blocking/mod.rs b/moli-fetch/src/blocking/mod.rs
index 520f2ffbc..4609016a5 100644
--- a/moli-fetch/src/blocking/mod.rs
+++ b/moli-fetch/src/blocking/mod.rs
@@ -39,6 +39,7 @@ pub(crate) use self::collectors::{
use crate::{
BrowserRequestMetadata, FetchConfig, NegotiatedHttpVersion, NetworkRequestExtraInfo,
RedirectInfo, Request, RequestAuthScheme, RequestAuthTarget, ResponseHead,
+ proxy::HttpProxyRoute,
};
const MAX_REDIRECTS: usize = 10;
@@ -534,6 +535,7 @@ pub(crate) fn store_response_cookies(
pub(crate) fn configure_easy<H: Handler>(
easy: &mut Easy2<H>,
config: &FetchConfig,
+ proxy_route: &HttpProxyRoute,
request: &Request,
request_url: &Url,
cookie_header: Option<&str>,
@@ -635,14 +637,18 @@ pub(crate) fn configure_easy<H: Handler>(
}
}
- if let Some(proxy) = config.http_proxy() {
- easy.proxy(proxy)
- .with_context(|| anyhow!("failed to configure HTTP proxy `{proxy}`"))?;
- }
- if let Some(no_proxy) = config.http_no_proxy() {
- easy.noproxy(no_proxy)
- .with_context(|| anyhow!("failed to configure HTTP no_proxy `{no_proxy}`"))?;
+ match proxy_route {
+ HttpProxyRoute::Direct => easy
+ .proxy("")
+ .context("failed to disable the HTTP proxy for a direct request")?,
+ HttpProxyRoute::Proxy(proxy) => easy
+ .proxy(proxy)
+ .with_context(|| anyhow!("failed to configure HTTP proxy `{proxy}`"))?,
}
+ // Proxy bypass has already been resolved into `proxy_route`. Keep curl
+ // from re-evaluating environment `no_proxy` after that security decision.
+ easy.noproxy("")
+ .context("failed to disable curl HTTP no_proxy evaluation")?;
if !config.http_host_resolve().is_empty() {
let mut resolve = List::new();
let force_permanent = config.network_address_policy().is_enforced();
diff --git a/moli-fetch/src/dns.rs b/moli-fetch/src/dns.rs
index 3a18842be..5ad2e597f 100644
--- a/moli-fetch/src/dns.rs
+++ b/moli-fetch/src/dns.rs
@@ -6,6 +6,7 @@ use url::{Host, Url};
use crate::{
FetchConfig,
blocking::{normalized_http_host_resolve_entries, resolve_host_resolve_override_ips},
+ proxy::HttpProxyRoute,
};
/// Fetch-side DNS admission decision.
@@ -22,8 +23,12 @@ enum FetchCurlDnsAdmission {
SharedResolver(DnsTarget),
}
-pub(crate) fn curl_dns_resolution(config: &FetchConfig, url: &Url) -> Result<CurlDnsResolution> {
- match curl_dns_admission_with_env(config, url, |name| std::env::var(name).ok())? {
+pub(crate) fn curl_dns_resolution(
+ config: &FetchConfig,
+ url: &Url,
+ proxy_route: &HttpProxyRoute,
+) -> Result<CurlDnsResolution> {
+ match curl_dns_admission(config, url, proxy_route)? {
FetchCurlDnsAdmission::CurlManaged => Ok(CurlDnsResolution::curl_managed()),
FetchCurlDnsAdmission::SharedResolver(target) => {
let policy = config.network_address_policy();
@@ -39,10 +44,10 @@ pub(crate) fn curl_dns_resolution(config: &FetchConfig, url: &Url) -> Result<Cur
}
}
-fn curl_dns_admission_with_env(
+fn curl_dns_admission(
config: &FetchConfig,
url: &Url,
- mut env: impl FnMut(&str) -> Option<String>,
+ proxy_route: &HttpProxyRoute,
) -> Result<FetchCurlDnsAdmission> {
if !matches!(url.scheme(), "http" | "https") {
return Ok(FetchCurlDnsAdmission::CurlManaged);
@@ -53,7 +58,7 @@ fn curl_dns_admission_with_env(
let Some(port) = url.port_or_known_default() else {
return Ok(FetchCurlDnsAdmission::CurlManaged);
};
- if request_uses_proxy(config, url, host, port, &mut env) {
+ if proxy_route.is_proxy() {
if config.network_address_policy().is_enforced() {
bail!(
"cannot enforce network address policy for proxied hostname `{host}` in `{url}`; the proxy must not resolve an unchecked target hostname"
@@ -69,126 +74,27 @@ fn curl_dns_admission_with_env(
)))
}
-fn request_uses_proxy(
- config: &FetchConfig,
- url: &Url,
- host: &str,
- port: u16,
- env: &mut impl FnMut(&str) -> Option<String>,
-) -> bool {
- let proxy = match config.http_proxy() {
- Some("") => return false,
- Some(proxy) => Some(proxy.to_owned()),
- None => env_proxy_for_scheme(url.scheme(), env),
- };
- let Some(_) = proxy.filter(|proxy| !proxy.is_empty()) else {
- return false;
- };
- let no_proxy = match config.http_no_proxy() {
- Some(no_proxy) => Some(no_proxy.to_owned()),
- None => env_no_proxy(env),
- };
- !no_proxy_matches(host, port, no_proxy.as_deref())
-}
-
-fn env_proxy_for_scheme(
- scheme: &str,
- env: &mut impl FnMut(&str) -> Option<String>,
-) -> Option<String> {
- let names: &[&str] = match scheme {
- // curl deliberately ignores uppercase HTTP_PROXY because CGI servers
- // commonly expose an attacker-controlled Proxy header under that name.
- "http" => &["http_proxy"],
- "https" => &["https_proxy", "HTTPS_PROXY"],
- _ => &[],
- };
- for name in names {
- if let Some(value) = env(name).filter(|value| !value.is_empty()) {
- return Some(value);
- }
- }
- for name in ["all_proxy", "ALL_PROXY"] {
- if let Some(value) = env(name).filter(|value| !value.is_empty()) {
- return Some(value);
- }
- }
- None
-}
-
-fn env_no_proxy(env: &mut impl FnMut(&str) -> Option<String>) -> Option<String> {
- env("no_proxy")
- .filter(|value| !value.is_empty())
- .or_else(|| env("NO_PROXY").filter(|value| !value.is_empty()))
-}
-
-fn no_proxy_matches(host: &str, port: u16, no_proxy: Option<&str>) -> bool {
- let Some(no_proxy) = no_proxy else {
- return false;
- };
- let host = host.trim_matches(['[', ']']).to_ascii_lowercase();
- no_proxy.split(',').any(|token| {
- let token = token.trim();
- if token.is_empty() {
- return false;
- }
- if token == "*" {
- return true;
- }
- let (token_host, token_port) = split_no_proxy_host_port(token);
- if let Some(token_port) = token_port
- && token_port != port
- {
- return false;
- }
- let token_host = token_host
- .trim_matches(['[', ']'])
- .trim_start_matches('.')
- .to_ascii_lowercase();
- !token_host.is_empty()
- && (host == token_host
- || host
- .strip_suffix(&token_host)
- .is_some_and(|prefix| prefix.ends_with('.')))
- })
-}
-
-fn split_no_proxy_host_port(token: &str) -> (&str, Option<u16>) {
- let Some((host, port)) = token.rsplit_once(':') else {
- return (token, None);
- };
- match port.parse::<u16>() {
- Ok(port) if !host.contains(':') => (host, Some(port)),
- _ => (token, None),
- }
-}
-
#[cfg(test)]
mod tests {
- use std::collections::HashMap;
-
use super::*;
fn admission(
config: &FetchConfig,
raw_url: &str,
- env: &[(&str, &str)],
+ proxy_route: &HttpProxyRoute,
) -> FetchCurlDnsAdmission {
- admission_result(config, raw_url, env).expect("test DNS admission should succeed")
+ admission_result(config, raw_url, proxy_route).expect("test DNS admission should succeed")
}
fn admission_result(
config: &FetchConfig,
raw_url: &str,
- env: &[(&str, &str)],
+ proxy_route: &HttpProxyRoute,
) -> Result<FetchCurlDnsAdmission> {
- let env = env
- .iter()
- .map(|(name, value)| ((*name).to_owned(), (*value).to_owned()))
- .collect::<HashMap<_, _>>();
- curl_dns_admission_with_env(
+ curl_dns_admission(
config,
&Url::parse(raw_url).expect("test URL should parse"),
- |name| env.get(name).cloned(),
+ proxy_route,
)
}
@@ -201,11 +107,15 @@ mod tests {
let config = FetchConfig::default();
assert_eq!(
- admission(&config, "http://example.test/path", &[]),
+ admission(&config, "http://example.test/path", &HttpProxyRoute::Direct),
shared_target("example.test", 80)
);
assert_eq!(
- admission(&config, "https://example.test:8443/path", &[]),
+ admission(
+ &config,
+ "https://example.test:8443/path",
+ &HttpProxyRoute::Direct,
+ ),
shared_target("example.test", 8443)
);
}
@@ -215,118 +125,51 @@ mod tests {
let mut config = FetchConfig::default();
assert_eq!(
- admission(&config, "http://127.0.0.1/path", &[]),
+ admission(&config, "http://127.0.0.1/path", &HttpProxyRoute::Direct,),
FetchCurlDnsAdmission::CurlManaged
);
assert_eq!(
- admission(&config, "http://[::1]/path", &[]),
+ admission(&config, "http://[::1]/path", &HttpProxyRoute::Direct,),
FetchCurlDnsAdmission::CurlManaged
);
config.set_http_host_resolve(vec!["example.test:80:127.0.0.1".to_owned()]);
assert_eq!(
- admission(&config, "http://example.test/path", &[]),
+ admission(&config, "http://example.test/path", &HttpProxyRoute::Direct,),
FetchCurlDnsAdmission::CurlManaged
);
assert_eq!(
- admission(&config, "http://other.test/path", &[]),
+ admission(&config, "http://other.test/path", &HttpProxyRoute::Direct,),
shared_target("other.test", 80),
"an unrelated host-resolve entry must not return DNS ownership to curl"
);
}
#[test]
- fn explicit_proxy_uses_curl_resolution_unless_no_proxy_matches() {
- let mut config = FetchConfig::default();
- config.set_http_proxy(Some("http://proxy.test:8080".to_owned()));
+ fn selected_proxy_route_uses_curl_resolution() {
+ let config = FetchConfig::default();
assert_eq!(
- admission(&config, "https://api.example.test/path", &[]),
+ admission(
+ &config,
+ "https://api.example.test/path",
+ &HttpProxyRoute::Proxy("http://proxy.test:8080".to_owned()),
+ ),
FetchCurlDnsAdmission::CurlManaged
);
- config.set_http_no_proxy(Some(".example.test".to_owned()));
- assert_eq!(
- admission(&config, "https://api.example.test/path", &[]),
- shared_target("api.example.test", 443)
- );
}
#[test]
fn enforced_policy_rejects_proxy_resolved_hostname() {
let mut config = FetchConfig::default();
- config.set_http_proxy(Some("http://proxy.test:8080".to_owned()));
config.set_network_blocking(true, Vec::new());
- let error = admission_result(&config, "https://api.example.test/path", &[])
- .expect_err("a strict policy cannot verify proxy-side DNS");
+ let error = admission_result(
+ &config,
+ "https://api.example.test/path",
+ &HttpProxyRoute::Proxy("http://proxy.test:8080".to_owned()),
+ )
+ .expect_err("a strict policy cannot verify proxy-side DNS");
assert!(error.to_string().contains("proxied hostname"));
}
-
- #[test]
- fn empty_explicit_proxy_disables_environment_proxy_fallback() {
- let mut config = FetchConfig::default();
- config.set_http_proxy(Some(String::new()));
-
- assert_eq!(
- admission(
- &config,
- "http://example.test/path",
- &[("http_proxy", "http://proxy.test:8080")],
- ),
- shared_target("example.test", 80)
- );
- }
-
- #[test]
- fn environment_proxy_and_no_proxy_follow_curl_precedence() {
- let config = FetchConfig::default();
-
- assert_eq!(
- admission(
- &config,
- "http://example.test/path",
- &[("http_proxy", "http://proxy.test:8080")],
- ),
- FetchCurlDnsAdmission::CurlManaged
- );
- assert_eq!(
- admission(
- &config,
- "https://example.test/path",
- &[("HTTPS_PROXY", "http://proxy.test:8080")],
- ),
- FetchCurlDnsAdmission::CurlManaged
- );
- assert_eq!(
- admission(
- &config,
- "https://api.example.test/path",
- &[
- ("all_proxy", "http://proxy.test:8080"),
- ("NO_PROXY", "example.test"),
- ],
- ),
- shared_target("api.example.test", 443)
- );
- }
-
- #[test]
- fn no_proxy_port_and_domain_boundaries_are_exact() {
- assert!(no_proxy_matches(
- "api.example.test",
- 8443,
- Some("example.test:8443")
- ));
- assert!(!no_proxy_matches(
- "api.example.test",
- 443,
- Some("example.test:8443")
- ));
- assert!(!no_proxy_matches(
- "notexample.test",
- 443,
- Some("example.test")
- ));
- assert!(no_proxy_matches("anything.test", 443, Some("*")));
- }
}
diff --git a/moli-fetch/src/lib.rs b/moli-fetch/src/lib.rs
index 1195f3892..af3e216da 100644
--- a/moli-fetch/src/lib.rs
+++ b/moli-fetch/src/lib.rs
@@ -15,6 +15,7 @@ mod error;
mod fetch_url_list;
mod headers;
mod network_fetch_result;
+mod proxy;
mod proxy_connect;
mod referrer_policy;
mod request;
diff --git a/moli-fetch/src/proxy.rs b/moli-fetch/src/proxy.rs
new file mode 100644
index 000000000..06bbccba2
--- /dev/null
+++ b/moli-fetch/src/proxy.rs
@@ -0,0 +1,254 @@
+use std::net::IpAddr;
+
+use cidr::AnyIpCidr;
+use url::Url;
+
+use crate::FetchConfig;
+
+/// Concrete proxy route selected once for one HTTP request target.
+///
+/// The same value must drive both DNS admission and curl configuration so
+/// environment variables and `no_proxy` cannot be interpreted twice.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub(crate) enum HttpProxyRoute {
+ Direct,
+ Proxy(String),
+}
+
+impl HttpProxyRoute {
+ pub(crate) fn is_proxy(&self) -> bool {
+ matches!(self, Self::Proxy(_))
+ }
+}
+
+pub(crate) fn resolve_http_proxy_route(config: &FetchConfig, url: &Url) -> HttpProxyRoute {
+ resolve_http_proxy_route_with_env(config, url, |name| std::env::var(name).ok())
+}
+
+fn resolve_http_proxy_route_with_env(
+ config: &FetchConfig,
+ url: &Url,
+ mut env: impl FnMut(&str) -> Option<String>,
+) -> HttpProxyRoute {
+ let proxy = match config.http_proxy() {
+ Some("") => return HttpProxyRoute::Direct,
+ Some(proxy) => Some(proxy.to_owned()),
+ None => env_proxy_for_scheme(url.scheme(), &mut env),
+ };
+ let Some(proxy) = proxy.filter(|proxy| !proxy.is_empty()) else {
+ return HttpProxyRoute::Direct;
+ };
+
+ let no_proxy = match config.http_no_proxy() {
+ Some(no_proxy) => Some(no_proxy.to_owned()),
+ None => env_no_proxy(&mut env),
+ };
+ if let (Some(host), Some(port)) = (url.host_str(), url.port_or_known_default())
+ && no_proxy_matches(host, port, no_proxy.as_deref())
+ {
+ return HttpProxyRoute::Direct;
+ }
+
+ HttpProxyRoute::Proxy(proxy)
+}
+
+fn env_proxy_for_scheme(
+ scheme: &str,
+ env: &mut impl FnMut(&str) -> Option<String>,
+) -> Option<String> {
+ let names: &[&str] = match scheme {
+ // curl deliberately ignores uppercase HTTP_PROXY because CGI servers
+ // commonly expose an attacker-controlled Proxy header under that name.
+ "http" => &["http_proxy"],
+ "https" => &["https_proxy", "HTTPS_PROXY"],
+ _ => &[],
+ };
+ for name in names {
+ if let Some(value) = env(name).filter(|value| !value.is_empty()) {
+ return Some(value);
+ }
+ }
+ for name in ["all_proxy", "ALL_PROXY"] {
+ if let Some(value) = env(name).filter(|value| !value.is_empty()) {
+ return Some(value);
+ }
+ }
+ None
+}
+
+fn env_no_proxy(env: &mut impl FnMut(&str) -> Option<String>) -> Option<String> {
+ env("no_proxy")
+ .filter(|value| !value.is_empty())
+ .or_else(|| env("NO_PROXY").filter(|value| !value.is_empty()))
+}
+
+fn no_proxy_matches(host: &str, port: u16, no_proxy: Option<&str>) -> bool {
+ let Some(no_proxy) = no_proxy else {
+ return false;
+ };
+ let host = host
+ .trim_matches(['[', ']'])
+ .trim_end_matches('.')
+ .to_ascii_lowercase();
+ let host_ip = host.parse::<IpAddr>().ok();
+ no_proxy.split(',').any(|token| {
+ let token = token.trim();
+ if token.is_empty() {
+ return false;
+ }
+ if token == "*" {
+ return true;
+ }
+ let (token_host, token_port) = split_no_proxy_host_port(token);
+ if let Some(token_port) = token_port
+ && token_port != port
+ {
+ return false;
+ }
+ let token_host = token_host
+ .trim_matches(['[', ']'])
+ .trim_start_matches('.')
+ .trim_end_matches('.')
+ .to_ascii_lowercase();
+ if token_host.is_empty() {
+ return false;
+ }
+ if let Some(host_ip) = host_ip {
+ return token_host.parse::<IpAddr>().is_ok_and(|ip| ip == host_ip)
+ || token_host
+ .parse::<AnyIpCidr>()
+ .is_ok_and(|cidr| cidr.contains(&host_ip));
+ }
+ host == token_host
+ || host
+ .strip_suffix(&token_host)
+ .is_some_and(|prefix| prefix.ends_with('.'))
+ })
+}
+
+fn split_no_proxy_host_port(token: &str) -> (&str, Option<u16>) {
+ if let Some(bracketed) = token.strip_prefix('[')
+ && let Some((host, port)) = bracketed.rsplit_once("]:")
+ && let Ok(port) = port.parse::<u16>()
+ {
+ return (host, Some(port));
+ }
+ let Some((host, port)) = token.rsplit_once(':') else {
+ return (token, None);
+ };
+ match port.parse::<u16>() {
+ Ok(port) if !host.contains(':') => (host, Some(port)),
+ _ => (token, None),
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use std::collections::HashMap;
+
+ use super::*;
+
+ fn route(config: &FetchConfig, raw_url: &str, env: &[(&str, &str)]) -> HttpProxyRoute {
+ let env = env
+ .iter()
+ .map(|(name, value)| ((*name).to_owned(), (*value).to_owned()))
+ .collect::<HashMap<_, _>>();
+ resolve_http_proxy_route_with_env(
+ config,
+ &Url::parse(raw_url).expect("test URL should parse"),
+ |name| env.get(name).cloned(),
+ )
+ }
+
+ #[test]
+ fn explicit_proxy_and_no_proxy_select_one_route() {
+ let mut config = FetchConfig::default();
+ config.set_http_proxy(Some("http://proxy.test:8080".to_owned()));
+
+ assert_eq!(
+ route(&config, "https://api.example.test/path", &[]),
+ HttpProxyRoute::Proxy("http://proxy.test:8080".to_owned())
+ );
+ config.set_http_no_proxy(Some(".example.test".to_owned()));
+ assert_eq!(
+ route(&config, "https://api.example.test/path", &[]),
+ HttpProxyRoute::Direct
+ );
+ }
+
+ #[test]
+ fn empty_explicit_proxy_disables_environment_proxy_fallback() {
+ let mut config = FetchConfig::default();
+ config.set_http_proxy(Some(String::new()));
+
+ assert_eq!(
+ route(
+ &config,
+ "http://example.test/path",
+ &[("http_proxy", "http://proxy.test:8080")],
+ ),
+ HttpProxyRoute::Direct
+ );
+ }
+
+ #[test]
+ fn environment_proxy_and_no_proxy_follow_expected_precedence() {
+ let config = FetchConfig::default();
+
+ assert_eq!(
+ route(
+ &config,
+ "http://example.test/path",
+ &[("http_proxy", "http://proxy.test:8080")],
+ ),
+ HttpProxyRoute::Proxy("http://proxy.test:8080".to_owned())
+ );
+ assert_eq!(
+ route(
+ &config,
+ "https://example.test/path",
+ &[("HTTPS_PROXY", "http://proxy.test:8080")],
+ ),
+ HttpProxyRoute::Proxy("http://proxy.test:8080".to_owned())
+ );
+ assert_eq!(
+ route(
+ &config,
+ "https://api.example.test/path",
+ &[
+ ("all_proxy", "http://proxy.test:8080"),
+ ("NO_PROXY", "example.test"),
+ ],
+ ),
+ HttpProxyRoute::Direct
+ );
+ }
+
+ #[test]
+ fn no_proxy_port_and_domain_boundaries_are_exact() {
+ assert!(no_proxy_matches(
+ "api.example.test",
+ 8443,
+ Some("example.test:8443")
+ ));
+ assert!(!no_proxy_matches(
+ "api.example.test",
+ 443,
+ Some("example.test:8443")
+ ));
+ assert!(!no_proxy_matches(
+ "notexample.test",
+ 443,
+ Some("example.test")
+ ));
+ assert!(no_proxy_matches("anything.test", 443, Some("*")));
+ assert!(no_proxy_matches(
+ "api.example.test.",
+ 443,
+ Some("example.test.")
+ ));
+ assert!(no_proxy_matches("192.0.2.42", 80, Some("192.0.2.0/24")));
+ assert!(!no_proxy_matches("198.51.100.42", 80, Some("192.0.2.0/24")));
+ assert!(no_proxy_matches("[::1]", 8443, Some("[::1]:8443")));
+ }
+}
diff --git a/moli-fetch/src/runtime.rs b/moli-fetch/src/runtime.rs
index be1824d79..5611feb51 100644
--- a/moli-fetch/src/runtime.rs
+++ b/moli-fetch/src/runtime.rs
@@ -56,6 +56,7 @@ use crate::{
},
dns::curl_dns_resolution,
network_fetch_result::NetworkObservationRecorder,
+ proxy::resolve_http_proxy_route,
proxy_connect::{ProxyConnectResponse, ProxyConnectResponseRecorder},
};
@@ -895,17 +896,19 @@ impl RuntimeOwner {
.buffered_mut()
.expect("buffered request should use buffered collector")
.begin_request(self.config.http_max_response_size());
+ let proxy_route = resolve_http_proxy_route(&self.config, &job.current_url);
if let Err(error) = configure_network_observation(
&mut easy,
&job.request,
request_cookie_report.as_ref(),
- self.config.http_proxy().is_some() && job.current_url.scheme() == "https",
+ proxy_route.is_proxy() && job.current_url.scheme() == "https",
) {
return Err((job.response_tx, error));
}
let outgoing_headers = match configure_easy(
&mut easy,
&self.config,
+ &proxy_route,
&prepared_request.request,
&job.current_url,
cookie_header.as_deref(),
@@ -934,7 +937,8 @@ impl RuntimeOwner {
);
let label = job.current_url.to_string();
- let dns_resolution = match curl_dns_resolution(&self.config, &job.current_url) {
+ let dns_resolution = match curl_dns_resolution(&self.config, &job.current_url, &proxy_route)
+ {
Ok(resolution) => resolution,
Err(error) => return Err((job.response_tx, error)),
};
@@ -1073,17 +1077,19 @@ impl RuntimeOwner {
cookie_header.clone(),
));
+ let proxy_route = resolve_http_proxy_route(&self.config, &job.current_url);
if let Err(error) = configure_network_observation(
&mut easy,
&job.request,
request_cookie_report.as_ref(),
- self.config.http_proxy().is_some() && job.current_url.scheme() == "https",
+ proxy_route.is_proxy() && job.current_url.scheme() == "https",
) {
return Err((Box::new(job), Some(easy), error));
}
let outgoing_headers = match configure_easy(
&mut easy,
&self.config,
+ &proxy_route,
&prepared_request.request,
&job.current_url,
cookie_header.as_deref(),
@@ -1124,7 +1130,8 @@ impl RuntimeOwner {
collector.set_client_hint_response_policy(prepared_request.response_policy);
let label = job.current_url.to_string();
- let dns_resolution = match curl_dns_resolution(&self.config, &job.current_url) {
+ let dns_resolution = match curl_dns_resolution(&self.config, &job.current_url, &proxy_route)
+ {
Ok(resolution) => resolution,
Err(error) => return Err((Box::new(job), Some(easy), error)),
};
@@ -1273,17 +1280,19 @@ impl RuntimeOwner {
job.current_url.clone(),
cookie_header.clone(),
));
+ let proxy_route = resolve_http_proxy_route(&self.config, &job.current_url);
if let Err(error) = configure_network_observation(
&mut easy,
&job.request,
request_cookie_report.as_ref(),
- self.config.http_proxy().is_some() && job.current_url.scheme() == "https",
+ proxy_route.is_proxy() && job.current_url.scheme() == "https",
) {
return Err((Box::new(job), Some(easy), error));
}
let outgoing_headers = match configure_easy(
&mut easy,
&self.config,
+ &proxy_route,
&prepared_request.request,
&job.current_url,
cookie_header.as_deref(),
@@ -1327,7 +1336,8 @@ impl RuntimeOwner {
collector.set_client_hint_response_policy(prepared_request.response_policy);
let label = job.current_url.to_string();
- let dns_resolution = match curl_dns_resolution(&self.config, &job.current_url) {
+ let dns_resolution = match curl_dns_resolution(&self.config, &job.current_url, &proxy_route)
+ {
Ok(resolution) => resolution,
Err(error) => return Err((Box::new(job), Some(easy), error)),
};
diff --git a/moli-fetch/src/tests/mod.rs b/moli-fetch/src/tests/mod.rs
index 774378cd1..4c4d43c3f 100644
--- a/moli-fetch/src/tests/mod.rs
+++ b/moli-fetch/src/tests/mod.rs
@@ -1934,6 +1934,28 @@ fn fetch_client_rejects_proxy_side_dns_under_address_policy() {
);
}
+#[test]
+fn fetch_client_applies_selected_no_proxy_route_to_curl() {
+ let server = ScriptedHttpServer::spawn(vec![ScriptedResponse::ok("direct")]);
+ let port = Url::parse(&server.url())
+ .unwrap()
+ .port()
+ .expect("scripted server URL should include a port");
+ let mut config = FetchConfig::default();
+ config.set_http_proxy(Some("http://127.0.0.1:1".to_owned()));
+ config.set_http_no_proxy(Some(format!("127.0.0.1:{port}")));
+
+ let response = fetch_with_config_for_test(
+ &config,
+ Request::get(&format!("http://127.0.0.1:{port}/direct")).unwrap(),
+ )
+ .expect("the selected direct route must disable curl proxy evaluation");
+
+ assert_eq!(response.body_text(), "direct");
+ assert_eq!(server.hits(), 1);
+ server.shutdown();
+}
+
#[test]
fn fetch_client_rejects_private_network_targets_via_host_resolve_override() {
let server = ScriptedHttpServer::spawn(vec![ScriptedResponse::ok("should-not-be-reached")]);
@@ -2021,12 +2043,12 @@ fn fetch_client_rejects_http_bad_ports_before_network_io() {
}
#[test]
-fn fetch_client_preserves_libcurl_env_proxy_fallback() {
+fn fetch_client_preserves_env_proxy_fallback() {
let proxy = ScriptedHttpServer::spawn(vec![ScriptedResponse::ok("proxied-env")]);
let proxy_origin = proxy.origin();
let output = Command::new(std::env::current_exe().expect("test binary path"))
.arg("--exact")
- .arg("tests::env_proxy_child_uses_libcurl_env_proxy_fallback")
+ .arg("tests::env_proxy_child_uses_selected_env_proxy_route")
.arg("--ignored")
.arg("--nocapture")
.env(ENV_PROXY_CHILD_TEST, "1")
@@ -2061,8 +2083,8 @@ fn fetch_client_preserves_libcurl_env_proxy_fallback() {
}
#[test]
-#[ignore = "spawned by fetch_client_preserves_libcurl_env_proxy_fallback"]
-fn env_proxy_child_uses_libcurl_env_proxy_fallback() -> Result<()> {
+#[ignore = "spawned by fetch_client_preserves_env_proxy_fallback"]
+fn env_proxy_child_uses_selected_env_proxy_route() -> Result<()> {
if std::env::var_os(ENV_PROXY_CHILD_TEST).is_none() {
return Ok(());
}
diff --git a/moli/src/fetch_dump/tests.rs b/moli/src/fetch_dump/tests.rs
index 13829cda0..78f03f86f 100644
--- a/moli/src/fetch_dump/tests.rs
+++ b/moli/src/fetch_dump/tests.rs
@@ -389,7 +389,7 @@ async fn render_page_dump_async_includes_network_trace_config_summary() -> Resul
trace_network: true,
network_trace_config: Some(crate::network_trace::NetworkTraceConfigSummary {
explicit_http_proxy: true,
- libcurl_env_proxy_fallback: false,
+ env_proxy_fallback: false,
http_no_proxy: true,
proxy_bearer_token: true,
tls_verify_host: true,
@@ -414,10 +414,7 @@ async fn render_page_dump_async_includes_network_trace_config_summary() -> Resul
let payload: Value = serde_json::from_str(&rendered)?;
assert_eq!(payload["network"]["config"]["explicit_http_proxy"], true);
- assert_eq!(
- payload["network"]["config"]["libcurl_env_proxy_fallback"],
- false
- );
+ assert_eq!(payload["network"]["config"]["env_proxy_fallback"], false);
assert_eq!(payload["network"]["config"]["proxy_bearer_token"], true);
assert_eq!(payload["network"]["config"]["connect_timeout_ms"], 2500);
http_server.abort();
diff --git a/moli/src/network_trace.rs b/moli/src/network_trace.rs
index b5b2cc3bc..5afd4e4bd 100644
--- a/moli/src/network_trace.rs
+++ b/moli/src/network_trace.rs
@@ -10,7 +10,7 @@ use serde_json::{Map, Value, json};
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct NetworkTraceConfigSummary {
pub(crate) explicit_http_proxy: bool,
- pub(crate) libcurl_env_proxy_fallback: bool,
+ pub(crate) env_proxy_fallback: bool,
pub(crate) http_no_proxy: bool,
pub(crate) proxy_bearer_token: bool,
pub(crate) tls_verify_host: bool,
@@ -38,7 +38,7 @@ impl From<&FetchConfig> for NetworkTraceConfigSummary {
fn from(config: &FetchConfig) -> Self {
Self {
explicit_http_proxy: config.http_proxy().is_some(),
- libcurl_env_proxy_fallback: config.http_proxy().is_none(),
+ env_proxy_fallback: config.http_proxy().is_none(),
http_no_proxy: config.http_no_proxy().is_some(),
proxy_bearer_token: config.proxy_bearer_token().is_some(),
tls_verify_host: config.tls_verify_host(),
@@ -276,7 +276,7 @@ pub(crate) fn render_http_error_network_trace(
fn render_config_summary(config: &NetworkTraceConfigSummary) -> Value {
json!({
"explicit_http_proxy": config.explicit_http_proxy,
- "libcurl_env_proxy_fallback": config.libcurl_env_proxy_fallback,
+ "env_proxy_fallback": config.env_proxy_fallback,
"http_no_proxy": config.http_no_proxy,
"proxy_bearer_token": config.proxy_bearer_token,
"tls_verify_host": config.tls_verify_host,
@@ -1138,7 +1138,7 @@ mod tests {
fn trace_config_summary_reports_proxy_state_without_sensitive_values() {
let summary = NetworkTraceConfigSummary {
explicit_http_proxy: true,
- libcurl_env_proxy_fallback: false,
+ env_proxy_fallback: false,
http_no_proxy: true,
proxy_bearer_token: true,
tls_verify_host: false,
@@ -1160,7 +1160,7 @@ mod tests {
let rendered = render_config_summary(&summary);
assert_eq!(rendered["explicit_http_proxy"], true);
- assert_eq!(rendered["libcurl_env_proxy_fallback"], false);
+ assert_eq!(rendered["env_proxy_fallback"], false);
assert_eq!(rendered["http_no_proxy"], true);
assert_eq!(rendered["proxy_bearer_token"], true);
assert_eq!(rendered["tls_verify_host"], false);
@@ -1178,7 +1178,7 @@ mod tests {
fn http_error_network_trace_includes_config_and_auth_diagnostics() -> anyhow::Result<()> {
let config = NetworkTraceConfigSummary {
explicit_http_proxy: false,
- libcurl_env_proxy_fallback: true,
+ env_proxy_fallback: true,
http_no_proxy: false,
proxy_bearer_token: false,
tls_verify_host: true,
@@ -1207,7 +1207,7 @@ mod tests {
Some(&config),
);
- assert_eq!(trace["config"]["libcurl_env_proxy_fallback"], true);
+ assert_eq!(trace["config"]["env_proxy_fallback"], true);
assert_eq!(trace["main_document"]["status"], 401);
assert_eq!(
trace["main_document"]["diagnostics"]["server_auth_schemes"][0],