vetto 0.2.22

Daemon-less sandbox + security layer for AI coding agents (Landlock/Seatbelt, TUI statusline, post-session audit reports)
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
//! Project ecosystem detection, interactive wizard, and tailored policy generation for `vetto init`.

use anyhow::{bail, Context, Result};
use std::io::{BufRead, Write};
use std::path::Path;

use crate::policy::presets::agent_network_allowlist;
use crate::shim::registry::ShimRegistry;

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ProjectAnalysis {
    pub project_name: String,
    pub detected_ecosystems: Vec<&'static str>,
    pub detected_agents: Vec<&'static str>,
    pub recommended_allow_read: Vec<String>,
    pub recommended_network_domains: Vec<String>,
    pub detected_shims: Vec<String>,
}

pub fn analyze_project(root: &Path) -> ProjectAnalysis {
    let mut analysis = ProjectAnalysis::default();

    let name = root
        .canonicalize()
        .ok()
        .and_then(|p| p.file_name().map(|n| n.to_string_lossy().to_string()))
        .unwrap_or_else(|| "project".to_string());
    analysis.project_name = name;

    // Detect binary shims for project
    analysis.detected_shims = ShimRegistry::detect_for_project(root);

    // Rust
    if root.join("Cargo.toml").exists() {
        analysis.detected_ecosystems.push("Rust");
        analysis
            .recommended_allow_read
            .push("$HOME/.cargo/registry".to_string());
        analysis
            .recommended_allow_read
            .push("$HOME/.cargo/git".to_string());
        analysis
            .recommended_allow_read
            .push("$HOME/.rustup".to_string());
        analysis
            .recommended_network_domains
            .push("crates.io:443".to_string());
        analysis
            .recommended_network_domains
            .push("static.crates.io:443".to_string());
    }

    // Node.js / TypeScript
    let is_node = root.join("package.json").exists()
        || root.join("pnpm-lock.yaml").exists()
        || root.join("yarn.lock").exists()
        || root.join("bun.lockb").exists();
    if is_node {
        if root.join("tsconfig.json").exists() {
            analysis.detected_ecosystems.push("Node.js (TypeScript)");
        } else {
            analysis.detected_ecosystems.push("Node.js");
        }
        analysis
            .recommended_allow_read
            .push("$HOME/.npm".to_string());
        analysis
            .recommended_allow_read
            .push("$HOME/.local/share/pnpm/store".to_string());
        analysis
            .recommended_network_domains
            .push("registry.npmjs.org:443".to_string());
    }

    // Python
    let is_python = root.join("pyproject.toml").exists()
        || root.join("requirements.txt").exists()
        || root.join("Pipfile").exists()
        || root.join("poetry.lock").exists()
        || root.join("setup.py").exists();
    if is_python {
        analysis.detected_ecosystems.push("Python");
        analysis
            .recommended_allow_read
            .push("$HOME/.cache/pip".to_string());
        analysis
            .recommended_allow_read
            .push("$HOME/.cache/uv".to_string());
        analysis
            .recommended_network_domains
            .push("pypi.org:443".to_string());
        analysis
            .recommended_network_domains
            .push("files.pythonhosted.org:443".to_string());
    }

    // Go
    if root.join("go.mod").exists() {
        analysis.detected_ecosystems.push("Go");
        analysis
            .recommended_allow_read
            .push("$HOME/go/pkg/mod".to_string());
        analysis
            .recommended_network_domains
            .push("proxy.golang.org:443".to_string());
        analysis
            .recommended_network_domains
            .push("sum.golang.org:443".to_string());
    }

    // AI Agents in Repo
    if root.join(".cursor").exists() || root.join(".cursorrules").exists() {
        analysis.detected_agents.push("Cursor");
    }
    if root.join(".claude").exists() || root.join("CLAUDE.md").exists() {
        analysis.detected_agents.push("Claude Code");
    }
    if root.join("codex.toml").exists() || root.join(".codex").exists() {
        analysis.detected_agents.push("OpenAI Codex");
    }
    if root.join(".aider.conf.yml").exists() || root.join(".aider.tags.cache.v3").exists() {
        analysis.detected_agents.push("Aider");
    }

    // Always recommend GitHub domain if git is present
    if root.join(".git").exists() {
        analysis
            .recommended_network_domains
            .push("github.com:443".to_string());
        analysis
            .recommended_network_domains
            .push("api.github.com:443".to_string());
    }

    analysis.recommended_allow_read.sort();
    analysis.recommended_allow_read.dedup();
    analysis.recommended_network_domains.sort();
    analysis.recommended_network_domains.dedup();

    analysis
}

