openmoat 0.23.1

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
435
436
437
438
439
440
441
442
443
444
445
//! `moat sandbox show` and `moat sandbox sync`: the host sandbox settings the policy compiles to.

use std::io::{self, Write as _};
use std::path::PathBuf;

use openmoat_core::rule_id::AGENT_CREDENTIALS;

use anyhow::{Result, bail};
use openmoat_hosts::Host;
use serde_json::{Value, json};

use crate::cli::{Format, SandboxShowArgs};
use crate::exit::Code;
use crate::home::Home;
use crate::install::{HookState, HostConfig};
use crate::integrity::{self, HookPins};
use crate::render::{self, Deferred};
use crate::sandbox::backend::{Pin, SandboxBackend, SecretMask};
use crate::sandbox::landlock::abi;
use crate::sandbox::{Grants, Plan, Report, install, landlock_rules, own_login, seatbelt_profile};

/// One host's generated settings, ready to print.
struct Shown<'a> {
    host: Host,
    path: PathBuf,
    /// What OpenMoat owns in the file, in the file's own format.
    text: String,
    report: &'a Report,
}

pub fn show(args: &SandboxShowArgs) -> Result<Code> {
    let home = Home::locate()?;
    let policy = home.load_policy()?;
    let plan = Plan::new(&home, &policy)?;
    let ctx = crate::context::eval_context(&home, None, None)?;
    let seatbelt = seatbelt_profile(&policy, &ctx, Grants::default())?;
    let landlock = landlock_rules(&policy, &ctx, Grants::default())?;
    // `moat run` in this directory, before it adds the agent, `--write` paths
    // and its proxy's port.
    let landlock_text = serde_json::to_string_pretty(&landlock.rules)?;
    let lightweight = [
        ("Seatbelt (macOS)", &seatbelt.profile, &seatbelt.report),
        ("Landlock (Linux)", &landlock_text, &landlock.report),
    ];
    // The kernel's Landlock ABI, on Linux (ADR-082).
    let kernel = super::run::landlock();
    let masks = shown_masks(&plan, &policy.secrets)?;
    let mut hosts = Vec::new();
    for host in install::hosts() {
        let Some(output) = plan.output(host) else {
            continue;
        };
        hosts.push(Shown {
            host,
            path: install::settings_path(&home, host)?,
            text: output.shown()?,
            report: output.report(),
        });
    }
    if args.format == Format::Json {
        let doc: serde_json::Map<String, Value> = hosts
            .iter()
            .map(|s| {
                let entry = match install::unavailable(s.host) {
                    Some(note) => json!({ "path": s.path, "unavailable": note }),
                    None => json!({ "path": s.path, "settings": s.text, "report": s.report }),
                };
                (s.host.id().to_owned(), entry)
            })
            .collect();
        render::json(&json!({
            "default_read_roots": plan.default_read_roots,
            "hook_only": hook_only_json(&policy),
            "agent_logins": agent_logins_json(&policy),
            "hosts": doc,
            "lightweight": {
                "seatbelt": { "profile": &seatbelt.profile, "report": &seatbelt.report },
                "landlock": {
                    "rules": &landlock.rules,
                    "report": &landlock.report,
                    "abi": kernel.as_ref().map(abi::json),
                },
            },
        }))?;
        return Ok(Code::Ok);
    }
    let mut out = io::stdout().lock();
    if plan.default_read_roots {
        writeln!(
            out,
            "note: the policy has no `sandbox.read_roots`; the default policy's list applies"
        )?;
    }
    write_secrets(&mut out, &policy.secrets, masks.as_ref())?;
    write_hook_only(&mut out, &policy)?;
    write_agent_logins(&mut out, &policy)?;
    for shown in hosts {
        if let Some(note) = install::unavailable(shown.host) {
            writeln!(out, "{}  {note}", shown.host.display_name())?;
            continue;
        }
        writeln!(
            out,
            "{}  {}",
            shown.host.display_name(),
            shown.path.display()
        )?;
        writeln!(out, "{}", shown.text.trim_end())?;
        write_report(&mut out, shown.report)?;
    }
    for (title, text, report) in lightweight {
        writeln!(
            out,
            "{title}  `openmoat run` in this directory (Lightweight tier)"
        )?;
        writeln!(out, "{}", text.trim_end())?;
        write_report(&mut out, report)?;
    }
    if let Some(kernel) = &kernel {
        writeln!(out, "{}", abi::summary(kernel))?;
        for line in abi::lines(kernel) {
            writeln!(out, "  {line}")?;
        }
    }
    writeln!(out, "dry run: nothing was written")?;
    Ok(Code::Ok)
}

