|
| 1 | +//! Integration test: `aimx hooks list` filters output by caller euid. |
| 2 | +//! |
| 3 | +//! Reuses the `aimx-test-alice` / `aimx-test-bob` fixture also used by |
| 4 | +//! `tests/uds_authz.rs` and `tests/mailbox_isolation.rs`. Spins up a |
| 5 | +//! temp config with two mailboxes (one per test user, each carrying a |
| 6 | +//! hook) and runs `aimx hooks list` once as alice, once as bob, once as |
| 7 | +//! root via `runuser -u <user> -- env AIMX_CONFIG_DIR=… <binary> hooks list`. |
| 8 | +//! Asserts: |
| 9 | +//! |
| 10 | +//! - Alice sees only her hook; bob's hook is absent (no name leak). |
| 11 | +//! - Bob sees only his hook; alice's hook is absent. |
| 12 | +//! - Root sees both hooks. |
| 13 | +//! |
| 14 | +//! Coverage gap rationale: `src/mailbox.rs::caller_owns` is unit-tested, |
| 15 | +//! but no test wired the predicate end-to-end through `aimx hooks list`. |
| 16 | +//! A future refactor that bypassed the filter (e.g., dropped the |
| 17 | +//! `rows.retain(...)` call in `src/hooks.rs::list`) would have shipped |
| 18 | +//! silently. This test pins the wire-level behaviour. |
| 19 | +//! |
| 20 | +//! Gated on both `#[ignore]` and `AIMX_INTEGRATION_SUDO=1` so it only |
| 21 | +//! runs inside a CI step that explicitly elevates with `sudo`. |
| 22 | +
|
| 23 | +#![cfg(unix)] |
| 24 | + |
| 25 | +use std::os::unix::fs::PermissionsExt; |
| 26 | +use std::path::{Path, PathBuf}; |
| 27 | +use std::process::{Command, Stdio}; |
| 28 | + |
| 29 | +const ALICE: &str = "aimx-test-alice"; |
| 30 | +const BOB: &str = "aimx-test-bob"; |
| 31 | + |
| 32 | +fn ensure_user(name: &str) { |
| 33 | + let already = Command::new("id") |
| 34 | + .arg(name) |
| 35 | + .stdout(Stdio::null()) |
| 36 | + .stderr(Stdio::null()) |
| 37 | + .status() |
| 38 | + .map(|s| s.success()) |
| 39 | + .unwrap_or(false); |
| 40 | + if already { |
| 41 | + return; |
| 42 | + } |
| 43 | + let status = Command::new("useradd") |
| 44 | + .arg("--system") |
| 45 | + .arg("--no-create-home") |
| 46 | + .arg("--shell") |
| 47 | + .arg("/usr/sbin/nologin") |
| 48 | + .arg(name) |
| 49 | + .status() |
| 50 | + .expect("failed to spawn useradd"); |
| 51 | + assert!(status.success(), "useradd failed for {name}"); |
| 52 | +} |
| 53 | + |
| 54 | +fn aimx_binary_path() -> PathBuf { |
| 55 | + PathBuf::from(env!("CARGO_BIN_EXE_aimx")) |
| 56 | +} |
| 57 | + |
| 58 | +fn rand_suffix() -> u64 { |
| 59 | + use std::time::{SystemTime, UNIX_EPOCH}; |
| 60 | + SystemTime::now() |
| 61 | + .duration_since(UNIX_EPOCH) |
| 62 | + .map(|d| d.as_nanos() as u64) |
| 63 | + .unwrap_or(0) |
| 64 | +} |
| 65 | + |
| 66 | +/// Build a tempdir containing a world-readable `config.toml` with two |
| 67 | +/// mailboxes (alice + bob), each carrying one hook with a distinctive |
| 68 | +/// name we can assert against. Returns the tempdir root so the caller |
| 69 | +/// can pass it as `AIMX_CONFIG_DIR`. |
| 70 | +fn write_test_config() -> PathBuf { |
| 71 | + let tmp_root = std::env::temp_dir().join(format!( |
| 72 | + "aimx-hooks-list-filter-{}-{}", |
| 73 | + std::process::id(), |
| 74 | + rand_suffix() |
| 75 | + )); |
| 76 | + std::fs::create_dir_all(&tmp_root).unwrap(); |
| 77 | + std::fs::set_permissions(&tmp_root, PermissionsExt::from_mode(0o755)).unwrap(); |
| 78 | + |
| 79 | + let data_dir = tmp_root.join("data"); |
| 80 | + std::fs::create_dir_all(data_dir.join("inbox")).unwrap(); |
| 81 | + std::fs::create_dir_all(data_dir.join("sent")).unwrap(); |
| 82 | + std::fs::set_permissions(&data_dir, PermissionsExt::from_mode(0o755)).unwrap(); |
| 83 | + |
| 84 | + let config_content = format!( |
| 85 | + "domain = \"hooks-list-filter.example.com\"\n\ |
| 86 | + data_dir = \"{data_dir}\"\n\ |
| 87 | + dkim_selector = \"aimx\"\n\n\ |
| 88 | + [mailboxes.catchall]\n\ |
| 89 | + address = \"*@hooks-list-filter.example.com\"\n\ |
| 90 | + owner = \"aimx-catchall\"\n\n\ |
| 91 | + [mailboxes.{alice}]\n\ |
| 92 | + address = \"{alice}@hooks-list-filter.example.com\"\n\ |
| 93 | + owner = \"{alice}\"\n\n\ |
| 94 | + [[mailboxes.{alice}.hooks]]\n\ |
| 95 | + name = \"alice-hook-marker\"\n\ |
| 96 | + event = \"on_receive\"\n\ |
| 97 | + cmd = [\"/bin/true\"]\n\n\ |
| 98 | + [mailboxes.{bob}]\n\ |
| 99 | + address = \"{bob}@hooks-list-filter.example.com\"\n\ |
| 100 | + owner = \"{bob}\"\n\n\ |
| 101 | + [[mailboxes.{bob}.hooks]]\n\ |
| 102 | + name = \"bob-hook-marker\"\n\ |
| 103 | + event = \"on_receive\"\n\ |
| 104 | + cmd = [\"/bin/true\"]\n", |
| 105 | + data_dir = data_dir.display(), |
| 106 | + alice = ALICE, |
| 107 | + bob = BOB, |
| 108 | + ); |
| 109 | + let config_path = tmp_root.join("config.toml"); |
| 110 | + std::fs::write(&config_path, &config_content).unwrap(); |
| 111 | + // World-readable so the non-root test users can load it. Production |
| 112 | + // `/etc/aimx/config.toml` is `0640 root:root`; this test's tempdir |
| 113 | + // is opt-in to wider perms so the filter logic is reachable from |
| 114 | + // the runuser-invoked CLI. |
| 115 | + std::fs::set_permissions(&config_path, PermissionsExt::from_mode(0o644)).unwrap(); |
| 116 | + tmp_root |
| 117 | +} |
| 118 | + |
| 119 | +/// Copy the test binary out of `target/debug/` (whose ancestors aren't |
| 120 | +/// world-traversable on the GitHub Actions runner: `/home/runner/...`) |
| 121 | +/// into a `0755` location under the test's own tempdir. Without this, |
| 122 | +/// `runuser -u alice -- <binary>` fails with `Permission denied` |
| 123 | +/// before the binary even gets to exec. |
| 124 | +fn install_aimx_in(dest_dir: &Path) -> PathBuf { |
| 125 | + let src = aimx_binary_path(); |
| 126 | + let dst = dest_dir.join("aimx"); |
| 127 | + std::fs::copy(&src, &dst).expect("failed to copy aimx binary into tempdir"); |
| 128 | + std::fs::set_permissions(&dst, PermissionsExt::from_mode(0o755)) |
| 129 | + .expect("failed to chmod copied aimx binary"); |
| 130 | + dst |
| 131 | +} |
| 132 | + |
| 133 | +/// Run `aimx hooks list` as `user` (None = current process, i.e. root) |
| 134 | +/// using `binary` against the supplied `AIMX_CONFIG_DIR`. Returns |
| 135 | +/// combined stdout + stderr (since the CLI sometimes uses both). |
| 136 | +fn run_hooks_list_as(user: Option<&str>, binary: &Path, config_dir: &Path) -> String { |
| 137 | + let output = match user { |
| 138 | + Some(u) => Command::new("runuser") |
| 139 | + .arg("-u") |
| 140 | + .arg(u) |
| 141 | + .arg("--") |
| 142 | + .arg("env") |
| 143 | + .arg(format!("AIMX_CONFIG_DIR={}", config_dir.display())) |
| 144 | + .arg("NO_COLOR=1") |
| 145 | + .arg(binary) |
| 146 | + .arg("hooks") |
| 147 | + .arg("list") |
| 148 | + .output() |
| 149 | + .expect("failed to spawn runuser env aimx hooks list"), |
| 150 | + None => Command::new(binary) |
| 151 | + .env("AIMX_CONFIG_DIR", config_dir) |
| 152 | + .env("NO_COLOR", "1") |
| 153 | + .arg("hooks") |
| 154 | + .arg("list") |
| 155 | + .output() |
| 156 | + .expect("failed to spawn aimx hooks list"), |
| 157 | + }; |
| 158 | + let combined = format!( |
| 159 | + "{}{}", |
| 160 | + String::from_utf8_lossy(&output.stdout), |
| 161 | + String::from_utf8_lossy(&output.stderr) |
| 162 | + ); |
| 163 | + assert!( |
| 164 | + output.status.success(), |
| 165 | + "aimx hooks list exited non-zero (user={user:?}): {combined}" |
| 166 | + ); |
| 167 | + combined |
| 168 | +} |
| 169 | + |
| 170 | +#[test] |
| 171 | +#[ignore = "requires two test uids on the host (aimx-test-alice, aimx-test-bob)"] |
| 172 | +fn hooks_list_filters_to_caller_owned_mailboxes_for_non_root_callers() { |
| 173 | + if std::env::var("AIMX_INTEGRATION_SUDO").as_deref() != Ok("1") { |
| 174 | + eprintln!("skipping: AIMX_INTEGRATION_SUDO=1 not set"); |
| 175 | + return; |
| 176 | + } |
| 177 | + assert_eq!( |
| 178 | + unsafe { libc::geteuid() }, |
| 179 | + 0, |
| 180 | + "this test must run as root (sudo cargo test ...)" |
| 181 | + ); |
| 182 | + |
| 183 | + ensure_user(ALICE); |
| 184 | + ensure_user(BOB); |
| 185 | + // Catchall owner is referenced in the test config; loader bails |
| 186 | + // (orphan owner) without it. |
| 187 | + let _ = Command::new("useradd") |
| 188 | + .arg("--system") |
| 189 | + .arg("--no-create-home") |
| 190 | + .arg("--shell") |
| 191 | + .arg("/usr/sbin/nologin") |
| 192 | + .arg("aimx-catchall") |
| 193 | + .status(); |
| 194 | + |
| 195 | + let config_dir = write_test_config(); |
| 196 | + let aimx_bin = install_aimx_in(&config_dir); |
| 197 | + |
| 198 | + // Alice sees only her hook. |
| 199 | + let alice_out = run_hooks_list_as(Some(ALICE), &aimx_bin, &config_dir); |
| 200 | + assert!( |
| 201 | + alice_out.contains("alice-hook-marker"), |
| 202 | + "alice should see her own hook in `aimx hooks list` output: {alice_out}" |
| 203 | + ); |
| 204 | + assert!( |
| 205 | + !alice_out.contains("bob-hook-marker"), |
| 206 | + "alice MUST NOT see bob's hook (filter regression): {alice_out}" |
| 207 | + ); |
| 208 | + |
| 209 | + // Bob sees only his hook. |
| 210 | + let bob_out = run_hooks_list_as(Some(BOB), &aimx_bin, &config_dir); |
| 211 | + assert!( |
| 212 | + bob_out.contains("bob-hook-marker"), |
| 213 | + "bob should see his own hook in `aimx hooks list` output: {bob_out}" |
| 214 | + ); |
| 215 | + assert!( |
| 216 | + !bob_out.contains("alice-hook-marker"), |
| 217 | + "bob MUST NOT see alice's hook (filter regression): {bob_out}" |
| 218 | + ); |
| 219 | + |
| 220 | + // Root sees both — confirms the filter is uid-gated, not always-on. |
| 221 | + let root_out = run_hooks_list_as(None, &aimx_bin, &config_dir); |
| 222 | + assert!( |
| 223 | + root_out.contains("alice-hook-marker"), |
| 224 | + "root should see alice's hook: {root_out}" |
| 225 | + ); |
| 226 | + assert!( |
| 227 | + root_out.contains("bob-hook-marker"), |
| 228 | + "root should see bob's hook: {root_out}" |
| 229 | + ); |
| 230 | + |
| 231 | + // Cleanup tempdir; the test users are owned by the CI workflow. |
| 232 | + let _ = std::fs::remove_dir_all(&config_dir); |
| 233 | +} |
0 commit comments