OXYGENHUB
◆ v0.1.0 spectra / tests / tls.rs
4.7 KiB · Rust · last changed Serve other machines over TLS, trusted through the instance key (#20) 1 day ago source blame history raw
1
//! The daemon over TLS (spec §8.6, spectra#20): a certificate of its own,
2
//! vouched for by the instance key; a client that trusts that, and only
3
//! that; signed MCP calls over the network; no uid-bound agent over TCP.
4
5
mod common;
6
7
use common::{add_agent, run, setup, text, Serving};
8
use serde_json::json;
9
use spectra_core::keys::KeyPair;
10
use spectra_mcp::client::{rpc_result, Client, Endpoint};
11
use spectra_mcp::tls::Pinned;
12
13
fn unpinned(port: u16) -> (Client, std::sync::Arc<Pinned>) {
14
    let v = Pinned::new(None);
15
    let e = Endpoint::Tls { addr: format!("127.0.0.1:{port}"), config: v.client_config().unwrap() };
16
    (Client::at(e, None), v)
17
}
18
19
#[tokio::test]
20
async fn a_client_trusts_the_certificate_the_instance_key_signed() {
21
    let dir = setup();
22
    let d = dir.path();
23
    let agent = add_agent(d, "claude-oxygen", &[]);
24
    let (srv, port) = Serving::start_tls(d);
25
26
    // Unauthenticated, before trusting anything: who is this?
27
    let (c, seen) = unpinned(port);
28
    let r = c.send("GET", "/api/v1/instance", &[], vec![]).await.unwrap();
29
    assert_eq!(r.status, 200, "{}", String::from_utf8_lossy(&r.body));
30
    let answer = r.json().unwrap();
31
    let (_, pin) = seen.seen().expect("the handshake showed a certificate");
32
    let checked = spectra_mcp::instance::check(&answer, &pin).unwrap();
33
    let instance = KeyPair::instance(&d.join("data"), "spectra").unwrap();
34
    assert_eq!(checked.instance.hash(), instance.key, "the instance key vouches for the certificate");
35
    let owner = KeyPair::load_home(&d.join("owner")).unwrap();
36
    assert_eq!(checked.owner.as_deref(), Some(owner.key.to_hex().as_str()));
37
38
    // A certificate the instance did not sign is not accepted.
39
    assert!(spectra_mcp::instance::check(&answer, &[7; 32]).is_err());
40
41
    // Pinned, an agent's signed MCP call works over the network.
42
    let c = Client::at(Endpoint::tls(&format!("https://127.0.0.1:{port}"), pin).unwrap(), Some(agent));
43
    let call = json!({ "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": "agents", "arguments": {},
44
        "_meta": { "io.modelcontextprotocol/protocolVersion": "2026-07-28" } } });
45
    let r = c.mcp("2026-07-28", &call).await.unwrap();
46
    let v = rpc_result(&r).unwrap();
47
    assert_eq!(v["result"]["isError"], false, "{v}");
48
    assert!(v["result"]["content"][0]["text"].as_str().unwrap().contains("claude-oxygen · Andy McCall's agent"));
49
50
    // Any other certificate is refused at the handshake.
51
    let wrong = Client::at(Endpoint::tls(&format!("https://127.0.0.1:{port}"), [0; 32]).unwrap(), None);
52
    assert!(wrong.send("GET", "/api/v1/instance", &[], vec![]).await.is_err());
53
    srv.stop();
54
55
    // The certificate is kept, and recorded once, across restarts; verify
56
    // checks the instance's signature on it.
57
    let (srv, port2) = Serving::start_tls(d);
58
    let (c, seen) = unpinned(port2);
59
    c.send("GET", "/api/v1/instance", &[], vec![]).await.unwrap();
60
    assert_eq!(seen.seen().unwrap().1, pin);
61
    srv.stop();
62
    let o = run(d, &["admin", "verify"]);
63
    assert!(o.status.success(), "{}", text(&o));
64
}
65
66
#[tokio::test]
67
async fn an_agent_bound_to_a_uid_is_refused_over_the_network() {
68
    let dir = setup();
69
    let d = dir.path();
70
    let uid = unsafe { libc::getuid() }.to_string();
71
    let bound = add_agent(d, "bound", &["--uid", &uid]);
72
    let (srv, port) = Serving::start_tls(d);
73
    let (probe, seen) = unpinned(port);
74
    probe.send("GET", "/api/v1/instance", &[], vec![]).await.unwrap();
75
    let pin = seen.seen().unwrap().1;
76
    let c = Client::at(Endpoint::tls(&format!("https://127.0.0.1:{port}"), pin).unwrap(), Some(bound.clone()));
77
    let r = c.mcp("2026-07-28", &json!({ "jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {} })).await.unwrap();
78
    assert_eq!(r.status, 401);
79
    assert!(String::from_utf8_lossy(&r.body).contains("must come over the Unix socket"), "{}", String::from_utf8_lossy(&r.body));
80
    // Over the socket, from its own uid, it is welcome.
81
    let local = Client::new(&srv.socket, Some(bound));
82
    let r = local.mcp("2026-07-28", &json!({ "jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {} })).await.unwrap();
83
    assert_eq!(r.status, 200);
84
    srv.stop();
85
    let o = run(d, &["admin", "verify"]);
86
    assert!(o.status.success(), "{}", text(&o));
87
    let log = spectra_core::ledger::Ledger::open_read_only(&d.join("data/spectra.db")).await.unwrap();
88
    let g = log.events_of_kinds(1, &["guard.impersonation"], 10).await.unwrap();
89
    assert_eq!(g.len(), 1, "recorded as an impersonation attempt");
90
}
91
92
#[test]
93
fn plain_http_beyond_loopback_is_refused() {
94
    let dir = setup();
95
    let o = run(dir.path(), &["serve", "--bind", "0.0.0.0:0", "--tls", "off"]);
96
    assert!(!o.status.success());
97
    assert!(text(&o).contains("over TLS only"), "{}", text(&o));
98
}