vtcode_safety/sandboxing/
debug.rs1use std::path::Path;
7use std::process::Stdio;
8
9use anyhow::{Context, Result};
10use tokio::process::Command;
11
12use super::{CommandSpec, LinuxSandboxLauncher, SandboxManager, SandboxPolicy, SandboxType};
13
14#[derive(Debug)]
16pub struct SandboxDebugResult {
17 success: bool,
19 stdout: String,
21 stderr: String,
23 sandbox_active: bool,
25}
26
27impl SandboxDebugResult {
28 #[must_use]
30 pub fn success(&self) -> bool {
31 self.success
32 }
33
34 #[must_use]
39 pub fn sandbox_active(&self) -> bool {
40 self.sandbox_active
41 }
42
43 #[must_use]
45 pub fn stdout(&self) -> &str {
46 &self.stdout
47 }
48
49 #[must_use]
51 pub fn stderr(&self) -> &str {
52 &self.stderr
53 }
54
55 fn unavailable(sandbox_type: SandboxType) -> Self {
57 Self {
58 success: false,
59 stdout: String::new(),
60 stderr: format!("Sandbox type {sandbox_type:?} is not available on this platform"),
61 sandbox_active: false,
62 }
63 }
64}
65
66#[expect(
70 unused_results,
71 reason = "Command builder methods configure the owned process command and return a fluent mutable reference."
72)]
73pub async fn debug_sandbox(
74 sandbox_type: SandboxType,
75 policy: &SandboxPolicy,
76 command: &[String],
77 cwd: &Path,
78 linux_launcher: Option<&LinuxSandboxLauncher>,
79 proxy: Option<&super::network_proxy::ProxyEndpoints>,
80) -> Result<SandboxDebugResult> {
81 if !sandbox_type.is_available() {
82 return Ok(SandboxDebugResult::unavailable(sandbox_type));
83 }
84
85 if command.is_empty() {
86 anyhow::bail!("Command cannot be empty");
87 }
88
89 let program = command.first().context("Command cannot be empty")?;
90 let args = command.get(1..).unwrap_or_default().to_vec();
91 let spec = CommandSpec::new(program).with_args(args).with_cwd(cwd);
92
93 let manager = SandboxManager::new();
94 let exec_env = manager
95 .transform_with_proxy(spec, policy, cwd, linux_launcher, proxy)
96 .context("Failed to transform command for sandbox")?;
97
98 let mut cmd = Command::new(&exec_env.program);
99 cmd.args(&exec_env.args)
100 .current_dir(&exec_env.cwd)
101 .env_clear()
102 .envs(&exec_env.env)
103 .stdout(Stdio::piped())
104 .stderr(Stdio::piped());
105
106 let output = cmd.output().await.context("Failed to execute sandboxed command")?;
107
108 Ok(SandboxDebugResult {
109 success: output.status.success(),
110 stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
111 stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
112 sandbox_active: exec_env.sandbox_active,
113 })
114}
115
116pub async fn test_path_writable(
118 policy: &SandboxPolicy,
119 test_path: &Path,
120 cwd: &Path,
121 linux_launcher: Option<&LinuxSandboxLauncher>,
122) -> Result<bool> {
123 let test_file = test_path.join(".vtcode_sandbox_test");
124 let test_command = vec![
125 "sh".to_string(),
126 "-c".to_string(),
127 format!("touch '{}' && rm -f '{}'", test_file.display(), test_file.display()),
128 ];
129
130 let result =
131 debug_sandbox(SandboxType::platform_default(), policy, &test_command, cwd, linux_launcher, None).await?;
132
133 Ok(result.success)
134}
135
136pub async fn test_network_blocked(
138 policy: &SandboxPolicy,
139 cwd: &Path,
140 linux_launcher: Option<&LinuxSandboxLauncher>,
141) -> Result<bool> {
142 let test_command = vec![
143 "sh".to_string(),
144 "-c".to_string(),
145 "curl -s --connect-timeout 2 https://example.com > /dev/null 2>&1".to_string(),
146 ];
147
148 let result =
149 debug_sandbox(SandboxType::platform_default(), policy, &test_command, cwd, linux_launcher, None).await?;
150
151 Ok(!result.success)
152}
153
154pub fn sandbox_capabilities_summary() -> String {
156 let mut summary = String::new();
157
158 summary.push_str("VT Code Sandbox Capabilities\n");
159 summary.push_str("=============================\n\n");
160
161 summary.push_str(&format!("Platform default: {:?}\n\n", SandboxType::platform_default()));
162
163 summary.push_str("Available sandbox types:\n");
164 for sandbox_type in [
165 SandboxType::MacosSeatbelt,
166 SandboxType::LinuxLandlock,
167 SandboxType::WindowsRestrictedToken,
168 ] {
169 let available = if sandbox_type.is_available() { "✓" } else { "✗" };
170 summary.push_str(&format!(" {available} {sandbox_type:?}\n"));
171 }
172
173 summary.push_str("\nSandbox policies:\n");
174 summary.push_str(" - ReadOnly: Read files, no writes except /dev/null, optional network policy\n");
175 summary.push_str(" - WorkspaceWrite: Read all, write to workspace, optional network allowlist\n");
176 summary.push_str(" - DangerFullAccess: No restrictions (use with caution)\n");
177
178 summary.push_str("\nSecurity features:\n");
179 summary.push_str(" - Sensitive path blocking (~/.ssh, ~/.aws, etc.)\n");
180 summary.push_str(" - read-only git/agent/editor config dirs (.git, .codex, .claude, .cursor, .vscode, …)\n");
181 summary.push_str(" - Environment variable sanitization\n");
182 summary.push_str(" - Seccomp syscall filtering (Linux)\n");
183 summary.push_str(" - Resource limits (memory, PIDs, disk, CPU)\n");
184
185 summary
186}
187
188#[derive(Debug, Clone, Copy, PartialEq, Eq)]
190pub enum DebugSubcommand {
191 Seatbelt,
193 Landlock,
195 Capabilities,
197}
198
199impl DebugSubcommand {
200 fn sandbox_type(&self) -> SandboxType {
202 match self {
203 Self::Seatbelt => SandboxType::MacosSeatbelt,
204 Self::Landlock => SandboxType::LinuxLandlock,
205 Self::Capabilities => SandboxType::platform_default(),
206 }
207 }
208}
209
210#[cfg(test)]
211mod tests {
212 use super::*;
213
214 #[test]
215 fn test_capabilities_summary() {
216 let summary = sandbox_capabilities_summary();
217 assert!(summary.contains("VT Code Sandbox Capabilities"));
218 assert!(summary.contains("Platform default"));
219 assert!(summary.contains("ReadOnly"));
220 assert!(summary.contains("WorkspaceWrite"));
221 }
222
223 #[test]
224 fn test_debug_subcommand() {
225 assert_eq!(DebugSubcommand::Seatbelt.sandbox_type(), SandboxType::MacosSeatbelt);
226 assert_eq!(DebugSubcommand::Landlock.sandbox_type(), SandboxType::LinuxLandlock);
227 }
228
229 #[tokio::test]
230 async fn test_debug_sandbox_unavailable() {
231 let result = SandboxDebugResult::unavailable(SandboxType::LinuxLandlock);
232 assert!(!result.success);
233 assert!(!result.sandbox_active);
234 assert!(result.stderr.contains("not available"));
235 }
236}