diff --git a/LLMPROXY.md b/LLMPROXY.md index e7fdda2..395f62a 100644 --- a/LLMPROXY.md +++ b/LLMPROXY.md @@ -20,6 +20,7 @@ All configuration is done with environment variables. Below, in alphabetical ord | Variable | Default | Meaning | | --- | --- | --- | +| `LLM_PROXY_DISABLE_CERT_VERIFICATION` | verify | `1` (also `true`/`yes`/`on`) disables upstream TLS certificate-chain validation for the proxy's own upstream connections (both MITM upstreams and forwarded requests). Default verifies. | | `LLM_PROXY_ENABLED` | enabled | `off`, `0`, `false` or `no` disables the proxy entirely: it is not started and no proxy environment is enforced. | | `LLM_PROXY_LISTEN_ADDR` | `127.0.0.1:8899` | Address the proxy listens on; also the value `HTTP_PROXY` is forced to. | | `LLM_PROXY_INTERCEPT_SUFFIXES` | everything | Comma-separated domain suffixes (e.g. `cognitiveservices.azure.com`) whose TLS is terminated locally. Empty means all hosts. | diff --git a/llm-proxy/src/main.rs b/llm-proxy/src/main.rs index 69b2833..e1416e1 100644 --- a/llm-proxy/src/main.rs +++ b/llm-proxy/src/main.rs @@ -24,9 +24,10 @@ use base64::Engine as _; use rcgen::{ CertificateParams, DnType, ExtendedKeyUsagePurpose, Issuer, KeyPair, SanType, }; -use rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer, ServerName}; +use rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer, ServerName, UnixTime}; +use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier}; use rustls::version::{TLS12, TLS13}; -use rustls::{ClientConfig, RootCertStore, ServerConfig, SupportedProtocolVersion}; +use rustls::{ClientConfig, DigitallySignedStruct, RootCertStore, ServerConfig, SignatureScheme, SupportedProtocolVersion}; use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, @@ -482,6 +483,74 @@ struct MitmState { cache: Mutex>>, } +/// Insecure opt-out selected with LLM_PROXY_DISABLE_CERT_VERIFICATION=1: +/// upstream TLS certificates are accepted without chain validation. +#[derive(Debug)] +struct NoCertVerifier; + +impl ServerCertVerifier for NoCertVerifier { + fn verify_server_cert( + &self, + _end_entity: &CertificateDer<'_>, + _intermediates: &[CertificateDer<'_>], + _server_name: &ServerName<'_>, + _ocsp_response: &[u8], + _now: UnixTime, + ) -> Result { + Ok(ServerCertVerified::assertion()) + } + + fn verify_tls12_signature( + &self, + message: &[u8], + cert: &CertificateDer<'_>, + dss: &DigitallySignedStruct, + ) -> Result { + let provider = rustls::crypto::ring::default_provider(); + rustls::crypto::verify_tls12_signature( + message, + cert, + dss, + &provider.signature_verification_algorithms, + ) + } + + fn verify_tls13_signature( + &self, + message: &[u8], + cert: &CertificateDer<'_>, + dss: &DigitallySignedStruct, + ) -> Result { + let provider = rustls::crypto::ring::default_provider(); + rustls::crypto::verify_tls13_signature( + message, + cert, + dss, + &provider.signature_verification_algorithms, + ) + } + + fn supported_verify_schemes(&self) -> Vec { + rustls::crypto::ring::default_provider() + .signature_verification_algorithms + .supported_schemes() + } +} + +fn cert_verification_disabled_raw(raw: &str) -> bool { + matches!( + raw.trim().to_ascii_lowercase().as_str(), + "1" | "true" | "yes" | "on" + ) +} + +/// True when LLM_PROXY_DISABLE_CERT_VERIFICATION opts out of upstream +/// TLS certificate-chain validation. Default (unset/anything else): verify. +fn upstream_cert_verification_disabled() -> bool { + env::var("LLM_PROXY_DISABLE_CERT_VERIFICATION") + .is_ok_and(|v| cert_verification_disabled_raw(&v)) +} + fn init_mitm(versions: Vec<&'static SupportedProtocolVersion>) -> Option> { if env::var("LLM_PROXY_MITM").is_ok_and(|v| v.eq_ignore_ascii_case("off")) { return None; @@ -864,11 +933,18 @@ async fn mitm_relay( }; tokio::spawn(async move { let setup = async { - let mut roots = RootCertStore::empty(); - roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); - let cc = ClientConfig::builder_with_protocol_versions(&mitm.versions) - .with_root_certificates(roots) - .with_no_client_auth(); + let cc = if upstream_cert_verification_disabled() { + ClientConfig::builder_with_protocol_versions(&mitm.versions) + .dangerous() + .with_custom_certificate_verifier(Arc::new(NoCertVerifier)) + .with_no_client_auth() + } else { + let mut roots = RootCertStore::empty(); + roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); + ClientConfig::builder_with_protocol_versions(&mitm.versions) + .with_root_certificates(roots) + .with_no_client_auth() + }; let server_name: ServerName<'static> = match ServerName::try_from(host.clone()) { Ok(n) => n, Err(_) => match host.parse::() { @@ -1142,6 +1218,9 @@ async fn main() { let (tls_min, tls_max, tls_versions) = tls_bounds_or_exit(); let mut client_builder = reqwest::Client::builder().redirect(reqwest::redirect::Policy::none()); + if upstream_cert_verification_disabled() { + client_builder = client_builder.danger_accept_invalid_certs(true); + } if let Some(v) = tls_min { client_builder = client_builder.min_tls_version(reqwest_tls_version(v)); } @@ -1207,6 +1286,13 @@ async fn main() { .await .expect("bind listen address"); + if upstream_cert_verification_disabled() { + log_line!( + Level::Warn, + "llm-proxy: upstream TLS certificate verification DISABLED via LLM_PROXY_DISABLE_CERT_VERIFICATION" + ); + } + log_line!( Level::Info, "llm-proxy (rust) listening on http://{listen_addr} -> {route} (upstream proxy: {proxy_desc}, mitm: {mitm_desc}, tls: {tls_desc}, level: {level_desc}, llm-logs: {log_display})", @@ -1556,4 +1642,14 @@ mod tests { assert!(Level::Warn < Level::Info); assert!(Level::Info < Level::Debug); } + + #[test] + fn parses_disable_cert_verification() { + for on in ["1", "true", "TRUE", "yes", "on", " On "] { + assert!(cert_verification_disabled_raw(on), "{on}"); + } + for off in ["", " ", "0", "false", "no", "off", "2"] { + assert!(!cert_verification_disabled_raw(off), "{off}"); + } + } }