workmux 0.1.167

An opinionated workflow tool that orchestrates git worktrees and tmux
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
//! OS-native sandboxing for host-exec child processes.
//!
//! Restricts file and process access for commands executed on the host on
//! behalf of a sandboxed guest. Uses `sandbox-exec` (Seatbelt) on macOS
//! and `bwrap` (Bubblewrap) on Linux.
//!
//! The goal is defense-in-depth: even if a guest writes a malicious build
//! file (justfile, build.rs, package.json), the host-exec child process
//! cannot read sensitive files (~/.ssh, ~/.aws) or write outside the
//! worktree and toolchain caches.

use anyhow::{Context, Result};
use std::collections::HashMap;
use std::path::Path;
use std::process::{Child, Command, Stdio};
use tracing::debug;

/// Directories under $HOME that are denied read access.
/// These contain credentials, keys, and other secrets.
const DENY_READ_DIRS: &[&str] = &[
    ".ssh",
    ".aws",
    ".gnupg",
    ".kube",
    ".azure",
    ".config/gcloud",
    ".config/workmux",
    ".docker",
    ".claude",               // Claude credentials
    ".gemini",               // Gemini credentials
    ".codex",                // Codex credentials
    ".local/share/opencode", // OpenCode credentials
    ".config/gh",            // GitHub CLI credentials
    ".config/op",            // 1Password CLI config
    ".config/sops",          // SOPS age/PGP keys
    ".password-store",       // pass password manager
];

/// Files under $HOME that are denied read access.
/// Separate from DENY_READ_DIRS because Linux bwrap needs different
/// handling (bind /dev/null over files, tmpfs over directories).
const DENY_READ_FILES: &[&str] = &[
    ".npmrc",               // can contain auth tokens
    ".pypirc",              // can contain auth tokens
    ".netrc",               // network credentials
    ".gem/credentials",     // rubygems auth
    ".claude-sandbox.json", // sandbox credentials
    ".bash_history",        // shell history
    ".zsh_history",         // shell history
    ".vault-token",         // HashiCorp Vault token
];

/// macOS-specific deny paths (relative to HOME).
#[cfg(target_os = "macos")]
const DENY_READ_PATHS_MACOS: &[&str] = &[
    "Library/Keychains",
    "Library/Cookies",
    "Library/Application Support/Google/Chrome",
    "Library/Application Support/Firefox",
    "Library/Application Support/Arc", // Arc browser data
    ".zsh_sessions",                   // macOS zsh session data
];

/// Directories under $HOME that are allowed write access (caches, toolchain state).
/// Everything else under $HOME is write-denied.
const ALLOW_WRITE_DIRS: &[&str] = &[
    ".cache",
    ".cargo",
    ".rustup",
    ".npm",
    ".local/state",
    ".local/share/devbox",
];

/// macOS-specific write-allowed paths.
#[cfg(target_os = "macos")]
const ALLOW_WRITE_DIRS_MACOS: &[&str] = &["Library/Caches", "Library/Logs"];

/// Spawn a command inside an OS-native sandbox.
///
/// On macOS, uses `sandbox-exec`. On Linux, uses `bwrap` resolved from
/// trusted system paths. When `allow_unsandboxed` is true, skips the
/// filesystem sandbox on either platform.
pub fn spawn_sandboxed(
    program: &str,
    args: &[String],
    worktree: &Path,
    envs: &HashMap<String, String>,
    allow_unsandboxed: bool,
) -> Result<Child> {
    if allow_unsandboxed {
        tracing::warn!(
            "dangerously_allow_unsandboxed_host_exec is set, skipping filesystem sandbox"
        );
        return spawn_unsandboxed(program, args, worktree, envs);
    }

    #[cfg(target_os = "macos")]
    {
        spawn_macos(program, args, worktree, envs)
    }

    #[cfg(target_os = "linux")]
    {
        spawn_linux(program, args, worktree, envs)
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        tracing::warn!("host-exec sandboxing not supported on this OS, running unsandboxed");
        spawn_unsandboxed(program, args, worktree, envs)
    }
}

fn spawn_unsandboxed(
    program: &str,
    args: &[String],
    worktree: &Path,
    envs: &HashMap<String, String>,
) -> Result<Child> {
    let mut cmd = Command::new(program);
    cmd.args(args);
    cmd.current_dir(worktree);
    cmd.env_clear();
    cmd.envs(envs);
    cmd.stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    cmd.spawn().context("Failed to spawn command")
}

// ── macOS: sandbox-exec ─────────────────────────────────────────────────

#[cfg(target_os = "macos")]
fn spawn_macos(
    program: &str,
    args: &[String],
    worktree: &Path,
    envs: &HashMap<String, String>,
) -> Result<Child> {
    let home = std::env::var("HOME").unwrap_or_else(|_| "/var/empty".to_string());
    let worktree_str = worktree
        .to_str()
        .context("Worktree path is not valid UTF-8")?;

    let profile = generate_macos_profile();

    let mut cmd = Command::new("/usr/bin/sandbox-exec");
    // Use -D parameter substitution to inject paths safely (no string interpolation)
    cmd.arg("-p").arg(&profile);
    cmd.arg("-D").arg(format!("HOME_DIR={}", home));
    cmd.arg("-D").arg(format!("WORKTREE={}", worktree_str));
    cmd.arg(program);
    cmd.args(args);
    cmd.current_dir(worktree);
    cmd.env_clear();
    cmd.envs(envs);
    cmd.stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());

    debug!(program, "spawning under sandbox-exec");
    cmd.spawn().context("Failed to spawn sandbox-exec")
}

