openmoat 0.29.0

Policy checks for AI coding agents: decides allow, defer, review or deny for each tool call the hook of Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot CLI and seven more agents reports, configures Claude Code's, Codex's and Cursor's own OS sandboxes from the same policy, and keeps a local audit log.
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
//! Installation-time snapshot of the search path and where key programs live.
//!
//! `moat init` records `PATH` and the absolute location of security-relevant
//! programs in `~/.moat/environment.json` (pinned by the policy lock). `moat guard`
//! resolves command names through this snapshot, never through the environment
//! the hook inherited, so a poisoned `PATH` or a planted `git` cannot change which
//! binary the kernel believes is about to run.

use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{Context as _, Result, bail};
use openmoat_core::ProgramResolver;
use serde::{Deserialize, Serialize};

use crate::context::path_string;
use crate::home::{Home, write_private};

const SNAPSHOT_VERSION: u32 = 1;

/// Programs whose location is recorded at install time.
pub const PINNED_PROGRAMS: &[&str] = &[
    "sh",
    "bash",
    "zsh",
    "git",
    "gh",
    "ssh",
    "scp",
    // checks policy signatures (`signing.rs`, ADR-031)
    "ssh-keygen",
    "curl",
    "wget",
    "sudo",
    "node",
    "npm",
    "npx",
    "pnpm",
    "yarn",
    "bun",
    "python",
    "python3",
    "pip",
    "pip3",
    "uv",
    "cargo",
    "rustc",
    "go",
    "swift",
    "docker",
    "kubectl",
    "terraform",
    "aws",
    "gcloud",
    "az",
    "make",
    "brew",
];

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Snapshot {
    pub version: u32,
    pub captured_at_ms: i64,
    /// Search path directories, in order, as seen by `moat init`.
    pub path: Vec<PathBuf>,
    /// Program name → absolute path found at install time.
    pub programs: BTreeMap<String, PathBuf>,
    /// Windows executable extensions (`PATHEXT`) in search order, lowercase.
    /// Captured with the search path so the hook's environment cannot change it.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub pathext: Vec<String>,
}

/// Used when `PATHEXT` is unset or empty, and for snapshots written before it
/// was recorded.
const DEFAULT_PATHEXT: &[&str] = &[".com", ".exe", ".bat", ".cmd"];

impl Snapshot {
    /// Capture the current process's search path and resolve the pinned programs.
    pub fn capture() -> Self {
        let path: Vec<PathBuf> = std::env::var_os("PATH")
            .map(|p| std::env::split_paths(&p).collect())
            .unwrap_or_default();
        let pathext = if cfg!(windows) {
            parse_pathext(&std::env::var("PATHEXT").unwrap_or_default())
        } else {
            Vec::new()
        };
        // Pinned only from absolute entries: a pin of `./git` would let any
        // `git` planted in the directory a command runs in pass as the pinned one.
        let absolute = absolute_only(path.iter().cloned());
        let mut programs = BTreeMap::new();
        for name in PINNED_PROGRAMS {
            if let Some(found) = find_in(&absolute, &pathext, name) {
                programs.insert((*name).to_owned(), found);
            }
        }
        Self {
            version: SNAPSHOT_VERSION,
            captured_at_ms: crate::time::now_ms(),
            path,
            programs,
            pathext,
        }
    }

    pub fn load(path: &Path) -> Result<Self> {
        let text = fs::read_to_string(path)
            .with_context(|| format!("reading environment snapshot {}", path.display()))?;
        Self::parse(&text, path)
    }

    /// [`Snapshot::load`] of `home`'s snapshot, from the bytes the lock check
    /// kept when it kept them ([`Home::load_state`]).
    pub fn load_in(home: &Home) -> Result<Self> {
        home.load_state(&home.environment_path(), Self::parse, Self::load)
    }

    /// The snapshot in `text`, read from `path` (named in errors).
    pub fn parse(text: &str, path: &Path) -> Result<Self> {
        let snapshot: Self = serde_json::from_str(text)
            .with_context(|| format!("parsing environment snapshot {}", path.display()))?;
        if snapshot.version != SNAPSHOT_VERSION {
            bail!(
                "environment snapshot {} has version {}; this build supports {SNAPSHOT_VERSION}",
                path.display(),
                snapshot.version
            );
        }
        Ok(snapshot)
    }

    pub fn save(&self, path: &Path) -> Result<()> {
        let text = serde_json::to_string_pretty(self)? + "\n";
        write_private(path, text.as_bytes())
    }
}

impl ProgramResolver for Snapshot {
    fn resolve(&self, program: &str) -> Option<String> {
        find_in(&self.path, &self.pathext, program).map(|p| path_string(&p))
    }

    fn pinned(&self, program: &str) -> Option<String> {
        self.programs.get(program).map(|p| path_string(p))
    }
}

/// A [`Snapshot`] that also knows OpenMoat's own binary: the one the policy
/// lock records (`binary`) and its `moat` alias beside it, so the guard decides
/// a command that runs that file under another name as `openmoat`
/// ([`ProgramResolver::own_binaries`]).
pub struct WithOwnBinary<'a> {
    pub snapshot: &'a Snapshot,
    pub binary: &'a str,
}

