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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use std::collections::HashSet;
use std::sync::LazyLock;
/// Commands known to be safe (read-only, no side effects).
/// Ported from Dippy's `SIMPLE_SAFE` frozenset.
static SIMPLE_SAFE: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
HashSet::from([
// File viewing
"cat",
"head",
"tail",
"less",
"more",
"bat",
"hexdump",
"strings",
"xxd",
"od",
// Compressed file viewing
"zcat",
"bzcat",
"xzcat",
"zstdcat",
// Binary analysis
"nm",
"objdump",
"readelf",
"ldd",
"otool",
"size",
"file",
// Directory listing
"ls",
"tree",
"exa",
"eza",
"lsd",
// File info
"stat",
"wc",
"du",
"df",
// Text processing (read-only)
"grep",
"rg",
"ag",
"diff",
"cut",
"tr",
// sort has a dedicated handler (handles -o output flag)
"uniq",
"paste",
"join",
"comm",
"fold",
"fmt",
"nl",
"column",
"expand",
"unexpand",
"rev",
"tac",
"shuf",
// Encoding/hashing
"base64",
"base32",
"md5sum",
"sha1sum",
"sha256sum",
"sha512sum",
"cksum",
"sum",
// Search (find, fd, env, sort, yq have dedicated handlers — not in this list)
"locate",
"which",
"whereis",
"type",
"whence",
// System info
"whoami",
"hostname",
"uname",
"id",
"groups",
"uptime",
"pwd",
"date",
// env has a dedicated handler (can delegate inner commands)
"printenv",
"locale",
// Process info
"ps",
"top",
"htop",
"lsof",
"vmstat",
"iostat",
"free",
"pgrep",
// Network info (read-only)
"ping",
"dig",
"nslookup",
"traceroute",
"tracepath",
"netstat",
"ss",
// ifconfig and ip have dedicated handlers
"host",
"getent",
// Help/docs
"man",
"info",
"whatis",
"apropos",
"tldr",
"help",
// Shell builtins (safe)
"echo",
"printf",
"true",
"false",
"test",
"[",
":",
// Path manipulation
"basename",
"dirname",
"realpath",
"readlink",
// Math
"bc",
"expr",
"seq",
// Misc read-only
"tty",
"stty",
"tput",
"yes",
"sleep",
// Version/capabilities
"nproc",
"getconf",
"arch",
"lsb_release",
// Modern CLI tools
"jq",
// yq has a dedicated handler (handles -i inplace)
"fzf",
"tokei",
"cloc",
"scc",
"hyperfine",
// Encoding
"iconv",
"dos2unix",
"unix2dos",
// Disk/fs info
"mount",
"findmnt",
"lsblk",
"blkid",
// dmesg has a dedicated handler (clear flags)
])
});
/// Commands that wrap other commands — analyze the inner command instead.
static WRAPPER_COMMANDS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
HashSet::from([
"time", "timeout", "nice", "strace", "ltrace", "nohup", "command", "builtin",
])
});
/// Check if a command is in the simple-safe allowlist.
#[must_use]
pub fn is_simple_safe(cmd: &str) -> bool {
SIMPLE_SAFE.contains(cmd)
}
/// Check if a command is a wrapper (should analyze inner command).
#[must_use]
pub fn is_wrapper(cmd: &str) -> bool {
WRAPPER_COMMANDS.contains(cmd)
}
/// Number of commands in the simple-safe allowlist.
#[must_use]
pub fn simple_safe_count() -> usize {
SIMPLE_SAFE.len()
}
/// Number of commands in the wrapper allowlist.
#[must_use]
pub fn wrapper_count() -> usize {
WRAPPER_COMMANDS.len()
}
/// Return all simple-safe commands, sorted alphabetically.
#[must_use]
pub fn all_simple_safe() -> Vec<&'static str> {
let mut cmds: Vec<_> = SIMPLE_SAFE.iter().copied().collect();
cmds.sort_unstable();
cmds
}
/// Return all wrapper commands, sorted alphabetically.
#[must_use]
pub fn all_wrappers() -> Vec<&'static str> {
let mut cmds: Vec<_> = WRAPPER_COMMANDS.iter().copied().collect();
cmds.sort_unstable();
cmds
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn known_safe_commands() {
assert!(is_simple_safe("cat"));
assert!(is_simple_safe("ls"));
assert!(is_simple_safe("grep"));
assert!(is_simple_safe("whoami"));
assert!(is_simple_safe("jq"));
}
#[test]
fn unknown_commands_not_safe() {
assert!(!is_simple_safe("rm"));
assert!(!is_simple_safe("sudo"));
assert!(!is_simple_safe("arbitrary_command"));
}
#[test]
fn wrapper_commands() {
assert!(is_wrapper("time"));
assert!(is_wrapper("timeout"));
assert!(is_wrapper("nice"));
assert!(is_wrapper("nohup"));
assert!(!is_wrapper("cat"));
}
}