From 1f5e4a59a0e645df9efb112f9683ffce5e65e02d Mon Sep 17 00:00:00 2001 From: mnovak Date: Tue, 10 Nov 2026 21:04:59 +0100 Subject: [PATCH] [control] UI socket, tests first: with [ui] socket the proof signs in only over the 0660 socket (refused over TCP even when right), and a route may name a socket only for the admin host and only control's own (red: no socket yet) --- crates/control/tests/ui_auth.rs | 147 ++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/crates/control/tests/ui_auth.rs b/crates/control/tests/ui_auth.rs index f9794ef..17a6979 100644 --- a/crates/control/tests/ui_auth.rs +++ b/crates/control/tests/ui_auth.rs @@ -238,6 +238,153 @@ mod unix { ); } + // ---------------------------------------------------------------- G14 option b (phase 4) + + /// Control with `[ui] socket`: the UI comes in on a Unix socket in a directory only the + /// ianitor group can use; the loopback port stays for API tokens (scripts, the CLI). + async fn socket_env(site: String, ui_host: &str) -> (Env, std::path::PathBuf) { + let mut env = Env::new(EnvSpec { + site, + ..Default::default() + }) + .await; + std::fs::write( + env.dir.join("creds/ui-proof"), + format!( + "{PROOF} +" + ), + ) + .unwrap(); + let sock = env.dir.join("ui.sock"); + let text = std::fs::read_to_string(env.config_path()).unwrap() + + &format!( + " +[ui] +hostname = \"{ui_host}\" +proof_credential = \"ui-proof\" +socket = \"{}\" +", + sock.display() + ); + std::fs::write(env.config_path(), text).unwrap(); + env.start_control(None); + for _ in 0..100 { + if sock.exists() { + break; + } + tokio::time::sleep(std::time::Duration::from_millis(50)).await; + } + assert!( + sock.exists(), + "no UI socket: +{}", + env.control_log() + ); + (env, sock) + } + + async fn over_socket(sock: &std::path::Path, headers: &[(&str, &str)]) -> (u16, String) { + let mut req = Req::get("ui.test", "/api/v1/overview"); + for (n, v) in headers { + req = req.header(n, v); + } + let r = client::unix(sock, &req).await.unwrap(); + (r.status, r.text()) + } + + /// The per-boot proof alone no longer signs anyone in: off the socket it is refused even + /// when it is right, so a local process that learned it (say, by listening on control's + /// port while control restarted) gets nothing; on the socket, which only edge and Caddy + /// (group ianitor) can open, it works as before. + #[tokio::test(flavor = "multi_thread")] + async fn with_a_ui_socket_the_proof_counts_only_on_the_socket() { + use std::os::unix::fs::PermissionsExt; + let (env, sock) = socket_env(String::new(), "ui.test").await; + let mode = std::fs::metadata(&sock).unwrap().permissions().mode() & 0o777; + assert_eq!(mode, 0o660, "owner and group only"); + + let signed = [("Remote-User", "mark"), ("X-Ianitor-Proof", PROOF)]; + let r = env + .call_with("GET", "/api/v1/overview", &signed, None) + .await; + assert_eq!(r.status, 401, "the right proof over TCP: {}", r.text); + assert_eq!(r.json["error"]["code"], "ui.no_proof", "{}", r.text); + assert!(!env.control_log().contains("by mark")); + + let (status, text) = over_socket(&sock, &signed).await; + assert_eq!(status, 200, "{text}"); + // The socket does not replace the proof: without it, a Remote-User is still refused. + let (status, text) = over_socket(&sock, &[("Remote-User", "mark")]).await; + assert_eq!(status, 401, "{text}"); + let (status, text) = over_socket( + &sock, + &[("Remote-User", "mark"), ("X-Ianitor-Proof", "guess")], + ) + .await; + assert_eq!(status, 401, "{text}"); + + // API tokens keep working on the loopback port. + let r = env.call("GET", "/api/v1/overview", None).await; + assert_eq!(r.status, 200, "{}", r.text); + } + + /// A route file may name a socket only for the admin host, and only control's own. + #[tokio::test(flavor = "multi_thread")] + async fn a_socket_upstream_is_only_for_the_admin_host_and_controls_own_socket() { + let auth = MockAuthelia::start().await; + let site = format!( + "secrets = [\"ui-proof\"] +[forward_auth] +url = \"{}\" +", + auth.url() + ); + let (env, sock) = socket_env(site, "a.test").await; + for (hostname, upstream) in [ + ("b.test", format!("unix:{}", sock.display())), + ("a.test", "unix:/run/docker.sock".to_owned()), + ] { + let host = json!({"host": { + "hostname": hostname, "upstreams": [upstream], + "tls": "domi", "auth": "forward-auth", + }}); + let r = env.call("POST", "/api/v1/hosts", Some(host)).await; + assert_eq!(r.status, 422, "{hostname} {upstream}: {}", r.text); + assert_eq!( + r.json["errors"][0]["code"], "host.upstream.unix_socket", + "{}", + r.text + ); + } + + // The admin host on control's socket: through edge, Authelia decides, the UI gets in. + let host = json!({"host": { + "hostname": "a.test", "upstreams": [format!("unix:{}", sock.display())], + "tls": "domi", "auth": "forward-auth", + "request_headers": [{"action": "set", "name": "X-Ianitor-Proof", "secret": "ui-proof"}] + }}); + let rec = env.apply("POST", "/api/v1/hosts", Some(host)).await; + assert_eq!( + rec["state"], + "kept", + "{rec} +{}", + env.control_log() + ); + let edge = env.edge.as_ref().unwrap(); + let r = client::https( + edge.https, + "a.test", + env.ca.roots(), + &Req::get("a.test", "/api/v1/overview").header("cookie", "authelia_session=good"), + ) + .await + .unwrap(); + assert_eq!(r.status, 200, "{}", r.text()); + assert!(r.text().contains("\"hosts\""), "{}", r.text()); + } + /// QC: signed in at Authelia (alice) but the client also sends a proof guess: edge must /// replace, not append, the proof; and Remote-User must be Authelia's. #[tokio::test(flavor = "multi_thread")]