vallum 0.8.15

Security boundary between AI coding agents and your shell — redacts secrets, neutralizes prompt injection, sanitizes untrusted terminal output, audits every command.
Documentation
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Lexical path rules for agent file tools (Write/Edit/Read …). The Claude
//! hook gates file-tool calls through here: no regex, no filesystem access —
//! `~`/`$HOME` expansion plus textual `.`/`..` resolution only (symlinks are
//! NOT resolved; disclosed in SECURITY.md). All rules are Ask-severity.

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileOp {
    Write,
    Read,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileRuleMatch {
    pub rule_name: &'static str,
    pub reason: &'static str,
}

struct FileRule {
    name: &'static str,
    op: FileOp,
    reason: &'static str,
    matches: fn(path: &str, home: &str, file_name: &str) -> bool,
}

use super::sensitive::under;

const PROFILE_NAMES: &[&str] = &[
    ".zshenv",
    ".zshrc",
    ".zprofile",
    ".bashrc",
    ".bash_profile",
    ".profile",
];

fn rules() -> &'static [FileRule] {
    &[
        FileRule {
            name: "file_write_shell_profile",
            op: FileOp::Write,
            reason: "Writing to a shell startup file (persistence, CVE-2026-55607 class)",
            matches: |path, home, file_name| {
                !home.is_empty()
                    && PROFILE_NAMES.contains(&file_name)
                    && path == format!("{home}/{file_name}")
            },
        },
        FileRule {
            name: "file_write_ssh_config",
            op: FileOp::Write,
            reason: "Writing under ~/.ssh (persistent access)",
            matches: |path, home, _| under(path, &format!("{home}/.ssh")),
        },
        FileRule {
            name: "file_write_git_hooks",
            op: FileOp::Write,
            reason: "Writing a git hook (persistence)",
            matches: |path, _, _| path.contains("/.git/hooks/"),
        },
        FileRule {
            name: "file_write_crontab_dir",
            op: FileOp::Write,
            reason: "Writing a cron file (persistence)",
            matches: |path, _, _| {
                path == "/etc/crontab"
                    || path
                        .strip_prefix("/etc/cron.")
                        .is_some_and(|r| r.contains('/'))
                    || under(path, "/var/spool/cron")
            },
        },
        FileRule {
            name: "file_write_launch_agents",
            op: FileOp::Write,
            reason: "Writing a LaunchAgent/LaunchDaemon (persistence)",
            // Literals are lowercase: `evaluate` case-folds the path before
            // matching (macOS APFS is case-insensitive).
            matches: |path, home, _| {
                under(path, &format!("{home}/library/launchagents"))
                    || under(path, "/library/launchagents")
                    || under(path, "/library/launchdaemons")
            },
        },
        FileRule {
            name: "file_write_systemd_user",
            op: FileOp::Write,
            reason: "Writing a systemd user unit (persistence)",
            matches: |path, home, _| under(path, &format!("{home}/.config/systemd/user")),
        },
        FileRule {
            name: "file_write_agent_config",
            op: FileOp::Write,
            reason: "Writing to an AI agent config/hook file (possible hook injection)",
            matches: |path, _, file_name| {
                path.ends_with("/.claude/settings.json")
                    || path.ends_with("/.claude/settings.local.json")
                    || path.ends_with("/.cursor/hooks.json")
                    || path.ends_with("/.codex/hooks.json")
                    || path.ends_with("/.codex/config.toml")
                    || path.ends_with("/.gemini/settings.json")
                    || file_name == ".mcp.json"
            },
        },
        FileRule {
            name: "file_write_vallum",
            op: FileOp::Write,
            reason: "Writing to Vallum's own config/state directory (guardrail self-disable)",
            matches: |path, home, _| under(path, &format!("{home}/.vallum")),
        },
        FileRule {
            name: "file_read_sensitive",
            op: FileOp::Read,
            reason: "Reading a private key, credential file, or shadow password file",
            // The shared vocabulary, so this rule and the shell rules that
            // compile from `sensitive::hard_re()` can never drift apart.
            // A plain `fn` pointer, not a forwarding closure: the latter trips
            // `clippy::redundant_closure`, and the gate runs `-D warnings`.
            matches: super::sensitive::is_hard_path,
        },
    ]
}

/// Expand `~`/`$HOME`, absolutize against the cwd, and resolve `.`/`..`
/// textually. Never touches the filesystem and never fails: an odd input is
/// normalized as far as possible and matched as-is.
fn normalize(raw: &str, home: &str) -> String {
    let trimmed = raw.trim();
    let mut p = if trimmed == "~" || trimmed == "$HOME" {
        home.to_string()
    } else if let Some(rest) = trimmed.strip_prefix("~/") {
        format!("{home}/{rest}")
    } else if let Some(rest) = trimmed.strip_prefix("$HOME/") {
        format!("{home}/{rest}")
    } else {
        trimmed.to_string()
    };
    if !p.starts_with('/') {
        if let Ok(cwd) = std::env::current_dir() {
            p = format!("{}/{}", cwd.to_string_lossy(), p);
        }
    }
    let mut out: Vec<&str> = Vec::new();
    for seg in p.split('/') {
        match seg {
            "" | "." => {}
            ".." => {
                out.pop();
            }
            s => out.push(s),
        }
    }
    format!("/{}", out.join("/"))
}

