openmoat 0.6.0

Policy checks for AI coding agents: decides allow, defer, ask or deny for every tool call from Claude Code, Codex and Cursor, configures Claude Code's and Codex'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
use openmoat_core::{DEFAULT_POLICY, Policy};
use serde_json::{Value, json};

use super::*;
use crate::sandbox::{assert_golden, lower_for_hosts};

mod git;

pub(super) fn generated(yaml: &str) -> Generated {
    generated_for(yaml, false)
}

pub(super) fn generated_for(yaml: &str, linux: bool) -> Generated {
    let policy = Policy::parse(yaml).expect("test policy lints");
    let ir = lower_for_hosts(&policy, "/Users/me", None, Vec::new(), false).expect("lowers");
    generate(&ir, crate::sandbox::proxy_port(&policy), linux).expect("generates")
}

fn list(generated: &Generated, pointer: &str) -> Vec<String> {
    let value = Value::Object(generated.sandbox.clone());
    serde_json::from_value(value.pointer(pointer).cloned().unwrap_or(json!([]))).unwrap()
}

/// The settings and report for the default policy, as a reviewer reads them.
#[test]
fn default_policy_matches_the_golden_settings() {
    let out = generated(DEFAULT_POLICY);
    let actual = serde_json::to_string_pretty(&json!({
        "sandbox": out.sandbox,
        "permissions": { BLOCK_READS: out.block_reads },
        "losses": out.report.losses.iter().map(ToString::to_string).collect::<Vec<_>>(),
        "allowances": out.report.allowances.iter().map(ToString::to_string).collect::<Vec<_>>(),
    }))
    .unwrap();
    assert_golden("claude-default.json", &(actual + "\n"));
}

#[test]
fn every_pattern_is_absolute() {
    let out = generated(DEFAULT_POLICY);
    for pointer in [
        "/filesystem/denyRead",
        "/filesystem/allowRead",
        "/filesystem/denyWrite",
    ] {
        for p in list(&out, pointer) {
            assert!(
                p.starts_with('/'),
                "{pointer}: {p} would resolve against ~/.claude"
            );
        }
    }
    let deny = list(&out, "/filesystem/denyRead");
    for expected in [
        "/**/.env",
        "/**/.env.*",
        "/Users/me/.ssh",
        "/Users/me/.cargo/credentials.toml",
    ] {
        assert!(deny.contains(&expected.to_owned()), "{expected}: {deny:?}");
    }
    assert!(list(&out, "/filesystem/allowRead").contains(&"/Users/me/.cargo".to_owned()));
    assert!(
        !list(&out, "/filesystem/allowRead")
            .iter()
            .any(|p| p.contains(".env")),
        "a `**/` exception would re-open .env.example everywhere"
    );
    assert!(out.block_reads);
}

/// #359, #372: on Linux the runtime skips `/**/.env`, so each such read deny is
/// also a `Read(./**/…)` rule, which it expands under the working directory, and
/// each `/**/<name>` write deny an `Edit(./<name>)` rule, which it keeps.
#[test]
fn linux_adds_working_directory_rules_for_wildcard_denies() {
    let mac = generated(DEFAULT_POLICY);
    let linux = generated_for(DEFAULT_POLICY, true);
    let (every_os, linux_only) = linux.deny_rules.split_at(mac.deny_rules.len());
    assert_eq!(every_os, mac.deny_rules, "the rules of every OS come first");
    assert_eq!(
        linux_only,
        [
            "Read(./**/.env)",
            "Read(./**/.env.*)",
            "Read(./**/.envrc)",
            "Read(./**/.moat/audit.db*)",
            "Read(./**/.moat/policy.lock)",
            "Edit(./.env)",
            "Edit(./.envrc)",
            "Edit(./.moat)",
            "Edit(./.cursor/hooks.json)",
            "Edit(./.cursor/sandbox.json)",
            "Edit(./.codex/hooks.json)",
        ],
        "another agent's hook file below a directory the policy denies writing \
         (`.cursor`, `.codex`) is denied by path (#434); Claude Code keeps its own \
         `.claude` settings read-only itself"
    );
    assert_eq!(linux.sandbox, mac.sandbox, "the sandbox block is the same");
    let dropped = linux
        .report
        .allowances
        .iter()
        .find(|a| a.rule == "claude-code.linux-write-globs");
    let dropped = &dropped.expect("write globs are listed").patterns;
    for pattern in [
        "/**/.env.*",
        "/**/.moat/**",
        "/**/bin/moat",
        "/**/.git/hooks",
    ] {
        assert!(
            dropped.contains(&pattern.to_owned()),
            "{pattern}: {dropped:?}"
        );
    }
    assert!(
        !dropped.iter().any(|p| p.starts_with("/Users/me/")),
        "{dropped:?}"
    );
    for rule in [
        "claude-code.linux-read-rules",
        "claude-code.linux-read-globs",
    ] {
        let reported = linux.report.losses.iter().any(|l| l.rule == rule)
            || linux.report.allowances.iter().any(|a| a.rule == rule);
        assert!(reported, "{rule}: {:?}", linux.report);
    }
    let none = generated_for(
        "version: 1\ndeny:\n  - id: s\n    fs.read: ['~/.ssh/**']\n    fs.write: ['~/.ssh/**']\n",
        true,
    );
    assert_eq!(
        none.deny_rules,
        ["Read(//Users/me/.ssh)", "Edit(//Users/me/.ssh)"],
        "absolute denies need no working-directory rule"
    );
    assert!(!none.report.losses.iter().any(|l| l.rule.contains("linux")));
    assert!(
        !none
            .report
            .allowances
            .iter()
            .any(|a| a.rule.contains("linux"))
    );
}

