clash 0.6.1

Command Line Agent Safety Harness — permission policies for coding agents
Documentation
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//! macOS sandbox backend using Seatbelt (sandbox-exec / SBPL).
//!
//! Generates a Sandbox Profile Language (SBPL) profile from the SandboxPolicy
//! and applies it via `sandbox-exec -p <profile>`.

use std::path::Path;

use crate::policy::sandbox_types::{
    Cap, NetworkPolicy, PathMatch, RuleEffect, SandboxPolicy, resolve_symlinks,
};
use tracing::{Level, instrument};

use super::{SandboxError, SupportLevel};

/// Apply sandbox policy and exec the command via sandbox-exec.
///
/// `trace_path` is accepted for API consistency with the platform-agnostic
/// `exec_sandboxed()` in `mod.rs`. Modern macOS does not support the SBPL
/// `(trace)` directive or the `sandbox-exec -t` flag, so tracing is a no-op.
/// Sandbox violation detection relies on stderr heuristics in PostToolUse
/// (`sandbox_hints`).
#[instrument(level = Level::TRACE, skip(policy))]
pub fn exec_sandboxed(
    policy: &SandboxPolicy,
    cwd: &Path,
    command: &[String],
    _trace_path: Option<&Path>,
) -> Result<std::convert::Infallible, SandboxError> {
    let cwd_str = cwd.to_string_lossy();
    let profile = compile_to_sbpl(policy, &cwd_str);

    // Build: sandbox-exec -p <profile> -- <command...>
    let mut args = vec!["sandbox-exec".to_string()];
    args.push("-p".to_string());
    args.push(profile);
    args.push("--".to_string());
    args.extend_from_slice(command);

    super::do_exec(&args)
}

/// Check if sandbox-exec is available.
#[instrument(level = Level::TRACE)]
pub fn check_support() -> SupportLevel {
    if Path::new("/usr/bin/sandbox-exec").exists() {
        SupportLevel::Full
    } else {
        SupportLevel::Unsupported {
            reason: "/usr/bin/sandbox-exec not found".into(),
        }
    }
}

