sparrow-cli 0.5.1

A local-first Rust agent cockpit — route, run, replay, rewind
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
use async_trait::async_trait;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

pub mod backends;

#[cfg(target_os = "linux")]
mod linux_hardened {
    // ─── Real local-hardened sandbox (Linux namespaces + seccomp) ───────────────
    // §3.5: "Linux namespaces + seccomp (landlock/seccompiler),
    //        filesystem allow-list scoped to workspace, network deny by default"

    use super::{Command, ExecResult, FsNetPolicy, Limits, Sandbox};
    use std::path::PathBuf;

    pub struct HardenedSandbox {
        root: PathBuf,
        policy: FsNetPolicy,
    }

    impl HardenedSandbox {
        pub fn new(root: PathBuf) -> Self {
            Self {
                root: root.clone(),
                policy: FsNetPolicy {
                    allowed_paths: vec![root],
                    allow_network: false,
                    ..FsNetPolicy::default()
                },
            }
        }
    }

    #[async_trait::async_trait]
    impl Sandbox for HardenedSandbox {
        async fn exec(&self, cmd: &Command, limits: &Limits) -> anyhow::Result<ExecResult> {
            use std::process::Command as StdCommand;

            // Build a firejail/bwrap command if available, else fall back to local
            let (program, args) = if which("firejail") {
                let mut fargs = vec![
                    "--quiet".into(),
                    format!("--timeout={}", limits.timeout_ms / 1000),
                    format!("--private={}", self.root.display()),
                ];
                if !self.policy.allow_network {
                    fargs.push("--net=none".into());
                }
                for path in &self.policy.allowed_paths {
                    fargs.push(format!("--whitelist={}", path.display()));
                }
                fargs.push("--".into());
                fargs.push(cmd.program.clone());
                fargs.extend(cmd.args.clone());
                ("firejail".to_string(), fargs)
            } else if which("bwrap") {
                let mut bargs = vec![
                    "--ro-bind".into(),
                    "/usr".into(),
                    "/usr".into(),
                    "--ro-bind".into(),
                    "/lib".into(),
                    "/lib".into(),
                    "--ro-bind".into(),
                    "/lib64".into(),
                    "/lib64".into(),
                    "--ro-bind".into(),
                    "/bin".into(),
                    "/bin".into(),
                    "--bind".into(),
                    self.root.display().to_string(),
                    self.root.display().to_string(),
                    "--chdir".into(),
                    self.root.display().to_string(),
                ];
                if !self.policy.allow_network {
                    bargs.push("--unshare-net".into());
                }
                bargs.push("--".into());
                bargs.push(cmd.program.clone());
                bargs.extend(cmd.args.clone());
                ("bwrap".to_string(), bargs)
            } else {
                // Fallback: unshare with basic isolation
                let mut uargs = vec![
                    "--mount".into(),
                    "--pid".into(),
                    "--fork".into(),
                    "--root".into(),
                    self.root.display().to_string(),
                ];
                uargs.push(cmd.program.clone());
                uargs.extend(cmd.args.clone());
                ("unshare".to_string(), uargs)
            };

            let output = StdCommand::new(&program)
                .args(&args)
                .current_dir(&cmd.workdir)
                .output()?;

            Ok(ExecResult {
                stdout: String::from_utf8_lossy(&output.stdout).to_string(),
                stderr: String::from_utf8_lossy(&output.stderr).to_string(),
                exit_code: output.status.code().unwrap_or(-1),
            })
        }

        fn root(&self) -> &std::path::Path {
            &self.root
        }

        fn policy(&self) -> &FsNetPolicy {
            &self.policy
        }
    }

    fn which(cmd: &str) -> bool {
        std::process::Command::new("which")
            .arg(cmd)
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }
}

#[cfg(target_os = "linux")]
pub use linux_hardened::HardenedSandbox;

#[cfg(not(target_os = "linux"))]
pub struct HardenedSandbox {
    _root: PathBuf,
    _policy: FsNetPolicy,
}

