//! The daemon over TLS (spec §8.6, spectra#20): a certificate of its own, //! vouched for by the instance key; a client that trusts that, and only //! that; signed MCP calls over the network; no uid-bound agent over TCP. mod common; use common::{add_agent, run, setup, text, Serving}; use serde_json::json; use spectra_core::keys::KeyPair; use spectra_mcp::client::{rpc_result, Client, Endpoint}; use spectra_mcp::tls::Pinned; fn unpinned(port: u16) -> (Client, std::sync::Arc) { let v = Pinned::new(None); let e = Endpoint::Tls { addr: format!("127.0.0.1:{port}"), config: v.client_config().unwrap() }; (Client::at(e, None), v) } #[tokio::test] async fn a_client_trusts_the_certificate_the_instance_key_signed() { let dir = setup(); let d = dir.path(); let agent = add_agent(d, "claude-oxygen", &[]); let (srv, port) = Serving::start_tls(d); // Unauthenticated, before trusting anything: who is this? let (c, seen) = unpinned(port); let r = c.send("GET", "/api/v1/instance", &[], vec![]).await.unwrap(); assert_eq!(r.status, 200, "{}", String::from_utf8_lossy(&r.body)); let answer = r.json().unwrap(); let (_, pin) = seen.seen().expect("the handshake showed a certificate"); let checked = spectra_mcp::instance::check(&answer, &pin).unwrap(); let instance = KeyPair::instance(&d.join("data"), "spectra").unwrap(); assert_eq!(checked.instance.hash(), instance.key, "the instance key vouches for the certificate"); let owner = KeyPair::load_home(&d.join("owner")).unwrap(); assert_eq!(checked.owner.as_deref(), Some(owner.key.to_hex().as_str())); // A certificate the instance did not sign is not accepted. assert!(spectra_mcp::instance::check(&answer, &[7; 32]).is_err()); // Pinned, an agent's signed MCP call works over the network. let c = Client::at(Endpoint::tls(&format!("https://127.0.0.1:{port}"), pin).unwrap(), Some(agent)); let call = json!({ "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": "agents", "arguments": {}, "_meta": { "io.modelcontextprotocol/protocolVersion": "2026-07-28" } } }); let r = c.mcp("2026-07-28", &call).await.unwrap(); let v = rpc_result(&r).unwrap(); assert_eq!(v["result"]["isError"], false, "{v}"); assert!(v["result"]["content"][0]["text"].as_str().unwrap().contains("claude-oxygen · Andy McCall's agent")); // Any other certificate is refused at the handshake. let wrong = Client::at(Endpoint::tls(&format!("https://127.0.0.1:{port}"), [0; 32]).unwrap(), None); assert!(wrong.send("GET", "/api/v1/instance", &[], vec![]).await.is_err()); srv.stop(); // The certificate is kept, and recorded once, across restarts; verify // checks the instance's signature on it. let (srv, port2) = Serving::start_tls(d); let (c, seen) = unpinned(port2); c.send("GET", "/api/v1/instance", &[], vec![]).await.unwrap(); assert_eq!(seen.seen().unwrap().1, pin); srv.stop(); let o = run(d, &["admin", "verify"]); assert!(o.status.success(), "{}", text(&o)); } #[tokio::test] async fn an_agent_bound_to_a_uid_is_refused_over_the_network() { let dir = setup(); let d = dir.path(); let uid = unsafe { libc::getuid() }.to_string(); let bound = add_agent(d, "bound", &["--uid", &uid]); let (srv, port) = Serving::start_tls(d); let (probe, seen) = unpinned(port); probe.send("GET", "/api/v1/instance", &[], vec![]).await.unwrap(); let pin = seen.seen().unwrap().1; let c = Client::at(Endpoint::tls(&format!("https://127.0.0.1:{port}"), pin).unwrap(), Some(bound.clone())); let r = c.mcp("2026-07-28", &json!({ "jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {} })).await.unwrap(); assert_eq!(r.status, 401); assert!(String::from_utf8_lossy(&r.body).contains("must come over the Unix socket"), "{}", String::from_utf8_lossy(&r.body)); // Over the socket, from its own uid, it is welcome. let local = Client::new(&srv.socket, Some(bound)); let r = local.mcp("2026-07-28", &json!({ "jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {} })).await.unwrap(); assert_eq!(r.status, 200); srv.stop(); let o = run(d, &["admin", "verify"]); assert!(o.status.success(), "{}", text(&o)); let log = spectra_core::ledger::Ledger::open_read_only(&d.join("data/spectra.db")).await.unwrap(); let g = log.events_of_kinds(1, &["guard.impersonation"], 10).await.unwrap(); assert_eq!(g.len(), 1, "recorded as an impersonation attempt"); } #[test] fn plain_http_beyond_loopback_is_refused() { let dir = setup(); let o = run(dir.path(), &["serve", "--bind", "0.0.0.0:0", "--tls", "off"]); assert!(!o.status.success()); assert!(text(&o).contains("over TLS only"), "{}", text(&o)); }