/// `moat sandbox sync`: write the sandbox settings of every host that is set up
/// ([`HostConfig::set_up`]) from the current policy and re-pin them; any other
/// host's files are left as they are. A re-pin accepts what it pins, so like
/// `moat allow` it refuses over a drifted lock (#163) and without a person.
pub fn sync() -> Result<Code> {
    let home = Home::locate()?;
    let mut out = Deferred::default();
    sync_checked(&home, "openmoat sandbox sync", &mut out)?;
    out.finish()?;
    Ok(Code::Ok)
}

/// What [`sync_checked`] did: how many agents' sandbox settings changed.
pub struct Synced {
    pub updated: usize,
}

/// `moat sandbox sync` as `command` runs it (`openmoat policy upgrade` runs it
/// after writing the policy): refused from an agent, without a terminal and
/// over a drifted lock, as `sync` is; one line per host to `out`.
pub fn sync_checked(home: &Home, command: &str, out: &mut impl io::Write) -> Result<Synced> {
    crate::terminal::refuse_agent(command)?;
    if !crate::terminal::interactive() {
        bail!("`{command}` must be run by a person in a terminal, not from a hook or script");
    }
    integrity::refuse_drift(home, "sync")?;
    write_all(home, out)
}

/// Write every set-up host's sandbox settings and re-pin them, after the
/// checks of [`sync_checked`].
fn write_all(home: &Home, out: &mut impl io::Write) -> Result<Synced> {
    let plan = Plan::new(home, &home.load_policy()?)?;
    let binary = crate::install::hook_binary()?;
    let mut updated = 0;
    let (mut files, mut by_part) = (Vec::new(), Vec::new());
    if crate::install::migrate_hooks::update(home, &binary)? {
        writeln!(
            out,
            "✔ existing hooks updated; review and trust changed OpenMoat hooks in your agent"
        )?;
    }
    for host in install::hosts() {
        let path = install::settings_path(home, host)?;
        if !path.parent().is_some_and(std::path::Path::is_dir) {
            writeln!(out, "· {:<16} host not found", host.display_name())?;
            continue;
        }
        let config = HostConfig::for_host(home, host)?;
        if !config.set_up(&binary) {
            let why = match config.state(&binary) {
                HookState::Missing => "not set up",
                _ => "hook out of date or unreadable; `openmoat init` repairs it",
            };
            let (name, id) = (host.display_name(), host.id());
            writeln!(
                out,
                "· {name:<16} {why} (`openmoat init --hosts {id}` adds it)"
            )?;
            continue;
        }
        let changed = install::write(home, host, &plan, false)?;
        if let Some(note) = install::unavailable(host) {
            if changed {
                let removed = install::old_sandbox_removed(host);
                writeln!(out, "✔ {:<16} {removed}", host.display_name())?;
                files.push(path);
            }
            writeln!(out, "· {:<16} {note}", host.display_name())?;
            continue;
        }
        updated += usize::from(changed);
        let verb = if changed { "updated" } else { "unchanged" };
        writeln!(
            out,
            "✔ {:<16} {} (sandbox {verb})",
            host.display_name(),
            path.display()
        )?;
        write_report(out, install::report(host, &plan))?;
        // A file the host also edits is pinned by the part OpenMoat owns, any
        // other sandbox file whole.
        if matches!(
            install::backend(host).map(SandboxBackend::pin),
            Some(Pin::Part { .. })
        ) {
            by_part.push(path);
        } else {
            files.push(path);
        }
    }
    let lock = integrity::repin_with(home, &binary, HookPins::Keep, &files, &by_part)?;
    writeln!(
        out,
        "✔ lock             {} files and {} {} profile(s) pinned",
        lock.entries.len(),
        lock.codex_profiles.len(),
        install::pinned_by_part()
    )?;
    // The proxy reads the policy once at start-up, so a policy change needs a
    // reload. An installed service is restarted; a missing one is fine
    // (service routing is opt-in, #272).
    match super::proxy::restart_if_installed(home) {
        Ok(Some(message)) => writeln!(out, "✔ service          {message}")?,
        Ok(None) => {}
        Err(e) => writeln!(
            out,
            "! service          restart failed: {e:#}; `openmoat proxy install` restarts it by hand"
        )?,
    }
    Ok(Synced { updated })
}