/// Compile a SandboxPolicy into a Seatbelt SBPL profile string.
#[instrument(level = Level::TRACE)]
pub fn compile_to_sbpl(policy: &SandboxPolicy, cwd: &str) -> String {
    let mut p = String::from("(version 1)\n(deny default)\n");

    // Always needed for basic process operation
    p += "(allow process-fork)\n";
    p += "(allow sysctl-read)\n";
    p += "(allow mach-lookup)\n";
    p += "(allow mach-register)\n";

    // DNS resolution via mDNSResponder
    p += "(allow system-socket)\n";

    // Default capabilities applied to root
    emit_caps_for_path(&mut p, "/", policy.default, PathMatch::Subpath);

    // Root directory readable (literal, not recursive) — some commands need
    // to stat "/" itself (e.g. `ls /`, path resolution).
    p += "(allow file-read* (literal \"/\"))\n";

    // /dev/null always writable
    p += "(allow file-write* (literal \"/dev/null\"))\n";

    // Collect all ancestor directories that need literal read access so that
    // realpath() / stat() can traverse to rule paths.  Seatbelt evaluates each
    // stat() independently, so `(subpath "/Users/eliot/.cargo")` does NOT
    // implicitly allow stat("/Users") or stat("/Users/eliot").
    let mut ancestors: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();

    // Resolve all rule paths and collect ancestors, then sort by
    // specificity before emitting.  Seatbelt uses last-match-wins, so
    // we emit broadest rules first and most specific last — ensuring a
    // specific allow on /Users/eliot overrides a broad deny on /Users.
    struct ResolvedRule {
        effect: RuleEffect,
        caps: Cap,
        path_match: PathMatch,
        canonical: String,
        resolved: String,
    }

    let mut resolved_rules: Vec<ResolvedRule> = Vec::with_capacity(policy.rules.len());

    for rule in &policy.rules {
        let resolved = SandboxPolicy::resolve_path(&rule.path, cwd);
        // Strip trailing slashes — Seatbelt's `subpath` filter requires
        // paths without trailing separators (e.g. $TMPDIR ends with "/").
        let resolved = if resolved.len() > 1 {
            resolved.trim_end_matches('/').to_string()
        } else {
            resolved
        };
        let canonical = if rule.path_match != PathMatch::Regex {
            resolve_symlinks(&resolved)
        } else {
            resolved.clone()
        };

        // Collect ancestor directories for path traversal (both forms)
        if rule.path_match != PathMatch::Regex {
            for path in [&canonical, &resolved] {
                let mut dir = path.as_str();
                while let Some(pos) = dir.rfind('/') {
                    if pos == 0 {
                        break; // "/" is already handled above
                    }
                    dir = &dir[..pos];
                    if !ancestors.insert(dir.to_string()) {
                        break; // Already seen this ancestor and all its parents
                    }
                }
            }
        }

        resolved_rules.push(ResolvedRule {
            effect: rule.effect,
            caps: rule.caps,
            path_match: rule.path_match,
            canonical,
            resolved,
        });
    }

    // Sort by path depth (component count) ascending so broadest rules
    // are emitted first.  Within the same depth, deny before allow so
    // that a same-level allow wins via last-match-wins.
    resolved_rules.sort_by(|a, b| {
        let depth_a = a.canonical.matches('/').count();
        let depth_b = b.canonical.matches('/').count();
        depth_a.cmp(&depth_b).then_with(|| {
            // Deny = 0 (first), Allow = 1 (last) — allow wins at same depth
            let effect_ord = |e: &RuleEffect| match e {
                RuleEffect::Deny => 0,
                RuleEffect::Allow => 1,
            };
            effect_ord(&a.effect).cmp(&effect_ord(&b.effect))
        })
    });

    for rule in &resolved_rules {
        match rule.effect {
            RuleEffect::Allow => {
                emit_caps_for_path(&mut p, &rule.canonical, rule.caps, rule.path_match);
                // Also emit for the non-canonical path if different, since
                // Seatbelt may not resolve symlinks before matching.
                if rule.canonical != rule.resolved {
                    emit_caps_for_path(&mut p, &rule.resolved, rule.caps, rule.path_match);
                }
            }
            RuleEffect::Deny => {
                emit_deny_for_path(&mut p, &rule.canonical, rule.caps, rule.path_match);
                if rule.canonical != rule.resolved {
                    emit_deny_for_path(&mut p, &rule.resolved, rule.caps, rule.path_match);
                }
            }
        }
    }

    // Allow stat()/readdir() on ancestor directories so realpath() can
    // traverse to rule paths.
    for ancestor in &ancestors {
        p += &format!(
            "(allow file-read* (literal \"{}\"))\n",
            sbpl_escape(ancestor)
        );
    }

    // Network
    match &policy.network {
        NetworkPolicy::Deny => {
            p += "(deny network*)\n";
        }
        NetworkPolicy::Allow => {
            p += "(allow network*)\n";
        }
        NetworkPolicy::Localhost | NetworkPolicy::AllowDomains(_) => {
            // Allow only localhost connections. For Localhost, this is the
            // complete enforcement. For AllowDomains, a proxy on localhost
            // handles domain filtering.
            //
            // Seatbelt's (remote ip) filter only accepts "localhost" or "*" as
            // host — raw IPs like "127.0.0.1" are not valid. "localhost" covers
            // both IPv4 (127.0.0.1) and IPv6 (::1) loopback.
            p += "(allow network-outbound (remote ip \"localhost:*\"))\n";
            p += "(deny network*)\n";
        }
    }

    p
}

/// Escape a path string for use inside an SBPL string literal.
///
/// Backslashes are escaped to `\\` and double quotes are escaped to `\"`,
/// preventing a crafted path from breaking out of the Seatbelt profile
/// string literal.
fn sbpl_escape(path: &str) -> String {
    path.replace('\\', "\\\\").replace('"', "\\\"")
}

