vtcode-safety 0.185.2

Command safety detection, execution policies, and sandboxing for VT Code
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
//! Local HTTP CONNECT / SOCKS5 proxy used to enforce hostname allowlists.
//!
//! The OS sandbox cannot inspect `connect()` destinations (seccomp BPF sees a
//! pointer, not the sockaddr contents), so exact domain rules are enforced by
//! a loopback proxy: the sandbox permits connections only to the proxy, and
//! the proxy resolves DNS **after** deciding that a hostname is allowed.
//! Deny rules take precedence over allow rules; anything unmatched is denied.

#![expect(
    unused_results,
    reason = "Detached per-connection tasks and byte-copy counts are intentionally discarded."
)]

use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use std::time::Duration;

use anyhow::{Context, Result, anyhow, bail};
use tokio::io::{AsyncReadExt, AsyncWriteExt, copy_bidirectional};
use tokio::net::{TcpListener, TcpStream};
use tokio::task::JoinHandle;
use tokio::time::timeout;
use tracing::warn;

use crate::sandboxing::NetworkAllowlistEntry;

#[cfg(test)]
mod tests;

const IO_TIMEOUT: Duration = Duration::from_secs(30);
const MAX_HTTP_HEADER: usize = 8 * 1024;

/// Effect of a domain rule.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DomainRuleAction {
    Allow,
    Deny,
}

/// One hostname rule. Domains are case-insensitive; `*.example.com` also
/// matches the bare `example.com`, matching the existing allowlist semantics.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DomainRule {
    pub domain: String,
    pub port: u16,
    pub action: DomainRuleAction,
}

impl DomainRule {
    #[must_use]
    pub fn allow(domain: impl Into<String>, port: u16) -> Self {
        Self {
            domain: domain.into(),
            port,
            action: DomainRuleAction::Allow,
        }
    }

    #[must_use]
    pub fn deny(domain: impl Into<String>, port: u16) -> Self {
        Self {
            domain: domain.into(),
            port,
            action: DomainRuleAction::Deny,
        }
    }

    fn matches(&self, host: &str, port: u16) -> bool {
        if self.port != port {
            return false;
        }
        let host = host.trim_end_matches('.').to_ascii_lowercase();
        let pattern = self.domain.trim_end_matches('.').to_ascii_lowercase();
        if let Some(suffix) = pattern.strip_prefix("*.") {
            host == suffix || host.ends_with(&format!(".{suffix}"))
        } else {
            host == pattern
        }
    }
}

/// Decision for a proxy request. Unknown destinations are denied.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProxyDecision {
    Allow,
    Deny,
}

/// Ordered domain rules; deny wins, then allow, then default deny.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DomainPolicy {
    rules: Vec<DomainRule>,
}

impl DomainPolicy {
    #[must_use]
    pub fn new(rules: Vec<DomainRule>) -> Self {
        Self { rules }
    }

    /// Build an allow-only policy from the sandbox config's allowlist.
    #[must_use]
    pub fn from_allowlist(entries: &[NetworkAllowlistEntry]) -> Self {
        Self::new(
            entries
                .iter()
                .map(|entry| DomainRule::allow(entry.domain.clone(), entry.port))
                .collect(),
        )
    }

    #[must_use]
    pub fn decide(&self, host: &str, port: u16) -> ProxyDecision {
        if self
            .rules
            .iter()
            .any(|rule| rule.action == DomainRuleAction::Deny && rule.matches(host, port))
        {
            return ProxyDecision::Deny;
        }
        if self
            .rules
            .iter()
            .any(|rule| rule.action == DomainRuleAction::Allow && rule.matches(host, port))
        {
            return ProxyDecision::Allow;
        }
        ProxyDecision::Deny
    }
}

/// Whether this platform can restrict a child to the proxy exactly.
///
/// macOS Seatbelt can allow only the proxy's loopback ports under `(deny
/// default)`. Linux cannot: seccomp cannot inspect `connect()` destinations
/// and this build has no network-namespace relay, so hostname allowlists stay
/// fail-closed there rather than granting unrestricted IP sockets.
#[must_use]
pub const fn proxy_enforcement_available() -> bool {
    cfg!(target_os = "macos")
}

/// Loopback addresses the sandbox must allow outbound to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ProxyEndpoints {
    pub http: SocketAddr,
    pub socks5: SocketAddr,
}

impl ProxyEndpoints {
    /// Environment variables that route common HTTP clients through the proxy.
    /// The sandbox's environment allowlist drops unknown variables, so these
    /// are injected after sanitization.
    #[must_use]
    pub fn env_vars(&self) -> Vec<(&'static str, String)> {
        // Use `localhost`, which is also the host the Seatbelt profile permits.
        let http = format!("http://localhost:{}", self.http.port());
        let socks = format!("socks5://localhost:{}", self.socks5.port());
        vec![
            ("HTTP_PROXY", http.clone()),
            ("http_proxy", http.clone()),
            ("HTTPS_PROXY", http.clone()),
            ("https_proxy", http.clone()),
            ("ALL_PROXY", socks.clone()),
            ("all_proxy", socks),
            // Override any inherited bypass list: an unmatched host must go
            // through the policy-enforcing proxy, never direct.
            ("NO_PROXY", String::new()),
            ("no_proxy", String::new()),
            ("PIP_PROXY", http.clone()),
            ("NPM_CONFIG_PROXY", http.clone()),
            ("NPM_CONFIG_HTTPS_PROXY", http.clone()),
            ("CARGO_HTTP_PROXY", http),
        ]
    }
}

