//! Model provider transport. //! //! The API key never appears on a command line or in a log. It is injected into //! a curl configuration file passed on stdin, while curl reads the JSON request //! body from a 0600 temporary file. The body contains only the approved facts //! built by `phoebus_ai::provider`. use std::path::Path; use phoebus_ai::provider::{ModelTransport, ProviderKind}; use phoebus_ai::AiError; use phoebus_core::SecretString; use crate::command::CommandSpec; use crate::error::{ExecError, Result}; use crate::exec::Exec; /// Read an API key from a file that must be mode 0600. The value is returned /// as a `SecretString`; callers must register it with the redactor (the /// transport does this automatically). pub fn load_api_key(path: &Path) -> Result { use std::os::unix::fs::PermissionsExt; let meta = std::fs::metadata(path).map_err(|e| ExecError::io(path.to_path_buf(), e))?; if meta.permissions().mode() & 0o077 != 0 { return Err(ExecError::operation( "ai", "read API key", format!("{} must be mode 0600", path.display()), )); } let key = std::fs::read_to_string(path).map_err(|e| ExecError::io(path.to_path_buf(), e))?; let key = key.trim().to_string(); if key.is_empty() { return Err(ExecError::operation( "ai", "read API key", "API key file is empty", )); } // The key is interpolated into a curl config file, so a quote, backslash, // whitespace or control character could break out of the // `header = "Authorization: Bearer ..."` directive and inject additional // curl options. Such a credential is malformed; refuse it. if key .chars() .any(|c| c.is_whitespace() || c.is_control() || c == '"' || c == '\\') { return Err(ExecError::operation( "ai", "read API key", "API key contains a character that is unsafe in a transport config", )); } Ok(SecretString::new(key)) } /// Default model-call timeout: local CPU inference is slow, and the old /// 120 s default aborted it with `curl exit 28`. pub const DEFAULT_AI_TIMEOUT_SECS: u64 = 300; /// Model-call timeout in seconds, overrideable via `PHOEBUS_AI_TIMEOUT`. /// Unparseable, zero or missing values fall back to the default; zero is /// refused because curl treats `max-time = 0` as "no timeout". pub fn ai_timeout_secs() -> u64 { std::env::var("PHOEBUS_AI_TIMEOUT") .ok() .and_then(|v| v.trim().parse::().ok()) .filter(|v| *v > 0) .unwrap_or(DEFAULT_AI_TIMEOUT_SECS) } /// A transport that talks to an OpenAI-compatible endpoint with curl. /// /// The API key is optional: a self-hosted local model usually needs none, while /// DeepSeek/OpenAI require one. pub struct CurlTransport<'a> { exec: &'a Exec, api_key: Option, sandboxed: bool, } impl<'a> CurlTransport<'a> { pub fn new(exec: &'a Exec, api_key: Option) -> Self { if let Some(key) = &api_key { exec.register_secret(key); } Self { exec, api_key, sandboxed: false, } } /// Run the network call inside the OS-level model sandbox (installed /// assistant). Fails closed when `systemd-run` is unavailable. pub fn sandboxed(mut self) -> Self { self.sandboxed = true; self } } impl ModelTransport for CurlTransport<'_> { fn send( &self, _provider: ProviderKind, url: &str, body: &str, ) -> std::result::Result { // Fail closed on an unsafe scheme or a URL that could inject curl // config directives before anything is written. phoebus_ai::provider::validate_endpoint(url)?; if self.sandboxed { return self.send_sandboxed(url, body); } let mut request = tempfile::Builder::new() .prefix("phoebus-ai-request.") .tempfile() .map_err(|e| AiError::Provider(e.to_string()))?; use std::io::Write; request .write_all(body.as_bytes()) .and_then(|_| request.flush()) .map_err(|e| AiError::Provider(e.to_string()))?; // curl reads this config from stdin, so Exec marks the stdin as hidden // and never logs it. The key is therefore absent from argv and logs. let auth = match &self.api_key { Some(key) => format!("header = \"Authorization: Bearer {}\"\n", key.expose()), None => String::new(), }; // `proto` restricts the transfer to http(s); `location` is deliberately // absent so a redirect is never followed (a redirect could send the // Authorization header to another origin). `max-filesize` bounds the // response so a hostile or broken endpoint cannot exhaust memory. // `max-time` covers slow local CPU inference (see `ai_timeout_secs`). let config = format!( "url = \"{url}\"\n\ proto = \"=http,https\"\n\ header = \"Content-Type: application/json\"\n\ {auth}\ data = \"@{path}\"\n\ silent\nshow-error\nfail-with-body\nmax-time = {timeout}\n\ connect-timeout = 30\nmax-filesize = {max}\n", path = request.path().display(), timeout = ai_timeout_secs(), max = phoebus_ai::provider::MAX_RESPONSE_BYTES, ); let spec = CommandSpec::new("ai", "query model", "curl") .args(["-sS", "-K", "-"]) .stdin(config.into_bytes()); let out = self .exec .run(spec) .map_err(|e| AiError::Provider(e.to_string()))?; Ok(out.stdout_lossy()) } } impl CurlTransport<'_> { /// Sandboxed variant: the request body is embedded in the curl config on /// stdin (never on disk, never in argv), and curl runs inside a hardened, /// unprivileged unit. `PrivateTmp=yes` is therefore safe. fn send_sandboxed(&self, url: &str, body: &str) -> std::result::Result { let auth = match &self.api_key { Some(key) => format!("header = \"Authorization: Bearer {}\"\n", key.expose()), None => String::new(), }; let escaped = body.replace('\\', "\\\\").replace('"', "\\\""); let config = format!( "url = \"{url}\"\n\ proto = \"=http,https\"\n\ header = \"Content-Type: application/json\"\n\ {auth}\ data = \"{escaped}\"\n\ silent\nshow-error\nfail-with-body\nmax-time = {timeout}\n\ connect-timeout = 30\nmax-filesize = {max}\n", timeout = ai_timeout_secs(), max = phoebus_ai::provider::MAX_RESPONSE_BYTES, ); let out = crate::sandbox::run( self.exec, "curl", &["-sS".into(), "-K".into(), "-".into()], config.into_bytes(), ) .map_err(|e| AiError::Provider(e.to_string()))?; Ok(out.stdout_lossy()) } } #[cfg(test)] mod tests { use super::*; use crate::command::{CommandOutput, RecordingRunner}; use crate::log::Logger; use phoebus_core::Redactor; use std::os::unix::fs::PermissionsExt; use std::time::Duration; fn make_exec(runner: RecordingRunner) -> Exec { let path = tempfile::tempdir().unwrap().keep(); let logger = Logger::open(path.join("l.log"), Redactor::new(), false); Exec::new(Box::new(runner), logger, false, path.join("root"), path) } #[test] fn api_key_must_be_mode_0600() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("key"); std::fs::write(&path, "sk-test\n").unwrap(); std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap(); assert!(load_api_key(&path).is_err()); std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap(); assert_eq!(load_api_key(&path).unwrap().expose(), "sk-test"); } #[test] fn api_key_content_must_be_safe_for_a_curl_config() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("key"); let write = |value: &str| { std::fs::write(&path, value).unwrap(); std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap(); }; // A quote breaks out of the `header = "..."` directive; a backslash or a // control character could inject another curl option. for unsafe_key in [ "sk-ok\"\nheader = \"X-Injected: yes", "sk-ok\\nheader = \"X-Injected: yes", "sk-\nsecret", "sk-\tsecret", "sk-\rsecret", ] { write(unsafe_key); assert!( load_api_key(&path).is_err(), "unsafe credential must be refused: {unsafe_key:?}" ); } // A realistic token is still accepted. write("sk-abc_123-XYZ.="); assert_eq!(load_api_key(&path).unwrap().expose(), "sk-abc_123-XYZ.="); } #[test] fn curl_transport_keeps_the_key_out_of_arguments_and_logs() { let runner = RecordingRunner::new(); runner.set_response( "curl", CommandOutput { status: 0, success: true, stdout: br#"{"choices":[{"message":{"content":"{}"}}]}"#.to_vec(), stderr: Vec::new(), duration: Duration::from_millis(1), }, ); let exec = make_exec(runner.clone()); let transport = CurlTransport::new(&exec, Some(SecretString::new("sk-secret-value"))); let out = transport .send( ProviderKind::OpenAi, "https://example.invalid/v1/chat/completions", r#"{"model":"m"}"#, ) .unwrap(); assert!(out.contains("choices")); let specs = runner.recorded(); let spec = &specs[0]; assert_eq!(spec.program, "curl"); assert!( !spec.args.iter().any(|a| a.contains("sk-secret-value")), "the key must not be in argv: {:?}", spec.args ); let stdin = String::from_utf8(spec.stdin.clone().unwrap()).unwrap(); assert!(stdin.contains("Authorization: Bearer sk-secret-value")); // And the key is registered for redaction. assert_eq!(exec.redactor().redact("sk-secret-value"), ""); } #[test] fn sandboxed_send_uses_systemd_run_and_keeps_the_key_on_stdin() { if !crate::sandbox::available() { eprintln!("systemd-run unavailable; sandboxed transport path not exercised here"); return; } let runner = RecordingRunner::new(); runner.set_response( "systemd-run", CommandOutput { status: 0, success: true, stdout: b"{}".to_vec(), stderr: Vec::new(), duration: Duration::from_millis(1), }, ); let exec = make_exec(runner.clone()); let transport = CurlTransport::new(&exec, Some(SecretString::new("sk-secret-value"))).sandboxed(); transport .send( ProviderKind::OpenAi, "https://example.invalid/v1/chat/completions", r#"{"model":"m"}"#, ) .unwrap(); let specs = runner.recorded(); let spec = &specs[0]; assert_eq!(spec.program, "systemd-run"); assert!( spec.args.iter().any(|a| a.contains("DynamicUser=yes")), "the sandbox properties must be present: {:?}", spec.args ); assert!( !spec.args.iter().any(|a| a.contains("sk-secret-value")), "the key must not be in argv: {:?}", spec.args ); let stdin = String::from_utf8(spec.stdin.clone().unwrap()).unwrap(); assert!(stdin.contains("Authorization: Bearer sk-secret-value")); assert!(stdin.contains("data = ")); } #[test] fn curl_transport_refuses_an_unsafe_endpoint() { let runner = RecordingRunner::new(); let exec = make_exec(runner.clone()); let transport = CurlTransport::new(&exec, None); for bad in [ "file:///etc/passwd", "gopher://127.0.0.1:70/", "http://example.invalid/v1\"; header = \"X: y", "https://example.invalid/v1\nheader = \"X: y\"", ] { let err = transport .send(ProviderKind::Local, bad, "{}") .expect_err("unsafe endpoint must be refused"); assert!( matches!(err, AiError::Provider(_)), "unexpected error for {bad}: {err:?}" ); } assert!( runner.recorded().is_empty(), "curl must not run for a refused URL" ); } #[test] fn curl_transport_bounds_the_response_and_disables_redirects() { let runner = RecordingRunner::new(); runner.set_response( "curl", CommandOutput { status: 0, success: true, stdout: b"{}".to_vec(), stderr: Vec::new(), duration: Duration::from_millis(1), }, ); let exec = make_exec(runner.clone()); let transport = CurlTransport::new(&exec, None); transport .send( ProviderKind::Local, "http://127.0.0.1:11434/v1/chat/completions", "{}", ) .unwrap(); let stdin = String::from_utf8(runner.recorded()[0].stdin.clone().unwrap()).unwrap(); assert!(stdin.contains("max-filesize"), "{stdin}"); assert!(stdin.contains("proto = \"=http,https\""), "{stdin}"); assert!( !stdin.contains("location"), "redirects must stay disabled: {stdin}" ); } /// Save, set, and restore PHOEBUS_AI_TIMEOUT around a check. Only this /// module's tests touch the variable, and no other test asserts a /// timeout value, so parallel execution stays deterministic. fn with_ai_timeout(var: Option<&str>, check: impl FnOnce()) { let prev = std::env::var("PHOEBUS_AI_TIMEOUT").ok(); match var { Some(v) => std::env::set_var("PHOEBUS_AI_TIMEOUT", v), None => std::env::remove_var("PHOEBUS_AI_TIMEOUT"), } check(); match prev { Some(v) => std::env::set_var("PHOEBUS_AI_TIMEOUT", v), None => std::env::remove_var("PHOEBUS_AI_TIMEOUT"), } } #[test] fn ai_timeout_defaults_to_300s_and_rejects_bad_values() { with_ai_timeout(None, || assert_eq!(ai_timeout_secs(), 300)); with_ai_timeout(Some("600"), || assert_eq!(ai_timeout_secs(), 600)); with_ai_timeout(Some(" 90 "), || assert_eq!(ai_timeout_secs(), 90)); // Zero would mean "no timeout" to curl; garbage and empty fall back. for bad in ["0", "", "abc", "-5", "12.5"] { with_ai_timeout(Some(bad), || { assert_eq!(ai_timeout_secs(), 300, "bad value {bad:?}") }); } } #[test] fn curl_transport_uses_the_configured_timeout() { let runner = RecordingRunner::new(); runner.set_response( "curl", CommandOutput { status: 0, success: true, stdout: b"{}".to_vec(), stderr: Vec::new(), duration: Duration::from_millis(1), }, ); let exec = make_exec(runner.clone()); with_ai_timeout(Some("600"), || { let transport = CurlTransport::new(&exec, None); transport .send( ProviderKind::Local, "http://127.0.0.1:11434/v1/chat/completions", "{}", ) .unwrap(); }); let stdin = String::from_utf8(runner.recorded()[0].stdin.clone().unwrap()).unwrap(); assert!(stdin.contains("max-time = 600"), "{stdin}"); // And the default covers slow local CPU inference (was 120 s). with_ai_timeout(None, || { let transport = CurlTransport::new(&exec, None); transport .send( ProviderKind::Local, "http://127.0.0.1:11434/v1/chat/completions", "{}", ) .unwrap(); }); let stdin = String::from_utf8(runner.recorded()[1].stdin.clone().unwrap()).unwrap(); assert!(stdin.contains("max-time = 300"), "{stdin}"); } #[test] fn curl_transport_omits_auth_without_a_key() { let runner = RecordingRunner::new(); runner.set_response( "curl", CommandOutput { status: 0, success: true, stdout: b"{}".to_vec(), stderr: Vec::new(), duration: Duration::from_millis(1), }, ); let exec = make_exec(runner.clone()); let transport = CurlTransport::new(&exec, None); transport .send( ProviderKind::Local, "http://127.0.0.1:11434/v1/chat/completions", "{}", ) .unwrap(); let spec = &runner.recorded()[0]; let stdin = String::from_utf8(spec.stdin.clone().unwrap()).unwrap(); assert!(!stdin.contains("Authorization")); assert!(stdin.contains("http://127.0.0.1")); } }