Skip to main content

agentmux_common/
local_http.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The HTTP client of a program that only talks to the local AgentMux server (the MCP server, bashwrap, the desktop
5//! host's credential broker): plain http on the loopback, never https.
6//!
7//! reqwest 0.13 reads the machine's certificates whenever a client is built, as soon as any crate built in the same
8//! cargo invocation turns TLS on (the server package builds `agentmux-srv`, `agentmux-mcp` and `agentmux-bashwrap`
9//! together, so all three get it), and on a Linux machine with none it refuses to build the client: `agentmux-mcp`
10//! panicked at start on such a machine. These programs never make an https request, so their clients are built to
11//! trust nothing: no certificates are read, and a build cannot fail for want of them. srv, which does make https
12//! requests, has its own helper (`crates/srv-core/src/http_client.rs`); `scripts/check-srv-http-clients.mjs` fails CI on a
13//! client built any other way in these crates.
14
15/// A client builder that trusts nothing and reads no certificates: set what the site needs and `build()`.
16pub fn builder() -> reqwest::ClientBuilder {
17    crate::tls_provider::install_ring_provider();
18    reqwest::Client::builder().tls_certs_only(Vec::<reqwest::Certificate>::new())
19}
20
21/// A client with reqwest's defaults, for a site that sets nothing. Never fails: with the provider installed, trusting
22/// nothing is a valid configuration.
23pub fn client() -> reqwest::Client {
24    builder().build().unwrap_or_else(|e| {
25        // Unreachable once the provider is installed.
26        panic!("the TLS provider is missing, so no HTTP client can be built: {e}")
27    })
28}
29
30#[cfg(all(test, target_os = "linux"))]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn a_client_is_built_where_reqwests_own_constructor_cannot() {
36        // A machine with no certificates: reqwest reads only the file SSL_CERT_FILE names when it is set. The environment
37        // is read when a client is built; nextest runs each test in its own process, so changing it here touches no other test.
38        let dir = tempfile::tempdir().unwrap();
39        let empty = dir.path().join("empty.pem");
40        std::fs::write(&empty, "").unwrap();
41        std::env::set_var("SSL_CERT_FILE", &empty);
42        std::env::remove_var("SSL_CERT_DIR");
43        crate::tls_provider::install_ring_provider();
44        assert!(reqwest::Client::builder().build().is_err(), "the premise: this machine has no certificates");
45        assert!(builder().build().is_ok());
46        let _ = client();
47    }
48}