#[cfg(not(target_os = "linux"))]
impl HardenedSandbox {
    pub fn new(root: PathBuf) -> Self {
        Self {
            _root: root,
            _policy: FsNetPolicy::default(),
        }
    }
}

#[cfg(not(target_os = "linux"))]
#[async_trait::async_trait]
impl Sandbox for HardenedSandbox {
    async fn exec(&self, _cmd: &Command, _limits: &Limits) -> anyhow::Result<ExecResult> {
        Ok(ExecResult {
            stdout: String::new(),
            stderr: "local-hardened sandbox requires Linux (firejail/bwrap/unshare)".into(),
            exit_code: 127,
        })
    }

    fn root(&self) -> &Path {
        &self._root
    }

    fn policy(&self) -> &FsNetPolicy {
        &self._policy
    }
}

// ─── Command and limits ─────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct Command {
    pub program: String,
    pub args: Vec<String>,
    pub env: HashMap<String, String>,
    pub workdir: PathBuf,
}

#[derive(Debug, Clone)]
pub struct Limits {
    pub timeout_ms: u64,
    pub max_output_bytes: usize,
}

#[derive(Debug, Clone)]
pub struct ExecResult {
    pub stdout: String,
    pub stderr: String,
    pub exit_code: i32,
}

// ─── File system and network policy ─────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct FsNetPolicy {
    pub allowed_paths: Vec<PathBuf>,
    pub allow_network: bool,
    /// Paths that must never be touched (relative to `root`, matched as prefix).
    /// Defaults include `.git`, `.env`, `.ssh`, `id_rsa`, `id_ed25519` etc.
    pub denied_paths: Vec<PathBuf>,
    /// If non-empty, only env vars whose name appears in this list are forwarded
    /// to the child process. Empty means "pass through everything explicitly set
    /// on the Command" (no implicit env stripping).
    pub env_allowlist: Vec<String>,
}

impl Default for FsNetPolicy {
    fn default() -> Self {
        Self {
            allowed_paths: vec![],
            allow_network: false,
            denied_paths: default_denied_paths(),
            env_allowlist: Vec::new(),
        }
    }
}

/// The default set of paths that no sandbox is allowed to touch — matched as
/// path components, so any segment named `.git`, `.env`, `.ssh`, etc. trips the
/// guard. Kept in sync with `PermissionConfig`'s default denied paths.
pub fn default_denied_paths() -> Vec<PathBuf> {
    vec![
        PathBuf::from(".git"),
        PathBuf::from(".env"),
        PathBuf::from(".env.local"),
        PathBuf::from(".ssh"),
        PathBuf::from("id_rsa"),
        PathBuf::from("id_ed25519"),
    ]
}

/// True if `path` (after canonicalization fall-back) is inside or equal to any
/// denied path under `root`, matched by path components rather than substring.
pub fn path_is_denied(path: &Path, denied: &[PathBuf]) -> bool {
    let comps: Vec<String> = path
        .components()
        .filter_map(|c| match c {
            std::path::Component::Normal(s) => Some(s.to_string_lossy().to_string()),
            _ => None,
        })
        .collect();
    for d in denied {
        let d_comps: Vec<String> = d
            .components()
            .filter_map(|c| match c {
                std::path::Component::Normal(s) => Some(s.to_string_lossy().to_string()),
                _ => None,
            })
            .collect();
        if d_comps.is_empty() {
            continue;
        }
        if comps
            .windows(d_comps.len())
            .any(|w| w == d_comps.as_slice())
        {
            return true;
        }
        if comps.last() == d_comps.last() && d_comps.len() == 1 {
            return true;
        }
    }
    false
}

// ─── THE SANDBOX TRAIT ──────────────────────────────────────────────────────────

/// Isolates `exec`/`Mutating` actions. Backends are selectable per run.
#[async_trait]
pub trait Sandbox: Send + Sync {
    async fn exec(&self, cmd: &Command, limits: &Limits) -> anyhow::Result<ExecResult>;
    fn root(&self) -> &Path;
    fn policy(&self) -> &FsNetPolicy;
}