/// Generate the macOS Seatbelt profile string.
///
/// Uses `(param ...)` references for HOME_DIR and WORKTREE so paths are
/// injected via `-D` flags rather than string interpolation. This prevents
/// profile injection via crafted paths.
#[cfg(target_os = "macos")]
fn generate_macos_profile() -> String {
    let mut profile = String::from("(version 1)\n(allow default)\n\n");

    // Deny reading sensitive directories and files under HOME
    profile.push_str("; Deny reading credentials and secrets\n");
    profile.push_str("(deny file-read* (with no-report)\n");
    for dir in DENY_READ_DIRS {
        profile.push_str(&format!(
            "    (subpath (string-append (param \"HOME_DIR\") \"/{}\" ))\n",
            dir
        ));
    }
    for file in DENY_READ_FILES {
        // Use literal for files (subpath works for both files and dirs in Seatbelt)
        profile.push_str(&format!(
            "    (subpath (string-append (param \"HOME_DIR\") \"/{}\" ))\n",
            file
        ));
    }
    for dir in DENY_READ_PATHS_MACOS {
        profile.push_str(&format!(
            "    (subpath (string-append (param \"HOME_DIR\") \"/{}\" ))\n",
            dir
        ));
    }
    profile.push_str(")\n\n");

    // Deny writing to HOME (broad)
    profile.push_str("; Deny writing to HOME except allowed caches\n");
    profile.push_str("(deny file-write* (with no-report)\n");
    profile.push_str("    (subpath (param \"HOME_DIR\"))\n");
    profile.push_str(")\n\n");

    // Allow writing to worktree
    profile.push_str("; Allow full access to worktree\n");
    profile.push_str("(allow file-read* file-write*\n");
    profile.push_str("    (subpath (param \"WORKTREE\"))\n");
    profile.push_str(")\n\n");

    // Allow writing to cache/toolchain dirs
    profile.push_str("; Allow writing to caches and toolchains\n");
    profile.push_str("(allow file-write*\n");
    for dir in ALLOW_WRITE_DIRS {
        profile.push_str(&format!(
            "    (subpath (string-append (param \"HOME_DIR\") \"/{}\" ))\n",
            dir
        ));
    }
    for dir in ALLOW_WRITE_DIRS_MACOS {
        profile.push_str(&format!(
            "    (subpath (string-append (param \"HOME_DIR\") \"/{}\" ))\n",
            dir
        ));
    }
    profile.push_str(")\n\n");

    // Allow writing to temp dirs
    profile.push_str("; Allow temp directories\n");
    profile.push_str("(allow file-read* file-write*\n");
    profile.push_str("    (subpath \"/tmp\")\n");
    profile.push_str("    (subpath \"/private/tmp\")\n");
    profile.push_str("    (subpath \"/var/folders\")\n");
    profile.push_str(")\n\n");

    // Allow nix store access
    profile.push_str("; Allow nix store (read-only by default, allow write for installs)\n");
    profile.push_str("(allow file-read* file-write* (subpath \"/nix\"))\n");

    profile
}

// ── Linux: bwrap ────────────────────────────────────────────────────────

/// Trusted system paths where bwrap may be installed.
/// We avoid PATH resolution to prevent hijacking via writable directories.
#[cfg(target_os = "linux")]
const TRUSTED_BWRAP_PATHS: &[&str] = &[
    "/usr/bin/bwrap",
    "/usr/sbin/bwrap",
    "/usr/local/bin/bwrap",
    "/run/current-system/sw/bin/bwrap",     // NixOS
    "/home/linuxbrew/.linuxbrew/bin/bwrap", // Homebrew on Linux
];

/// Find bwrap at a trusted absolute path. Returns the first match.
#[cfg(target_os = "linux")]
fn find_bwrap() -> Option<&'static str> {
    TRUSTED_BWRAP_PATHS
        .iter()
        .find(|p| Path::new(p).is_file())
        .copied()
}

#[cfg(target_os = "linux")]
fn spawn_linux(
    program: &str,
    args: &[String],
    worktree: &Path,
    envs: &HashMap<String, String>,
) -> Result<Child> {
    if let Some(bwrap_path) = find_bwrap() {
        spawn_bwrap(bwrap_path, program, args, worktree, envs)
    } else {
        anyhow::bail!(
            "bwrap (bubblewrap) not found at any trusted path ({}). \
            Install bubblewrap or set sandbox.dangerously_allow_unsandboxed_host_exec: true \
            in your global config to run without filesystem isolation",
            TRUSTED_BWRAP_PATHS.join(", ")
        )
    }
}

