clash 0.5.3

Command Line Agent Safety Harness — permission policies for coding agents
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
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
//! HTTP forward proxy for domain-level network filtering.
//!
//! Provides a lightweight HTTP proxy that enforces domain allowlists at the
//! sandbox boundary.  Supports both CONNECT tunneling (for HTTPS) and plain
//! HTTP forwarding.  Built on tokio + hyper for correct HTTP framing and
//! connection lifecycle management.

use std::convert::Infallible;
use std::io;
use std::net::SocketAddr;
use std::sync::Arc;
use std::thread;

use bytes::Bytes;
use http_body_util::{BodyExt, Empty, Full};
use hyper::body::Incoming;
use hyper::server::conn::http1 as server_http1;
use hyper::service::service_fn;
use hyper::{Method, Request, Response};
use hyper_util::rt::TokioIo;
use tokio::io::copy_bidirectional;
use tokio::net::TcpStream;
use tokio::sync::oneshot;
use tracing::{debug, trace, warn};

// ---------------------------------------------------------------------------
// Body helpers
// ---------------------------------------------------------------------------

type BoxBody = http_body_util::combinators::BoxBody<Bytes, Infallible>;

fn empty_body() -> BoxBody {
    Empty::<Bytes>::new()
        .map_err(|never| match never {})
        .boxed()
}

fn full_body(msg: impl Into<Bytes>) -> BoxBody {
    Full::new(msg.into())
        .map_err(|never| match never {})
        .boxed()
}

fn error_response(status: u16, reason: &str) -> Response<BoxBody> {
    let body_text = format!("{status} {reason}\r\n");
    // The builder only fails if the status code is invalid; we control all
    // call sites and always pass valid HTTP status codes.
    Response::builder()
        .status(status)
        .header("Content-Type", "text/plain")
        .header("Connection", "close")
        .body(full_body(body_text))
        .expect("constructing error response with valid status code")
}

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

/// Configuration for the filtering proxy.
pub struct ProxyConfig {
    /// Domains that are permitted through the proxy.  An entry of
    /// `"github.com"` allows both `github.com` itself **and** any subdomain
    /// such as `api.github.com`.
    pub allowed_domains: Vec<String>,
}

/// Handle to a running proxy.  Dropping the handle initiates a clean shutdown.
pub struct ProxyHandle {
    /// The `127.0.0.1:<port>` address the proxy is listening on.
    pub addr: SocketAddr,
    /// Dropping the sender signals the accept loop to stop.
    shutdown: Option<oneshot::Sender<()>>,
    /// The std::thread running the tokio runtime.
    runtime_thread: Option<thread::JoinHandle<()>>,
}

impl Drop for ProxyHandle {
    fn drop(&mut self) {
        // Signal shutdown by dropping the sender.
        drop(self.shutdown.take());
        if let Some(handle) = self.runtime_thread.take() {
            let _ = handle.join();
        }
    }
}

// ---------------------------------------------------------------------------
// Domain matching
// ---------------------------------------------------------------------------

/// Returns `true` if `host` is permitted by the allowlist.
///
/// Matching rules:
/// - Exact match: `"github.com"` matches host `"github.com"`.
/// - Subdomain match: `"github.com"` matches host `"api.github.com"`.
/// - No false positives: `"github.com"` does **not** match `"notgithub.com"`.
pub fn is_domain_allowed(host: &str, allowed: &[String]) -> bool {
    let host = host.to_ascii_lowercase();
    for domain in allowed {
        let domain = domain.to_ascii_lowercase();
        if host == domain {
            return true;
        }
        // Subdomain match: host must end with `.<domain>`.
        if host.ends_with(&format!(".{domain}")) {
            return true;
        }
    }
    false
}

// ---------------------------------------------------------------------------
// Proxy entry point
// ---------------------------------------------------------------------------

/// Start the filtering proxy.
///
/// Binds to `127.0.0.1:0` (OS-assigned ephemeral port), spawns a background
/// tokio runtime that accepts connections, and returns a [`ProxyHandle`] whose
/// lifetime controls the proxy.
pub fn start_proxy(config: ProxyConfig) -> io::Result<ProxyHandle> {
    // Bind with std so the address is available synchronously.
    let std_listener = std::net::TcpListener::bind("127.0.0.1:0")?;
    let addr = std_listener.local_addr()?;
    debug!(addr = %addr, "proxy listening");

    let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();
    let config = Arc::new(config);

    let runtime_thread = thread::Builder::new()
        .name("proxy-runtime".into())
        .spawn(move || {
            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .expect("failed to build tokio runtime for proxy");

            rt.block_on(accept_loop(std_listener, shutdown_rx, config));
        })?;

    Ok(ProxyHandle {
        addr,
        shutdown: Some(shutdown_tx),
        runtime_thread: Some(runtime_thread),
    })
}