/// The host whose own broker masks the policy's secrets, and what it does with
/// each of `secrets` in the settings as `sync` writes them.
fn shown_masks<'a>(
    plan: &Plan,
    secrets: &'a [openmoat_core::Secret],
) -> Result<Option<(Host, Vec<SecretMask<'a>>)>> {
    let masking = install::hosts()
        .filter(|host| install::backend(*host).is_some_and(SandboxBackend::masks_secrets))
        .find_map(|host| plan.output(host).map(|output| (host, output)));
    let Some((host, output)) = masking else {
        return Ok(None);
    };
    let settings = install::parse_json(&output.shown()?)?;
    Ok(Some((host, output.secret_masks(&settings, secrets))))
}

/// One line per policy secret: where its value is read (never the value) and
/// what `masks` says the host's broker does with it, or why it does not (the
/// `secrets.<id>` loss).
fn write_secrets(
    out: &mut impl io::Write,
    secrets: &[openmoat_core::Secret],
    masks: Option<&(Host, Vec<SecretMask<'_>>)>,
) -> io::Result<()> {
    if secrets.is_empty() {
        return Ok(());
    }
    writeln!(out, "Secrets  values are never shown")?;
    let Some((host, masks)) = masks else {
        return Ok(());
    };
    for SecretMask { secret, text, .. } in masks {
        let source = secret.source.describe();
        let state = if install::masks_unavailable() {
            format!(
                "{}'s sandbox is not available here, so it masks nothing",
                host.display_name()
            )
        } else {
            text.clone()
        };
        writeln!(
            out,
            "  {}  {} {} from {source}; {state}",
            secret.id, secret.host, secret.header
        )?;
    }
    Ok(())
}

/// A `hook_only` group's id and its file patterns by kind (`fs.read`, `fs.write`).
type HookOnly<'a> = (&'a str, Vec<(&'static str, &'a [String])>);

/// The `hook_only` deny groups (ADR-034) and their file patterns by kind:
/// the hook denies the agent's own reads and writes of them, the OS sandboxes
/// leave them to the other rules.
fn hook_only(policy: &openmoat_core::Policy) -> Vec<HookOnly<'_>> {
    use openmoat_core::Kind;
    policy
        .deny
        .iter()
        .filter(|group| group.hook_only)
        .map(|group| {
            let kinds = [Kind::FsRead, Kind::FsWrite]
                .into_iter()
                .map(|kind| (kind.as_str(), group.patterns(kind)))
                .filter(|(_, patterns)| !patterns.is_empty())
                .collect();
            (group.id.as_str(), kinds)
        })
        .collect()
}

/// [`hook_only`] for `--format json`.
fn hook_only_json(policy: &openmoat_core::Policy) -> Value {
    hook_only(policy)
        .into_iter()
        .map(|(id, kinds)| {
            let mut entry = serde_json::Map::new();
            entry.insert("id".to_owned(), json!(id));
            for (kind, patterns) in kinds {
                entry.insert(kind.to_owned(), json!(patterns));
            }
            Value::Object(entry)
        })
        .collect()
}

/// The [`hook_only`] groups, so nobody takes the OS sandbox to cover them.
fn write_hook_only(out: &mut impl io::Write, policy: &openmoat_core::Policy) -> io::Result<()> {
    let groups = hook_only(policy);
    if groups.is_empty() {
        return Ok(());
    }
    writeln!(
        out,
        "Hook only  decided by the hook only, not in the OS sandbox (ADR-034): the agent's own \
         reads and writes are denied, commands the sandbox runs can still use these files"
    )?;
    for (id, kinds) in groups {
        for (kind, patterns) in kinds {
            writeln!(out, "  {id}  {kind}: {}", patterns.join(", "))?;
        }
    }
    Ok(())
}

