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
use super::*;

const BEFORE: &str = "version: 1
# Deny rules come first.
deny:
  - id: secrets-paths
    reason: secret material
    fs.read: [\"~/.ssh/**\"]
allow:
  - id: dev-readonly
    reason: read-only tools
    shell: [\"git status\"]
  - id: project-fs
    fs.write: [\"${project}/**\"]
ask:
  - id: runs-later
    reason: runs later
    fs.write: [\"${project}/.git/hooks/**\"]
";

/// A new rule, a pattern added to a deny rule, one added to an allow rule, an
/// exception added to an allow rule, a reworded comment, and the rename.
const AFTER: &str = "version: 1
# Deny rules come first, then allow, then review.
deny:
  - id: secrets-paths
    reason: secret material
    fs.read: [\"~/.ssh/**\", \"~/.cline/data/secrets.json\"]
  - id: card-egress
    reason: sends a payment card number to a host off this machine
    net: [\"cards.example\"]
allow:
  - id: dev-readonly
    reason: read-only tools
    shell: [\"git status\", \"git config user.name\"]
  - id: project-fs
    fs.write: [\"${project}/**\", \"!${project}/**/.git/**\"]
review:
  - id: runs-later
    reason: runs later
    fs.write: [\"${project}/.git/hooks/**\"]
";

fn change(yours: &str, default: &str) -> Change {
    Change {
        clean: true,
        line: 1,
        yours: yours.to_owned(),
        default: default.to_owned(),
    }
}

/// The stretches the merge would apply for [`AFTER`]: one comment, three of code.
fn changes() -> Vec<Change> {
    vec![
        change(
            "# Deny rules come first.\n",
            "# Deny rules come first, then allow, then review.\n",
        ),
        change(
            "    fs.read: [\"~/.ssh/**\"]\n",
            "    fs.read: [\"~/.ssh/**\", \"~/.cline/data/secrets.json\"]\n  - id: card-egress\n",
        ),
        change(
            "    shell: [\"git status\"]\n",
            "    shell: [\"git status\", \"git config user.name\"]\n",
        ),
        change(
            "    fs.write: [\"${project}/**\"]\n",
            "    fs.write: [\"${project}/**\", \"!${project}/**/.git/**\"]\n",
        ),
    ]
}

#[test]
fn each_change_is_grouped_by_what_it_does() {
    let changes = changes();
    let applied: Vec<&Change> = changes.iter().collect();
    let summary = summarise(BEFORE, AFTER, &applied, true);
    assert_eq!(
        summary,
        Summary {
            protections: vec![
                "Denied: sends a payment card number to a host off this machine (`card-egress`)"
                    .to_owned(),
                "`project-fs` no longer allows `${project}/**/.git/**`".to_owned(),
                "`secrets-paths` now also denies `~/.cline/data/secrets.json`".to_owned(),
            ],
            fewer: vec!["`dev-readonly` now also allows `git config user.name`".to_owned()],
            renamed: true,
            wording: 1,
            folded: 0,
            dir_grants: Vec::new(),
            other: Vec::new(),
            unexplained: 0,
        }
    );
    let mut shown = Vec::new();
    summary.write(&mut shown).unwrap();
    let shown = String::from_utf8(shown).unwrap();
    println!("{shown}");
    assert_eq!(
        shown,
        "  New protections
    + Denied: sends a payment card number to a host off this machine (`card-egress`)
    + `project-fs` no longer allows `${project}/**/.git/**`
    + `secrets-paths` now also denies `~/.cline/data/secrets.json`
  Fewer interruptions
    − `dev-readonly` now also allows `git config user.name`
  Renamed
    ~ `ask` is now `review` (same behaviour)
  + 1 comment and wording update
"
    );
}

#[test]
fn a_removed_pattern_and_a_reworded_reason_are_told_apart() {
    let after = BEFORE
        .replace("[\"~/.ssh/**\"]", "[]\n    fs.write: [\"~/.ssh/**\"]")
        .replace(
            "reason: runs later",
            "reason: runs later, outside any sandbox",
        );
    let changes = [change("a\n", "b\n"), change("c\n", "d\n")];
    let applied: Vec<&Change> = changes.iter().collect();
    let summary = summarise(BEFORE, &after, &applied, false);
    assert_eq!(
        summary.fewer,
        ["`secrets-paths` no longer denies `~/.ssh/**`"],
        "{summary:?}"
    );
    assert_eq!(
        summary.protections,
        ["`secrets-paths` now also denies `~/.ssh/**`"],
        "a read rule became a write rule: {summary:?}"
    );
    assert_eq!(summary.wording, 1, "{summary:?}");
}

/// ADR-059: the titles an upgrade adds change the words shown, nothing decided;
/// a new rule is named by its title.
#[test]
fn a_title_added_is_a_wording_update_and_names_a_new_rule() {
    let after = BEFORE.replace(
        "    reason: secret material\n",
        "    reason: secret material\n    title: reading your keys\n    why: they cannot be taken back\n",
    );
    let changes = [change("a\n", "b\n")];
    let applied: Vec<&Change> = changes.iter().collect();
    let summary = summarise(BEFORE, &after, &applied, false);
    assert_eq!(summary.wording, 1, "{summary:?}");
    assert_eq!(summary.unexplained, 0, "{summary:?}");
    assert!(
        summary.protections.is_empty() && summary.other.is_empty(),
        "{summary:?}"
    );

    let titled = AFTER.replace(
        "    reason: sends a payment card number to a host off this machine\n",
        "    reason: r\n    title: sending a payment card number off this machine\n",
    );
    let summary = summarise(BEFORE, &titled, &[], false);
    assert!(
        summary.protections.contains(
            &"Denied: sending a payment card number off this machine (`card-egress`)".to_owned()
        ),
        "{summary:?}"
    );
}

#[test]
fn a_change_it_cannot_name_is_counted_for_d_to_show() {
    // The same patterns in another order: the policy decides the same.
    let after = BEFORE.replace(
        "    reason: read-only tools\n    shell: [\"git status\"]\n",
        "    shell: [\"git status\"]\n    reason: read-only tools\n",
    );
    let changes = [change("    reason: x\n", "    shell: x\n")];
    let applied: Vec<&Change> = changes.iter().collect();
    let summary = summarise(BEFORE, &after, &applied, false);
    assert_eq!(summary.unexplained, 1, "{summary:?}");
    let mut shown = Vec::new();
    summary.write(&mut shown).unwrap();
    assert_eq!(
        String::from_utf8(shown).unwrap(),
        "  Other changes: 1 (d to see)\n"
    );
    // A policy that does not read is never described as more than that.
    let summary = summarise(BEFORE, "version: [", &applied, false);
    assert_eq!(summary.unexplained, 1, "{summary:?}");
    assert!(summary.protections.is_empty() && summary.fewer.is_empty());
}

#[test]
fn a_section_besides_the_rules_is_named() {
    let after = format!("{BEFORE}executables:\n  git: [\"/usr/bin/git\"]\n");
    let changes = [change("", "executables:\n")];
    let applied: Vec<&Change> = changes.iter().collect();
    let summary = summarise(BEFORE, &after, &applied, false);
    assert_eq!(summary.other, ["`executables:` changed"], "{summary:?}");
    assert_eq!(summary.unexplained, 0);
}

/// The OpenMoat 0.3.0 default upgraded to this release's: what a person who
/// set up OpenMoat then is shown.
#[test]
fn the_0_3_0_default_upgraded_to_this_release() {
    let dir = tempfile::tempdir().unwrap();
    let home = crate::home::Home::at(dir.path().join("state"));
    std::fs::create_dir_all(home.root()).unwrap();
    let v0_3_0 = include_str!("../../../../upgrade/default-v0.3.0.yaml");
    std::fs::write(home.policy_path(), v0_3_0).unwrap();
    let plan = crate::upgrade::Plan::for_home(&home).unwrap();
    let applied: Vec<&Change> = plan.merge.applied().collect();
    let summary = summarise(&plan.current, &plan.merge.text, &applied, plan.renamed);
    let mut shown = Vec::new();
    summary.write(&mut shown).unwrap();
    let shown = String::from_utf8(shown).unwrap();
    println!("{shown}");
    assert!(summary.renamed, "{shown}");
    assert!(
        summary
            .protections
            .iter()
            .any(|l| l.contains("(`card-egress`)")),
        "{shown}"
    );
    assert!(
        summary.fewer.iter().any(|l| l.contains("web.search")) || shown.contains("defaults"),
        "the 0.4.0 `web.search: allow` default is named: {shown}"
    );
}

/// The 0.16.0 default's lines that 0.17.0 replaced: `runs-later` and
/// `project-fs` named `.vscode/tasks.json` alone, where 0.17.0 names all of `.vscode/`.
const V0_16_RUNS_LATER: (&str, &str) = (
    "      \"**/.vscode/**\", \"**/.vscode\", \"**/*.code-workspace\", ",
    "      \"**/.vscode/tasks.json\", ",
);
const V0_16_PROJECT_FS: (&str, &str) = (
    "      \"!${project}/**/.vscode/**\", \"!${project}/**/.vscode\", \"!${project}/**/*.code-workspace\",\n",
    "      \"!${project}/**/.vscode/tasks.json\",\n",
);

/// 0.16.0 → 0.17.0 as the maintainer met it: `.vscode/tasks.json` went into
/// `.vscode/**`, so it still needs review; nothing interrupts less.
#[test]
fn a_pattern_folded_into_a_broader_one_is_not_fewer_interruptions() {
    let after = openmoat_core::DEFAULT_POLICY.replace("\r\n", "\n");
    let before =
        [V0_16_RUNS_LATER, V0_16_PROJECT_FS]
            .iter()
            .fold(after.clone(), |text, (now, then)| {
                assert!(
                    text.contains(now),
                    "the 0.17.0 line is in the default: {now}"
                );
                text.replacen(now, then, 1)
            });
    let summary = summarise(&before, &after, &[], false);
    assert!(summary.fewer.is_empty(), "{summary:?}");
    assert_eq!(summary.folded, 2, "{summary:?}");
    assert!(
        summary
            .protections
            .iter()
            .any(|l| l.starts_with("`runs-later` now also holds") && l.contains("`**/.vscode/**`")),
        "{summary:?}"
    );
    assert!(
        summary
            .protections
            .iter()
            .any(|l| l.starts_with("`project-fs` no longer allows")),
        "{summary:?}"
    );
    let mut shown = Vec::new();
    summary.write(&mut shown).unwrap();
    let shown = String::from_utf8(shown).unwrap();
    assert!(!shown.contains("Fewer interruptions"), "{shown}");
    assert!(
        shown.contains("  + 2 narrow patterns now matched by a broader one\n"),
        "{shown}"
    );
}

/// A pattern taken out with nothing as broad left still interrupts less, in a
/// review group and as an exclusion of an allow group alike.
#[test]
fn a_removal_nothing_covers_is_still_fewer_interruptions() {
    let narrowed = BEFORE.replace(
        "    fs.write: [\"${project}/.git/hooks/**\"]",
        "    fs.write: [\"${project}/.git/hooks/pre-commit\"]",
    );
    let summary = summarise(BEFORE, &narrowed, &[], false);
    assert_eq!(
        summary.fewer,
        ["`runs-later` no longer holds for review `${project}/.git/hooks/**`"],
        "{summary:?}"
    );
    assert_eq!(summary.folded, 0, "{summary:?}");

    // `!` exclusions of `project-fs`: one folded into a broader one, one dropped
    let wider = AFTER.replace(
        "[\"${project}/**\", \"!${project}/**/.git/**\"]",
        "[\"${project}/**\", \"!${project}/**/.git/**\", \"!${project}/**/.vscode/tasks.json\", \"!${project}/.env\"]",
    );
    let broader = AFTER.replace(
        "[\"${project}/**\", \"!${project}/**/.git/**\"]",
        "[\"${project}/**\", \"!${project}/**/.git/**\", \"!${project}/**/.vscode/**\"]",
    );
    let summary = summarise(&wider, &broader, &[], false);
    assert_eq!(summary.folded, 1, "{summary:?}");
    assert_eq!(
        summary.fewer,
        ["`project-fs` now also allows `${project}/.env`"],
        "{summary:?}"
    );
    assert_eq!(
        summary.protections,
        ["`project-fs` no longer allows `${project}/**/.vscode/**`"],
        "{summary:?}"
    );
}

/// 0.26.0 → 0.27.0 as the maintainer met it: 15 `kernel-self` lines gained
/// `team` in their `{…}` list. That is one broadening, shown once under "New
/// protections", never as 15 patterns no longer denied.
#[test]
fn a_list_that_gained_an_entry_is_one_broadening_not_a_removal() {
    let v0_26_0 = include_str!("default-v0.26.0.yaml").replace("\r\n", "\n");
    let after = openmoat_core::DEFAULT_POLICY.replace("\r\n", "\n");
    let summary = summarise(&v0_26_0, &after, &[], false);
    let mut shown = Vec::new();
    summary.write(&mut shown).unwrap();
    let shown = String::from_utf8(shown).unwrap();
    println!("{shown}");
    assert!(
        summary.fewer.iter().all(|l| !l.contains("kernel-self")),
        "{shown}"
    );
    assert!(!shown.contains("no longer denies"), "{shown}");
    assert!(
        summary
            .protections
            .contains(&"`kernel-self`: 15 patterns now also cover `team`".to_owned()),
        "{shown}"
    );
    // The two `team join|update|leave` lines and the two for remote MCP
    // tokens (`~/.moat/mcp-remote/`) are new, not broadenings.
    assert!(
        summary
            .protections
            .contains(&"`kernel-self` now also denies 4 patterns".to_owned()),
        "{shown}"
    );
}

/// A true removal stays one, beside a broadening in the same group; a
/// broadened allow pattern and a broadened exception go where they lead.
#[test]
fn a_removal_beside_a_broadening_is_still_named() {
    let before = "version: 1
deny:
  - id: self
    shell: [\"x {allow,trust}*\", \"y {a,b}*\", \"z *\"]
allow:
  - id: tools
    shell: [\"git {status,log}\"]
    fs.write: [\"${project}/**\", \"!${project}/{.git}/**\"]
";
    let after = "version: 1
deny:
  - id: self
    shell: [\"x {allow,team,trust}*\", \"y {a,c}*\"]
allow:
  - id: tools
    shell: [\"git {status,log,diff}\"]
    fs.write: [\"${project}/**\", \"!${project}/{.git,.vscode}/**\"]
";
    let summary = summarise(before, after, &[], false);
    assert_eq!(
        summary.protections,
        [
            "`self`: 1 pattern now also covers `team`",
            "`self` now also denies `y {a,c}*`",
            "`tools`: 1 exception now also covers `.vscode`",
        ],
        "{summary:?}"
    );
    assert_eq!(
        summary.fewer,
        [
            "`self` no longer denies `y {a,b}*`, `z *`",
            "`tools`: 1 pattern now also covers `diff`",
        ],
        "{summary:?}"
    );
}

#[test]
fn comments_are_read_as_yaml_reads_them() {
    assert_eq!(strip_comment("  - id: x # note"), "  - id: x ");
    assert_eq!(
        strip_comment("    shell: [\"a # b\"]"),
        "    shell: [\"a # b\"]"
    );
    assert_eq!(strip_comment("# all of it"), "");
    assert_eq!(strip_comment("  url: a#b"), "  url: a#b");
    assert_eq!(
        without_comments("# one\n\n  - id: x  # two\n"),
        without_comments("# changed\n  - id: x\n")
    );
}