#[cfg(target_os = "linux")]
fn spawn_bwrap(
    bwrap_path: &str,
    program: &str,
    args: &[String],
    worktree: &Path,
    envs: &HashMap<String, String>,
) -> Result<Child> {
    let home = std::env::var("HOME").unwrap_or_else(|_| "/var/empty".to_string());
    let home_path = Path::new(&home);

    let mut cmd = Command::new(bwrap_path);

    // Read-only root filesystem
    cmd.args(["--ro-bind", "/", "/"]);
    cmd.args(["--dev", "/dev"]);
    cmd.args(["--proc", "/proc"]);
    cmd.args(["--tmpfs", "/tmp"]);

    // Writable worktree
    let wt = worktree
        .to_str()
        .context("Worktree path is not valid UTF-8")?;
    cmd.args(["--bind", wt, wt]);

    // Hide secret directories behind tmpfs
    for dir in DENY_READ_DIRS {
        let path = home_path.join(dir);
        if path.exists() {
            if let Some(s) = path.to_str() {
                cmd.args(["--tmpfs", s]);
            }
        }
    }

    // Hide secret files by binding /dev/null over them
    for file in DENY_READ_FILES {
        let path = home_path.join(file);
        if path.is_file() {
            if let Some(s) = path.to_str() {
                cmd.args(["--ro-bind", "/dev/null", s]);
            }
        }
    }

    // Writable caches -- create dirs if needed so bwrap can bind-mount them
    // (the root is read-only, so the process can't create them itself)
    for dir in ALLOW_WRITE_DIRS {
        let path = home_path.join(dir);
        if !path.exists() {
            if let Err(e) = std::fs::create_dir_all(&path) {
                debug!(?path, error = %e, "failed to create cache dir for bwrap binding");
                continue;
            }
        }
        if let Some(s) = path.to_str() {
            cmd.args(["--bind", s, s]);
        }
    }

    // Allow network (required for package managers)
    cmd.arg("--share-net");

    // Execute the command
    cmd.arg("--");
    cmd.arg(program);
    cmd.args(args);

    cmd.current_dir(worktree);
    cmd.env_clear();
    cmd.envs(envs);
    cmd.stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());

    debug!(program, "spawning under bwrap");
    cmd.spawn().context("Failed to spawn bwrap")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_deny_read_dirs_are_valid() {
        for dir in DENY_READ_DIRS {
            assert!(!dir.starts_with('/'), "should be relative: {}", dir);
            assert!(!dir.contains(".."), "no traversal: {}", dir);
            assert!(!dir.is_empty(), "no empty entries");
        }
    }

    #[test]
    fn test_deny_read_files_are_valid() {
        for file in DENY_READ_FILES {
            assert!(!file.starts_with('/'), "should be relative: {}", file);
            assert!(!file.contains(".."), "no traversal: {}", file);
            assert!(!file.is_empty(), "no empty entries");
        }
    }

    #[test]
    fn test_allow_write_dirs_are_valid() {
        for dir in ALLOW_WRITE_DIRS {
            assert!(!dir.starts_with('/'), "should be relative: {}", dir);
            assert!(!dir.contains(".."), "no traversal: {}", dir);
        }
    }

    #[test]
    fn test_deny_and_allow_dont_overlap() {
        // No entry should be both denied for reading and allowed for writing
        for deny in DENY_READ_DIRS.iter().chain(DENY_READ_FILES.iter()) {
            for allow in ALLOW_WRITE_DIRS {
                assert_ne!(
                    *deny, *allow,
                    "overlap between deny-read and allow-write: {}",
                    deny
                );
            }
        }
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_macos_profile_uses_params() {
        let profile = generate_macos_profile();
        // Must use param references, not hardcoded paths
        assert!(profile.contains("(param \"HOME_DIR\")"));
        assert!(profile.contains("(param \"WORKTREE\")"));
        // Must not contain literal home paths
        assert!(!profile.contains("/Users/"));
        assert!(!profile.contains("/home/"));
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_macos_profile_contains_all_deny_paths() {
        let profile = generate_macos_profile();
        for p in DENY_READ_DIRS
            .iter()
            .chain(DENY_READ_FILES.iter())
            .chain(DENY_READ_PATHS_MACOS.iter())
        {
            assert!(profile.contains(p), "missing deny path in profile: {p}");
        }
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_macos_profile_allows_worktree_and_caches() {
        let profile = generate_macos_profile();
        assert!(profile.contains("WORKTREE"));
        assert!(profile.contains(".cache"));
        assert!(profile.contains(".cargo"));
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_trusted_bwrap_paths_are_absolute() {
        for path in TRUSTED_BWRAP_PATHS {
            assert!(
                path.starts_with('/'),
                "trusted bwrap path must be absolute: {}",
                path
            );
        }
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_trusted_bwrap_paths_end_with_bwrap() {
        for path in TRUSTED_BWRAP_PATHS {
            assert!(
                path.ends_with("/bwrap"),
                "trusted bwrap path must end with /bwrap: {}",
                path
            );
        }
    }
}