/// Build an SBPL path filter from a path and match type.
///
/// For `Subpath` and `Literal` variants the path is escaped to prevent
/// injection via characters that are special inside SBPL string literals.
/// The `Regex` variant is left unescaped because its pattern comes from the
/// policy author, not from untrusted input, and regex has its own escaping
/// rules.
#[instrument(level = Level::TRACE)]
fn sbpl_filter(path: &str, path_match: PathMatch) -> String {
    match path_match {
        PathMatch::Subpath => format!("(subpath \"{}\")", sbpl_escape(path)),
        PathMatch::Literal => format!("(literal \"{}\")", sbpl_escape(path)),
        PathMatch::Regex => format!("(regex #\"{}\")", path),
    }
}

/// Emit SBPL allow statements for the given caps on a path.
#[instrument(level = Level::TRACE)]
fn emit_caps_for_path(profile: &mut String, path: &str, caps: Cap, path_match: PathMatch) {
    let filter = sbpl_filter(path, path_match);

    if caps.contains(Cap::READ) {
        profile.push_str(&format!("(allow file-read* {})\n", filter));
    }
    if caps.contains(Cap::WRITE) {
        // WRITE grants all file-write sub-operations (data, mode, flags,
        // setattr, xattr, times, create, unlink, mkdir, etc.) — matching
        // the wildcard approach used by READ → file-read*.
        profile.push_str(&format!("(allow file-write* {})\n", filter));
    } else {
        // Without WRITE, emit only the specific sub-operations requested.
        if caps.contains(Cap::CREATE) {
            // CREATE needs file-write-create plus data/xattr/mode operations
            // because tools like `cp` must write content and metadata to
            // newly created files (not just the creation syscall).
            profile.push_str(&format!("(allow file-write-create {})\n", filter));
            profile.push_str(&format!("(allow file-write-data {})\n", filter));
            profile.push_str(&format!("(allow file-write-xattr {})\n", filter));
            profile.push_str(&format!("(allow file-write-mode {})\n", filter));
            profile.push_str(&format!("(allow file-write-flags {})\n", filter));
        }
        if caps.contains(Cap::DELETE) {
            profile.push_str(&format!("(allow file-write-unlink {})\n", filter));
        }
    }
    if caps.contains(Cap::EXECUTE) {
        profile.push_str(&format!("(allow process-exec {})\n", filter));
    }
}

