openmoat 0.12.0

Policy checks for AI coding agents: decides allow, defer, ask or deny for every tool call from Claude Code, Codex, Cursor, OpenClaw, GitHub Copilot CLI, pi and Gemini CLI, 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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! Merging the generated block into Claude Code's user settings, and reading
//! it back for `moat doctor`.

use std::path::Path;

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

use super::deny_list::{deny_rules_in_sync, is_owned_rule, write_deny_rules};
use super::git_env;
use super::owned::{OWNED, strip_stale};
use super::{BLOCK_READS, Generated};

/// Merge `generated` into the settings document `root`, keeping every key
/// OpenMoat does not own. `owned` holds the `permissions.deny` rules an earlier
/// apply added (`hosts.json`): they are replaced, and on return `owned` holds
/// the rules this one added. `env` also gets git's `safe.bareRepository=explicit`.
/// Returns whether anything changed.
pub fn apply(root: &mut Value, generated: &Generated, owned: &mut Vec<String>) -> Result<bool> {
    let Value::Object(top) = root else {
        bail!("the settings file is not a JSON object");
    };
    let before = top.clone();
    let sandbox = object_at(top, "sandbox")?;
    strip_stale(sandbox, generated);
    for (key, value) in &generated.sandbox {
        match value {
            Value::Object(owned) => {
                let nested = object_at(sandbox, key)?;
                for (sub, v) in owned {
                    nested.insert(sub.clone(), v.clone());
                }
            }
            other => {
                sandbox.insert(key.clone(), other.clone());
            }
        }
    }
    if generated.block_reads {
        object_at(top, "permissions")?.insert(BLOCK_READS.into(), json!(true));
    }
    write_deny_rules(top, generated, owned)?;
    git_env::apply(top)?;
    Ok(*top != before)
}

/// Whether `rule` is spelled the way earlier versions wrote their Linux
/// `permissions.deny` rules: `Read(./**/…)`, `Edit(./<name>)` naming one
/// working-directory entry, or `Edit(./.<dir>/<name>)` naming one entry of a
/// hidden directory there.
pub(super) fn is_linux_spelling(rule: &Value) -> bool {
    let entry = |name: &str| !name.is_empty() && !name.contains(['/', ')', '*', '?', '[', '{']);
    rule.as_str().is_some_and(|r| {
        r.starts_with("Read(./**/")
            || r.strip_prefix("Edit(./")
                .and_then(|r| r.strip_suffix(')'))
                .is_some_and(|path| match path.split_once('/') {
                    None => entry(path),
                    Some((dir, name)) => dir.starts_with('.') && entry(dir) && entry(name),
                })
    })
}

/// Remove OpenMoat's rules from `permissions.deny`, and the list when that
/// empties it. Returns whether anything was removed.
pub(super) fn strip_deny_rules(permissions: &mut Map<String, Value>, owned: &[String]) -> bool {
    let Some(Value::Array(deny)) = permissions.get_mut("deny") else {
        return false;
    };
    let before = deny.len();
    deny.retain(|rule| !is_owned_rule(rule, owned));
    let removed = deny.len() != before;
    if removed && deny.is_empty() {
        permissions.remove("deny");
    }
    removed
}

