Skip to main content

nono_proxy/
server.rs

1//! Proxy server: TCP listener, connection dispatch, and lifecycle.
2//!
3//! The server binds to `127.0.0.1:0` (OS-assigned port), accepts TCP
4//! connections, reads the first HTTP line to determine the mode, and
5//! dispatches to the appropriate handler.
6//!
7//! CONNECT method -> [`connect`] or [`external`] handler
8//! Other methods  -> [`reverse`] handler (credential injection)
9
10use crate::audit;
11use crate::config::ProxyConfig;
12use crate::connect;
13use crate::credential::CredentialStore;
14use crate::error::{ProxyError, Result};
15use crate::external;
16use crate::filter::ProxyFilter;
17use crate::reverse;
18use crate::token;
19use std::net::SocketAddr;
20use std::sync::atomic::{AtomicUsize, Ordering};
21use std::sync::Arc;
22use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
23use tokio::net::TcpListener;
24use tokio::sync::watch;
25use tracing::{debug, info, warn};
26use zeroize::Zeroizing;
27
28/// Maximum total size of HTTP headers (64 KiB). Prevents OOM from
29/// malicious clients sending unbounded header data.
30const MAX_HEADER_SIZE: usize = 64 * 1024;
31
32/// Handle returned when the proxy server starts.
33///
34/// Contains the assigned port, session token, and a shutdown channel.
35/// Drop the handle or send to `shutdown_tx` to stop the proxy.
36pub struct ProxyHandle {
37    /// The actual port the proxy is listening on
38    pub port: u16,
39    /// Session token for client authentication
40    pub token: Zeroizing<String>,
41    /// Shared in-memory network audit log
42    audit_log: audit::SharedAuditLog,
43    /// Send `true` to trigger graceful shutdown
44    shutdown_tx: watch::Sender<bool>,
45    /// Route prefixes that have credentials actually loaded.
46    /// Routes whose credentials were unavailable are excluded so we
47    /// don't inject phantom tokens that shadow valid external credentials.
48    loaded_routes: std::collections::HashSet<String>,
49    /// Non-credential allowed hosts that should bypass the proxy (NO_PROXY).
50    /// Computed at startup: `allowed_hosts` minus credential upstream hosts.
51    no_proxy_hosts: Vec<String>,
52}
53
54impl ProxyHandle {
55    /// Signal the proxy to shut down gracefully.
56    pub fn shutdown(&self) {
57        let _ = self.shutdown_tx.send(true);
58    }
59
60    /// Drain and return collected network audit events.
61    #[must_use]
62    pub fn drain_audit_events(&self) -> Vec<nono::undo::NetworkAuditEvent> {
63        audit::drain_audit_events(&self.audit_log)
64    }
65
66    /// Environment variables to inject into the child process.
67    ///
68    /// The proxy URL includes `nono:<token>@` userinfo so that standard HTTP
69    /// clients (curl, Python requests, etc.) automatically send
70    /// `Proxy-Authorization: Basic ...` on every request. The raw token is
71    /// also provided via `NONO_PROXY_TOKEN` for nono-aware clients that
72    /// prefer Bearer auth.
73    #[must_use]
74    pub fn env_vars(&self) -> Vec<(String, String)> {
75        let proxy_url = format!("http://nono:{}@127.0.0.1:{}", &*self.token, self.port);
76
77        // Build NO_PROXY: always include loopback, plus non-credential
78        // allowed hosts. Credential upstreams are excluded so their traffic
79        // goes through the reverse proxy for L7 filtering + injection.
80        let mut no_proxy_parts = vec!["localhost".to_string(), "127.0.0.1".to_string()];
81        for host in &self.no_proxy_hosts {
82            // Strip port for NO_PROXY (most HTTP clients match on hostname).
83            // Handle IPv6 brackets: "[::1]:443" → "[::1]", "host:443" → "host"
84            let hostname = if host.contains("]:") {
85                // IPv6 with port: split at "]:port"
86                host.rsplit_once("]:")
87                    .map(|(h, _)| format!("{}]", h))
88                    .unwrap_or_else(|| host.clone())
89            } else {
90                host.rsplit_once(':')
91                    .and_then(|(h, p)| p.parse::<u16>().ok().map(|_| h.to_string()))
92                    .unwrap_or_else(|| host.clone())
93            };
94            if !no_proxy_parts.contains(&hostname.to_string()) {
95                no_proxy_parts.push(hostname.to_string());
96            }
97        }
98        let no_proxy = no_proxy_parts.join(",");
99
100        let mut vars = vec![
101            ("HTTP_PROXY".to_string(), proxy_url.clone()),
102            ("HTTPS_PROXY".to_string(), proxy_url.clone()),
103            ("NO_PROXY".to_string(), no_proxy.clone()),
104            ("NONO_PROXY_TOKEN".to_string(), self.token.to_string()),
105        ];
106
107        // Lowercase variants for compatibility
108        vars.push(("http_proxy".to_string(), proxy_url.clone()));
109        vars.push(("https_proxy".to_string(), proxy_url));
110        vars.push(("no_proxy".to_string(), no_proxy));
111
112        vars
113    }
114
115    /// Environment variables for reverse proxy credential routes.
116    ///
117    /// Returns two types of env vars per route:
118    /// 1. SDK base URL overrides (e.g., `OPENAI_BASE_URL=http://127.0.0.1:PORT/openai`)
119    /// 2. SDK API key vars set to the session token (e.g., `OPENAI_API_KEY=<token>`)
120    ///
121    /// The SDK sends the session token as its "API key" (phantom token pattern).
122    /// The proxy validates this token and swaps it for the real credential.
123    #[must_use]
124    pub fn credential_env_vars(&self, config: &ProxyConfig) -> Vec<(String, String)> {
125        let mut vars = Vec::new();
126        for route in &config.routes {
127            // Base URL override (e.g., OPENAI_BASE_URL)
128            let base_url_name = format!("{}_BASE_URL", route.prefix.to_uppercase());
129            let url = format!("http://127.0.0.1:{}/{}", self.port, route.prefix);
130            vars.push((base_url_name, url));
131
132            // Only inject phantom token env vars for routes whose credentials
133            // were actually loaded. If a credential was unavailable (e.g.,
134            // GITHUB_TOKEN env var not set), injecting a phantom token would
135            // shadow valid credentials from other sources (keyring, gh auth).
136            if !self.loaded_routes.contains(&route.prefix) {
137                continue;
138            }
139
140            // API key set to session token (phantom token pattern).
141            // Use explicit env_var if set (required for URI manager refs), otherwise
142            // fall back to uppercasing the credential_key (e.g., "openai_api_key" -> "OPENAI_API_KEY").
143            if let Some(ref env_var) = route.env_var {
144                vars.push((env_var.clone(), self.token.to_string()));
145            } else if let Some(ref cred_key) = route.credential_key {
146                let api_key_name = cred_key.to_uppercase();
147                vars.push((api_key_name, self.token.to_string()));
148            }
149        }
150        vars
151    }
152}
153
154/// Shared state for the proxy server.
155struct ProxyState {
156    filter: ProxyFilter,
157    session_token: Zeroizing<String>,
158    credential_store: CredentialStore,
159    config: ProxyConfig,
160    /// Shared TLS connector for upstream connections (reverse proxy mode).
161    /// Created once at startup to avoid rebuilding the root cert store per request.
162    tls_connector: tokio_rustls::TlsConnector,
163    /// Active connection count for connection limiting.
164    active_connections: AtomicUsize,
165    /// Shared network audit log for this proxy session.
166    audit_log: audit::SharedAuditLog,
167    /// Matcher for hosts that bypass the external proxy and route direct.
168    /// Built once at startup from `ExternalProxyConfig.bypass_hosts`.
169    bypass_matcher: external::BypassMatcher,
170}
171
172/// Start the proxy server.
173///
174/// Binds to `config.bind_addr:config.bind_port` (port 0 = OS-assigned),
175/// generates a session token, and begins accepting connections.
176///
177/// Returns a `ProxyHandle` with the assigned port and session token.
178/// The server runs until the handle is dropped or `shutdown()` is called.
179pub async fn start(config: ProxyConfig) -> Result<ProxyHandle> {
180    // Generate session token
181    let session_token = token::generate_session_token()?;
182
183    // Bind listener
184    let bind_addr = SocketAddr::new(config.bind_addr, config.bind_port);
185    let listener = TcpListener::bind(bind_addr)
186        .await
187        .map_err(|e| ProxyError::Bind {
188            addr: bind_addr.to_string(),
189            source: e,
190        })?;
191
192    let local_addr = listener.local_addr().map_err(|e| ProxyError::Bind {
193        addr: bind_addr.to_string(),
194        source: e,
195    })?;
196    let port = local_addr.port();
197
198    info!("Proxy server listening on {}", local_addr);
199
200    // Load credentials for reverse proxy routes
201    let credential_store = if config.routes.is_empty() {
202        CredentialStore::empty()
203    } else {
204        CredentialStore::load(&config.routes)?
205    };
206    let loaded_routes = credential_store.loaded_prefixes();
207
208    // Build filter
209    let filter = if config.allowed_hosts.is_empty() {
210        ProxyFilter::allow_all()
211    } else {
212        ProxyFilter::new(&config.allowed_hosts)
213    };
214
215    // Build shared TLS connector (root cert store is expensive to construct).
216    // Use the ring provider explicitly to avoid ambiguity when multiple
217    // crypto providers are in the dependency tree.
218    let mut root_store = rustls::RootCertStore::empty();
219    root_store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
220    let tls_config = rustls::ClientConfig::builder_with_provider(Arc::new(
221        rustls::crypto::ring::default_provider(),
222    ))
223    .with_safe_default_protocol_versions()
224    .map_err(|e| ProxyError::Config(format!("TLS config error: {}", e)))?
225    .with_root_certificates(root_store)
226    .with_no_client_auth();
227    let tls_connector = tokio_rustls::TlsConnector::from(Arc::new(tls_config));
228
229    // Build bypass matcher from external proxy config (once, not per-request)
230    let bypass_matcher = config
231        .external_proxy
232        .as_ref()
233        .map(|ext| external::BypassMatcher::new(&ext.bypass_hosts))
234        .unwrap_or_else(|| external::BypassMatcher::new(&[]));
235
236    // Shutdown channel
237    let (shutdown_tx, shutdown_rx) = watch::channel(false);
238    let audit_log = audit::new_audit_log();
239
240    // Compute NO_PROXY hosts: allowed_hosts minus credential upstreams.
241    // Non-credential hosts bypass the proxy (direct connection, still
242    // Landlock-enforced). Credential upstreams must go through the proxy
243    // for L7 path filtering and credential injection.
244    let credential_hosts = credential_store.credential_upstream_hosts();
245    let no_proxy_hosts: Vec<String> = config
246        .allowed_hosts
247        .iter()
248        .filter(|host| {
249            let normalised = {
250                let h = host.to_lowercase();
251                if h.contains(':') {
252                    h
253                } else {
254                    format!("{}:443", h)
255                }
256            };
257            !credential_hosts.contains(&normalised)
258        })
259        .cloned()
260        .collect();
261
262    if !no_proxy_hosts.is_empty() {
263        debug!("Smart NO_PROXY bypass hosts: {:?}", no_proxy_hosts);
264    }
265
266    let state = Arc::new(ProxyState {
267        filter,
268        session_token: session_token.clone(),
269        credential_store,
270        config,
271        tls_connector,
272        active_connections: AtomicUsize::new(0),
273        audit_log: Arc::clone(&audit_log),
274        bypass_matcher,
275    });
276
277    // Spawn accept loop as a task within the current runtime.
278    // The caller MUST ensure this runtime is being driven (e.g., via
279    // a dedicated thread calling block_on or a multi-thread runtime).
280    tokio::spawn(accept_loop(listener, state, shutdown_rx));
281
282    Ok(ProxyHandle {
283        port,
284        token: session_token,
285        audit_log,
286        shutdown_tx,
287        loaded_routes,
288        no_proxy_hosts,
289    })
290}
291
292/// Accept loop: listen for connections until shutdown.
293async fn accept_loop(
294    listener: TcpListener,
295    state: Arc<ProxyState>,
296    mut shutdown_rx: watch::Receiver<bool>,
297) {
298    loop {
299        tokio::select! {
300            result = listener.accept() => {
301                match result {
302                    Ok((stream, addr)) => {
303                        // Connection limit enforcement
304                        let max = state.config.max_connections;
305                        if max > 0 {
306                            let current = state.active_connections.load(Ordering::Relaxed);
307                            if current >= max {
308                                warn!("Connection limit reached ({}/{}), rejecting {}", current, max, addr);
309                                // Drop the stream (connection refused)
310                                drop(stream);
311                                continue;
312                            }
313                        }
314                        state.active_connections.fetch_add(1, Ordering::Relaxed);
315
316                        debug!("Accepted connection from {}", addr);
317                        let state = Arc::clone(&state);
318                        tokio::spawn(async move {
319                            if let Err(e) = handle_connection(stream, &state).await {
320                                debug!("Connection handler error: {}", e);
321                            }
322                            state.active_connections.fetch_sub(1, Ordering::Relaxed);
323                        });
324                    }
325                    Err(e) => {
326                        warn!("Accept error: {}", e);
327                    }
328                }
329            }
330            _ = shutdown_rx.changed() => {
331                if *shutdown_rx.borrow() {
332                    info!("Proxy server shutting down");
333                    return;
334                }
335            }
336        }
337    }
338}
339
340/// Handle a single client connection.
341///
342/// Reads the first HTTP line to determine the proxy mode:
343/// - CONNECT method -> tunnel (Mode 1 or 3)
344/// - Other methods  -> reverse proxy (Mode 2)
345async fn handle_connection(mut stream: tokio::net::TcpStream, state: &ProxyState) -> Result<()> {
346    // Read the first line and headers through a BufReader.
347    // We keep the BufReader alive until we've consumed the full header
348    // to prevent data loss (BufReader may read ahead into the body).
349    let mut buf_reader = BufReader::new(&mut stream);
350    let mut first_line = String::new();
351    buf_reader.read_line(&mut first_line).await?;
352
353    if first_line.is_empty() {
354        return Ok(()); // Client disconnected
355    }
356
357    // Read remaining headers (up to empty line), with size limit to prevent OOM.
358    let mut header_bytes = Vec::new();
359    loop {
360        let mut line = String::new();
361        let n = buf_reader.read_line(&mut line).await?;
362        if n == 0 || line.trim().is_empty() {
363            break;
364        }
365        header_bytes.extend_from_slice(line.as_bytes());
366        if header_bytes.len() > MAX_HEADER_SIZE {
367            drop(buf_reader);
368            let response = "HTTP/1.1 431 Request Header Fields Too Large\r\n\r\n";
369            stream.write_all(response.as_bytes()).await?;
370            return Ok(());
371        }
372    }
373
374    // Extract any data buffered beyond headers before dropping BufReader.
375    // BufReader may have read ahead into the request body. We capture
376    // those bytes and pass them to the reverse proxy handler so no body
377    // data is lost. For CONNECT requests this is always empty (no body).
378    let buffered = buf_reader.buffer().to_vec();
379    drop(buf_reader);
380
381    let first_line = first_line.trim_end();
382
383    // Dispatch by method
384    if first_line.starts_with("CONNECT ") {
385        // Block CONNECT tunnels to credential upstreams. These must go
386        // through the reverse proxy path so L7 path filtering and
387        // credential injection are enforced. A CONNECT tunnel would
388        // bypass both (raw TLS pipe, proxy never sees HTTP method/path).
389        if !state.credential_store.is_empty() {
390            if let Some(authority) = first_line.split_whitespace().nth(1) {
391                // Normalise authority to host:port. Handle IPv6 brackets:
392                // "[::1]:443" already has port, "[::1]" needs default, "host:443" has port.
393                let host_port = if authority.starts_with('[') {
394                    // IPv6 literal
395                    if authority.contains("]:") {
396                        authority.to_lowercase()
397                    } else {
398                        format!("{}:443", authority.to_lowercase())
399                    }
400                } else if authority.contains(':') {
401                    authority.to_lowercase()
402                } else {
403                    format!("{}:443", authority.to_lowercase())
404                };
405                if state.credential_store.is_credential_upstream(&host_port) {
406                    let (host, port) = host_port
407                        .rsplit_once(':')
408                        .map(|(h, p)| (h, p.parse::<u16>().unwrap_or(443)))
409                        .unwrap_or((&host_port, 443));
410                    warn!(
411                        "Blocked CONNECT to credential upstream {} — use reverse proxy path instead",
412                        authority
413                    );
414                    audit::log_denied(
415                        Some(&state.audit_log),
416                        audit::ProxyMode::Connect,
417                        host,
418                        port,
419                        "credential upstream: CONNECT bypasses L7 filtering",
420                    );
421                    let response = "HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n";
422                    stream.write_all(response.as_bytes()).await?;
423                    return Ok(());
424                }
425            }
426        }
427
428        // Check if external proxy is configured and host is not bypassed
429        let use_external = if let Some(ref ext_config) = state.config.external_proxy {
430            if state.bypass_matcher.is_empty() {
431                Some(ext_config)
432            } else {
433                // Parse host from CONNECT line to check bypass
434                let host = first_line
435                    .split_whitespace()
436                    .nth(1)
437                    .and_then(|authority| {
438                        authority
439                            .rsplit_once(':')
440                            .map(|(h, _)| h)
441                            .or(Some(authority))
442                    })
443                    .unwrap_or("");
444                if state.bypass_matcher.matches(host) {
445                    debug!("Bypassing external proxy for {}", host);
446                    None
447                } else {
448                    Some(ext_config)
449                }
450            }
451        } else {
452            None
453        };
454
455        if let Some(ext_config) = use_external {
456            external::handle_external_proxy(
457                first_line,
458                &mut stream,
459                &header_bytes,
460                &state.filter,
461                &state.session_token,
462                ext_config,
463                Some(&state.audit_log),
464            )
465            .await
466        } else if state.config.external_proxy.is_some() {
467            // Bypass route: enforce strict session token validation before
468            // routing direct. Without this, bypassed hosts would inherit
469            // connect::handle_connect()'s lenient auth (which tolerates
470            // missing Proxy-Authorization for Node.js undici compat).
471            token::validate_proxy_auth(&header_bytes, &state.session_token)?;
472            connect::handle_connect(
473                first_line,
474                &mut stream,
475                &state.filter,
476                &state.session_token,
477                &header_bytes,
478                Some(&state.audit_log),
479            )
480            .await
481        } else {
482            connect::handle_connect(
483                first_line,
484                &mut stream,
485                &state.filter,
486                &state.session_token,
487                &header_bytes,
488                Some(&state.audit_log),
489            )
490            .await
491        }
492    } else if !state.credential_store.is_empty() {
493        // Non-CONNECT request with credential routes -> reverse proxy
494        let ctx = reverse::ReverseProxyCtx {
495            credential_store: &state.credential_store,
496            session_token: &state.session_token,
497            filter: &state.filter,
498            tls_connector: &state.tls_connector,
499            audit_log: Some(&state.audit_log),
500        };
501        reverse::handle_reverse_proxy(first_line, &mut stream, &header_bytes, &ctx, &buffered).await
502    } else {
503        // No credential routes configured, reject non-CONNECT requests
504        let response = "HTTP/1.1 400 Bad Request\r\n\r\n";
505        stream.write_all(response.as_bytes()).await?;
506        Ok(())
507    }
508}
509
510#[cfg(test)]
511#[allow(clippy::unwrap_used)]
512mod tests {
513    use super::*;
514
515    #[tokio::test]
516    async fn test_proxy_starts_and_binds() {
517        let config = ProxyConfig::default();
518        let handle = start(config).await.unwrap();
519
520        // Port should be non-zero (OS-assigned)
521        assert!(handle.port > 0);
522        // Token should be 64 hex chars
523        assert_eq!(handle.token.len(), 64);
524
525        // Shutdown
526        handle.shutdown();
527    }
528
529    #[tokio::test]
530    async fn test_proxy_env_vars() {
531        let config = ProxyConfig::default();
532        let handle = start(config).await.unwrap();
533
534        let vars = handle.env_vars();
535        let http_proxy = vars.iter().find(|(k, _)| k == "HTTP_PROXY");
536        assert!(http_proxy.is_some());
537        assert!(http_proxy.unwrap().1.starts_with("http://nono:"));
538
539        let token_var = vars.iter().find(|(k, _)| k == "NONO_PROXY_TOKEN");
540        assert!(token_var.is_some());
541        assert_eq!(token_var.unwrap().1.len(), 64);
542
543        let node_proxy_flag = vars.iter().find(|(k, _)| k == "NODE_USE_ENV_PROXY");
544        assert!(
545            node_proxy_flag.is_none(),
546            "proxy env should avoid Node-specific flags that can perturb non-Node runtimes"
547        );
548
549        handle.shutdown();
550    }
551
552    #[tokio::test]
553    async fn test_proxy_credential_env_vars() {
554        let config = ProxyConfig {
555            routes: vec![crate::config::RouteConfig {
556                prefix: "openai".to_string(),
557                upstream: "https://api.openai.com".to_string(),
558                credential_key: None,
559                inject_mode: crate::config::InjectMode::Header,
560                inject_header: "Authorization".to_string(),
561                credential_format: "Bearer {}".to_string(),
562                path_pattern: None,
563                path_replacement: None,
564                query_param_name: None,
565                env_var: None,
566                endpoint_rules: vec![],
567                tls_ca: None,
568            }],
569            ..Default::default()
570        };
571        let handle = start(config.clone()).await.unwrap();
572
573        let vars = handle.credential_env_vars(&config);
574        assert_eq!(vars.len(), 1);
575        assert_eq!(vars[0].0, "OPENAI_BASE_URL");
576        assert!(vars[0].1.contains("/openai"));
577
578        handle.shutdown();
579    }
580
581    #[test]
582    fn test_proxy_credential_env_vars_fallback_to_uppercase_key() {
583        // When env_var is None and credential_key is set, the env var name
584        // should be derived from uppercasing credential_key. This is the
585        // backward-compatible path for keyring-backed credentials.
586        let (shutdown_tx, _) = tokio::sync::watch::channel(false);
587        let handle = ProxyHandle {
588            port: 12345,
589            token: Zeroizing::new("test_token".to_string()),
590            audit_log: audit::new_audit_log(),
591            shutdown_tx,
592            loaded_routes: ["openai".to_string()].into_iter().collect(),
593            no_proxy_hosts: Vec::new(),
594        };
595        let config = ProxyConfig {
596            routes: vec![crate::config::RouteConfig {
597                prefix: "openai".to_string(),
598                upstream: "https://api.openai.com".to_string(),
599                credential_key: Some("openai_api_key".to_string()),
600                inject_mode: crate::config::InjectMode::Header,
601                inject_header: "Authorization".to_string(),
602                credential_format: "Bearer {}".to_string(),
603                path_pattern: None,
604                path_replacement: None,
605                query_param_name: None,
606                env_var: None, // No explicit env_var — should fall back to uppercase
607                endpoint_rules: vec![],
608                tls_ca: None,
609            }],
610            ..Default::default()
611        };
612
613        let vars = handle.credential_env_vars(&config);
614        assert_eq!(vars.len(), 2); // BASE_URL + API_KEY
615
616        // Should derive OPENAI_API_KEY from uppercasing "openai_api_key"
617        let api_key_var = vars.iter().find(|(k, _)| k == "OPENAI_API_KEY");
618        assert!(
619            api_key_var.is_some(),
620            "Should derive env var name from credential_key.to_uppercase()"
621        );
622
623        let (_, val) = api_key_var.expect("OPENAI_API_KEY should exist");
624        assert_eq!(val, "test_token");
625    }
626
627    #[test]
628    fn test_proxy_credential_env_vars_with_explicit_env_var() {
629        // When env_var is set on a route, it should be used instead of
630        // deriving from credential_key. This is essential for URI manager
631        // credential refs (e.g., op://, apple-password://)
632        // where uppercasing produces nonsensical env var names.
633        //
634        // We construct a ProxyHandle directly to test env var generation
635        // without starting a real proxy (which would try to load credentials).
636        let (shutdown_tx, _) = tokio::sync::watch::channel(false);
637        let handle = ProxyHandle {
638            port: 12345,
639            token: Zeroizing::new("test_token".to_string()),
640            audit_log: audit::new_audit_log(),
641            shutdown_tx,
642            loaded_routes: ["openai".to_string()].into_iter().collect(),
643            no_proxy_hosts: Vec::new(),
644        };
645        let config = ProxyConfig {
646            routes: vec![crate::config::RouteConfig {
647                prefix: "openai".to_string(),
648                upstream: "https://api.openai.com".to_string(),
649                credential_key: Some("op://Development/OpenAI/credential".to_string()),
650                inject_mode: crate::config::InjectMode::Header,
651                inject_header: "Authorization".to_string(),
652                credential_format: "Bearer {}".to_string(),
653                path_pattern: None,
654                path_replacement: None,
655                query_param_name: None,
656                env_var: Some("OPENAI_API_KEY".to_string()),
657                endpoint_rules: vec![],
658                tls_ca: None,
659            }],
660            ..Default::default()
661        };
662
663        let vars = handle.credential_env_vars(&config);
664        assert_eq!(vars.len(), 2); // BASE_URL + API_KEY
665
666        let api_key_var = vars.iter().find(|(k, _)| k == "OPENAI_API_KEY");
667        assert!(
668            api_key_var.is_some(),
669            "Should use explicit env_var name, not derive from credential_key"
670        );
671
672        // Verify the value is the phantom token, not the real credential
673        let (_, val) = api_key_var.expect("OPENAI_API_KEY var should exist");
674        assert_eq!(val, "test_token");
675
676        // Verify no nonsensical OP:// env var was generated
677        let bad_var = vars.iter().find(|(k, _)| k.starts_with("OP://"));
678        assert!(
679            bad_var.is_none(),
680            "Should not generate env var from op:// URI uppercase"
681        );
682    }
683
684    #[test]
685    fn test_proxy_credential_env_vars_skips_unloaded_routes() {
686        // When a credential is unavailable (e.g., GITHUB_TOKEN not set),
687        // the route should NOT inject a phantom token env var. Otherwise
688        // the phantom token shadows valid credentials from other sources
689        // like the system keyring. See: #234
690        let (shutdown_tx, _) = tokio::sync::watch::channel(false);
691        let handle = ProxyHandle {
692            port: 12345,
693            token: Zeroizing::new("test_token".to_string()),
694            audit_log: audit::new_audit_log(),
695            shutdown_tx,
696            // Only "openai" was loaded; "github" credential was unavailable
697            loaded_routes: ["openai".to_string()].into_iter().collect(),
698            no_proxy_hosts: Vec::new(),
699        };
700        let config = ProxyConfig {
701            routes: vec![
702                crate::config::RouteConfig {
703                    prefix: "openai".to_string(),
704                    upstream: "https://api.openai.com".to_string(),
705                    credential_key: Some("openai_api_key".to_string()),
706                    inject_mode: crate::config::InjectMode::Header,
707                    inject_header: "Authorization".to_string(),
708                    credential_format: "Bearer {}".to_string(),
709                    path_pattern: None,
710                    path_replacement: None,
711                    query_param_name: None,
712                    env_var: None,
713                    endpoint_rules: vec![],
714                    tls_ca: None,
715                },
716                crate::config::RouteConfig {
717                    prefix: "github".to_string(),
718                    upstream: "https://api.github.com".to_string(),
719                    credential_key: Some("env://GITHUB_TOKEN".to_string()),
720                    inject_mode: crate::config::InjectMode::Header,
721                    inject_header: "Authorization".to_string(),
722                    credential_format: "token {}".to_string(),
723                    path_pattern: None,
724                    path_replacement: None,
725                    query_param_name: None,
726                    env_var: Some("GITHUB_TOKEN".to_string()),
727                    endpoint_rules: vec![],
728                    tls_ca: None,
729                },
730            ],
731            ..Default::default()
732        };
733
734        let vars = handle.credential_env_vars(&config);
735
736        // openai should have BASE_URL + API_KEY (credential loaded)
737        let openai_base = vars.iter().find(|(k, _)| k == "OPENAI_BASE_URL");
738        assert!(openai_base.is_some(), "loaded route should have BASE_URL");
739        let openai_key = vars.iter().find(|(k, _)| k == "OPENAI_API_KEY");
740        assert!(openai_key.is_some(), "loaded route should have API key");
741
742        // github should have BASE_URL (always set for declared routes) but
743        // must NOT have GITHUB_TOKEN (credential was not loaded)
744        let github_base = vars.iter().find(|(k, _)| k == "GITHUB_BASE_URL");
745        assert!(
746            github_base.is_some(),
747            "declared route should still have BASE_URL"
748        );
749        let github_token = vars.iter().find(|(k, _)| k == "GITHUB_TOKEN");
750        assert!(
751            github_token.is_none(),
752            "unloaded route must not inject phantom GITHUB_TOKEN"
753        );
754    }
755
756    #[test]
757    fn test_no_proxy_excludes_credential_upstreams() {
758        let (shutdown_tx, _) = tokio::sync::watch::channel(false);
759        let handle = ProxyHandle {
760            port: 12345,
761            token: Zeroizing::new("test_token".to_string()),
762            audit_log: audit::new_audit_log(),
763            shutdown_tx,
764            loaded_routes: std::collections::HashSet::new(),
765            no_proxy_hosts: vec![
766                "nats.internal:4222".to_string(),
767                "opencode.internal:4096".to_string(),
768            ],
769        };
770
771        let vars = handle.env_vars();
772        let no_proxy = vars.iter().find(|(k, _)| k == "NO_PROXY").unwrap();
773        assert!(
774            no_proxy.1.contains("nats.internal"),
775            "non-credential host should be in NO_PROXY"
776        );
777        assert!(
778            no_proxy.1.contains("opencode.internal"),
779            "non-credential host should be in NO_PROXY"
780        );
781        assert!(
782            no_proxy.1.contains("localhost"),
783            "localhost should always be in NO_PROXY"
784        );
785    }
786
787    #[test]
788    fn test_no_proxy_empty_when_no_non_credential_hosts() {
789        let (shutdown_tx, _) = tokio::sync::watch::channel(false);
790        let handle = ProxyHandle {
791            port: 12345,
792            token: Zeroizing::new("test_token".to_string()),
793            audit_log: audit::new_audit_log(),
794            shutdown_tx,
795            loaded_routes: std::collections::HashSet::new(),
796            no_proxy_hosts: Vec::new(),
797        };
798
799        let vars = handle.env_vars();
800        let no_proxy = vars.iter().find(|(k, _)| k == "NO_PROXY").unwrap();
801        assert_eq!(
802            no_proxy.1, "localhost,127.0.0.1",
803            "NO_PROXY should only contain loopback when no bypass hosts"
804        );
805    }
806}