#[test]
fn directory_nodes_with_entries_below_are_left_out_and_listed() {
    let out = generated(DEFAULT_POLICY);
    let deny = list(&out, "/filesystem/denyWrite");
    assert!(
        !deny.contains(&"/**/.claude".to_owned()),
        "would make .claude/worktrees read-only"
    );
    assert!(deny.contains(&"/**/.claude/settings.json".to_owned()));
    assert!(
        deny.contains(&"/Users/me/.codex".to_owned()),
        "a tree rule keeps its directory"
    );
    let nodes = out
        .report
        .allowances
        .iter()
        .find(|a| a.rule == "claude-code.directory-nodes");
    assert!(nodes.is_some_and(|a| a.patterns.contains(&"/**/.claude".to_owned())));
}

#[test]
fn network_lists_domains_and_keeps_addresses_denied() {
    let out = generated(DEFAULT_POLICY);
    let allowed = list(&out, "/network/allowedDomains");
    assert!(allowed.contains(&"registry.npmjs.org".to_owned()));
    assert!(
        !allowed.iter().any(|h| h == "localhost"),
        "an ask is a deny"
    );
    let denied = list(&out, "/network/deniedDomains");
    assert!(denied.contains(&"metadata.google.internal".to_owned()));
    assert!(!denied.iter().any(|h| h.contains("169.254")));
    assert_eq!(out.sandbox["network"]["strictAllowlist"], json!(true));
}

#[test]
fn both_proxy_ports_name_moat_proxy_only_when_the_policy_opts_in() {
    let out = generated(DEFAULT_POLICY);
    assert!(out.sandbox["network"].get("httpProxyPort").is_none());
    assert!(out.sandbox["network"].get("socksProxyPort").is_none());
    assert!(
        !out.report
            .losses
            .iter()
            .any(|l| l.rule == "claude-code.proxy")
    );
    let own = generated("version: 1\nsandbox:\n  proxy_port: 18555\n");
    assert_eq!(own.sandbox["network"]["httpProxyPort"], json!(18555));
    assert_eq!(own.sandbox["network"]["socksProxyPort"], json!(18555));
    assert!(
        own.report
            .losses
            .iter()
            .any(|l| l.rule == "claude-code.proxy" && l.message.contains("127.0.0.1:18555")),
        "{:?}",
        own.report.losses
    );
}

#[test]
fn a_read_root_inside_a_deny_is_not_reopened() {
    let out = generated(
        "version: 1\ndeny:\n  - id: s\n    fs.read: ['~/.ssh/**']\nsandbox:\n  read_roots: ['~/.ssh/pub', '/usr']\n",
    );
    assert_eq!(list(&out, "/filesystem/allowRead"), ["/usr"]);
    assert!(
        out.report
            .losses
            .iter()
            .any(|l| l.message.contains("/Users/me/.ssh/pub"))
    );
}

/// The settings and report for a policy with one env mask and one file mask,
/// as a reviewer reads them: the paired fixture is the stable contract that
/// Claude Code sees from `moat sandbox sync`.
#[test]
fn secrets_policy_matches_the_golden_settings() {
    let out = generated(SECRETS_POLICY);
    let actual = serde_json::to_string_pretty(&json!({
        "sandbox": out.sandbox,
        "permissions": { BLOCK_READS: out.block_reads },
        "losses": out.report.losses.iter().map(ToString::to_string).collect::<Vec<_>>(),
        "allowances": out.report.allowances.iter().map(ToString::to_string).collect::<Vec<_>>(),
    }))
    .unwrap();
    assert_golden("claude-secrets.json", &(actual + "\n"));
}

