openmoat 0.27.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Where the ledger finds the hook commands, plugins and program-running
//! settings an agent loads besides OpenMoat's own hook (ADR-109). Every place
//! comes from a host's spec: its hook file and layout, its project settings,
//! hook and plugin directories, its other hook files, its plugin folders
//! ([`openmoat_hosts::Ledger`]) and the helper commands its settings reader
//! names; and, for every host, the repository's git configuration. Files are
//! read, never run.

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

use openmoat_hosts::{
    Activity, ForeignEntry, HookFile, Host, PluginHooks, ProjectSettingsSpec, SettingsFile,
};
use serde_json::Value;

use super::{Found, Scan};
use crate::home::Home;
use crate::install::HostConfig;
use crate::integrity::sha256_hex;

/// A file larger than this is pinned by its bytes, unread.
const MAX_BYTES: u64 = 1024 * 1024;
/// How many directories one [`Activity::ANY`] level expands to.
const MAX_DIRS: usize = 256;
/// How many files of a plugin directory a digest covers.
const MAX_FILES: usize = 512;

/// What `hosts` load for a session in `cwd` (the current directory when
/// `None`) besides OpenMoat's hook, and the repository's git programs.
pub(super) fn scan(home: &Home, hosts: &[Host], cwd: Option<&Path>) -> Scan {
    let mut scan = Collect::default();
    let cwd = cwd
        .map(Path::to_path_buf)
        .or_else(|| std::env::current_dir().ok());
    let user_home = crate::home::user_home().ok();
    for host in hosts {
        let Ok(config) = HostConfig::for_host(home, *host) else {
            continue;
        };
        user_level(&mut scan, &config, user_home.as_deref());
        if let (Some(cwd), Some(user_home)) = (&cwd, &user_home) {
            project_level(&mut scan, &config, cwd, user_home);
            let others = crate::project_settings::other_hooks::found(
                *host,
                config.dir(),
                &config.settings_path,
                cwd,
                user_home,
            );
            for path in others {
                scan.whole(
                    Some(*host),
                    &path,
                    "hook file the agent runs beside OpenMoat's",
                );
            }
        }
    }
    if let (Some(cwd), Some(user_home)) = (&cwd, &user_home) {
        git(&mut scan, cwd, user_home);
    }
    Scan {
        found: scan.found.into_values().collect(),
        scanned: scan.scanned,
    }
}

#[derive(Default)]
struct Collect {
    found: BTreeMap<String, Found>,
    scanned: std::collections::BTreeSet<String>,
}

impl Collect {
    fn add(&mut self, found: Found) {
        self.scanned.insert(found.file.display().to_string());
        self.found.entry(found.id.clone()).or_insert(found);
    }

    /// The hook entries of `layout` in the file at `path`, which holds the
    /// hooks object at its top (or, `inline`, is the event map itself); a
    /// file that cannot be read as JSON is pinned by its bytes.
    fn entries(&mut self, host: Host, layout: &'static dyn HookFile, path: &Path, what: &str) {
        self.scanned.insert(path.display().to_string());
        match read_json(path) {
            Some(Ok(root)) => self.entries_of(host, layout, path, what, &root, false),
            Some(Err(())) => {
                self.whole(Some(host), path, "hook file OpenMoat cannot read as JSON");
            }
            None => {}
        }
    }

    fn entries_of(
        &mut self,
        host: Host,
        layout: &'static dyn HookFile,
        path: &Path,
        what: &str,
        root: &Value,
        inline: bool,
    ) {
        let entries = if inline {
            layout.foreign_entries_of_map(root, host.id())
        } else {
            layout.foreign_entries(root, host.id())
        };
        for entry in entries {
            self.add(entry_found(host, path, what, &entry));
        }
    }

    /// A file (or a directory, by every file in it) the host loads and runs,
    /// pinned by its bytes, with nothing the classifier can read.
    fn whole(&mut self, host: Option<Host>, path: &Path, what: &str) {
        let Some(digest) = digest_path(path) else {
            return;
        };
        self.add(Found {
            id: path.display().to_string(),
            host,
            file: path.to_path_buf(),
            what: what.to_owned(),
            commands: Vec::new(),
            digest,
        });
    }

    /// The helper commands `config`'s settings reader names in `root`.
    fn programs(&mut self, host: Host, path: &Path, root: &Value, user_home: Option<&Path>) {
        let (Some(spec), Some(object)) = (host.spec().project_settings.as_ref(), root.as_object())
        else {
            return;
        };
        let base = path.parent().unwrap_or(path);
        let file = SettingsFile::new(object, base, path, user_home);
        for (key, command) in spec.reader.programs(&file) {
            self.add(Found {
                id: format!("{}#{key}", path.display()),
                host: Some(host),
                file: path.to_path_buf(),
                what: format!("`{key}` command"),
                digest: sha256_hex(command.as_bytes()),
                commands: vec![command],
            });
        }
    }
}