impl ProgramResolver for WithOwnBinary<'_> {
    fn resolve(&self, program: &str) -> Option<String> {
        self.snapshot.resolve(program)
    }

    fn pinned(&self, program: &str) -> Option<String> {
        self.snapshot.pinned(program)
    }

    fn local_bin(&self, dirs: &[String], name: &str) -> bool {
        crate::project_packages::local_bin(dirs, name)
    }

    fn declared(&self, dirs: &[String], ecosystem: openmoat_core::Ecosystem, name: &str) -> bool {
        crate::project_packages::declared(dirs, ecosystem, name)
    }

    fn own_binaries(&self) -> Vec<String> {
        let binary = Path::new(self.binary);
        std::iter::once(binary.to_path_buf())
            .chain(crate::install::other_name(binary))
            .map(|p| path_string(&p))
            .collect()
    }
}

/// Where `program` is, as the snapshot `moat init` recorded resolves it: the
/// pinned location, else the first on the recorded search path. `None`
/// without a snapshot, so a poisoned `PATH` never picks the program.
pub fn program(home: &Home, program: &str) -> Option<PathBuf> {
    Snapshot::load_in(home).ok()?.program(program)
}

impl Snapshot {
    /// [`program`] in this snapshot, for a program OpenMoat itself runs. Only
    /// an absolute location counts: a relative search-path entry (`.`,
    /// `node_modules/.bin`) names a directory of whatever repository the
    /// command runs in, where an agent can put a program of that name.
    pub fn program(&self, program: &str) -> Option<PathBuf> {
        self.programs
            .get(program)
            .filter(|p| p.is_absolute())
            .cloned()
            .or_else(|| {
                find_in(
                    &absolute_only(self.path.iter().cloned()),
                    &self.pathext,
                    program,
                )
            })
    }
}

/// The absolute directories of `dirs`, in order. A relative entry (`.`,
/// `bin`, `node_modules/.bin`) names a directory of whatever directory the
/// process runs in, where an agent can put a program of any name.
fn absolute_only(dirs: impl IntoIterator<Item = PathBuf>) -> Vec<PathBuf> {
    dirs.into_iter().filter(|dir| dir.is_absolute()).collect()
}

/// This process's `PATH`, relative entries left out ([`absolute_only`]).
pub fn absolute_search_path() -> Vec<PathBuf> {
    std::env::var_os("PATH")
        .map(|p| absolute_only(std::env::split_paths(&p)))
        .unwrap_or_default()
}

/// The `program` this process's `PATH` names, found only in its absolute
/// directories: what every program OpenMoat runs, probes (`--version`),
/// trusts or records outside the snapshot is looked up by. Run from a
/// repository with `.` on `PATH`, a planted `claude` or `git` there is never
/// the one found. The snapshot's own lookups are [`Snapshot::program`] and,
/// for deciding what a command would run, [`Snapshot`]'s `resolve`.
pub fn find_on_path(program: &str) -> Option<PathBuf> {
    find_in(&absolute_search_path(), &[], program)
}

/// `PATHEXT` (`.COM;.EXE;…`) as lowercase extensions; the default list when empty.
fn parse_pathext(raw: &str) -> Vec<String> {
    let exts: Vec<String> = raw
        .split(';')
        .map(str::trim)
        .filter(|e| e.starts_with('.') && e.len() > 1)
        .map(str::to_ascii_lowercase)
        .collect();
    if exts.is_empty() {
        DEFAULT_PATHEXT.iter().map(|e| (*e).to_owned()).collect()
    } else {
        exts
    }
}

/// First executable named `program` in `dirs`, following the platform's rules:
/// on Windows the name as given, then with each `pathext` extension.
pub fn find_in(dirs: &[PathBuf], pathext: &[String], program: &str) -> Option<PathBuf> {
    let candidates: Vec<String> = if cfg!(windows) {
        let default: Vec<String>;
        let exts = if pathext.is_empty() {
            default = parse_pathext("");
            &default
        } else {
            pathext
        };
        std::iter::once(program.to_owned())
            .chain(exts.iter().map(|e| format!("{program}{e}")))
            .collect()
    } else {
        vec![program.to_owned()]
    };
    dirs.iter()
        .filter(|d| !d.as_os_str().is_empty())
        .flat_map(|d| candidates.iter().map(move |c| d.join(c)))
        .find(|p| is_executable(p))
}

#[cfg(unix)]
fn is_executable(path: &Path) -> bool {
    use std::os::unix::fs::PermissionsExt;
    fs::metadata(path).is_ok_and(|m| m.is_file() && m.permissions().mode() & 0o111 != 0)
}