const SECRETS_POLICY: &str = "version: 1\n\
     allow:\n  - id: hosts\n    net: ['api.github.com', 'registry.npmjs.org']\n\
     secrets:\n  \
     - id: gh\n    host: api.github.com\n    header: Authorization\n    source: { env: GITHUB_TOKEN }\n  \
     - id: npm\n    host: registry.npmjs.org\n    header: Authorization\n    source: { file: ~/.config/moat/npm }\n";

/// #363: policy `secrets:` compile into `sandbox.credentials` with the `mask`
/// mode plus `tlsTerminate`, which Claude Code's own broker needs for HTTPS
/// injection; the entry's `injectHosts` matches the policy's host.
#[test]
fn secrets_become_claude_mask_credentials() {
    let out = generated(
        "version: 1\n\
         allow:\n  - id: a\n    net: ['api.github.com', 'registry.npmjs.org']\n\
         secrets:\n  \
         - id: gh\n    host: api.github.com\n    header: Authorization\n    source: { env: GITHUB_TOKEN }\n  \
         - id: npm\n    host: registry.npmjs.org\n    header: Authorization\n    source: { file: ~/.config/moat/npm }\n",
    );
    assert_eq!(out.sandbox["network"]["tlsTerminate"], json!({}));
    let env_vars = out.sandbox["credentials"]["envVars"].as_array().unwrap();
    assert_eq!(env_vars.len(), 1);
    assert_eq!(env_vars[0]["name"], json!("GITHUB_TOKEN"));
    assert_eq!(env_vars[0]["mode"], json!("mask"));
    assert_eq!(env_vars[0]["injectHosts"], json!(["api.github.com"]));
    let files = out.sandbox["credentials"]["files"].as_array().unwrap();
    assert_eq!(files.len(), 1);
    assert_eq!(files[0]["path"], json!("~/.config/moat/npm"));
    assert_eq!(files[0]["injectHosts"], json!(["registry.npmjs.org"]));
    assert!(
        out.sandbox["credentials"]
            .get("allowPlaintextInject")
            .is_none()
    );
    assert!(
        out.report
            .allowances
            .iter()
            .any(|a| a.rule == "claude-code.mask-credentials"
                && a.patterns == vec!["gh".to_owned(), "npm".to_owned()])
    );
}

/// `plain_http: true` on any mask entry turns `allowPlaintextInject` on so the
/// value may reach plain-HTTP destinations, and the allowance lists the
/// affected secrets so a reviewer sees which ones a plain-HTTP request may carry.
#[test]
fn plain_http_turns_on_allow_plaintext_inject() {
    let out = generated(
        "version: 1\n\
         allow:\n  - id: a\n    net: ['localhost']\n\
         secrets:\n  \
         - id: local\n    host: localhost\n    header: Authorization\n    source: { env: LOCAL_TOKEN }\n    plain_http: true\n",
    );
    assert_eq!(
        out.sandbox["credentials"]["allowPlaintextInject"],
        json!(true)
    );
    assert!(out.report.allowances.iter().any(
        |a| a.rule == "claude-code.plain-http-inject" && a.patterns == vec!["local".to_owned()]
    ));
}

/// `keychain` sources and secrets whose host is not in `allowedDomains` cannot
/// be injected by Claude Code's broker, so each is dropped from the mask block
/// with a loss that names the secret; the broker still works through `moat proxy`.
#[test]
fn unsupported_sources_and_unlisted_hosts_are_dropped_with_a_loss() {
    let out = generated(
        "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  \
         - id: kc\n    host: api.github.com\n    header: X-Api-Key\n    source: { keychain: { service: moat, account: k } }\n  \
         - id: elsewhere\n    host: example.com\n    header: Authorization\n    source: { env: EX }\n",
    );
    let env_vars = out.sandbox["credentials"]["envVars"].as_array().unwrap();
    assert_eq!(
        env_vars.len(),
        1,
        "only the env entry survives: {env_vars:?}"
    );
    assert!(out.sandbox["credentials"].get("files").is_none());
    let keychain = out.report.losses.iter().find(|l| l.rule == "secrets.kc");
    let unlisted = out
        .report
        .losses
        .iter()
        .find(|l| l.rule == "secrets.elsewhere");
    assert!(
        keychain.is_some_and(|l| l.message.contains("keychain")),
        "{:?}",
        out.report.losses
    );
    assert!(
        unlisted.is_some_and(|l| l.message.contains("`example.com` is not in `net` allow")),
        "{:?}",
        out.report.losses
    );
}

