rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use super::*;
use pretty_assertions::assert_eq;
use std::sync::atomic::{AtomicUsize, Ordering};

fn to(host: &str, port: u16) -> Destination {
    Destination {
        host: Host::Name(host.to_owned()),
        port,
    }
}

fn direct(addresses: &[&str]) -> Route {
    Route::Direct(addresses.iter().map(|a| a.parse().unwrap()).collect())
}

fn refused(text: &str) -> Route {
    Route::Refused(format!(
        "{text} is a link-local or cloud-metadata address, which the browser does not connect to"
    ))
}

fn resolve(name: &str) -> Vec<IpAddr> {
    let answer: &[&str] = match name {
        "example.com" => &["93.184.215.14"],
        "metadata.test" => &["169.254.169.254"],
        "mixed.test" => &["93.184.215.14", "169.254.169.254"],
        "dual.test" => &["2001:db9::5", "93.184.215.14"],
        _ => &[],
    };
    answer.iter().map(|a| a.parse().unwrap()).collect()
}

// Covers: with no proxy of the user's, the browser connects only to
// addresses rho looked up and checked: local and LAN servers and public
// hosts connect, `*.localhost` (also with the root's trailing dot) reaches
// loopback without DNS, and link-local
// and metadata addresses are refused, named as an address in Chrome's form
// (IPv6 without brackets) or behind a name, even when only one of a name's
// addresses is.
// Owner: browser connection routing
#[tokio::test]
async fn connections_go_only_to_checked_addresses() {
    let cases = [
        (to("127.0.0.1", 3000), direct(&["127.0.0.1:3000"])),
        (to("::1", 8000), direct(&["[::1]:8000"])),
        (
            to("localhost", 5173),
            direct(&["[::1]:5173", "127.0.0.1:5173"]),
        ),
        (
            to("app.localhost", 3000),
            direct(&["[::1]:3000", "127.0.0.1:3000"]),
        ),
        (
            to("app.localhost.", 3000),
            direct(&["[::1]:3000", "127.0.0.1:3000"]),
        ),
        (to("192.168.1.20", 8080), direct(&["192.168.1.20:8080"])),
        (to("example.com", 443), direct(&["93.184.215.14:443"])),
        (
            to("169.254.169.254", 80),
            refused("169.254.169.254 (169.254.169.254)"),
        ),
        (to("fe80::1", 80), refused("fe80::1 (fe80::1)")),
        (
            to("::ffff:a9fe:a9fe", 80),
            refused("::ffff:169.254.169.254 (::ffff:169.254.169.254)"),
        ),
        (
            to("100.100.100.200", 80),
            refused("100.100.100.200 (100.100.100.200)"),
        ),
        (
            to("metadata.test", 80),
            refused("metadata.test (169.254.169.254)"),
        ),
        (
            to("mixed.test", 443),
            refused("mixed.test (169.254.169.254)"),
        ),
    ];
    let none = Matcher::builder().build();
    for (destination, expected) in cases {
        let routed = ssrf::with_resolver(resolve, route(&destination, &none)).await;
        assert_eq!(routed.unwrap(), expected, "{destination:?}");
    }
}

// Covers: a name that passes the page check and then resolves to a
// metadata address when the browser connects is refused, because the
// connection uses rho's own, second lookup rather than Chrome's.
// Owner: browser connection routing (DNS rebinding)
#[tokio::test]
async fn a_name_that_rebinds_to_metadata_is_refused_at_connect() {
    let lookups = Arc::new(AtomicUsize::new(0));
    let rebinding = {
        let lookups = Arc::clone(&lookups);
        move |_: &str| match lookups.fetch_add(1, Ordering::SeqCst) {
            0 => vec!["93.184.215.14".parse().unwrap()],
            _ => vec!["169.254.169.254".parse().unwrap()],
        }
    };
    let none = Matcher::builder().build();
    let (checked, routed) = ssrf::with_resolver(rebinding, async {
        let checked = super::super::navigation::allowed("http://rebind.test/").await;
        (checked, route(&to("rebind.test", 80), &none).await.unwrap())
    })
    .await;
    assert_eq!(
        (checked, routed),
        (Ok(()), refused("rebind.test (169.254.169.254)"))
    );
}

fn named(upstream: &Upstream, name: &str, port: u16) -> Route {
    Route::ThroughNamed {
        upstream: upstream.clone(),
        to: to(name, port),
    }
}

fn at(upstream: &Upstream, addresses: &[&str]) -> Route {
    Route::ThroughAt {
        upstream: upstream.clone(),
        addresses: addresses.iter().map(|a| a.parse().unwrap()).collect(),
    }
}