/// Generate a fully-commented policy.toml template covering all sections.
pub fn generate_policy_toml(analysis: &ProjectAnalysis) -> String {
    let eco_str = if analysis.detected_ecosystems.is_empty() {
        "Generic".to_string()
    } else {
        analysis.detected_ecosystems.join(", ")
    };

    let agents_str = if analysis.detected_agents.is_empty() {
        "Auto-detected".to_string()
    } else {
        analysis.detected_agents.join(", ")
    };

    let mut out = format!(
        r#"# policy.toml - Project Security Policy
# Generated by `vetto init` for {eco_str} ({agents_str})
#
# Documentation: https://github.com/shleder/vetto

[metadata]
name = "{}"
description = "Vetto security policy tailored for {}"
extends = ["default"]

[security]
# When immutable = true, lower configuration layers cannot override or relax rules.
# immutable = false

[filesystem]
# Project directory and scratch space are writable:
allow_write = [
  "$PROJECT",
  "$PROJECT/target/",
  "/tmp",
  "/dev/null",
]

# Sensitive directories denied from write access:
# deny_write = [
#   "$PROJECT/.git",
# ]

# System toolchain caches and package registries allowed for reading:
allow_read = [
"#,
        analysis.project_name, eco_str
    );

    if analysis.recommended_allow_read.is_empty() {
        out.push_str("  \"$PROJECT\",\n");
    } else {
        for path in &analysis.recommended_allow_read {
            out.push_str(&format!("  \"{path}\",\n"));
        }
    }

    out.push_str(
        r#"]

# Explicitly denied read paths:
# deny_read = [
#   "$HOME/.ssh",
#   "$HOME/.gnupg",
# ]

[display_only_deny]
# Sensitive credential-shaped files masked and blocked inside the sandbox:
paths = [
  "$PROJECT/.env",
  "$PROJECT/.env.*",
  "$PROJECT/*.pem",
  "$PROJECT/*.key",
  "$PROJECT/*.pfx",
  "$PROJECT/*.kdbx",
]

[environment]
# Environment variables passed through to the sandboxed agent:
pass_through = [
  "HOME",
  "PATH",
  "USER",
  "LANG",
  "LC_*",
]

# Environment variables explicitly denied / stripped:
# deny = [
#   "AWS_SECRET_ACCESS_KEY",
#   "GITHUB_TOKEN",
# ]

[network]
# Network isolation mode: "off" | "allowlist"
mode = "allowlist"
allow = [
"#,
    );

    if analysis.recommended_network_domains.is_empty() {
        out.push_str("  \"github.com:443\",\n");
    } else {
        for domain in &analysis.recommended_network_domains {
            out.push_str(&format!("  \"{domain}\",\n"));
        }
    }

    out.push_str(
        r#"]

[limits]
# Optional resource ceilings for sandboxed processes:
# cpu_seconds = 3600
# address_space_bytes = 8589934592  # 8 GiB
# processes = 512
# open_files = 4096
# file_size_bytes = 1073741824      # 1 GiB
"#,
    );

    out
}

/// Run the 3-question interactive setup wizard.
pub fn run_wizard(
    root: &Path,
    reader: &mut impl BufRead,
    writer: &mut impl Write,
) -> Result<String> {
    writer.write_all(b"vetto first-run wizard:\n")?;
    writer.write_all(b"1. Which AI coding agent do you use? [claude / codex / opencode / gemini / cursor / aider / windsurf / none]: ")?;
    writer.flush()?;

    let mut agent_line = String::new();
    reader.read_line(&mut agent_line)?;
    let agent_choice = agent_line.trim().to_ascii_lowercase();
    let agent = if agent_choice.is_empty() || agent_choice == "none" {
        None
    } else {
        Some(agent_choice)
    };

    writer.write_all(b"2. Does the agent need internet / network access? [y/N / agent-only]: ")?;
    writer.flush()?;

    let mut net_line = String::new();
    reader.read_line(&mut net_line)?;
    let net_choice = net_line.trim().to_ascii_lowercase();
    let allow_net = net_choice == "y" || net_choice == "yes" || net_choice == "agent-only";

    writer.write_all(b"3. What should be considered the project workspace root? [default: .]: ")?;
    writer.flush()?;

    let mut root_line = String::new();
    reader.read_line(&mut root_line)?;
    let root_choice = root_line.trim();
    let workspace_root = if root_choice.is_empty() {
        "$PROJECT"
    } else {
        root_choice
    };

    let mut analysis = analyze_project(root);
    if let Some(ref a) = agent {
        analysis.detected_agents = vec![match a.as_str() {
            "claude" => "Claude Code",
            "codex" => "OpenAI Codex",
            "gemini" => "Google Gemini",
            "antigravity" => "Antigravity CLI",
            "aider" => "Aider",
            "opencode" => "OpenCode",
            "cursor" => "Cursor",
            "cline" => "Cline",
            "copilot" => "GitHub Copilot",
            "windsurf" => "Windsurf",
            "continue" => "Continue",
            "goose" => "Block Goose",
            "openhands" => "OpenHands",
            "swe-agent" | "swe_agent" => "SWE-agent",
            "plandex" => "Plandex",
            "mentat" => "Mentat",
            "gpt-engineer" | "gpt_engineer" => "GPT Engineer",
            "devin" => "Cognition Devin",
            "crust" => "Crust AI",
            "amp" => "Amp AI",
            _ => "Custom Agent",
        }];
        if allow_net {
            let domains = agent_network_allowlist(a);
            analysis.recommended_network_domains.extend(domains);
            analysis.recommended_network_domains.sort();
            analysis.recommended_network_domains.dedup();
        }
    }

    if !allow_net {
        analysis.recommended_network_domains.clear();
    }

    let mut toml = generate_policy_toml(&analysis);
    if workspace_root != "$PROJECT" {
        toml = toml.replace("\"$PROJECT\"", &format!("\"{workspace_root}\""));
    }

    Ok(toml)
}

pub fn run_init(root: &Path, force: bool, wizard: bool) -> Result<()> {
    let policy_path = root.join("policy.toml");
    let legacy_path = root.join("vetto.toml");

    if (policy_path.exists() || legacy_path.exists()) && !force {
        bail!("policy file already exists in this directory (use --force to overwrite)");
    }

    let toml_content = if wizard {
        let mut stdin = std::io::stdin().lock();
        let mut stdout = std::io::stdout();
        run_wizard(root, &mut stdin, &mut stdout)?
    } else {
        let analysis = analyze_project(root);
        generate_policy_toml(&analysis)
    };

    std::fs::write(&policy_path, toml_content)
        .with_context(|| format!("failed to write {}", policy_path.display()))?;

    println!(
        "vetto: initialized security policy at {}",
        policy_path.display()
    );
    println!();
    println!("Next steps:");
    println!("  # Run your AI coding agent inside the sandbox:");
    println!("  vetto -- <agent_command>");
    println!();
    println!("  # Or run with zero-config auto-detection:");
    println!("  vetto");
    println!();
    println!("  # Inspect effective policy:");
    println!("  vetto policy explain");

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::io::Cursor;
    use std::path::PathBuf;

    fn temp_test_dir(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "vetto-init-test-{name}-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        dir
    }

    #[test]
    fn detects_rust_node_and_claude_project() {
        let dir = temp_test_dir("rust-node");
        let path = dir.as_path();

        fs::write(path.join("Cargo.toml"), "[package]\nname = \"test\"").unwrap();
        fs::write(path.join("package.json"), "{}").unwrap();
        fs::write(path.join("tsconfig.json"), "{}").unwrap();
        fs::write(path.join("CLAUDE.md"), "# Claude instructions").unwrap();

        let analysis = analyze_project(path);
        assert!(analysis.detected_ecosystems.contains(&"Rust"));
        assert!(analysis
            .detected_ecosystems
            .contains(&"Node.js (TypeScript)"));
        assert!(analysis.detected_agents.contains(&"Claude Code"));
        assert!(analysis
            .recommended_allow_read
            .contains(&"$HOME/.cargo/registry".to_string()));
        assert!(analysis
            .recommended_network_domains
            .contains(&"crates.io:443".to_string()));
        assert!(analysis
            .recommended_network_domains
            .contains(&"registry.npmjs.org:443".to_string()));
        assert!(analysis.detected_shims.contains(&"cargo".to_string()));
        assert!(analysis.detected_shims.contains(&"node".to_string()));

        let toml = generate_policy_toml(&analysis);
        assert!(toml.contains("Rust, Node.js (TypeScript)"));
        assert!(toml.contains("$HOME/.cargo/registry"));
        assert!(toml.contains("$PROJECT/.env"));
        assert!(toml.contains("[metadata]"));
        assert!(toml.contains("[security]"));
        assert!(toml.contains("[filesystem]"));
        assert!(toml.contains("[display_only_deny]"));
        assert!(toml.contains("[environment]"));
        assert!(toml.contains("[network]"));
        assert!(toml.contains("[limits]"));

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn wizard_answers_tailor_generated_policy() {
        let dir = temp_test_dir("wizard");
        let input = "claude\nyes\n/workspace\n";
        let mut reader = Cursor::new(input);
        let mut writer = Vec::new();

        let toml = run_wizard(&dir, &mut reader, &mut writer).unwrap();
        assert!(toml.contains("Claude Code"));
        assert!(toml.contains("api.anthropic.com"));
        assert!(toml.contains("/workspace"));

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn run_init_creates_policy_file_and_respects_force() {
        let dir = temp_test_dir("init-force");
        let path = dir.as_path();

        assert!(run_init(path, false, false).is_ok());
        assert!(path.join("policy.toml").exists());

        // Second run without force should fail
        assert!(run_init(path, false, false).is_err());

        // Run with force should succeed
        assert!(run_init(path, true, false).is_ok());

        let _ = fs::remove_dir_all(&dir);
    }
}