fn entry_found(host: Host, path: &Path, what: &str, entry: &ForeignEntry) -> Found {
    let digest = sha256_hex(openmoat_hosts::canonical_json(&entry.entry).as_bytes());
    Found {
        id: format!("{}#{}@{}", path.display(), entry.event, &digest[..16]),
        host: Some(host),
        file: path.to_path_buf(),
        what: format!("{what} `{}`", entry.event),
        commands: entry.commands.clone(),
        digest,
    }
}

/// The person's own files: the hook file's other entries, its helper
/// commands, and the hooks of every installed plugin.
fn user_level(scan: &mut Collect, config: &HostConfig, user_home: Option<&Path>) {
    let host = config.host;
    let Some(install) = host.spec().install.as_ref() else {
        return;
    };
    if let Some(layout) = install.hook_file.entries() {
        scan.scanned
            .insert(config.settings_path.display().to_string());
        // The lock pins this file; a file it cannot read denies every call.
        if let Ok(root) = config.read() {
            scan.entries_of(host, layout, &config.settings_path, "hook", &root, false);
            scan.programs(host, &config.settings_path, &root, user_home);
        }
        for plugins in host.spec().ledger.plugin_hooks {
            plugin_hooks(scan, host, layout, config.dir(), plugins);
        }
    }
}

/// Every installed plugin's hook file and the hooks its manifest names.
fn plugin_hooks(
    scan: &mut Collect,
    host: Host,
    layout: &'static dyn HookFile,
    dir: &Path,
    plugins: &PluginHooks,
) {
    for plugin in expand(dir, plugins.dirs) {
        let file = joined(&plugin, plugins.file);
        if file.is_file() {
            scan.entries(host, layout, &file, "plugin hook");
        }
        let Some((manifest, key)) = plugins.manifest else {
            continue;
        };
        let manifest = joined(&plugin, manifest);
        let Some(Ok(root)) = read_json(&manifest) else {
            continue;
        };
        let named = match root.get(key) {
            Some(Value::Array(list)) => list.clone(),
            Some(other) => vec![other.clone()],
            None => Vec::new(),
        };
        for item in named {
            match item {
                Value::String(path) => {
                    let Some(file) = inside(&plugin, &path) else {
                        continue;
                    };
                    if file.is_file() {
                        scan.entries(host, layout, &file, "plugin hook");
                    }
                }
                Value::Object(_) => {
                    scan.scanned.insert(manifest.display().to_string());
                    scan.entries_of(host, layout, &manifest, "plugin manifest hook", &item, true);
                }
                _ => {}
            }
        }
    }
}

/// A project's files: settings in the hook file's layout, the hook and plugin
/// directories, in the working directory and each one up to the project root.
fn project_level(scan: &mut Collect, config: &HostConfig, cwd: &Path, user_home: &Path) {
    let host = config.host;
    let (Some(spec), Some(install)) = (
        host.spec().project_settings.as_ref(),
        host.spec().install.as_ref(),
    ) else {
        return;
    };
    let layout = install.hook_file.entries();
    for root in crate::project_settings::project_dirs(cwd, user_home) {
        for name in host.spec().ledger.project_hook_files {
            let path = root.join(spec.dir).join(name);
            // The person's own file, read at the user level.
            if path == config.settings_path || !path.is_file() {
                continue;
            }
            if let Some(layout) = layout {
                scan.entries(host, layout, &path, "project hook");
            }
            if let Some(Ok(value)) = read_json(&path) {
                scan.programs(host, &path, &value, Some(user_home));
            }
        }
        project_dirs_of(scan, host, layout, spec, &root);
    }
}

/// The project's hook directory files and plugin hook files.
fn project_dirs_of(
    scan: &mut Collect,
    host: Host,
    layout: Option<&'static dyn HookFile>,
    spec: &ProjectSettingsSpec,
    root: &Path,
) {
    if let Some(dir) = &spec.hook_dir {
        for path in dir.paths() {
            for file in files_in(&root.join(path)) {
                let ext = file.extension().and_then(|e| e.to_str()).unwrap_or("");
                if !dir.extensions.contains(&ext) {
                    continue;
                }
                match layout {
                    Some(layout) if ext == "json" => {
                        scan.entries(host, layout, &file, "project hook");
                    }
                    _ => scan.whole(Some(host), &file, "project file the agent loads and runs"),
                }
            }
        }
    }
    if let Some(plugins) = &spec.plugin_dir {
        for plugin in dirs_in(&root.join(plugins.path)) {
            let file = plugin.join(plugins.hook_file);
            if let Some(layout) = layout
                && file.is_file()
            {
                scan.entries(host, layout, &file, "project plugin hook");
            }
        }
    }
}