/// Running proxy. Dropping it stops both accept loops.
pub struct NetworkProxy {
    endpoints: ProxyEndpoints,
    http_task: JoinHandle<()>,
    socks_task: JoinHandle<()>,
}

impl NetworkProxy {
    #[must_use]
    pub fn endpoints(&self) -> ProxyEndpoints {
        self.endpoints
    }
}

impl Drop for NetworkProxy {
    fn drop(&mut self) {
        self.http_task.abort();
        self.socks_task.abort();
    }
}

/// Start HTTP and SOCKS5 listeners on ephemeral loopback ports.
pub async fn start_proxy(policy: DomainPolicy) -> Result<NetworkProxy> {
    let http_listener = TcpListener::bind(("127.0.0.1", 0)).await.context("bind loopback HTTP proxy")?;
    let socks_listener = TcpListener::bind(("127.0.0.1", 0))
        .await
        .context("bind loopback SOCKS5 proxy")?;
    let endpoints = ProxyEndpoints {
        http: http_listener.local_addr().context("HTTP proxy local address")?,
        socks5: socks_listener.local_addr().context("SOCKS5 proxy local address")?,
    };
    let policy = Arc::new(policy);
    let http_policy = Arc::clone(&policy);
    let http_task = tokio::spawn(async move { serve_http(http_listener, http_policy).await });
    let socks_task = tokio::spawn(async move { serve_socks(socks_listener, policy).await });
    Ok(NetworkProxy { endpoints, http_task, socks_task })
}

async fn serve_http(listener: TcpListener, policy: Arc<DomainPolicy>) {
    loop {
        match listener.accept().await {
            Ok((stream, _)) => {
                let policy = Arc::clone(&policy);
                tokio::spawn(async move {
                    if let Err(error) = handle_http(stream, policy).await {
                        warn!(%error, "HTTP proxy request failed");
                    }
                });
            }
            Err(error) => {
                warn!(%error, "HTTP proxy accept failed");
                return;
            }
        }
    }
}

async fn serve_socks(listener: TcpListener, policy: Arc<DomainPolicy>) {
    loop {
        match listener.accept().await {
            Ok((stream, _)) => {
                let policy = Arc::clone(&policy);
                tokio::spawn(async move {
                    if let Err(error) = handle_socks(stream, policy).await {
                        warn!(%error, "SOCKS5 proxy request failed");
                    }
                });
            }
            Err(error) => {
                warn!(%error, "SOCKS5 proxy accept failed");
                return;
            }
        }
    }
}

async fn handle_http(mut client: TcpStream, policy: Arc<DomainPolicy>) -> Result<()> {
    let mut header = Vec::new();
    let mut chunk = [0u8; 1024];
    loop {
        if header.len() > MAX_HTTP_HEADER {
            bail!("HTTP proxy request header too large");
        }
        let read = timeout(IO_TIMEOUT, client.read(&mut chunk))
            .await
            .context("HTTP proxy read timeout")??;
        if read == 0 {
            return Ok(());
        }
        let bytes = chunk.get(..read).ok_or_else(|| anyhow!("HTTP proxy read exceeded buffer"))?;
        header.extend_from_slice(bytes);
        if header.windows(4).any(|window| window == b"\r\n\r\n") {
            break;
        }
    }
    let after_request_line = header
        .windows(2)
        .position(|window| window == b"\r\n")
        .ok_or_else(|| anyhow!("HTTP proxy request line missing"))?
        + 2;
    let request_line = std::str::from_utf8(
        header
            .get(..after_request_line.saturating_sub(2))
            .ok_or_else(|| anyhow!("short HTTP header"))?,
    )
    .context("HTTP proxy request line is not UTF-8")?
    .lines()
    .next()
    .unwrap_or_default();
    let mut parts = request_line.split_whitespace();
    let method = parts.next().unwrap_or_default();
    let target = parts.next().unwrap_or_default();

    let (host, port, is_connect) = if method.eq_ignore_ascii_case("CONNECT") {
        let (host, port) = parse_host_port(target, 443)?;
        (host, port, true)
    } else if target.starts_with("http://") || target.starts_with("https://") {
        let (host, port) = parse_url_authority(target)?;
        (host, port, false)
    } else {
        bail!("HTTP proxy requires CONNECT or an absolute-form URL");
    };

    if policy.decide(&host, port) == ProxyDecision::Deny {
        client
            .write_all(b"HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")
            .await
            .context("write HTTP proxy denial")?;
        return Ok(());
    }

    // DNS resolution happens only after the hostname passed the policy check.
    let mut upstream = timeout(IO_TIMEOUT, TcpStream::connect((host.as_str(), port)))
        .await
        .context("HTTP proxy connect timeout")?
        .context("HTTP proxy upstream connect")?;
    if is_connect {
        client
            .write_all(b"HTTP/1.1 200 Connection Established\r\n\r\n")
            .await
            .context("write CONNECT reply")?;
    } else {
        let origin_form = target
            .strip_prefix("http://")
            .or_else(|| target.strip_prefix("https://"))
            .and_then(|rest| rest.find('/').and_then(|index| rest.get(index..)))
            .unwrap_or("/");
        let rewritten = format!("{method} {origin_form} HTTP/1.1\r\n");
        upstream
            .write_all(rewritten.as_bytes())
            .await
            .context("forward HTTP request line")?;
        upstream
            // Keep the original headers and the terminating blank line; only
            // the request line is rewritten from absolute to origin form.
            .write_all(header.get(after_request_line..).ok_or_else(|| anyhow!("short HTTP header"))?)
            .await
            .context("forward HTTP headers")?;
    }
    copy_bidirectional(&mut client, &mut upstream)
        .await
        .context("relay HTTP proxy traffic")?;
    Ok(())
}