/// No `secrets:` means no `credentials` block and no `tlsTerminate`, so the
/// default policy keeps its current settings and golden fixture unchanged.
#[test]
fn no_secrets_means_no_credentials_or_tls_terminate() {
    let out = generated(DEFAULT_POLICY);
    assert!(out.sandbox.get("credentials").is_none());
    assert!(out.sandbox["network"].get("tlsTerminate").is_none());
}

/// #401: Claude Code's `allowPlaintextInject` is block-scoped, so lifting TLS
/// for a `plain_http: true` entry alongside an HTTPS-only entry would widen
/// the latter (ADR-019 lowering-widens). Mixed blocks drop the `plain_http`
/// entries from Claude Code's mask list, keep `allowPlaintextInject` out, and
/// name each dropped secret in a loss; `moat proxy` still handles them.
#[test]
fn mixed_plain_http_does_not_widen() {
    let out = generated(
        "version: 1\n\
         allow:\n  - id: a\n    net: ['api.github.com', 'localhost']\n\
         secrets:\n  \
         - id: gh\n    host: api.github.com\n    header: Authorization\n    source: { env: GITHUB_TOKEN }\n  \
         - id: local\n    host: localhost\n    header: Authorization\n    source: { env: LOCAL_TOKEN }\n    plain_http: true\n",
    );
    let env_vars = out.sandbox["credentials"]["envVars"].as_array().unwrap();
    assert_eq!(
        env_vars.len(),
        1,
        "only the HTTPS secret survives: {env_vars:?}"
    );
    assert_eq!(env_vars[0]["name"], json!("GITHUB_TOKEN"));
    assert!(
        out.sandbox["credentials"]
            .get("allowPlaintextInject")
            .is_none(),
        "no block-scoped TLS lift in a mixed block"
    );
    let dropped = out.report.losses.iter().find(|l| l.rule == "secrets.local");
    assert!(
        dropped.is_some_and(
            |l| l.message.contains("block-scoped `allowPlaintextInject`")
                && l.message.contains("`moat proxy`")
        ),
        "{:?}",
        out.report.losses
    );
    assert!(
        !out.report
            .allowances
            .iter()
            .any(|a| a.rule == "claude-code.plain-http-inject"),
        "{:?}",
        out.report.allowances
    );
    let masked = out
        .report
        .allowances
        .iter()
        .find(|a| a.rule == "claude-code.mask-credentials");
    assert!(masked.is_some_and(|a| a.patterns == vec!["gh".to_owned()]));
}

/// A policy where every secret is `plain_http: true` still gets
/// `allowPlaintextInject: true`: the happy path from #363 keeps working.
#[test]
fn all_plain_http_still_lifts_tls() {
    let out = generated(
        "version: 1\n\
         allow:\n  - id: a\n    net: ['localhost', 'dev.internal']\n\
         secrets:\n  \
         - id: a\n    host: localhost\n    header: Authorization\n    source: { env: A_TOKEN }\n    plain_http: true\n  \
         - id: b\n    host: dev.internal\n    header: Authorization\n    source: { env: B_TOKEN }\n    plain_http: true\n",
    );
    assert_eq!(
        out.sandbox["credentials"]["allowPlaintextInject"],
        json!(true)
    );
    let env_vars = out.sandbox["credentials"]["envVars"].as_array().unwrap();
    assert_eq!(env_vars.len(), 2);
    assert!(
        out.report
            .allowances
            .iter()
            .any(|a| a.rule == "claude-code.plain-http-inject"
                && a.patterns == vec!["a".to_owned(), "b".to_owned()])
    );
}

/// The settings and report for a mixed policy, paired with the golden fixture
/// a reviewer eyeballs: `allowPlaintextInject` is absent, the `plain_http`
/// secret is dropped, and the loss names it.
#[test]
fn mixed_plain_http_policy_matches_the_golden_settings() {
    let out = generated(
        "version: 1\n\
         allow:\n  - id: a\n    net: ['api.github.com', 'localhost']\n\
         secrets:\n  \
         - id: gh\n    host: api.github.com\n    header: Authorization\n    source: { env: GITHUB_TOKEN }\n  \
         - id: local\n    host: localhost\n    header: Authorization\n    source: { env: LOCAL_TOKEN }\n    plain_http: true\n",
    );
    let actual = serde_json::to_string_pretty(&json!({
        "sandbox": out.sandbox,
        "permissions": { BLOCK_READS: out.block_reads },
        "losses": out.report.losses.iter().map(ToString::to_string).collect::<Vec<_>>(),
        "allowances": out.report.allowances.iter().map(ToString::to_string).collect::<Vec<_>>(),
    }))
    .unwrap();
    assert_golden("claude-mixed-plain-http.json", &(actual + "\n"));
}