#[cfg(not(unix))]
fn is_executable(path: &Path) -> bool {
    path.is_file()
}

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

    #[cfg(unix)]
    fn executable(dir: &Path, name: &str) -> PathBuf {
        use std::os::unix::fs::PermissionsExt;
        let p = dir.join(name);
        fs::write(&p, "#!/bin/sh\n").unwrap();
        fs::set_permissions(&p, fs::Permissions::from_mode(0o755)).unwrap();
        p
    }

    #[cfg(unix)]
    #[test]
    fn resolves_in_search_path_order_and_skips_non_executables() {
        let dir = tempfile::tempdir().unwrap();
        let first = dir.path().join("first");
        let second = dir.path().join("second");
        fs::create_dir_all(&first).unwrap();
        fs::create_dir_all(&second).unwrap();
        fs::write(first.join("git"), "not executable").unwrap();
        let real = executable(&second, "git");
        let snap = Snapshot {
            version: SNAPSHOT_VERSION,
            captured_at_ms: 0,
            path: vec![first.clone(), second.clone()],
            programs: BTreeMap::from([("git".to_owned(), real.clone())]),
            pathext: Vec::new(),
        };
        assert_eq!(snap.resolve("git").as_deref(), Some(real.to_str().unwrap()));
        assert_eq!(snap.pinned("git").as_deref(), Some(real.to_str().unwrap()));
        assert_eq!(snap.resolve("nope"), None);

        let planted = executable(&first, "git");
        assert_eq!(
            snap.resolve("git").as_deref(),
            Some(planted.to_str().unwrap())
        );
    }

    /// A program OpenMoat runs itself (`git` for the push check, `ssh-keygen`)
    /// is never found through a relative search-path entry, which names a
    /// directory of the repository the call runs in.
    #[cfg(unix)]
    #[test]
    fn a_program_openmoat_runs_is_never_found_through_a_relative_entry() {
        let dir = tempfile::tempdir().unwrap();
        let system = dir.path().join("system");
        fs::create_dir_all(&system).unwrap();
        let real = executable(&system, "git");
        let snap = |path: Vec<PathBuf>, pinned: Option<PathBuf>| Snapshot {
            version: SNAPSHOT_VERSION,
            captured_at_ms: 0,
            path,
            programs: pinned
                .map(|p| BTreeMap::from([("git".to_owned(), p)]))
                .unwrap_or_default(),
            pathext: Vec::new(),
        };
        let relative = PathBuf::from("node_modules/.bin");
        let found = snap(vec![relative.clone(), system.clone()], None).program("git");
        assert_eq!(found, Some(real.clone()));
        let pinned_relative = snap(vec![system], Some(relative.join("git"))).program("git");
        assert_eq!(pinned_relative, Some(real), "a relative pin is no pin");
        assert_eq!(snap(vec![relative], None).program("git"), None);
    }

    /// A relative entry that reaches a directory holding a planted program
    /// finds it with [`find_in`], and never once [`absolute_only`] filtered the
    /// entries, as [`find_on_path`] and the snapshot's pins do.
    #[cfg(unix)]
    #[test]
    fn a_relative_entry_reaching_a_planted_program_is_skipped() {
        let dir = tempfile::tempdir().unwrap();
        let planted = executable(dir.path(), "claude");
        let cwd = std::env::current_dir().unwrap();
        let up = "../".repeat(cwd.components().count().saturating_sub(1));
        let relative = PathBuf::from(format!("{up}{}", dir.path().display()).replace("//", "/"));
        assert!(relative.is_relative());
        let dirs = vec![relative];
        assert!(find_in(&dirs, &[], "claude").is_some_and(|p| p.ends_with("claude")));
        assert_eq!(find_in(&absolute_only(dirs.clone()), &[], "claude"), None);
        let with_system = absolute_only(dirs.into_iter().chain([dir.path().to_path_buf()]));
        assert_eq!(find_in(&with_system, &[], "claude"), Some(planted));
    }

    #[cfg(windows)]
    #[test]
    fn windows_resolution_follows_the_recorded_pathext() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("tool.ps1"), "").unwrap();
        let dirs = [dir.path().to_path_buf()];
        assert_eq!(find_in(&dirs, &[], "tool"), None);
        let found = find_in(&dirs, &[".ps1".to_owned()], "tool").unwrap();
        assert!(path_string(&found).ends_with("/tool.ps1"), "{found:?}");
    }

    #[test]
    fn pathext_is_parsed_lowercase_with_a_default() {
        assert_eq!(
            parse_pathext(".COM;.EXE; .PS1 ;;bad"),
            [".com", ".exe", ".ps1"]
        );
        assert_eq!(parse_pathext(""), DEFAULT_PATHEXT);
        let old: Snapshot =
            serde_json::from_str(r#"{"version":1,"captured_at_ms":0,"path":[],"programs":{}}"#)
                .unwrap();
        assert!(
            old.pathext.is_empty(),
            "snapshots without pathext still load"
        );
    }

    #[test]
    fn round_trips_and_rejects_other_versions() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("environment.json");
        let snap = Snapshot::capture();
        snap.save(&file).unwrap();
        assert_eq!(Snapshot::load(&file).unwrap(), snap);
        fs::write(
            &file,
            r#"{"version":7,"captured_at_ms":0,"path":[],"programs":{}}"#,
        )
        .unwrap();
        assert!(
            Snapshot::load(&file)
                .unwrap_err()
                .to_string()
                .contains("version 7")
        );
    }
}