// ─── Local sandbox implementation ───────────────────────────────────────────────

pub struct LocalSandbox {
    root: PathBuf,
    policy: FsNetPolicy,
}

impl LocalSandbox {
    pub fn new(root: PathBuf) -> Self {
        Self {
            root: root.clone(),
            policy: FsNetPolicy {
                allowed_paths: vec![root],
                allow_network: true,
                ..FsNetPolicy::default()
            },
        }
    }

    pub fn hardened(root: PathBuf) -> Self {
        Self {
            root: root.clone(),
            policy: FsNetPolicy {
                allowed_paths: vec![root],
                allow_network: false, // deny by default for hardened
                ..FsNetPolicy::default()
            },
        }
    }

    pub fn with_policy(mut self, policy: FsNetPolicy) -> Self {
        self.policy = policy;
        self
    }
}

#[async_trait]
impl Sandbox for LocalSandbox {
    async fn exec(&self, cmd: &Command, limits: &Limits) -> anyhow::Result<ExecResult> {
        use std::process::Command as StdCommand;
        use std::time::Instant;

        let root = self
            .root
            .canonicalize()
            .unwrap_or_else(|_| self.root.clone());
        let workdir = cmd
            .workdir
            .canonicalize()
            .unwrap_or_else(|_| cmd.workdir.clone());
        if !workdir.starts_with(&root) {
            anyhow::bail!(
                "Command workdir escapes sandbox root: {}",
                cmd.workdir.display()
            );
        }

        if path_is_denied(&workdir, &self.policy.denied_paths) {
            anyhow::bail!(
                "Command workdir hits a protected path: {}",
                cmd.workdir.display()
            );
        }
        for arg in &cmd.args {
            let p = Path::new(arg);
            if path_is_denied(p, &self.policy.denied_paths) {
                anyhow::bail!("Command argument refers to a protected path: {}", arg);
            }
        }

        let env: HashMap<String, String> = if self.policy.env_allowlist.is_empty() {
            cmd.env.clone()
        } else {
            cmd.env
                .iter()
                .filter(|(k, _)| self.policy.env_allowlist.iter().any(|a| a == *k))
                .map(|(k, v)| (k.clone(), v.clone()))
                .collect()
        };

        let mut builder = StdCommand::new(&cmd.program);
        builder
            .args(&cmd.args)
            .current_dir(&workdir)
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped());
        if !self.policy.env_allowlist.is_empty() {
            builder.env_clear();
        }
        builder.envs(&env);
        let mut child = builder.spawn()?;

        let start = Instant::now();
        let timeout = std::time::Duration::from_millis(limits.timeout_ms);

        // Simple timeout via polling
        loop {
            match child.try_wait()? {
                Some(status) => {
                    let output = child.wait_with_output()?;
                    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
                    let stderr = String::from_utf8_lossy(&output.stderr).to_string();
                    let exit_code = status.code().unwrap_or(-1);

                    return Ok(ExecResult {
                        stdout: truncate(stdout, limits.max_output_bytes),
                        stderr: truncate(stderr, limits.max_output_bytes),
                        exit_code,
                    });
                }
                None => {
                    if start.elapsed() > timeout {
                        let _ = child.kill();
                        return Ok(ExecResult {
                            stdout: String::new(),
                            stderr: "TIMEOUT".to_string(),
                            exit_code: -1,
                        });
                    }
                    tokio::time::sleep(std::time::Duration::from_millis(50)).await;
                }
            }
        }
    }

    fn root(&self) -> &Path {
        &self.root
    }

    fn policy(&self) -> &FsNetPolicy {
        &self.policy
    }
}

fn truncate(s: String, max_bytes: usize) -> String {
    if s.len() <= max_bytes {
        s
    } else {
        let truncate_at = max_bytes.saturating_sub(100);
        format!(
            "{}\n... [truncated, {} bytes total]",
            &s[..truncate_at.min(s.len())],
            s.len()
        )
    }
}