1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! Agent mode must gate the *capability* to run a command, not one builtin's name.
//!
//! Until 2026-08-04, `sh` was the only builtin that called `safety::guard` with
//! `Effect::Exec`. But `timeout`, `xargs`, `proc.spawn`, `nohup`, `strace`,
//! `ltrace` and the `perf` builtins all hand a caller-controlled string to a
//! shell. So in agent mode — with `sh` disabled outright, which is the intended
//! hardened configuration — an agent could still run any command it liked, with
//! no approval prompt and no `exec`-classified audit entry.
//!
//! This was demonstrated, not merely inferred: `timeout(5, "touch <marker>")`
//! returned exit code 0 and the marker file existed afterwards.
//!
//! These tests assert the property that matters — an ungated command does not
//! run — rather than any particular error text.
use aethershell::safety::{self, Effect};
use aethershell::value::Value;
// `AETHER_*` is process-global and tests run in parallel threads.
static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
fn lock() -> std::sync::MutexGuard<'static, ()> {
ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner())
}
/// Enter agent mode with a fresh workspace, and return it.
fn agent_workspace(tag: &str) -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!("ae_exec_gate_{}_{}", tag, std::process::id()));
let _ = std::fs::create_dir_all(&dir);
std::env::set_var("AETHER_WORKSPACE", &dir);
std::env::set_var("AETHER_AUDIT_LOG", dir.join("audit.log"));
std::env::set_var("AETHER_MODE", "agent");
std::env::remove_var("AETHER_APPROVE");
std::env::remove_var("AETHER_APPROVE_ALL");
safety::governor_reset();
dir
}
fn leave_agent_mode() {
for k in [
"AETHER_MODE",
"AETHER_WORKSPACE",
"AETHER_AUDIT_LOG",
"AETHER_APPROVE",
"AETHER_APPROVE_ALL",
] {
std::env::remove_var(k);
}
}
/// The observable proof: after an ungated call, the side effect must not exist.
#[test]
fn timeout_cannot_run_a_command_unapproved_in_agent_mode() {
let _l = lock();
let dir = agent_workspace("timeout");
let marker = dir.join("PWNED.txt");
let _ = std::fs::remove_file(&marker);
let mut env = aethershell::env::Env::new();
let res = aethershell::builtins::call(
"timeout_cmd",
vec![
Value::Int(5),
Value::Str(format!("touch {}", marker.display())),
],
&mut env,
);
assert!(
res.is_err(),
"timeout ran a command in agent mode without approval: {res:?}"
);
assert!(
!marker.exists(),
"the command actually executed — the gate is decorative"
);
leave_agent_mode();
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn proc_spawn_cannot_launch_a_program_unapproved_in_agent_mode() {
let _l = lock();
let dir = agent_workspace("spawn");
let mut env = aethershell::env::Env::new();
// A program that exists on both platforms, so a failure here is the gate
// and not a missing binary.
let program = if cfg!(windows) { "cmd" } else { "true" };
let res = aethershell::builtins::call(
"proc_spawn",
vec![Value::Str(program.to_string())],
&mut env,
);
assert!(
res.is_err(),
"proc.spawn launched a program in agent mode without approval: {res:?}"
);
leave_agent_mode();
let _ = std::fs::remove_dir_all(&dir);
}
/// Human mode is a REPL and must stay default-allow: the gate is about agents.
///
/// This asserts the call is not *refused by the gate*, not that it succeeds.
/// `timeout_cmd` shells out to GNU `timeout`, which macOS does not ship (it has
/// `gtimeout` via coreutils), so "the command ran" is a statement about the
/// runner's PATH rather than about the policy — and asserting it fails CI on
/// macOS for a reason that has nothing to do with what this test is for.
#[test]
fn human_mode_is_unaffected() {
let _l = lock();
leave_agent_mode();
let dir = std::env::temp_dir().join(format!("ae_exec_gate_human_{}", std::process::id()));
let _ = std::fs::create_dir_all(&dir);
let marker = dir.join("ok.txt");
let _ = std::fs::remove_file(&marker);
let mut env = aethershell::env::Env::new();
let res = aethershell::builtins::call(
"timeout_cmd",
vec![
Value::Int(5),
Value::Str(format!("touch {}", marker.display())),
],
&mut env,
);
if let Err(e) = &res {
let rendered = e.to_string();
for refusal in ["E_NEEDS_APPROVAL", "E_POLICY_DENY", "E_OUTSIDE_WORKSPACE"] {
assert!(
!rendered.contains(refusal),
"human mode must not be gated, but got {refusal}: {rendered}"
);
}
}
let _ = std::fs::remove_dir_all(&dir);
}
/// The classification consumed by `agent_api`'s discovery must agree with the
/// gate. If these drift, an agent is told `timeout` is side-effect free.
#[test]
fn every_guarded_exec_builtin_is_classified_as_exec() {
// `nohup_run` and `lxc_exec` were on this list until their implementations
// were deleted as unreachable. Asserting the effect class of a builtin that
// does not exist is a claim about nothing -- the same shape as a stale
// allowlist entry, and the fourth instance of it found in one day.
for name in [
"sh",
"timeout_cmd",
"xargs_exec",
"proc_spawn",
"strace_cmd",
"ltrace_cmd",
"perf_stat",
"perf_record",
"tmux_new",
"tmux_send",
] {
assert_eq!(
safety::effect_of(name),
Effect::Exec,
"{name} runs a caller-supplied command but is not classified as exec"
);
}
}