/// Whether `policy` denies the agents' logins in the OS sandboxes (ADR-054):
/// its `agent-credentials` group is there and not `hook_only`.
fn agent_logins_denied(policy: &openmoat_core::Policy) -> bool {
    policy
        .deny
        .iter()
        .any(|g| g.id == AGENT_CREDENTIALS && !g.hook_only)
}

/// Each agent with a login and the patterns `openmoat run` keeps readable
/// when it starts that agent, in the host list's order.
fn kept_logins() -> Vec<(Host, Vec<String>)> {
    Host::ALL
        .into_iter()
        .map(|host| (host, own_login::own_patterns(host)))
        .filter(|(_, patterns)| !patterns.is_empty())
        .collect()
}

/// [`kept_logins`] for `--format json`, under the rule they come from.
fn agent_logins_json(policy: &openmoat_core::Policy) -> Value {
    let denied = agent_logins_denied(policy);
    let kept: serde_json::Map<String, Value> = kept_logins()
        .into_iter()
        .filter(|_| denied)
        .map(|(host, patterns)| (host.id().to_owned(), json!(patterns)))
        .collect();
    json!({ "rule": AGENT_CREDENTIALS, "denied": denied, "run_keeps": kept })
}

/// Where the agents' logins are denied, and what `openmoat run` keeps of them.
fn write_agent_logins(out: &mut impl io::Write, policy: &openmoat_core::Policy) -> io::Result<()> {
    if !agent_logins_denied(policy) {
        return Ok(());
    }
    writeln!(
        out,
        "Agent logins  every sandbox denies the agents' login files to the commands an agent \
         runs (`{}`, ADR-054); the agents' own sandboxes confine only those commands, so the \
         agent still signs in. `openmoat run` holds the agent itself, so it keeps the login of \
         the agent it starts readable (`{}`), or for another program the logins inside its \
         `--write` paths:",
        AGENT_CREDENTIALS,
        own_login::ALLOWANCE
    )?;
    for (host, patterns) in kept_logins() {
        writeln!(out, "  {}  {}", host.display_name(), patterns.join(", "))?;
    }
    Ok(())
}

/// Losses and allowances, as `sandbox show`, `sandbox sync` and `doctor` print them.
pub(super) fn write_report(out: &mut impl io::Write, report: &Report) -> io::Result<()> {
    for loss in &report.losses {
        writeln!(out, "  stricter: {loss}")?;
    }
    for allowance in &report.allowances {
        writeln!(out, "  wider:    {allowance}")?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use openmoat_core::{DEFAULT_POLICY, Policy};

    use super::{hook_only_json, write_hook_only};

    #[test]
    fn show_lists_the_hook_only_groups_as_outside_the_os_sandbox() {
        let policy = Policy::parse(DEFAULT_POLICY).unwrap();
        let mut out = Vec::new();
        write_hook_only(&mut out, &policy).unwrap();
        let text = String::from_utf8(out).unwrap();
        assert!(
            text.contains("decided by the hook only, not in the OS sandbox"),
            "{text}"
        );
        assert!(
            text.contains("build-credentials  fs.read: ~/.m2/settings.xml"),
            "{text}"
        );
        assert!(text.contains("build-credentials  fs.write:"), "{text}");
        let json = hook_only_json(&policy);
        assert_eq!(json[0]["id"], "build-credentials");
        assert_eq!(json[0]["fs.read"][0], "~/.m2/settings.xml");
    }

    #[test]
    fn a_policy_without_hook_only_groups_shows_nothing_for_them() {
        let policy = Policy::parse("version: 1\n").unwrap();
        let mut out = Vec::new();
        write_hook_only(&mut out, &policy).unwrap();
        assert_eq!(out, Vec::<u8>::new());
        assert_eq!(hook_only_json(&policy), serde_json::json!([]));
    }
}