// SPDX-License-Identifier: CPAL-1.0 // Copyright (c) 2026 Marek Novák. Part of Ianitor, under the Common Public Attribution // License 1.0: see LICENSE (its Exhibit A is this file's notice). //! Unix-socket upstreams (G14 option b, tests first): the admin host reaches control on a //! Unix socket in a directory only the ianitor group can use, so no local process can take //! control's place while it restarts. Route files are hostile input: the path is strict, and //! which socket a host may use is control's decision (its `[ui] socket`), not core's. #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] use ianitor_core::compile::{self, CompileOptions}; use ianitor_core::edge_config::EdgeConfig; use ianitor_core::{Model, SourceFile, ValidationError, load}; const SOCKET: &str = "/run/ianitor-control/ui.sock"; fn load_host(upstreams: &str) -> Result> { let body = format!("hostname = \"ui.test\"\ntls = \"domi\"\nupstreams = [{upstreams}]\n"); load( &SourceFile { name: "ianitor.toml", contents: "", }, &[SourceFile { name: "hosts/ui.test.toml", contents: &body, }], ) .map_err(ianitor_core::ValidationErrors::into_errors) } fn options() -> CompileOptions { CompileOptions { http_listen: "0.0.0.0:80".parse().unwrap(), https_listen: "0.0.0.0:443".parse().unwrap(), cert_dir: "/var/lib/ianitor/certs".into(), state_dir: "/var/lib/ianitor".into(), access_log: "/var/log/ianitor/access.log".into(), caddy_credentials_dir: "/run/credentials/caddy-fallback.service".into(), caddy_admin: None, caddy_admin_socket: None, trusted_proxies: Vec::new(), proxy_socket: None, } } #[test] fn a_unix_socket_upstream_reaches_both_engines() { let model = load_host(&format!("\"unix:{SOCKET}\"")).unwrap(); let up = &model.hosts()[0].upstreams[0]; assert!(up.is_unix()); assert_eq!(up.to_string(), format!("unix:{SOCKET}")); let edge: EdgeConfig = serde_json::from_str(&compile::edge(&model, &options()).unwrap()).unwrap(); let u = &edge.hosts[0].upstreams[0]; assert_eq!(u.address, format!("unix:{SOCKET}")); assert!(!u.tls); let caddy: serde_json::Value = serde_json::from_str(&compile::caddy(&model, &options()).unwrap()).unwrap(); let text = caddy.to_string(); assert!( text.contains("\"dial\":\"unix//run/ianitor-control/ui.sock\""), "{text}" ); } #[test] fn a_unix_socket_path_is_strict() { let long = format!("unix:/run/{}.sock", "a".repeat(100)); for bad in [ "unix:relative.sock", "unix:", "unix:/", "unix:/run/", "unix://run/x.sock", "unix:/run//x.sock", "unix:/run/../etc/x.sock", "unix:/run/./x.sock", "unix:/run/x y.sock", "unix:/run/{env.HOME}.sock", "unix:/run/x.sock?q=1", "unix:/run/x\\y.sock", "unix:/run/ü.sock", long.as_str(), ] { let errs = load_host(&format!("{:?}", bad)) .err() .unwrap_or_else(|| panic!("{bad} was accepted")); assert!( errs.iter().any(|e| e.code() == "host.upstream.unix_path"), "{bad}: {errs:?}" ); } } #[test] fn forward_auth_never_goes_to_a_socket() { let errs = load( &SourceFile { name: "ianitor.toml", contents: "[forward_auth]\nurl = \"unix:/run/authelia.sock\"\n", }, &[], ) .unwrap_err(); assert!(!errs.errors().is_empty()); } #[test] fn a_unix_upstream_and_a_tcp_one_are_both_kept() { let model = load_host(&format!("\"unix:{SOCKET}\", \"http://127.0.0.1:7878\"")).unwrap(); assert_eq!(model.hosts()[0].upstreams.len(), 2); let dup = load_host(&format!("\"unix:{SOCKET}\", \"unix:{SOCKET}\"")).unwrap_err(); assert!(dup.iter().any(|e| e.code() == "host.upstream.duplicate")); }