Skip to content

Commit bec3967

Browse files
authored
Pin aimx hooks list non-root filter behaviour with an integration test (#177)
The CLI's `aimx hooks list` filters its output to caller-owned mailboxes for non-root callers (`src/hooks.rs::list` calls `caller_owns` per row). The predicate is unit-tested, but no test wired it end-to-end through the binary — a future refactor that bypassed the `rows.retain(...)` call would have shipped silently. Adds `tests/hooks_list_filter.rs` (gated `#[ignore]` + `AIMX_INTEGRATION_SUDO=1`, matching the `tests/uds_authz.rs` pattern): - Provisions a temp config with two mailboxes (alice + bob), each carrying a hook with a distinctive name marker. - Runs `aimx hooks list` as alice via `runuser`; asserts alice sees her hook and not bob's. - Runs as bob; asserts bob sees his hook and not alice's. - Runs as root; asserts both hooks are visible (the filter is uid-gated, not always-on). The test copies the aimx test binary into the test's tempdir before invoking via `runuser` because the GitHub Actions runner's `/home/runner/...` path isn't world-traversable; without the copy alice can't even reach `target/debug/aimx`. CI's existing `mailbox-dir-perms-isolation` job is extended with a new "Run hooks-list filter test" step. The job already provisions `aimx-test-alice` / `aimx-test-bob` for the other isolation tests.
1 parent 2db82e2 commit bec3967

2 files changed

Lines changed: 245 additions & 0 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ jobs:
7272
run: |
7373
cargo test --no-run --test mailbox_isolation
7474
cargo test --no-run --test uds_authz
75+
cargo test --no-run --test hooks_list_filter
7576
7677
- name: Run mailbox isolation test (under sudo)
7778
run: |
@@ -96,6 +97,17 @@ jobs:
9697
test -x "$BIN"
9798
sudo AIMX_INTEGRATION_SUDO=1 "$BIN" --ignored --test-threads=1
9899
100+
- name: Run hooks-list filter test (under sudo)
101+
run: |
102+
# Pins the wire-level behaviour of `aimx hooks list` filtering
103+
# output by caller euid. Spawns the CLI as alice / bob / root
104+
# against a temp config that carries one hook per user; asserts
105+
# each non-root caller sees only their own hook and root sees
106+
# both. Reuses the alice/bob test users provisioned above.
107+
BIN=$(ls -t target/debug/deps/hooks_list_filter-* | grep -v '\.d$' | head -n 1)
108+
test -x "$BIN"
109+
sudo AIMX_INTEGRATION_SUDO=1 "$BIN" --ignored
110+
99111
- name: Tear down test users
100112
if: always()
101113
run: |

‎tests/hooks_list_filter.rs‎

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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

Comments
 (0)