/// Emit SBPL deny statements for the given caps on a path.
#[instrument(level = Level::TRACE)]
fn emit_deny_for_path(profile: &mut String, path: &str, caps: Cap, path_match: PathMatch) {
    let filter = sbpl_filter(path, path_match);

    if caps.contains(Cap::READ) {
        profile.push_str(&format!("(deny file-read* {})\n", filter));
    }
    if caps.contains(Cap::WRITE) {
        profile.push_str(&format!("(deny file-write* {})\n", filter));
    } else {
        if caps.contains(Cap::CREATE) {
            profile.push_str(&format!("(deny file-write-create {})\n", filter));
            profile.push_str(&format!("(deny file-write-data {})\n", filter));
            profile.push_str(&format!("(deny file-write-xattr {})\n", filter));
            profile.push_str(&format!("(deny file-write-mode {})\n", filter));
            profile.push_str(&format!("(deny file-write-flags {})\n", filter));
        }
        if caps.contains(Cap::DELETE) {
            profile.push_str(&format!("(deny file-write-unlink {})\n", filter));
        }
    }
    if caps.contains(Cap::EXECUTE) {
        profile.push_str(&format!("(deny process-exec {})\n", filter));
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::policy::sandbox_types::SandboxRule;

    // ── sbpl_escape ──────────────────────────────────────────────────

    #[test]
    fn escape_normal_path_unchanged() {
        assert_eq!(sbpl_escape("/usr/local/bin"), "/usr/local/bin");
    }

    #[test]
    fn escape_path_with_double_quote() {
        assert_eq!(sbpl_escape("/tmp/evil\"path"), "/tmp/evil\\\"path");
    }

    #[test]
    fn escape_path_with_backslash() {
        assert_eq!(sbpl_escape("/tmp/evil\\path"), "/tmp/evil\\\\path");
    }

    #[test]
    fn escape_path_with_both_backslash_and_quote() {
        assert_eq!(sbpl_escape("/tmp/e\\vi\"l"), "/tmp/e\\\\vi\\\"l");
    }

    // ── sbpl_filter with adversarial paths ───────────────────────────

    #[test]
    fn filter_subpath_normal() {
        assert_eq!(
            sbpl_filter("/usr/local", PathMatch::Subpath),
            "(subpath \"/usr/local\")"
        );
    }

    #[test]
    fn filter_literal_normal() {
        assert_eq!(
            sbpl_filter("/usr/local/bin/rustc", PathMatch::Literal),
            "(literal \"/usr/local/bin/rustc\")"
        );
    }

    #[test]
    fn filter_regex_normal() {
        assert_eq!(
            sbpl_filter("/tmp/build-.*", PathMatch::Regex),
            "(regex #\"/tmp/build-.*\")"
        );
    }

    #[test]
    fn filter_subpath_with_quote_injection() {
        // A malicious path that tries to close the string and inject SBPL
        let malicious = "/tmp/evil\") (allow default) (\"";
        let result = sbpl_filter(malicious, PathMatch::Subpath);
        assert_eq!(result, "(subpath \"/tmp/evil\\\") (allow default) (\\\"\")");
        // The quotes are escaped, so the SBPL string literal stays intact.
        assert!(result.starts_with("(subpath \""));
        assert!(result.ends_with("\")"));
    }

    #[test]
    fn filter_literal_with_backslash_and_quote() {
        let adversarial = "/tmp/a\\b\"c";
        let result = sbpl_filter(adversarial, PathMatch::Literal);
        assert_eq!(result, "(literal \"/tmp/a\\\\b\\\"c\")");
    }

    #[test]
    fn filter_regex_is_not_escaped() {
        // Regex variant must NOT escape — policy authors control regex content.
        let pattern = "/tmp/foo\"bar";
        let result = sbpl_filter(pattern, PathMatch::Regex);
        assert_eq!(result, "(regex #\"/tmp/foo\"bar\")");
    }

    // ── Network policy SBPL ────────────────────────────────────────

    #[test]
    fn sbpl_localhost_allows_only_loopback() {
        let policy = SandboxPolicy {
            default: Cap::READ | Cap::EXECUTE,
            rules: vec![],
            network: NetworkPolicy::Localhost,
            doc: None,
        };
        let profile = compile_to_sbpl(&policy, "/tmp");
        assert!(
            profile.contains("(allow network-outbound (remote ip \"localhost:*\"))"),
            "Localhost policy should allow outbound to localhost"
        );
        assert!(
            profile.contains("(deny network*)"),
            "Localhost policy should deny all other network"
        );
    }

    // ── Rule specificity ordering ─────────────────────────────────

    #[test]
    fn specific_allow_overrides_broad_deny() {
        // Reproduces the real-world bug: deny on /Users shadowed allow
        // on /Users/eliot because deny was emitted after allow.
        let policy = SandboxPolicy {
            default: Cap::EXECUTE,
            rules: vec![
                SandboxRule {
                    effect: RuleEffect::Allow,
                    caps: Cap::READ | Cap::WRITE,
                    path: "/Users/eliot".into(),
                    path_match: PathMatch::Subpath,
                    follow_worktrees: false,
                    doc: None,
                },
                SandboxRule {
                    effect: RuleEffect::Deny,
                    caps: Cap::READ | Cap::WRITE | Cap::CREATE | Cap::DELETE | Cap::EXECUTE,
                    path: "/Users".into(),
                    path_match: PathMatch::Subpath,
                    follow_worktrees: false,
                    doc: None,
                },
            ],
            network: NetworkPolicy::Deny,
            doc: None,
        };
        let profile = compile_to_sbpl(&policy, "/tmp");

        // The deny on /Users must appear BEFORE the allow on /Users/eliot
        // so that Seatbelt's last-match-wins gives the specific allow priority.
        let deny_pos = profile
            .find("(deny file-read* (subpath \"/Users\"))")
            .expect("should contain deny on /Users");
        let allow_pos = profile
            .find("(allow file-read* (subpath \"/Users/eliot\"))")
            .expect("should contain allow on /Users/eliot");
        assert!(
            deny_pos < allow_pos,
            "deny /Users (pos {deny_pos}) must come before allow /Users/eliot (pos {allow_pos})\nprofile:\n{profile}"
        );
    }

    #[test]
    fn deny_at_same_depth_loses_to_allow() {
        // When allow and deny target the same path, allow should come last
        // (and win) in the compiled profile.
        let policy = SandboxPolicy {
            default: Cap::empty() | Cap::EXECUTE,
            rules: vec![
                SandboxRule {
                    effect: RuleEffect::Deny,
                    caps: Cap::WRITE,
                    path: "/data".into(),
                    path_match: PathMatch::Subpath,
                    follow_worktrees: false,
                    doc: None,
                },
                SandboxRule {
                    effect: RuleEffect::Allow,
                    caps: Cap::WRITE,
                    path: "/data".into(),
                    path_match: PathMatch::Subpath,
                    follow_worktrees: false,
                    doc: None,
                },
            ],
            network: NetworkPolicy::Deny,
            doc: None,
        };
        let profile = compile_to_sbpl(&policy, "/tmp");
        let deny_pos = profile
            .find("(deny file-write* (subpath \"/data\"))")
            .expect("should contain deny write on /data");
        let allow_pos = profile
            .find("(allow file-write* (subpath \"/data\"))")
            .expect("should contain allow write on /data");
        assert!(
            deny_pos < allow_pos,
            "deny should come before allow at same depth\nprofile:\n{profile}"
        );
    }

    #[test]
    fn three_level_specificity() {
        // deny /Users → allow /Users/eliot → deny /Users/eliot/.ssh
        let policy = SandboxPolicy {
            default: Cap::EXECUTE,
            rules: vec![
                SandboxRule {
                    effect: RuleEffect::Allow,
                    caps: Cap::READ,
                    path: "/Users/eliot".into(),
                    path_match: PathMatch::Subpath,
                    follow_worktrees: false,
                    doc: None,
                },
                SandboxRule {
                    effect: RuleEffect::Deny,
                    caps: Cap::READ,
                    path: "/Users".into(),
                    path_match: PathMatch::Subpath,
                    follow_worktrees: false,
                    doc: None,
                },
                SandboxRule {
                    effect: RuleEffect::Deny,
                    caps: Cap::READ,
                    path: "/Users/eliot/.ssh".into(),
                    path_match: PathMatch::Subpath,
                    follow_worktrees: false,
                    doc: None,
                },
            ],
            network: NetworkPolicy::Deny,
            doc: None,
        };
        let profile = compile_to_sbpl(&policy, "/tmp");

        let deny_users = profile
            .find("(deny file-read* (subpath \"/Users\"))")
            .expect("deny /Users");
        let allow_eliot = profile
            .find("(allow file-read* (subpath \"/Users/eliot\"))")
            .expect("allow /Users/eliot");
        let deny_ssh = profile
            .find("(deny file-read* (subpath \"/Users/eliot/.ssh\"))")
            .expect("deny /Users/eliot/.ssh");

        assert!(
            deny_users < allow_eliot,
            "deny /Users must come before allow /Users/eliot"
        );
        assert!(
            allow_eliot < deny_ssh,
            "allow /Users/eliot must come before deny /Users/eliot/.ssh"
        );
    }

    #[test]
    fn sbpl_localhost_same_as_allow_domains() {
        let localhost_policy = SandboxPolicy {
            default: Cap::READ | Cap::EXECUTE,
            rules: vec![],
            network: NetworkPolicy::Localhost,
            doc: None,
        };
        let domains_policy = SandboxPolicy {
            default: Cap::READ | Cap::EXECUTE,
            rules: vec![],
            network: NetworkPolicy::AllowDomains(vec!["example.com".into()]),
            doc: None,
        };
        let localhost_profile = compile_to_sbpl(&localhost_policy, "/tmp");
        let domains_profile = compile_to_sbpl(&domains_policy, "/tmp");
        // Both should produce the same network section (localhost-only + deny rest)
        assert_eq!(localhost_profile, domains_profile);
    }
}