/// The inverse of [`apply`]: remove every key OpenMoat writes and the
/// `permissions.deny` rules it added (`owned`), then the objects that leaves
/// empty. Works without a policy, so `moat uninstall` never needs one. Returns
/// whether anything was removed.
pub fn remove(root: &mut Value, owned: &[String]) -> bool {
    let Value::Object(top) = root else {
        return false;
    };
    let mut removed = false;
    if let Some(Value::Object(sandbox)) = top.get_mut("sandbox") {
        for (key, subs) in OWNED {
            if subs.is_empty() {
                removed |= sandbox.remove(*key).is_some();
            } else if let Some(Value::Object(nested)) = sandbox.get_mut(*key) {
                let before = nested.len();
                nested.retain(|sub, _| !subs.contains(&sub.as_str()));
                if nested.len() != before {
                    removed = true;
                    if nested.is_empty() {
                        sandbox.remove(*key);
                    }
                }
            }
        }
    }
    if let Some(Value::Object(permissions)) = top.get_mut("permissions") {
        removed |= permissions.remove(BLOCK_READS).is_some();
        removed |= strip_deny_rules(permissions, owned);
    }
    removed |= git_env::remove(top);
    if removed {
        for key in ["sandbox", "permissions"] {
            if top
                .get(key)
                .and_then(Value::as_object)
                .is_some_and(Map::is_empty)
            {
                top.remove(key);
            }
        }
    }
    removed
}

/// Deny sandboxed writes to the settings files next to `settings`, wherever
/// `CLAUDE_CONFIG_DIR` puts them: the policy names only `~/.claude`, and OpenMoat
/// pins this file.
pub fn protect(generated: &mut Generated, settings: &Path) {
    let Some(dir) = settings.parent() else {
        return;
    };
    let paths: Vec<String> = ["settings.json", "settings.local.json"]
        .iter()
        .map(|name| crate::context::path_string(&dir.join(name)))
        .collect();
    deny_write(generated, &paths);
}

/// Add `paths` to `denyWrite` as written, after the generator's own passes
/// (a directory here covers its subtree, which the `moat` binary's wants).
pub fn deny_write(generated: &mut Generated, paths: &[String]) {
    let deny = generated
        .sandbox
        .get_mut("filesystem")
        .and_then(|f| f.get_mut("denyWrite"))
        .and_then(Value::as_array_mut);
    if let Some(deny) = deny {
        for path in paths {
            let path = json!(path);
            if !deny.contains(&path) {
                deny.push(path);
            }
        }
    }
}

/// Whether every key OpenMoat owns in `root` has its generated value, with
/// `ours` the `permissions.deny` rules it recorded adding.
pub fn in_sync(root: &Value, generated: &Generated, ours: &[String]) -> bool {
    let owned = generated.sandbox.iter().all(|(key, value)| match value {
        Value::Object(owned) => owned
            .iter()
            .all(|(sub, v)| root.pointer(&format!("/sandbox/{key}/{sub}")) == Some(v)),
        other => root.pointer(&format!("/sandbox/{key}")) == Some(other),
    });
    owned
        && (!generated.block_reads || block_reads_on(root))
        && deny_rules_in_sync(root, generated, ours)
        && git_env::in_sync(root)
}

fn block_reads_on(root: &Value) -> bool {
    root.pointer(&format!("/permissions/{BLOCK_READS}")) == Some(&json!(true))
}