/// Evaluate one file-tool access. Returns the first matching enabled rule
/// (rules are disjoint in practice), or None for Allow-equivalent.
pub fn evaluate(op: FileOp, raw_path: &str, disabled: &[String]) -> Option<FileRuleMatch> {
    let home = dirs::home_dir()
        .map(|h| h.to_string_lossy().into_owned())
        .unwrap_or_default();
    // Fold ASCII case before matching. macOS APFS is case-insensitive, so a
    // byte-exact comparison would miss `~/.SSH/id_rsa`, `~/.Zshenv`, etc. This
    // restores parity with the shell rules these paths shadow, all `(?i)`.
    let path = normalize(raw_path, &home).to_ascii_lowercase();
    let home = home.to_ascii_lowercase();
    let file_name = path.rsplit('/').next().unwrap_or("");
    rules()
        .iter()
        .filter(|r| r.op == op)
        .filter(|r| !disabled.iter().any(|d| d == r.name))
        .find(|r| (r.matches)(&path, &home, file_name))
        .map(|r| FileRuleMatch {
            rule_name: r.name,
            reason: r.reason,
        })
}

/// File-rule names, for `[policy] disabled` validation in doctor.
pub fn rule_names() -> Vec<&'static str> {
    rules().iter().map(|r| r.name).collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn home() -> String {
        dirs::home_dir().unwrap().to_string_lossy().into_owned()
    }

    fn write_hit(path: &str) -> Option<&'static str> {
        evaluate(FileOp::Write, path, &[]).map(|m| m.rule_name)
    }

    fn read_hit(path: &str) -> Option<&'static str> {
        evaluate(FileOp::Read, path, &[]).map(|m| m.rule_name)
    }

    #[test]
    fn matching_is_ascii_case_insensitive() {
        assert_eq!(read_hit("~/.SSH/id_rsa"), Some("file_read_sensitive"));
        assert_eq!(write_hit("~/.Zshenv"), Some("file_write_shell_profile"));
        assert_eq!(
            write_hit("~/.SSH/authorized_keys"),
            Some("file_write_ssh_config")
        );
        assert_eq!(
            write_hit("~/.VALLUM/config.toml"),
            Some("file_write_vallum")
        );
        assert_eq!(
            write_hit("/Users/x/proj/.GIT/hooks/pre-commit"),
            Some("file_write_git_hooks")
        );
        // .pub exemption must survive case-folding
        assert_eq!(read_hit("~/.ssh/id_rsa.PUB"), None);
    }

    #[test]
    fn tilde_and_home_var_expand() {
        assert_eq!(write_hit("~/.zshenv"), Some("file_write_shell_profile"));
        assert_eq!(write_hit("$HOME/.zshrc"), Some("file_write_shell_profile"));
        assert_eq!(
            write_hit(&format!("{}/.bashrc", home())),
            Some("file_write_shell_profile")
        );
    }

    #[test]
    fn dotdot_traversal_is_resolved() {
        assert_eq!(
            write_hit(&format!("{}/project/../.zshenv", home())),
            Some("file_write_shell_profile")
        );
    }

    #[test]
    fn profile_names_are_home_anchored() {
        // .zshrc inside a project dir is NOT the login shell's rc file.
        assert_eq!(write_hit(&format!("{}/proj/.zshrc", home())), None);
        // Suffix near-miss must not match.
        assert_eq!(write_hit("~/.zshrc.bak"), None);
    }

    #[test]
    fn ssh_dir_writes_ask() {
        assert_eq!(
            write_hit("~/.ssh/authorized_keys"),
            Some("file_write_ssh_config")
        );
        assert_eq!(write_hit("~/.ssh/config"), Some("file_write_ssh_config"));
        // Component anchoring: "notssh" and a project file named .ssh-something.
        assert_eq!(write_hit(&format!("{}/notssh/config", home())), None);
    }

    #[test]
    fn git_hooks_anywhere() {
        assert_eq!(
            write_hit("/Users/x/proj/.git/hooks/pre-commit"),
            Some("file_write_git_hooks")
        );
        assert_eq!(write_hit("/Users/x/proj/.github/workflows/ci.yml"), None);
    }

    #[test]
    fn cron_paths() {
        assert_eq!(write_hit("/etc/crontab"), Some("file_write_crontab_dir"));
        assert_eq!(
            write_hit("/etc/cron.d/backdoor"),
            Some("file_write_crontab_dir")
        );
        assert_eq!(
            write_hit("/var/spool/cron/crontabs/root"),
            Some("file_write_crontab_dir")
        );
        // Component anchoring: /etc/cronicle is not cron.
        assert_eq!(write_hit("/etc/cronicle/conf.json"), None);
    }

    #[test]
    fn launch_agents_and_daemons() {
        assert_eq!(
            write_hit("~/Library/LaunchAgents/com.evil.plist"),
            Some("file_write_launch_agents")
        );
        assert_eq!(
            write_hit("/Library/LaunchDaemons/com.evil.plist"),
            Some("file_write_launch_agents")
        );
        assert_eq!(write_hit("~/Library/Application Support/x.plist"), None);
    }

    #[test]
    fn systemd_user_units() {
        assert_eq!(
            write_hit("~/.config/systemd/user/evil.service"),
            Some("file_write_systemd_user")
        );
        assert_eq!(write_hit("~/.config/systemd/other.conf"), None);
    }

    #[test]
    fn agent_configs() {
        assert_eq!(
            write_hit("/Users/x/proj/.claude/settings.json"),
            Some("file_write_agent_config")
        );
        assert_eq!(
            write_hit("~/.claude/settings.local.json"),
            Some("file_write_agent_config")
        );
        assert_eq!(
            write_hit("~/.codex/config.toml"),
            Some("file_write_agent_config")
        );
        assert_eq!(
            write_hit("/Users/x/proj/.mcp.json"),
            Some("file_write_agent_config")
        );
        // Other files under .claude/ are fine (e.g. CLAUDE.md lives elsewhere anyway).
        assert_eq!(write_hit("~/.claude/projects/foo.md"), None);
    }

    #[test]
    fn vallum_dir_is_self_protected() {
        assert_eq!(
            write_hit("~/.vallum/config.toml"),
            Some("file_write_vallum")
        );
        assert_eq!(
            write_hit("~/.vallum/logs/policy.log"),
            Some("file_write_vallum")
        );
        assert_eq!(
            write_hit(&format!("{}/proj/.vallum-notes.md", home())),
            None
        );
    }

    #[test]
    fn sensitive_reads() {
        assert_eq!(read_hit("~/.ssh/id_rsa"), Some("file_read_sensitive"));
        assert_eq!(read_hit("~/.ssh/id_ed25519"), Some("file_read_sensitive"));
        // Public halves are fine.
        assert_eq!(read_hit("~/.ssh/id_rsa.pub"), None);
        assert_eq!(read_hit("~/.aws/credentials"), Some("file_read_sensitive"));
        assert_eq!(read_hit("/etc/shadow"), Some("file_read_sensitive"));
        assert_eq!(
            read_hit("~/.vallum/logs/approval.secret"),
            Some("file_read_sensitive")
        );
        // Reads are not gated by write rules and vice versa.
        assert_eq!(read_hit("~/.zshenv"), None);
        assert_eq!(write_hit("/etc/shadow"), None);
    }

    #[test]
    fn benign_everyday_paths_pass() {
        for p in [
            "/Users/x/proj/README.md",
            "/Users/x/proj/src/main.rs",
            "~/.config/git/ignore",
            "~/Downloads/notes.txt",
            "/tmp/scratch.json",
        ] {
            assert_eq!(write_hit(p), None, "write {p}");
            assert_eq!(read_hit(p), None, "read {p}");
        }
    }

    #[test]
    fn disabled_list_suppresses_a_rule() {
        let disabled = vec!["file_write_shell_profile".to_string()];
        assert!(evaluate(FileOp::Write, "~/.zshenv", &disabled).is_none());
        // Other rules unaffected.
        assert!(evaluate(FileOp::Write, "~/.ssh/config", &disabled).is_some());
    }

    #[test]
    fn widened_file_reads_ask() {
        let h = home();
        for p in [
            format!("{h}/.netrc"),
            format!("{h}/.git-credentials"),
            format!("{h}/.claude/.credentials.json"),
            format!("{h}/.codex/auth.json"),
            format!("{h}/.gemini/oauth_creds.json"),
            format!("{h}/.config/gh/hosts.yml"),
            format!("{h}/.gnupg/secring.gpg"),
            "/proc/self/environ".to_string(),
        ] {
            assert_eq!(
                read_hit(&p),
                Some("file_read_sensitive"),
                "{p} should be gated"
            );
        }
    }

    #[test]
    fn egress_only_files_are_free_to_read_via_file_tool() {
        // Two-tier split, lexical side: reading these is ordinary development
        // work. Only sending them over the network is gated.
        let h = home();
        for p in [
            format!("{h}/proj/.env"),
            format!("{h}/.npmrc"),
            format!("{h}/.kube/config"),
        ] {
            assert_eq!(read_hit(&p), None, "{p}");
        }
    }

    #[test]
    fn rule_names_lists_all_nine() {
        let names = rule_names();
        assert_eq!(names.len(), 9);
        assert!(names.contains(&"file_write_vallum"));
        assert!(names.contains(&"file_read_sensitive"));
    }
}