From 2999bafcfe3de9e641fee6be4fbe585f68530104 Mon Sep 17 00:00:00 2001 From: kleinbem Date: Sun, 22 Nov 2026 04:07:09 +0000 Subject: [PATCH 1/2] fix(plugins): probe plain-HTTP plugin endpoints without system CA certs or proxy --- .../src/plugin/health.rs | 32 ++++- .../src/plugin/health_probe_tests.rs | 126 ++++++++++++++++++ 2 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 mesh/crates/mesh-llm-host-runtime/src/plugin/health_probe_tests.rs diff --git a/mesh/crates/mesh-llm-host-runtime/src/plugin/health.rs b/mesh/crates/mesh-llm-host-runtime/src/plugin/health.rs index d66625f526..511bd57ec9 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/plugin/health.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/plugin/health.rs @@ -384,6 +384,32 @@ async fn probe_openai_compatible_http_endpoint(address: &str) -> EndpointHealthR probe_openai_compatible_http_endpoint_for_protocol(address, None).await } +fn is_loopback_host(url: &Url) -> bool { + match url.host() { + Some(url::Host::Ipv4(ip)) => ip.is_loopback(), + Some(url::Host::Ipv6(ip)) => ip.is_loopback(), + Some(url::Host::Domain(host)) => { + host.trim_end_matches('.').eq_ignore_ascii_case("localhost") + } + None => false, + } +} + +/// Plain-HTTP probes never talk TLS, so skip loading system CA certificates; +/// loopback probes also bypass proxy settings and redirects, matching +/// `network::local_http::local_http_client`. +fn health_probe_client_builder(url: &Url) -> reqwest::ClientBuilder { + if url.scheme() == "http" { + if is_loopback_host(url) { + mesh_llm_system::local_http::local_http_client_builder() + } else { + reqwest::Client::builder().tls_certs_only([]) + } + } else { + reqwest::Client::builder() + } +} + async fn probe_openai_compatible_http_endpoint_for_protocol( address: &str, protocol: Option<&str>, @@ -400,7 +426,7 @@ async fn probe_openai_compatible_http_endpoint_for_protocol( } }; - let client = match reqwest::Client::builder() + let client = match health_probe_client_builder(&models_url) .timeout(std::time::Duration::from_secs(3)) .build() { @@ -939,3 +965,7 @@ mod managed_task_tests { assert!(!model_matches_protocol(&depth, Some("openai_compatible"))); } } + +#[cfg(test)] +#[path = "health_probe_tests.rs"] +mod probe_client_tests; diff --git a/mesh/crates/mesh-llm-host-runtime/src/plugin/health_probe_tests.rs b/mesh/crates/mesh-llm-host-runtime/src/plugin/health_probe_tests.rs new file mode 100644 index 0000000000..9f5764a9b2 --- /dev/null +++ b/mesh/crates/mesh-llm-host-runtime/src/plugin/health_probe_tests.rs @@ -0,0 +1,126 @@ +use super::{ + health_probe_client_builder, is_loopback_host, + probe_openai_compatible_http_endpoint_for_protocol, +}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::TcpListener; +use url::Url; + +const NO_CA_OR_PROXY_CHILD: &str = "MESH_LLM_TEST_PLUGIN_HEALTH_NO_CA_CHILD"; + +#[test] +fn is_loopback_host_classifies_loopback_and_remote_urls() { + for url in [ + "http://127.0.0.1:8000/v1/models", + "http://[::1]:8000/v1/models", + "http://localhost:8000/v1/models", + "http://LOCALHOST.:8000/v1/models", + ] { + assert!( + is_loopback_host(&Url::parse(url).expect("parse loopback url")), + "expected loopback: {url}" + ); + } + for url in [ + "http://192.0.2.1:8000/v1/models", + "http://plugin.internal:8000/v1/models", + "https://api.example.com/v1/models", + ] { + assert!( + !is_loopback_host(&Url::parse(url).expect("parse remote url")), + "expected non-loopback: {url}" + ); + } +} + +// SSL_CERT_FILE and SSL_CERT_DIR point system root loading at an empty file and +// directory on Linux, and HTTP_PROXY points at a closed port; both are set on +// the child test process only. +#[test] +fn probe_http_endpoint_reaches_loopback_without_ca_bundle_or_proxy() { + let temp = tempfile::tempdir().expect("tempdir for empty CA bundle"); + let empty_file = temp.path().join("empty.pem"); + std::fs::write(&empty_file, "").expect("write empty cert file"); + let empty_dir = temp.path().join("certs"); + std::fs::create_dir(&empty_dir).expect("create empty cert dir"); + + let closed = std::net::TcpListener::bind("127.0.0.1:0").expect("bind closed proxy port"); + let proxy = format!("http://{}", closed.local_addr().expect("closed proxy addr")); + drop(closed); + + let output = std::process::Command::new(std::env::current_exe().expect("current test exe")) + .args([ + "--exact", + "plugin::health::probe_client_tests::probe_http_endpoint_without_ca_bundle_child", + "--nocapture", + ]) + .env(NO_CA_OR_PROXY_CHILD, "1") + .env("SSL_CERT_FILE", &empty_file) + .env("SSL_CERT_DIR", &empty_dir) + .env("HTTP_PROXY", &proxy) + .env("http_proxy", &proxy) + .env("ALL_PROXY", &proxy) + .env("all_proxy", &proxy) + .env_remove("NO_PROXY") + .env_remove("no_proxy") + .output() + .expect("spawn plugin health probe child"); + + assert!( + output.status.success(), + "plugin health probe child failed:\nstdout:\n{}\nstderr:\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + assert!( + String::from_utf8_lossy(&output.stdout).contains("1 passed"), + "child test filter must execute its assertion" + ); +} + +#[tokio::test] +async fn probe_http_endpoint_without_ca_bundle_child() { + if std::env::var_os(NO_CA_OR_PROXY_CHILD).is_none() { + return; + } + + // Non-loopback plain HTTP must also build without system CA certificates. + let remote_http = Url::parse("http://192.0.2.1:8000/v1/models").expect("parse remote http url"); + assert!( + health_probe_client_builder(&remote_http).build().is_ok(), + "non-loopback plain-HTTP probe client should build without CA certs" + ); + + let listener = TcpListener::bind("127.0.0.1:0") + .await + .expect("bind loopback probe server"); + let addr = listener.local_addr().expect("probe server addr"); + let server = tokio::spawn(async move { + let (mut socket, _) = listener.accept().await.expect("accept health probe"); + let mut buf = [0u8; 1024]; + let _ = socket.read(&mut buf).await; + let body = r#"{"data":[{"id":"demo-model","availability":"ready","workload_class":"causal_generation"}]}"#; + let response = format!( + "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", + body.len(), + body + ); + socket + .write_all(response.as_bytes()) + .await + .expect("write probe response"); + let _ = socket.shutdown().await; + }); + + let record = probe_openai_compatible_http_endpoint_for_protocol( + &format!("http://{addr}"), + Some("openai_compatible"), + ) + .await; + assert!( + record.available, + "loopback plain-HTTP plugin health probe should succeed without CA certs or proxy: {record:?}" + ); + assert_eq!(record.models, vec!["demo-model".to_string()]); + server.await.expect("probe server join"); +} From 69d214bce3d73d31fa1f69b090352d8f032a4ada Mon Sep 17 00:00:00 2001 From: kleinbem Date: Sun, 22 Nov 2026 10:43:41 +0000 Subject: [PATCH 2/2] fix(plugins): handle IPv4-mapped loopback and preserve proxy CA roots --- .../src/plugin/health.rs | 39 ++++++++++++------- .../src/plugin/health_probe_tests.rs | 17 +++++--- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/mesh/crates/mesh-llm-host-runtime/src/plugin/health.rs b/mesh/crates/mesh-llm-host-runtime/src/plugin/health.rs index 511bd57ec9..4daebd7687 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/plugin/health.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/plugin/health.rs @@ -387,7 +387,9 @@ async fn probe_openai_compatible_http_endpoint(address: &str) -> EndpointHealthR fn is_loopback_host(url: &Url) -> bool { match url.host() { Some(url::Host::Ipv4(ip)) => ip.is_loopback(), - Some(url::Host::Ipv6(ip)) => ip.is_loopback(), + Some(url::Host::Ipv6(ip)) => { + ip.is_loopback() || ip.to_ipv4_mapped().is_some_and(|v4| v4.is_loopback()) + } Some(url::Host::Domain(host)) => { host.trim_end_matches('.').eq_ignore_ascii_case("localhost") } @@ -395,19 +397,31 @@ fn is_loopback_host(url: &Url) -> bool { } } -/// Plain-HTTP probes never talk TLS, so skip loading system CA certificates; -/// loopback probes also bypass proxy settings and redirects, matching -/// `network::local_http::local_http_client`. -fn health_probe_client_builder(url: &Url) -> reqwest::ClientBuilder { +/// Build an HTTP client for plugin endpoint health probes. +/// +/// Loopback `http://` probes reuse `local_http_client_builder()` (no TLS roots, +/// no proxy, no redirects). Non-loopback `http://` probes try the default +/// builder first so system roots stay available for HTTPS proxies and redirects, +/// falling back to `tls_certs_only([])` when the host has no CA bundle. +fn build_health_probe_client( + url: &Url, + timeout: std::time::Duration, +) -> Result { if url.scheme() == "http" { if is_loopback_host(url) { - mesh_llm_system::local_http::local_http_client_builder() - } else { - reqwest::Client::builder().tls_certs_only([]) + return mesh_llm_system::local_http::local_http_client_builder() + .timeout(timeout) + .build(); } - } else { - reqwest::Client::builder() + if let Ok(client) = reqwest::Client::builder().timeout(timeout).build() { + return Ok(client); + } + return reqwest::Client::builder() + .tls_certs_only([]) + .timeout(timeout) + .build(); } + reqwest::Client::builder().timeout(timeout).build() } async fn probe_openai_compatible_http_endpoint_for_protocol( @@ -426,10 +440,7 @@ async fn probe_openai_compatible_http_endpoint_for_protocol( } }; - let client = match health_probe_client_builder(&models_url) - .timeout(std::time::Duration::from_secs(3)) - .build() - { + let client = match build_health_probe_client(&models_url, std::time::Duration::from_secs(3)) { Ok(client) => client, Err(err) => { return EndpointHealthRecord { diff --git a/mesh/crates/mesh-llm-host-runtime/src/plugin/health_probe_tests.rs b/mesh/crates/mesh-llm-host-runtime/src/plugin/health_probe_tests.rs index 9f5764a9b2..4de43bff92 100644 --- a/mesh/crates/mesh-llm-host-runtime/src/plugin/health_probe_tests.rs +++ b/mesh/crates/mesh-llm-host-runtime/src/plugin/health_probe_tests.rs @@ -1,7 +1,7 @@ use super::{ - health_probe_client_builder, is_loopback_host, - probe_openai_compatible_http_endpoint_for_protocol, + build_health_probe_client, is_loopback_host, probe_openai_compatible_http_endpoint_for_protocol, }; +use std::time::Duration; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpListener; use url::Url; @@ -13,6 +13,7 @@ fn is_loopback_host_classifies_loopback_and_remote_urls() { for url in [ "http://127.0.0.1:8000/v1/models", "http://[::1]:8000/v1/models", + "http://[::ffff:127.0.0.1]:8000/v1/models", "http://localhost:8000/v1/models", "http://LOCALHOST.:8000/v1/models", ] { @@ -23,6 +24,7 @@ fn is_loopback_host_classifies_loopback_and_remote_urls() { } for url in [ "http://192.0.2.1:8000/v1/models", + "http://[::ffff:192.0.2.1]:8000/v1/models", "http://plugin.internal:8000/v1/models", "https://api.example.com/v1/models", ] { @@ -84,11 +86,16 @@ async fn probe_http_endpoint_without_ca_bundle_child() { return; } - // Non-loopback plain HTTP must also build without system CA certificates. + // Non-loopback plain HTTP falls back when no system CA certificates exist. + #[cfg(target_os = "linux")] + assert!( + reqwest::Client::builder().build().is_err(), + "child environment should lack system CA certificates on Linux" + ); let remote_http = Url::parse("http://192.0.2.1:8000/v1/models").expect("parse remote http url"); assert!( - health_probe_client_builder(&remote_http).build().is_ok(), - "non-loopback plain-HTTP probe client should build without CA certs" + build_health_probe_client(&remote_http, Duration::from_secs(3)).is_ok(), + "non-loopback plain-HTTP probe client should fall back cleanly without CA certs" ); let listener = TcpListener::bind("127.0.0.1:0")