/// Settings in `root` that weaken the sandbox, worded for `moat doctor`.
pub fn weaknesses(root: &Value, expect_block_reads: bool, proxy_port: Option<u16>) -> Vec<String> {
    let at = |path: &str| root.pointer(path);
    let is = |path: &str, value: bool| at(path) == Some(&json!(value));
    let mut out = Vec::new();
    for key in ["httpProxyPort", "socksProxyPort"] {
        let set = at(&format!("/sandbox/network/{key}")).filter(|v| !v.is_null());
        match proxy_port {
            Some(port) if set != Some(&json!(port)) => out.push(format!(
                "sandbox.network.{key} is not {port}: that traffic does not go through `openmoat proxy`"
            )),
            None if set.is_some() => out.push(format!(
                "sandbox.network.{key} is set: another proxy decides that traffic, not the allowlist"
            )),
            _ => {}
        }
    }
    let mut flag = |bad: bool, text: &str| {
        if bad {
            out.push(text.to_owned());
        }
    };
    flag(
        !is("/sandbox/enabled", true),
        "sandbox.enabled is not true: Bash runs unsandboxed",
    );
    flag(
        !is("/sandbox/failIfUnavailable", true),
        "sandbox.failIfUnavailable is not true: commands run unsandboxed when the sandbox cannot start",
    );
    flag(
        !is("/sandbox/allowUnsandboxedCommands", false),
        "sandbox.allowUnsandboxedCommands is not false: the model may run a command outside the sandbox",
    );
    flag(
        at("/sandbox/excludedCommands")
            .and_then(Value::as_array)
            .is_some_and(|a| !a.is_empty()),
        "sandbox.excludedCommands is not empty: those commands run unsandboxed",
    );
    flag(
        !is("/sandbox/network/strictAllowlist", true),
        "sandbox.network.strictAllowlist is not true: unlisted hosts prompt instead of failing",
    );
    for (path, text) in [
        (
            "/sandbox/filesystem/disabled",
            "sandbox.filesystem.disabled turns file isolation off",
        ),
        (
            "/sandbox/network/allowAllUnixSockets",
            "sandbox.network.allowAllUnixSockets opens every Unix socket",
        ),
        (
            "/sandbox/enableWeakerNetworkIsolation",
            "sandbox.enableWeakerNetworkIsolation is on",
        ),
        (
            "/sandbox/enableWeakerNestedSandbox",
            "sandbox.enableWeakerNestedSandbox is on",
        ),
    ] {
        flag(is(path, true), text);
    }
    flag(
        at("/sandbox/ignoreViolations").is_some_and(|v| !v.is_null()),
        "sandbox.ignoreViolations hides sandbox violations",
    );
    flag(
        expect_block_reads && !block_reads_on(root),
        "permissions.blockReadsOutsideWorkingDirectories is not true: sandboxed commands read the whole home directory",
    );
    out
}

