claux 20260730.0.0

Terminal AI coding assistant with tool execution
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! Operating-system containment for commands spawned by the Bash tool.
//!
//! Linux workspace-write mode self-invokes Claux as a small helper. The helper
//! applies Landlock before replacing itself with the requested shell command,
//! which avoids running non-async-signal-safe setup in a `pre_exec` callback.

use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::path::{Path, PathBuf};
#[cfg(target_os = "linux")]
use std::process::Stdio;
use tokio::process::Command;

#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum BashFilesystemPolicy {
    /// Use workspace-write containment on Linux and preserve existing
    /// unrestricted behavior on platforms without a backend.
    #[default]
    Auto,
    WorkspaceWrite,
    Unrestricted,
}

impl BashFilesystemPolicy {
    pub fn permits_project_override(self, requested: Self, trusted: bool) -> bool {
        trusted || policy_rank(requested) <= policy_rank(self)
    }

    pub fn diagnose(self) -> BashPolicyDiagnostic {
        diagnose_with_availability(self, landlock_availability())
    }
}

impl fmt::Display for BashFilesystemPolicy {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Auto => formatter.write_str("auto"),
            Self::WorkspaceWrite => formatter.write_str("workspace_write"),
            Self::Unrestricted => formatter.write_str("unrestricted"),
        }
    }
}

fn policy_rank(policy: BashFilesystemPolicy) -> u8 {
    match policy {
        BashFilesystemPolicy::WorkspaceWrite => 0,
        BashFilesystemPolicy::Auto => 1,
        BashFilesystemPolicy::Unrestricted => 2,
    }
}

#[derive(Clone, Debug)]
pub struct CommandSandbox {
    effective_policy: BashFilesystemPolicy,
    workspace_root: PathBuf,
}

impl CommandSandbox {
    pub fn new(policy: BashFilesystemPolicy, workspace_root: impl AsRef<Path>) -> Result<Self> {
        Self::new_with_availability(policy, workspace_root, landlock_availability())
    }

    fn new_with_availability(
        policy: BashFilesystemPolicy,
        workspace_root: impl AsRef<Path>,
        availability: LandlockAvailability,
    ) -> Result<Self> {
        let workspace_root = workspace_root.as_ref().canonicalize().with_context(|| {
            format!(
                "could not resolve command sandbox workspace {}",
                workspace_root.as_ref().display()
            )
        })?;
        if !workspace_root.is_dir() {
            bail!(
                "command sandbox workspace is not a directory: {}",
                workspace_root.display()
            );
        }
        let diagnostic = diagnose_with_availability(policy, availability);
        if !diagnostic.healthy {
            bail!(
                "{}",
                diagnostic
                    .issue
                    .expect("unhealthy diagnostics have an issue")
            );
        }
        if let Some(issue) = diagnostic.issue {
            tracing::warn!("{issue}");
        }
        Ok(Self {
            effective_policy: diagnostic.effective_policy,
            workspace_root,
        })
    }

    #[cfg(test)]
    pub(crate) fn unrestricted_for_tests() -> Self {
        Self::new(
            BashFilesystemPolicy::Unrestricted,
            std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
        )
        .expect("test working directory must resolve")
    }

    pub fn command(&self, shell_command: &str) -> Result<Command> {
        if self.uses_workspace_write() {
            let executable =
                std::env::current_exe().context("could not locate the claux executable")?;
            let mut command = Command::new(executable);
            command
                .arg("__sandbox-exec")
                .arg("--workspace")
                .arg(&self.workspace_root)
                .arg("--command")
                .arg(shell_command);
            return Ok(command);
        }

        let mut command = Command::new("sh");
        command.arg("-c").arg(shell_command);
        Ok(command)
    }