/// Each git configuration key that runs a program, for every host.
fn git(scan: &mut Collect, cwd: &Path, user_home: &Path) {
    let mut seen: BTreeMap<String, usize> = BTreeMap::new();
    for (file, key, value) in crate::project_settings::git_config::programs(cwd, user_home) {
        let base = format!("{}#{key}", file.display());
        let count = seen.entry(base.clone()).or_insert(0);
        *count += 1;
        let id = if *count == 1 {
            base
        } else {
            format!("{base}#{count}")
        };
        // An alias runs what follows its `!` with the shell.
        let command = value.strip_prefix('!').unwrap_or(&value).to_owned();
        scan.add(Found {
            id,
            host: None,
            file,
            what: format!("git config `{key}`"),
            digest: sha256_hex(value.as_bytes()),
            commands: vec![command],
        });
    }
}

/// The file's JSON (JSON5 accepted); `None` when it is missing, `Err` when it
/// is too large or not JSON.
fn read_json(path: &Path) -> Option<Result<Value, ()>> {
    let meta = fs::metadata(path).ok()?;
    if !meta.is_file() {
        return None;
    }
    if meta.len() > MAX_BYTES {
        return Some(Err(()));
    }
    let text = fs::read_to_string(path).ok()?;
    Some(crate::json5::parse(&text).map(|p| p.value).map_err(|_| ()))
}

/// The SHA-256 of a file's bytes, or of a directory's files by their paths
/// and bytes, at most [`MAX_FILES`] of them; `None` when nothing is there.
fn digest_path(path: &Path) -> Option<String> {
    let meta = fs::symlink_metadata(path).ok()?;
    if meta.is_file() {
        return Some(sha256_hex(&fs::read(path).ok()?));
    }
    let mut files = Vec::new();
    walk(path, path, &mut files);
    files.sort();
    let mut text = Vec::new();
    for (rel, bytes) in files {
        text.extend_from_slice(rel.as_bytes());
        text.push(0);
        text.extend_from_slice(sha256_hex(&bytes).as_bytes());
        text.push(b'\n');
    }
    Some(sha256_hex(&text))
}

fn walk(root: &Path, dir: &Path, files: &mut Vec<(String, Vec<u8>)>) {
    let Ok(read) = fs::read_dir(dir) else {
        return;
    };
    for entry in read.filter_map(Result::ok) {
        if files.len() >= MAX_FILES {
            return;
        }
        let path = entry.path();
        let Ok(kind) = entry.file_type() else {
            continue;
        };
        if kind.is_dir() {
            walk(root, &path, files);
        } else if kind.is_file()
            && let Ok(bytes) = fs::read(&path)
        {
            let rel = path.strip_prefix(root).unwrap_or(&path);
            files.push((rel.to_string_lossy().into_owned(), bytes));
        }
    }
}

/// `parts` under `dir`, each [`Activity::ANY`] every directory at its level.
fn expand(dir: &Path, parts: &[&str]) -> Vec<PathBuf> {
    let mut dirs = vec![dir.to_path_buf()];
    for part in parts {
        dirs = if *part == Activity::ANY {
            dirs.iter()
                .flat_map(|d| dirs_in(d))
                .take(MAX_DIRS)
                .collect()
        } else {
            dirs.iter().map(|d| d.join(part)).collect()
        };
    }
    dirs
}

fn joined(dir: &Path, parts: &[&str]) -> PathBuf {
    parts
        .iter()
        .fold(dir.to_path_buf(), |path, part| path.join(part))
}

/// `rel` (`./config/hooks.json`) under `plugin`; `None` for a path that
/// climbs out or is absolute, which the host refuses to load.
fn inside(plugin: &Path, rel: &str) -> Option<PathBuf> {
    let rel = rel.strip_prefix("./").unwrap_or(rel);
    let climbs = Path::new(rel)
        .components()
        .any(|c| !matches!(c, std::path::Component::Normal(_)));
    (!climbs && !rel.is_empty()).then(|| plugin.join(rel))
}

/// The directories in `dir` but hidden ones, sorted; links are not followed.
fn dirs_in(dir: &Path) -> Vec<PathBuf> {
    entries(dir, fs::FileType::is_dir, false)
}

/// The files in `dir`, hidden ones too (a host's glob may load them), sorted.
fn files_in(dir: &Path) -> Vec<PathBuf> {
    entries(dir, fs::FileType::is_file, true)
}

fn entries(dir: &Path, keep: fn(&fs::FileType) -> bool, hidden: bool) -> Vec<PathBuf> {
    let Ok(read) = fs::read_dir(dir) else {
        return Vec::new();
    };
    let mut paths: Vec<PathBuf> = read
        .filter_map(Result::ok)
        .filter(|e| e.file_type().is_ok_and(|k| keep(&k)))
        .filter(|e| hidden || !e.file_name().to_string_lossy().starts_with('.'))
        .map(|e| e.path())
        .collect();
    paths.sort();
    paths
}

#[cfg(test)]
mod tests;