pub(super) fn object_at<'a>(
    map: &'a mut Map<String, Value>,
    key: &str,
) -> Result<&'a mut Map<String, Value>> {
    match map.entry(key).or_insert_with(|| Value::Object(Map::new())) {
        Value::Object(nested) => Ok(nested),
        _ => bail!("`{key}` in the settings file is not an object"),
    }
}

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

    use super::super::tests::{generated, generated_for};
    use super::*;

    #[test]
    fn apply_keeps_user_keys_and_is_idempotent() {
        let mut owned = Vec::new();
        let out = generated(DEFAULT_POLICY);
        let mut root = json!({
            "theme": "dark",
            "hooks": { "PreToolUse": [] },
            "permissions": { "allow": ["Bash(ls)"] },
            "sandbox": { "autoAllowBashIfSandboxed": false, "network": { "allowUnixSockets": ["/x.sock"] } },
        });
        assert!(!in_sync(&root, &out, &owned));
        assert!(apply(&mut root, &out, &mut owned).unwrap());
        assert!(
            !apply(&mut root, &out, &mut owned).unwrap(),
            "second apply changes nothing"
        );
        assert!(in_sync(&root, &out, &owned));
        assert_eq!(root["theme"], "dark");
        assert_eq!(root["permissions"]["allow"], json!(["Bash(ls)"]));
        assert_eq!(root["permissions"][BLOCK_READS], json!(true));
        assert_eq!(root["sandbox"]["autoAllowBashIfSandboxed"], json!(false));
        assert_eq!(
            root["sandbox"]["network"]["allowUnixSockets"],
            json!(["/x.sock"])
        );
        assert!(
            weaknesses(&root, true, None).is_empty(),
            "{:?}",
            weaknesses(&root, true, None)
        );
        assert!(apply(&mut json!([]), &out, &mut Vec::new()).is_err());
        assert!(apply(&mut json!({"sandbox": 1}), &out, &mut Vec::new()).is_err());
    }

    #[test]
    fn remove_undoes_apply_and_keeps_user_keys() {
        let mut owned = Vec::new();
        let original = json!({
            "theme": "dark",
            "permissions": { "allow": ["Bash(ls)"] },
            "sandbox": { "autoAllowBashIfSandboxed": false, "network": { "allowUnixSockets": ["/x.sock"] } },
        });
        let mut root = original.clone();
        apply(&mut root, &generated(DEFAULT_POLICY), &mut owned).unwrap();
        assert!(remove(&mut root, &owned));
        assert_eq!(root, original);
        assert!(!remove(&mut root, &owned));

        let mut root = json!({});
        apply(
            &mut root,
            &generated("version: 1\nsandbox:\n  proxy_port: 18555\n"),
            &mut owned,
        )
        .unwrap();
        assert!(remove(&mut root, &owned));
        assert_eq!(root, json!({}), "every key apply writes is owned");
    }

    #[test]
    fn linux_deny_rules_join_the_user_deny_list_and_leave_with_uninstall() {
        let mut owned = Vec::new();
        let out = generated_for(DEFAULT_POLICY, true);
        let user = [
            "Read(~/secret)",
            "Edit(./src/**)",
            "Edit(//tmp/x)",
            "Edit(./src/main.rs)",
            "Edit(./.vscode/a/b)",
        ];
        let mut deny = user.to_vec();
        deny.extend(["Read(./**/old)", "Edit(./old)", "Edit(./.old/hooks.json)"]);
        let mut root = json!({ "permissions": { "deny": deny } });
        assert!(apply(&mut root, &out, &mut owned).unwrap());
        assert!(!apply(&mut root, &out, &mut owned).unwrap(), "idempotent");
        assert!(in_sync(&root, &out, &owned));
        let mut expected: Vec<Value> = user.iter().map(|r| json!(r)).collect();
        expected.extend(out.deny_rules.iter().map(|r| json!(r)));
        assert_eq!(
            root["permissions"]["deny"],
            json!(expected),
            "stale rules go"
        );
        assert!(
            !in_sync(&root, &generated(DEFAULT_POLICY), &owned),
            "rules macOS does not write"
        );
        assert!(remove(&mut root, &owned));
        assert_eq!(root, json!({ "permissions": { "deny": user } }));

        let mut root = json!({});
        apply(&mut root, &out, &mut owned).unwrap();
        assert!(remove(&mut root, &owned));
        assert_eq!(root, json!({}), "the deny list it created goes too");
        assert!(
            apply(
                &mut json!({ "permissions": { "deny": {} } }),
                &out,
                &mut Vec::new()
            )
            .is_err()
        );
    }

    #[test]
    fn weaknesses_name_every_weakened_setting() {
        let mut owned = Vec::new();
        let out = generated(DEFAULT_POLICY);
        let mut root = json!({});
        apply(&mut root, &out, &mut owned).unwrap();
        root["sandbox"]["enabled"] = json!(false);
        root["sandbox"]["failIfUnavailable"] = Value::Null;
        root["sandbox"]["allowUnsandboxedCommands"] = json!(true);
        root["sandbox"]["excludedCommands"] = json!(["curl"]);
        root["sandbox"]["network"]["allowAllUnixSockets"] = json!(true);
        root["permissions"][BLOCK_READS] = json!(false);
        root["sandbox"]["network"]["httpProxyPort"] = json!(8080);
        let found = weaknesses(&root, true, None).join("\n");
        for key in [
            "httpProxyPort is set",
            "sandbox.enabled",
            "failIfUnavailable",
            "allowUnsandboxedCommands",
            "excludedCommands",
            "allowAllUnixSockets",
            BLOCK_READS,
        ] {
            assert!(found.contains(key), "{key}: {found}");
        }
        assert!(!in_sync(&root, &out, &owned));
    }

    #[test]
    fn weaknesses_name_a_changed_or_missing_moat_proxy_port() {
        let mut owned = Vec::new();
        let out = generated("version: 1\nsandbox:\n  proxy_port: 18555\n");
        let mut root = json!({});
        apply(&mut root, &out, &mut owned).unwrap();
        assert!(weaknesses(&root, false, Some(18555)).is_empty());
        root["sandbox"]["network"]["httpProxyPort"] = json!(8080);
        let network = root["sandbox"]["network"].as_object_mut().unwrap();
        network.remove("socksProxyPort");
        let found = weaknesses(&root, false, Some(18555)).join("\n");
        for key in ["httpProxyPort is not 18555", "socksProxyPort is not 18555"] {
            assert!(found.contains(key), "{key}: {found}");
        }
        assert!(!in_sync(&root, &out, &owned));
    }

    #[test]
    fn protect_denies_writes_to_the_settings_moat_pins() {
        let mut out = generated(DEFAULT_POLICY);
        protect(&mut out, Path::new("/cfg/claude/settings.json"));
        protect(&mut out, Path::new("/cfg/claude/settings.json"));
        let deny = &out.sandbox["filesystem"]["denyWrite"];
        let count = |p: &str| deny.as_array().unwrap().iter().filter(|v| *v == p).count();
        assert_eq!(count("/cfg/claude/settings.json"), 1, "once, however often");
        assert_eq!(count("/cfg/claude/settings.local.json"), 1);
        let binary = ["/opt/t/moatd".to_owned(), "/opt/t".to_owned()];
        deny_write(&mut out, &binary);
        deny_write(&mut out, &binary);
        let deny = &out.sandbox["filesystem"]["denyWrite"];
        let count = |p: &str| deny.as_array().unwrap().iter().filter(|v| *v == p).count();
        assert_eq!((count("/opt/t/moatd"), count("/opt/t")), (1, 1));
    }

    const WITH_SECRET: &str = "version: 1\n\
         allow:\n  - id: a\n    net: ['api.github.com']\n\
         secrets:\n  \
         - id: gh\n    host: api.github.com\n    header: Authorization\n    source: { env: GITHUB_TOKEN }\n";

    #[test]
    fn apply_writes_credentials_and_tls_terminate_from_the_policy() {
        let mut owned = Vec::new();
        let out = generated(WITH_SECRET);
        let aws = json!([{ "idName": "A", "secretName": "B" }]);
        let mut root = json!({ "sandbox": { "credentials": { "awsPairs": aws.clone() } } });
        assert!(apply(&mut root, &out, &mut owned).unwrap());
        assert!(!apply(&mut root, &out, &mut owned).unwrap(), "idempotent");
        assert!(in_sync(&root, &out, &owned));
        assert_eq!(root["sandbox"]["network"]["tlsTerminate"], json!({}));
        let env = &root["sandbox"]["credentials"]["envVars"][0];
        assert_eq!(env["name"], json!("GITHUB_TOKEN"));
        assert_eq!(env["mode"], json!("mask"));
        assert_eq!(
            root["sandbox"]["credentials"]["awsPairs"], aws,
            "user keys survive"
        );
        assert!(remove(&mut root, &owned));
        assert_eq!(
            root,
            json!({ "sandbox": { "credentials": { "awsPairs": aws } } })
        );
    }

    #[test]
    fn dropping_a_secret_strips_the_stale_mask_entry_and_tls_terminate() {
        let mut owned = Vec::new();
        let with = generated(WITH_SECRET);
        let without = generated(DEFAULT_POLICY);
        let mut root = json!({});
        apply(&mut root, &with, &mut owned).unwrap();
        assert!(root["sandbox"].get("credentials").is_some());
        assert_eq!(root["sandbox"]["network"]["tlsTerminate"], json!({}));
        assert!(
            apply(&mut root, &without, &mut owned).unwrap(),
            "removes the stale keys"
        );
        assert!(root["sandbox"].get("credentials").is_none());
        assert!(root["sandbox"]["network"].get("tlsTerminate").is_none());
        assert!(in_sync(&root, &without, &owned));
    }
}