async fn handle_socks(mut client: TcpStream, policy: Arc<DomainPolicy>) -> Result<()> {
    let mut greeting = [0u8; 258];
    let read = timeout(IO_TIMEOUT, client.read(&mut greeting))
        .await
        .context("SOCKS5 greeting timeout")??;
    if read < 2 || greeting[0] != 5 {
        bail!("unsupported SOCKS5 greeting");
    }
    client.write_all(&[5, 0]).await.context("write SOCKS5 greeting reply")?;

    let mut request = [0u8; 4];
    timeout(IO_TIMEOUT, client.read_exact(&mut request))
        .await
        .context("SOCKS5 request timeout")?
        .context("SOCKS5 request header")?;
    if request[0] != 5 || request[1] != 1 {
        client.write_all(&[5, 7, 0, 1, 0, 0, 0, 0, 0, 0]).await?;
        bail!("unsupported SOCKS5 command");
    }

    let host = match request[3] {
        1 => {
            let mut octets = [0u8; 4];
            client.read_exact(&mut octets).await.context("SOCKS5 IPv4 address")?;
            IpAddr::from(octets).to_string()
        }
        3 => {
            let mut length = [0u8; 1];
            client.read_exact(&mut length).await.context("SOCKS5 domain length")?;
            let mut bytes = vec![0u8; usize::from(length[0])];
            client.read_exact(&mut bytes).await.context("SOCKS5 domain")?;
            String::from_utf8(bytes).context("SOCKS5 domain is not UTF-8")?
        }
        4 => {
            let mut octets = [0u8; 16];
            client.read_exact(&mut octets).await.context("SOCKS5 IPv6 address")?;
            IpAddr::from(octets).to_string()
        }
        _ => bail!("unsupported SOCKS5 address type"),
    };
    let mut port_bytes = [0u8; 2];
    client.read_exact(&mut port_bytes).await.context("SOCKS5 port")?;
    let port = u16::from_be_bytes(port_bytes);

    if policy.decide(&host, port) == ProxyDecision::Deny {
        client
            .write_all(&[5, 2, 0, 1, 0, 0, 0, 0, 0, 0])
            .await
            .context("write SOCKS5 denial")?;
        return Ok(());
    }

    // Do not resolve the hostname until after the policy decision.
    let mut upstream = timeout(IO_TIMEOUT, TcpStream::connect((host.as_str(), port)))
        .await
        .context("SOCKS5 connect timeout")?
        .context("SOCKS5 upstream connect")?;
    client
        .write_all(&[5, 0, 0, 1, 0, 0, 0, 0, 0, 0])
        .await
        .context("write SOCKS5 success")?;
    copy_bidirectional(&mut client, &mut upstream)
        .await
        .context("relay SOCKS5 traffic")?;
    Ok(())
}

fn parse_host_port(target: &str, default_port: u16) -> Result<(String, u16)> {
    let host = target.trim().trim_end_matches('/');
    if let Some((host, port)) = host.rsplit_once(':') {
        let port = port.parse::<u16>().context("invalid proxy port")?;
        Ok((host.trim_matches(['[', ']']).to_string(), port))
    } else {
        Ok((host.to_string(), default_port))
    }
}

fn parse_url_authority(target: &str) -> Result<(String, u16)> {
    let rest = target
        .strip_prefix("http://")
        .or_else(|| target.strip_prefix("https://"))
        .ok_or_else(|| anyhow!("unsupported proxy URL scheme"))?;
    let authority = rest.split('/').next().unwrap_or_default();
    let authority = authority.rsplit('@').next().unwrap_or_default();
    let default_port = if target.starts_with("https://") { 443 } else { 80 };
    parse_host_port(authority, default_port)
}