// ---------------------------------------------------------------------------
// Accept loop
// ---------------------------------------------------------------------------

async fn accept_loop(
    std_listener: std::net::TcpListener,
    shutdown_rx: oneshot::Receiver<()>,
    config: Arc<ProxyConfig>,
) {
    std_listener
        .set_nonblocking(true)
        .expect("failed to set listener non-blocking");
    let listener = tokio::net::TcpListener::from_std(std_listener)
        .expect("failed to create tokio TcpListener");

    tokio::pin!(shutdown_rx);

    loop {
        tokio::select! {
            result = listener.accept() => {
                match result {
                    Ok((stream, peer)) => {
                        trace!(peer = %peer, "accepted connection");
                        let cfg = Arc::clone(&config);
                        tokio::task::spawn(async move {
                            if let Err(e) = serve_connection(stream, cfg).await {
                                debug!(peer = %peer, error = %e, "connection finished with error");
                            }
                        });
                    }
                    Err(e) => {
                        warn!(error = %e, "accept error");
                    }
                }
            }
            _ = &mut shutdown_rx => {
                debug!("proxy accept loop shutting down");
                break;
            }
        }
    }
    debug!("proxy accept loop exiting");
}

// ---------------------------------------------------------------------------
// Per-connection handler (hyper service)
// ---------------------------------------------------------------------------

async fn serve_connection(
    stream: TcpStream,
    config: Arc<ProxyConfig>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let io = TokioIo::new(stream);

    server_http1::Builder::new()
        .preserve_header_case(true)
        .title_case_headers(true)
        .serve_connection(
            io,
            service_fn(move |req| {
                let cfg = Arc::clone(&config);
                async move { proxy_handler(req, cfg).await }
            }),
        )
        .with_upgrades()
        .await?;

    Ok(())
}

async fn proxy_handler(
    req: Request<Incoming>,
    config: Arc<ProxyConfig>,
) -> Result<Response<BoxBody>, Infallible> {
    if req.method() == Method::CONNECT {
        handle_connect(req, &config).await
    } else {
        handle_http(req, &config).await
    }
}

// ---------------------------------------------------------------------------
// CONNECT (HTTPS tunneling)
// ---------------------------------------------------------------------------

async fn handle_connect(
    req: Request<Incoming>,
    config: &ProxyConfig,
) -> Result<Response<BoxBody>, Infallible> {
    let authority = match req.uri().authority() {
        Some(auth) => auth.to_string(),
        None => {
            warn!("CONNECT request missing authority");
            return Ok(error_response(400, "Bad Request"));
        }
    };

    let host = extract_host_from_authority(&authority);

    if !is_domain_allowed(host, &config.allowed_domains) {
        warn!(host = %host, "CONNECT blocked by allowlist");
        return Ok(error_response(403, "Forbidden"));
    }

    debug!(authority = %authority, "CONNECT allowed");

    // Verify upstream is reachable before sending 200.
    let upstream = match TcpStream::connect(&authority).await {
        Ok(s) => s,
        Err(e) => {
            warn!(authority = %authority, error = %e, "failed to connect upstream");
            return Ok(error_response(502, "Bad Gateway"));
        }
    };

    // Spawn the tunnel task.  It awaits the upgrade, then relays bytes.
    tokio::task::spawn(async move {
        match hyper::upgrade::on(req).await {
            Ok(upgraded) => {
                let mut client = TokioIo::new(upgraded);
                let mut server = upstream;
                if let Err(e) = copy_bidirectional(&mut client, &mut server).await {
                    debug!(error = %e, "tunnel relay finished with error");
                }
            }
            Err(e) => {
                warn!(error = %e, "CONNECT upgrade failed");
            }
        }
    });

    // Return 200 to trigger the upgrade.
    Ok(Response::new(empty_body()))
}

// ---------------------------------------------------------------------------
// Plain HTTP forwarding
// ---------------------------------------------------------------------------

