From 8912f71afb9697c8645209ec0e67f67098bb16a1 Mon Sep 17 00:00:00 2001 From: aur-build Date: Thu, 19 Nov 2026 15:15:53 +0000 Subject: config: the sandbox backend is an enum, not a string with a fallthrough MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `sandbox_backend` was a free-form string and `backend_for` matched it with `_ => loopback`. So `sandbox_backend = "kata"` — a real, intended backend, documented in the design — meant "run the untrusted PKGBUILD as the hub's own user", and nothing anywhere said so. There was also no validation of any kind in the crate: an empty `agent_token` with a remote backend, or a missing listen address, parsed fine and failed later as something else. The four flat fields become one tagged `sandbox` object with three kinds: `loopback` (needs nothing, still the default, so every config on disk keeps working), `remote` (listen + token), and `agent-sandbox` (api, namespace, bearer, image, listen, token). Required fields are required by the type, an unknown kind is a parse error naming the valid set, and `deny_unknown_fields` means a misspelled field is named rather than read as absent. `agent-sandbox` parses and is refused at backend construction until it is wired up — a valid kind that is not implemented is an error, never a substitution. Retired keys are refused when they carry intent: a config saying `sandbox_backend: "remote"` errors and names the replacement shape, because quietly reinterpreting it as loopback is the bug being removed. A config saying `"loopback"` with the empty strings `init` wrote is noise, not intent, and loads unchanged. The token and the cluster bearer are redacted in `Debug`, so a config that reaches a log line cannot leak them. --- tests/config.rs | 190 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) (limited to 'tests/config.rs') diff --git a/tests/config.rs b/tests/config.rs index c62c4c9..30b687b 100644 --- a/tests/config.rs +++ b/tests/config.rs @@ -132,3 +132,193 @@ fn an_absolute_root_is_used_verbatim() { .unwrap(); assert_eq!(Config::load(dir).unwrap().root, dir); } + +// --------------------------------------------------------------------------- +// The sandbox configuration: three kinds, and only three. +// +// This used to be `sandbox_backend: String` matched with a `_ =>` arm that +// produced loopback, so `"kata"` — a real, intended backend — silently meant +// "run the untrusted PKGBUILD as the hub's own user". The kinds are an enum +// now, so the invalid configurations do not parse, and the error says which +// field or which value was wrong. +// --------------------------------------------------------------------------- + +/// Write a config carrying exactly this `sandbox` value and load it. +fn load_sandbox(sandbox: &str) -> Result { + let tmp = tempfile::tempdir().unwrap(); + std::fs::write( + tmp.path().join("config.json"), + format!("{{\n \"sandbox\": {sandbox}\n}}\n"), + ) + .unwrap(); + Config::load(tmp.path()).map_err(|e| e.to_string()) +} + +/// Load a config written as an older `aur-build init` would have written it. +fn load_raw(body: &str) -> Result { + let tmp = tempfile::tempdir().unwrap(); + std::fs::write(tmp.path().join("config.json"), body).unwrap(); + Config::load(tmp.path()).map_err(|e| e.to_string()) +} + +#[test] +fn each_sandbox_kind_parses_with_its_required_fields() { + match load_sandbox(r#"{"kind":"loopback"}"#).unwrap().sandbox { + aur_build::config::Sandbox::Loopback => {} + other => panic!("expected loopback, got {other:?}"), + } + + let remote = load_sandbox( + r#"{"kind":"remote","listen":"127.0.0.1:8787","token":"s3cr3t"}"#, + ) + .unwrap(); + match &remote.sandbox { + aur_build::config::Sandbox::Remote { listen, token, shared_workspace } => { + assert_eq!(listen, "127.0.0.1:8787"); + assert_eq!(token, "s3cr3t"); + assert!(!*shared_workspace, "sharing the filesystem is asserted, never assumed"); + } + other => panic!("expected remote, got {other:?}"), + } + + let abs = load_sandbox( + r#"{"kind":"agent-sandbox","api":"https://k8s:6443","namespace":"builds", + "bearer":"kube-token","image":"aur-build-sandbox:latest", + "listen":"0.0.0.0:8787","token":"s3cr3t","shared_workspace":true}"#, + ) + .unwrap(); + match &abs.sandbox { + aur_build::config::Sandbox::AgentSandbox { api, namespace, bearer, image, .. } => { + assert_eq!(api, "https://k8s:6443"); + assert_eq!(namespace, "builds"); + assert_eq!(bearer, "kube-token"); + assert_eq!(image, "aur-build-sandbox:latest"); + } + other => panic!("expected agent-sandbox, got {other:?}"), + } + assert_eq!(abs.sandbox_image(), "aur-build-sandbox:latest"); +} + +#[test] +fn a_missing_required_field_names_the_field() { + let err = load_sandbox(r#"{"kind":"remote","listen":"127.0.0.1:8787"}"#).unwrap_err(); + assert!(err.contains("token"), "the error must name the missing field: {err}"); + + let err = load_sandbox(r#"{"kind":"remote","token":"t"}"#).unwrap_err(); + assert!(err.contains("listen"), "the error must name the missing field: {err}"); + + for missing in ["api", "namespace", "bearer", "image", "listen", "token"] { + let mut fields = vec![ + r#""api":"https://k8s:6443""#.to_string(), + r#""namespace":"builds""#.to_string(), + r#""bearer":"kube""#.to_string(), + r#""image":"img""#.to_string(), + r#""listen":"0.0.0.0:1""#.to_string(), + r#""token":"t""#.to_string(), + ]; + fields.retain(|f| !f.starts_with(&format!("\"{missing}\""))); + let err = load_sandbox(&format!("{{\"kind\":\"agent-sandbox\",{}}}", fields.join(","))) + .unwrap_err(); + assert!(err.contains(missing), "dropping {missing} must name it: {err}"); + } +} + +#[test] +fn a_misspelled_field_is_refused_not_ignored() { + // deny_unknown_fields: `listn` must not parse as "no listen address given". + let err = load_sandbox(r#"{"kind":"remote","listn":"127.0.0.1:1","token":"t"}"#).unwrap_err(); + assert!(err.contains("listn"), "a typo'd field name must be named: {err}"); +} + +#[test] +fn an_unknown_kind_names_the_value_and_the_valid_set() { + // The exact mistake this enum exists to catch: `kata` was a real intent, + // and the old match sent it quietly to loopback. + let err = load_sandbox(r#"{"kind":"kata"}"#).unwrap_err(); + assert!(err.contains("kata"), "must name the bad value: {err}"); + for kind in aur_build::config::Sandbox::KINDS { + assert!(err.contains(kind), "must name the valid set ({kind} missing): {err}"); + } +} + +#[test] +fn a_config_with_no_sandbox_object_is_the_local_backend() { + // The contract with every config.json already on disk: saying nothing means + // loopback, exactly as before. + let cfg = load_raw("{\n \"repo_name\": \"aur\"\n}\n").unwrap(); + assert!(matches!(cfg.sandbox, aur_build::config::Sandbox::Loopback)); + assert_eq!(cfg.repo_name, "aur", "the rest of the config is untouched"); + assert_eq!(cfg.sandbox_image(), aur_build::config::DEFAULT_SANDBOX_IMAGE); + assert!(cfg.sandbox.shared_workspace(), "loopback shares the filesystem by construction"); +} + +/// The file an older `aur-build init` wrote, verbatim. It carries the retired +/// keys, but they say `loopback` and three empty strings — which is what the +/// default already means — so it must still load rather than strand a working +/// local deployment on a syntax change. +#[test] +fn a_config_init_wrote_before_the_enum_still_loads() { + let cfg = load_raw( + r#"{ + "root": "/var/lib/aur-build", + "repo_name": "aur", + "arch": "x86_64", + "signing_key_id": "", + "bind": "127.0.0.1:8484", + "password": "", + "base_url": "http://127.0.0.1:8484", + "build_profile": "extra-x86_64", + "git_bin": "git", + "makepkg_bin": "makepkg", + "pacman_bin": "pacman", + "repo_add_bin": "repo-add", + "gpg_bin": "gpg", + "build_user": "", + "sandbox_backend": "loopback", + "agent_listen": "", + "agent_token": "", + "sandbox_image": "aur-build-sandbox:latest", + "max_concurrent_builds": 1, + "lease_secs": 90, + "min_free_disk_mb": 1024, + "max_transient_attempts": 3, + "dispatch_tick_secs": 2 +}"#, + ) + .unwrap(); + assert!(matches!(cfg.sandbox, aur_build::config::Sandbox::Loopback)); + assert_eq!(cfg.lease_secs, 90); +} + +/// …but retired keys that *would* have changed behaviour are refused, not +/// quietly reinterpreted. A retired `"remote"` becoming loopback is the exact +/// silent substitution the enum was introduced to make impossible. +#[test] +fn retired_sandbox_keys_that_mean_something_are_refused() { + let err = load_raw(r#"{ "sandbox_backend": "remote", "agent_listen": "0.0.0.0:1", "agent_token": "t" }"#) + .unwrap_err(); + assert!(err.contains("sandbox_backend"), "must name the retired key: {err}"); + assert!(err.contains("sandbox"), "must point at the replacement: {err}"); + + // Even one retired key with content is enough to refuse. + let err = load_raw(r#"{ "sandbox_backend": "kata" }"#).unwrap_err(); + assert!(err.contains("sandbox_backend"), "{err}"); + + // An empty retired key is noise, not intent, and stays tolerated. + assert!(load_raw(r#"{ "sandbox_backend": "loopback", "agent_token": "" }"#).is_ok()); +} + +#[test] +fn the_rendezvous_secrets_never_reach_debug() { + // A config that ends up in a log line, a panic, or an audit payload must + // not carry the token or the cluster bearer. + let cfg = load_sandbox( + r#"{"kind":"agent-sandbox","api":"https://k8s:6443","namespace":"builds", + "bearer":"kube-token","image":"img","listen":"0.0.0.0:1","token":"s3cr3t"}"#, + ) + .unwrap(); + let shown = format!("{cfg:?}"); + assert!(!shown.contains("s3cr3t"), "the rendezvous token leaked into Debug"); + assert!(!shown.contains("kube-token"), "the cluster bearer leaked into Debug"); + assert!(shown.contains("builds"), "the non-secret fields are still visible: {shown}"); +} -- cgit v1.2.3