    fn uses_workspace_write(&self) -> bool {
        match self.effective_policy {
            BashFilesystemPolicy::WorkspaceWrite => true,
            BashFilesystemPolicy::Auto => {
                unreachable!("auto is resolved when the command sandbox is created")
            }
            BashFilesystemPolicy::Unrestricted => false,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
// Availability variants are platform-specific in production but all are used
// by the platform-independent policy tests.
#[allow(dead_code)]
enum LandlockAvailability {
    Available,
    UnsupportedPlatform,
    Unavailable(String),
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BashPolicyDiagnostic {
    pub summary: String,
    pub issue: Option<String>,
    pub healthy: bool,
    effective_policy: BashFilesystemPolicy,
}

fn diagnose_with_availability(
    policy: BashFilesystemPolicy,
    availability: LandlockAvailability,
) -> BashPolicyDiagnostic {
    const WORKSPACE_WRITE_SUMMARY: &str =
        "workspace_write (Landlock; temporary and Git metadata paths remain writable)";

    match (policy, availability) {
        (BashFilesystemPolicy::Unrestricted, _) => BashPolicyDiagnostic {
            summary: "unrestricted".to_string(),
            issue: None,
            healthy: true,
            effective_policy: BashFilesystemPolicy::Unrestricted,
        },
        (BashFilesystemPolicy::Auto, LandlockAvailability::Available)
        | (BashFilesystemPolicy::WorkspaceWrite, LandlockAvailability::Available) => {
            BashPolicyDiagnostic {
                summary: WORKSPACE_WRITE_SUMMARY.to_string(),
                issue: None,
                healthy: true,
                effective_policy: BashFilesystemPolicy::WorkspaceWrite,
            }
        }
        (BashFilesystemPolicy::Auto, LandlockAvailability::UnsupportedPlatform) => {
            BashPolicyDiagnostic {
                summary: "unrestricted (no sandbox backend for this platform)".to_string(),
                issue: None,
                healthy: true,
                effective_policy: BashFilesystemPolicy::Unrestricted,
            }
        }
        (BashFilesystemPolicy::Auto, LandlockAvailability::Unavailable(reason)) => {
            BashPolicyDiagnostic {
                summary: "unrestricted (automatic Landlock fallback)".to_string(),
                issue: Some(format!(
                    "Landlock workspace-write sandbox unavailable; Bash will run unrestricted: \
                     {reason}"
                )),
                healthy: true,
                effective_policy: BashFilesystemPolicy::Unrestricted,
            }
        }
        (BashFilesystemPolicy::WorkspaceWrite, LandlockAvailability::UnsupportedPlatform) => {
            BashPolicyDiagnostic {
                summary: "workspace_write (unavailable)".to_string(),
                issue: Some(
                    "bash_filesystem_policy=workspace_write is currently only supported on Linux"
                        .to_string(),
                ),
                healthy: false,
                effective_policy: BashFilesystemPolicy::WorkspaceWrite,
            }
        }
        (BashFilesystemPolicy::WorkspaceWrite, LandlockAvailability::Unavailable(reason)) => {
            BashPolicyDiagnostic {
                summary: "workspace_write (unavailable)".to_string(),
                issue: Some(format!(
                    "bash_filesystem_policy=workspace_write requires complete Landlock ABI V3 \
                     enforcement: {reason}"
                )),
                healthy: false,
                effective_policy: BashFilesystemPolicy::WorkspaceWrite,
            }
        }
    }
}

#[cfg(target_os = "linux")]
fn landlock_availability() -> LandlockAvailability {
    let executable = match std::env::current_exe() {
        Ok(executable) => executable,
        Err(error) => {
            return LandlockAvailability::Unavailable(format!(
                "could not locate the claux executable: {error}"
            ));
        }
    };
    match std::process::Command::new(executable)
        .arg("__sandbox-probe")
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::piped())
        .output()
    {
        Ok(output) if output.status.success() => LandlockAvailability::Available,
        Ok(output) => {
            let reason = String::from_utf8_lossy(&output.stderr);
            let reason = reason
                .trim()
                .strip_prefix("Error: ")
                .unwrap_or(reason.trim());
            LandlockAvailability::Unavailable(if reason.is_empty() {
                format!("probe exited with {}", output.status)
            } else {
                reason.to_string()
            })
        }
        Err(error) => {
            LandlockAvailability::Unavailable(format!("could not run Landlock probe: {error}"))
        }
    }
}

#[cfg(not(target_os = "linux"))]
fn landlock_availability() -> LandlockAvailability {
    LandlockAvailability::UnsupportedPlatform
}

#[cfg(target_os = "linux")]
pub fn run_helper(workspace_root: &Path, shell_command: &str) -> Result<()> {
    apply_landlock(workspace_root)?;

    use std::os::unix::process::CommandExt;
    let error = std::process::Command::new("sh")
        .arg("-c")
        .arg(shell_command)
        .exec();
    Err(error).context("could not execute sandboxed shell command")
}

#[cfg(target_os = "linux")]
pub fn run_probe() -> Result<()> {
    apply_landlock(&std::env::current_dir()?)
}

#[cfg(not(target_os = "linux"))]
pub fn run_helper(_workspace_root: &Path, _shell_command: &str) -> Result<()> {
    bail!("bash workspace-write sandboxing is currently only supported on Linux")
}

#[cfg(not(target_os = "linux"))]
pub fn run_probe() -> Result<()> {
    bail!("Landlock is only available on Linux")
}

#[cfg(target_os = "linux")]
fn apply_landlock(workspace_root: &Path) -> Result<()> {
    use landlock::{
        Access, AccessFs, CompatLevel, Compatible, Ruleset, RulesetAttr, RulesetCreatedAttr, ABI,
    };

    let workspace_root = workspace_root.canonicalize().with_context(|| {
        format!(
            "could not resolve command sandbox workspace {}",
            workspace_root.display()
        )
    })?;
    // ABI V3 is the minimum complete write boundary because it added
    // TRUNCATE. Older ABIs could prevent creating an outside file while still
    // allowing an existing outside file to be truncated.
    let access_rw = AccessFs::from_all(ABI::V3);
    let access_ro = AccessFs::from_read(ABI::V3);

    let mut ruleset = Ruleset::default()
        .set_compatibility(CompatLevel::BestEffort)
        .handle_access(access_rw)?
        .create()?
        .add_rules(landlock::path_beneath_rules(&["/"], access_ro))?
        .no_new_privs(true);

    let writable_roots = writable_roots(&workspace_root);
    ruleset = ruleset.add_rules(landlock::path_beneath_rules(&writable_roots, access_rw))?;

    let status = ruleset.restrict_self()?;
    if status.ruleset != landlock::RulesetStatus::FullyEnforced {
        bail!(
            "Landlock ABI V3 is unavailable; refusing to run Bash without complete filesystem \
             containment"
        );
    }
    Ok(())
}

#[cfg(target_os = "linux")]
fn writable_roots(workspace_root: &Path) -> Vec<PathBuf> {
    let mut roots = vec![workspace_root.to_path_buf()];
    for path in [
        Some(std::env::temp_dir()),
        Some(PathBuf::from("/tmp")),
        Some(PathBuf::from("/var/tmp")),
        Some(PathBuf::from("/dev/null")),
    ]
    .into_iter()
    .flatten()
    {
        push_canonical_if_present(&mut roots, &path);
    }
    add_git_metadata_roots(&mut roots, workspace_root);
    roots.sort();
    roots.dedup();
    roots
}

#[cfg(target_os = "linux")]
fn add_git_metadata_roots(roots: &mut Vec<PathBuf>, workspace_root: &Path) {
    let Some(dot_git) = workspace_root
        .ancestors()
        .map(|directory| directory.join(".git"))
        .find(|path| path.exists())
    else {
        return;
    };
    if dot_git
        .symlink_metadata()
        .is_ok_and(|metadata| metadata.file_type().is_symlink())
    {
        return;
    }
    if dot_git.is_dir() {
        push_canonical_if_present(roots, &dot_git);
        return;
    }

    let Ok(contents) = std::fs::read_to_string(&dot_git) else {
        return;
    };
    let Some(git_dir) = contents.trim().strip_prefix("gitdir:") else {
        return;
    };
    let Some(worktree_root) = dot_git.parent() else {
        return;
    };
    let git_dir = resolve_relative(worktree_root, Path::new(git_dir.trim()));
    let Ok(git_dir) = git_dir.canonicalize() else {
        return;
    };
    let Ok(backlink) = std::fs::read_to_string(git_dir.join("gitdir")) else {
        return;
    };
    let backlink = resolve_relative(&git_dir, Path::new(backlink.trim()));
    let (Ok(backlink), Ok(dot_git)) = (backlink.canonicalize(), dot_git.canonicalize()) else {
        return;
    };
    if backlink != dot_git {
        return;
    }
    roots.push(git_dir.clone());

    let Ok(common_dir) = std::fs::read_to_string(git_dir.join("commondir")) else {
        return;
    };
    let common_dir = resolve_relative(&git_dir, Path::new(common_dir.trim()));
    let Ok(common_dir) = common_dir.canonicalize() else {
        return;
    };
    if common_dir.file_name().is_some_and(|name| name == ".git") && git_dir.starts_with(&common_dir)
    {
        roots.push(common_dir);
    }
}

#[cfg(target_os = "linux")]
fn resolve_relative(base: &Path, path: &Path) -> PathBuf {
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        base.join(path)
    }
}

#[cfg(target_os = "linux")]
fn push_canonical_if_present(roots: &mut Vec<PathBuf>, path: &Path) {
    if let Ok(path) = path.canonicalize() {
        roots.push(path);
    }
}

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

    #[test]
    fn untrusted_projects_can_only_tighten_bash_policy() {
        assert!(BashFilesystemPolicy::Auto
            .permits_project_override(BashFilesystemPolicy::WorkspaceWrite, false));
        assert!(!BashFilesystemPolicy::Auto
            .permits_project_override(BashFilesystemPolicy::Unrestricted, false));
        assert!(BashFilesystemPolicy::WorkspaceWrite
            .permits_project_override(BashFilesystemPolicy::Unrestricted, true));
    }

    #[test]
    fn unrestricted_commands_run_the_shell_directly() {
        let workspace = tempfile::tempdir().unwrap();
        let sandbox =
            CommandSandbox::new(BashFilesystemPolicy::Unrestricted, workspace.path()).unwrap();
        let command = sandbox.command("echo hello").unwrap();

        assert_eq!(command.as_std().get_program(), "sh");
        assert_eq!(
            command
                .as_std()
                .get_args()
                .map(|argument| argument.to_string_lossy().into_owned())
                .collect::<Vec<_>>(),
            ["-c", "echo hello"]
        );
    }

    #[test]
    fn auto_falls_back_with_a_warning_when_landlock_is_unavailable() {
        let workspace = tempfile::tempdir().unwrap();
        let sandbox = CommandSandbox::new_with_availability(
            BashFilesystemPolicy::Auto,
            workspace.path(),
            LandlockAvailability::Unavailable("kernel is too old".to_string()),
        )
        .unwrap();

        assert_eq!(
            sandbox
                .command("echo hello")
                .unwrap()
                .as_std()
                .get_program(),
            "sh"
        );
        assert_eq!(
            diagnose_with_availability(
                BashFilesystemPolicy::Auto,
                LandlockAvailability::Unavailable("kernel is too old".to_string())
            ),
            BashPolicyDiagnostic {
                summary: "unrestricted (automatic Landlock fallback)".to_string(),
                issue: Some(
                    "Landlock workspace-write sandbox unavailable; Bash will run unrestricted: \
                     kernel is too old"
                        .to_string()
                ),
                healthy: true,
                effective_policy: BashFilesystemPolicy::Unrestricted,
            }
        );
    }

    #[test]
    fn explicit_workspace_write_fails_closed_when_landlock_is_unavailable() {
        let workspace = tempfile::tempdir().unwrap();
        let error = CommandSandbox::new_with_availability(
            BashFilesystemPolicy::WorkspaceWrite,
            workspace.path(),
            LandlockAvailability::Unavailable("kernel is too old".to_string()),
        )
        .unwrap_err();

        assert!(error
            .to_string()
            .contains("requires complete Landlock ABI V3 enforcement"));
    }

    #[test]
    fn auto_is_quietly_unrestricted_on_platforms_without_a_backend() {
        assert_eq!(
            diagnose_with_availability(
                BashFilesystemPolicy::Auto,
                LandlockAvailability::UnsupportedPlatform
            ),
            BashPolicyDiagnostic {
                summary: "unrestricted (no sandbox backend for this platform)".to_string(),
                issue: None,
                healthy: true,
                effective_policy: BashFilesystemPolicy::Unrestricted,
            }
        );
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn workspace_write_commands_use_the_helper() {
        let workspace = tempfile::tempdir().unwrap();
        let sandbox = CommandSandbox::new_with_availability(
            BashFilesystemPolicy::WorkspaceWrite,
            workspace.path(),
            LandlockAvailability::Available,
        )
        .unwrap();
        let command = sandbox.command("echo hello").unwrap();
        let arguments = command
            .as_std()
            .get_args()
            .map(|argument| argument.to_string_lossy().into_owned())
            .collect::<Vec<_>>();

        assert_eq!(arguments[0], "__sandbox-exec");
        assert_eq!(arguments[1], "--workspace");
        assert_eq!(arguments[2], workspace.path().to_string_lossy());
        assert_eq!(arguments[3], "--command");
        assert_eq!(arguments[4], "echo hello");
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn git_metadata_outside_a_worktree_is_writable() {
        let parent = tempfile::tempdir().unwrap();
        let common = parent.path().join("repository/.git");
        let git_dir = common.join("worktrees/workspace");
        let workspace = parent.path().join("workspace/subdirectory");
        std::fs::create_dir_all(&git_dir).unwrap();
        std::fs::create_dir_all(&workspace).unwrap();
        std::fs::write(
            parent.path().join("workspace/.git"),
            "gitdir: ../repository/.git/worktrees/workspace\n",
        )
        .unwrap();
        std::fs::write(
            git_dir.join("gitdir"),
            format!("{}\n", parent.path().join("workspace/.git").display()),
        )
        .unwrap();
        std::fs::write(git_dir.join("commondir"), "../..\n").unwrap();

        let roots = writable_roots(&workspace);

        assert!(roots.contains(&git_dir.canonicalize().unwrap()));
        assert!(roots.contains(&common.canonicalize().unwrap()));
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn forged_git_metadata_cannot_add_an_arbitrary_writable_root() {
        let parent = tempfile::tempdir().unwrap();
        let workspace = parent.path().join("workspace");
        let outside = parent.path().join("outside");
        std::fs::create_dir(&workspace).unwrap();
        std::fs::create_dir(&outside).unwrap();
        std::fs::write(
            workspace.join(".git"),
            format!("gitdir: {}\n", outside.display()),
        )
        .unwrap();

        let roots = writable_roots(&workspace);

        assert!(!roots.contains(&outside.canonicalize().unwrap()));
    }
}