// Covers: a user's own proxy carries the browser's connections, chosen by
// port as HTTPS_PROXY or HTTP_PROXY (either for another port); an address
// is passed on as one; a `socks5://` proxy gets every address rho looked
// up and checked, a `socks5h://` one the name; NO_PROXY hosts and networks
// and loopback (`*.localhost` too, with or without the trailing dot)
// connect directly, as in Chrome; refused addresses are refused before any
// proxy sees them.
// Owner: browser connection routing (user proxies)
#[tokio::test]
async fn a_users_proxy_carries_connections_rho_cannot_check() {
    let corporate = Matcher::builder()
        .http("http://proxy.corp:3128")
        .https("http://proxy.corp:3129")
        .no("intranet.corp, 10.0.0.0/8, 2001:db8::/32")
        .build();
    let https_only = Matcher::builder().https("http://proxy.corp:3129").build();
    let socks5 = Matcher::builder().all("socks5://socks.corp").build();
    let socks5h = Matcher::builder().all("socks5h://socks.corp:1081").build();
    let for_http = Upstream::Http {
        address: "proxy.corp:3128".to_owned(),
    };
    let for_https = Upstream::Http {
        address: "proxy.corp:3129".to_owned(),
    };
    let socks = Upstream::Socks5 {
        address: "socks.corp:1080".to_owned(),
    };
    let socks_h = Upstream::Socks5 {
        address: "socks.corp:1081".to_owned(),
    };
    let cases = [
        (
            &corporate,
            to("example.com", 80),
            named(&for_http, "example.com", 80),
        ),
        (
            &corporate,
            to("example.com", 443),
            named(&for_https, "example.com", 443),
        ),
        // The proxy looks the name up, so rho does not refuse it.
        (
            &corporate,
            to("metadata.test", 80),
            named(&for_http, "metadata.test", 80),
        ),
        (
            &corporate,
            to("203.0.113.9", 443),
            at(&for_https, &["203.0.113.9:443"]),
        ),
        (
            &corporate,
            to("2001:db9::1", 443),
            at(&for_https, &["[2001:db9::1]:443"]),
        ),
        (
            &corporate,
            to("169.254.169.254", 80),
            refused("169.254.169.254 (169.254.169.254)"),
        ),
        (
            &corporate,
            to("localhost", 3000),
            direct(&["[::1]:3000", "127.0.0.1:3000"]),
        ),
        (
            &corporate,
            to("app.localhost.", 3000),
            direct(&["[::1]:3000", "127.0.0.1:3000"]),
        ),
        (
            &corporate,
            to("127.0.0.1", 3000),
            direct(&["127.0.0.1:3000"]),
        ),
        (&corporate, to("10.1.2.3", 80), direct(&["10.1.2.3:80"])),
        (
            &corporate,
            to("2001:db8::1", 443),
            direct(&["[2001:db8::1]:443"]),
        ),
        (
            &corporate,
            to("intranet.corp", 80),
            direct(&["10.0.0.9:80"]),
        ),
        (
            &https_only,
            to("example.com", 8443),
            named(&for_https, "example.com", 8443),
        ),
        (
            &socks5,
            to("dual.test", 443),
            at(&socks, &["[2001:db9::5]:443", "93.184.215.14:443"]),
        ),
        (
            &socks5,
            to("metadata.test", 80),
            refused("metadata.test (169.254.169.254)"),
        ),
        (
            &socks5h,
            to("metadata.test", 80),
            named(&socks_h, "metadata.test", 80),
        ),
    ];
    let resolve = |name: &str| match name {
        "intranet.corp" => vec!["10.0.0.9".parse().unwrap()],
        name => resolve(name),
    };
    for (proxies, destination, expected) in cases {
        let routed = ssrf::with_resolver(resolve, route(&destination, proxies)).await;
        assert_eq!(routed.unwrap(), expected, "{destination:?}");
    }
}

// Covers: a proxy rho cannot connect through, or one with credentials,
// which other accounts could borrow through rho's proxy, is refused when
// the browser's options are built rather than failing each page, also when
// NO_PROXY covers a common host.
// Owner: browser connection routing (user proxies)
#[test]
fn an_unusable_proxy_is_refused_up_front() {
    let cases = [
        (
            Matcher::builder().all("https://proxy.corp:443").build(),
            false,
        ),
        (
            Matcher::builder().all("socks4://proxy.corp:1080").build(),
            false,
        ),
        (
            Matcher::builder()
                .https("https://proxy.corp:443")
                .no("example.com")
                .build(),
            false,
        ),
        (
            Matcher::builder()
                .https("http://alice:s3cret@proxy.corp:3129")
                .build(),
            false,
        ),
        (
            Matcher::builder().all("socks5://bob:pw@socks.corp").build(),
            false,
        ),
        (
            Matcher::builder().all("http://proxy.corp:3128").build(),
            true,
        ),
        (Matcher::builder().build(), true),
    ];
    for (proxies, usable) in cases {
        let checked = format!("{proxies:?}");
        assert_eq!(user_proxies(proxies).is_ok(), usable, "{checked}");
    }
}