async fn handle_http(
    req: Request<Incoming>,
    config: &ProxyConfig,
) -> Result<Response<BoxBody>, Infallible> {
    let host = match extract_target_host(&req) {
        Some(h) => h,
        None => {
            warn!("could not determine target host");
            return Ok(error_response(400, "Bad Request"));
        }
    };

    if !is_domain_allowed(&host, &config.allowed_domains) {
        warn!(host = %host, "HTTP request blocked by allowlist");
        return Ok(error_response(403, "Forbidden"));
    }

    debug!(host = %host, method = %req.method(), "HTTP request allowed");

    // Determine upstream address.  Default to port 80.
    let addr = if let Some(authority) = req.uri().authority() {
        let auth_str = authority.to_string();
        if auth_str.contains(':') {
            auth_str
        } else {
            format!("{auth_str}:80")
        }
    } else {
        format!("{host}:80")
    };

    // Connect to upstream.
    let upstream = match TcpStream::connect(&addr).await {
        Ok(s) => s,
        Err(e) => {
            warn!(addr = %addr, error = %e, "failed to connect upstream");
            return Ok(error_response(502, "Bad Gateway"));
        }
    };

    let io = TokioIo::new(upstream);

    // HTTP/1.1 handshake with upstream.
    let (mut sender, conn) = match hyper::client::conn::http1::handshake(io).await {
        Ok(parts) => parts,
        Err(e) => {
            warn!(error = %e, "upstream handshake failed");
            return Ok(error_response(502, "Bad Gateway"));
        }
    };

    // Drive the upstream connection in the background.
    tokio::task::spawn(async move {
        if let Err(e) = conn.await {
            debug!(error = %e, "upstream connection error");
        }
    });

    // Transform URI from absolute-form to origin-form for the upstream.
    let (mut parts, body) = req.into_parts();
    let path_and_query = parts
        .uri
        .path_and_query()
        .map(|pq| pq.as_str())
        .unwrap_or("/");
    parts.uri = path_and_query.parse().unwrap_or_default();
    let upstream_req = Request::from_parts(parts, body);

    // Forward the request and collect the response.
    match sender.send_request(upstream_req).await {
        Ok(resp) => {
            let (resp_parts, incoming) = resp.into_parts();
            match incoming.collect().await {
                Ok(collected) => {
                    let body = full_body(collected.to_bytes());
                    Ok(Response::from_parts(resp_parts, body))
                }
                Err(e) => {
                    warn!(error = %e, "failed to read upstream response");
                    Ok(error_response(502, "Bad Gateway"))
                }
            }
        }
        Err(e) => {
            warn!(error = %e, "failed to forward request");
            Ok(error_response(502, "Bad Gateway"))
        }
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Extract the hostname portion from an `authority` string (`host:port`).
fn extract_host_from_authority(authority: &str) -> &str {
    // Handle IPv6 bracket notation: [::1]:443
    if authority.starts_with('[')
        && let Some(bracket_end) = authority.find(']')
    {
        return &authority[1..bracket_end];
    }
    authority
        .rsplit_once(':')
        .map(|(host, _port)| host)
        .unwrap_or(authority)
}

/// Extract the target hostname from a plain HTTP request.
///
/// Checks the absolute-form URI first, then falls back to the Host header.
fn extract_target_host(req: &Request<Incoming>) -> Option<String> {
    // Absolute-form URI: http://host[:port]/path
    if let Some(authority) = req.uri().authority() {
        let host = authority.host();
        if !host.is_empty() {
            return Some(host.to_string());
        }
    }

    // Host header fallback.
    if let Some(host_value) = req.headers().get(hyper::header::HOST)
        && let Ok(host_str) = host_value.to_str()
    {
        let host = host_str.split(':').next().unwrap_or(host_str);
        if !host.is_empty() {
            return Some(host.to_string());
        }
    }

    None
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::{BufRead, BufReader, Read, Write};
    use std::time::Duration;

    // -- Domain matching ---------------------------------------------------

    #[test]
    fn test_is_domain_allowed_exact() {
        let allowed = vec!["github.com".to_string()];
        assert!(is_domain_allowed("github.com", &allowed));
    }

    #[test]
    fn test_is_domain_allowed_subdomain() {
        let allowed = vec!["github.com".to_string()];
        assert!(is_domain_allowed("api.github.com", &allowed));
    }

    #[test]
    fn test_is_domain_allowed_no_false_positive() {
        let allowed = vec!["github.com".to_string()];
        assert!(!is_domain_allowed("notgithub.com", &allowed));
    }

    #[test]
    fn test_is_domain_allowed_denied() {
        let allowed = vec!["github.com".to_string()];
        assert!(!is_domain_allowed("evil.com", &allowed));
    }

    // -- Proxy lifecycle ---------------------------------------------------

    #[test]
    fn test_proxy_lifecycle() {
        let handle = start_proxy(ProxyConfig {
            allowed_domains: vec!["example.com".into()],
        })
        .unwrap();
        let addr = handle.addr;

        assert_eq!(addr.ip(), std::net::Ipv4Addr::LOCALHOST);
        assert_ne!(addr.port(), 0);

        // Should be able to connect.
        let stream = std::net::TcpStream::connect(addr).unwrap();
        drop(stream);

        // Drop the handle -- proxy should shut down deterministically.
        drop(handle);

        // After drop returns, the listener is closed.
        assert!(
            std::net::TcpStream::connect_timeout(&addr.into(), Duration::from_millis(200)).is_err(),
            "proxy should have stopped accepting connections"
        );
    }

    // -- Proxy blocks unlisted domains -------------------------------------

    #[test]
    fn test_proxy_denies_unlisted_domain() {
        let handle = start_proxy(ProxyConfig {
            allowed_domains: vec!["github.com".into()],
        })
        .unwrap();

        let mut stream = std::net::TcpStream::connect(handle.addr).unwrap();
        stream
            .set_read_timeout(Some(Duration::from_secs(5)))
            .unwrap();
        write!(
            stream,
            "CONNECT evil.com:443 HTTP/1.1\r\nHost: evil.com\r\n\r\n"
        )
        .unwrap();

        let mut reader = BufReader::new(&stream);
        let mut line = String::new();
        reader.read_line(&mut line).unwrap();
        let code: u16 = line.split_whitespace().nth(1).unwrap().parse().unwrap();
        assert_eq!(code, 403, "expected 403 Forbidden, got: {line}");
    }

    // -- Proxy allows listed domain via CONNECT ----------------------------

    #[test]
    fn test_proxy_allows_listed_domain_connect() {
        // Mock upstream server: accepts one connection, writes a greeting,
        // shuts down its write half, then drains reads.
        let upstream_listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind upstream");
        let upstream_port = upstream_listener.local_addr().unwrap().port();

        let upstream_thread = std::thread::spawn(move || {
            let (mut conn, _) = upstream_listener.accept().expect("upstream accept");
            conn.write_all(b"HELLO FROM UPSTREAM\n")
                .expect("upstream write");
            conn.flush().unwrap();
            let _ = conn.shutdown(std::net::Shutdown::Write);
            let mut buf = [0u8; 1024];
            while let Ok(n) = conn.read(&mut buf) {
                if n == 0 {
                    break;
                }
            }
        });

        let handle = start_proxy(ProxyConfig {
            allowed_domains: vec!["localhost".into()],
        })
        .unwrap();

        let mut stream = std::net::TcpStream::connect(handle.addr).unwrap();
        stream
            .set_read_timeout(Some(Duration::from_secs(5)))
            .unwrap();
        write!(
            stream,
            "CONNECT localhost:{upstream_port} HTTP/1.1\r\nHost: localhost\r\n\r\n"
        )
        .unwrap();

        let reader_stream = stream.try_clone().unwrap();
        let mut reader = BufReader::new(reader_stream);

        // Read status line.
        let mut status = String::new();
        reader.read_line(&mut status).unwrap();
        let code: u16 = status.split_whitespace().nth(1).unwrap().parse().unwrap();
        assert_eq!(code, 200, "expected 200, got: {status}");

        // Consume headers until blank line (hyper may add headers).
        loop {
            let mut hdr = String::new();
            reader.read_line(&mut hdr).unwrap();
            if hdr.trim().is_empty() {
                break;
            }
        }

        // Read the upstream greeting through the tunnel.
        let mut greeting = String::new();
        reader.read_line(&mut greeting).unwrap();
        assert_eq!(greeting, "HELLO FROM UPSTREAM\n");

        // Clean shutdown.
        let _ = stream.shutdown(std::net::Shutdown::Write);
        drop(stream);
        drop(reader);
        upstream_thread.join().expect("upstream thread panicked");
    }
}