rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Which pages the daemon's browser may load. Local dev servers, on this
//! machine or the LAN, load; link-local and cloud-metadata addresses do not,
//! however the browser is sent there: a typed URL, a redirect, a history
//! step, a link, a popup or a frame. A page could otherwise steer the agent
//! to 169.254.169.254 and have it read back the machine's cloud credentials.
//!
//! The browser checks every page and frame load with [`check`] before it is
//! sent (see [`rho_browser::guard`]), which explains a refused page clearly.
//! What holds it is [`super::connect`]: every connection goes through rho,
//! which connects only to the addresses it checked, except a name it hands
//! to a proxy of the user's that looks names up itself.

use crate::tools::web::ssrf;
use rho_browser::guard::NavigationCheck;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::sync::Arc;
use url::{Host, Url};

/// The check the browser runs on every page and frame it loads.
pub(crate) fn check() -> NavigationCheck {
    Arc::new(|url| Box::pin(async move { allowed(&url).await }))
}

/// `Ok` if the browser may load `url`, or why not. A host name is looked up
/// and refused if any address it has is refused, as `fetch_content` does.
/// A name whose answer changes before the browser connects is caught there
/// (see [`super::connect`]), unless an `http://` or `socks5h://` proxy of
/// the user's looks it up instead.
pub(crate) async fn allowed(url: &str) -> Result<(), String> {
    // Only network loads reach the check, and only http(s) has a host to
    // check; Chrome itself refuses a web page's links to `file:`.
    let Ok(url) = Url::parse(url) else {
        return Ok(());
    };
    if !matches!(url.scheme(), "http" | "https") {
        return Ok(());
    }
    let addresses = match url.host() {
        None => return Ok(()),
        Some(Host::Ipv4(address)) => vec![IpAddr::V4(address)],
        Some(Host::Ipv6(address)) => vec![IpAddr::V6(address)],
        // Chrome sends these to this machine without asking DNS.
        Some(Host::Domain(name)) if is_localhost(name) => return Ok(()),
        // A name that does not resolve fails in the browser as well.
        Some(Host::Domain(name)) => ssrf::resolve_host(name).await.unwrap_or_default(),
    };
    match addresses
        .into_iter()
        .find(|address| is_link_local_or_metadata(*address))
    {
        Some(address) => Err(refusal(&url, address)),
        None => Ok(()),
    }
}

/// Whether Chrome takes `name` to this machine without asking DNS:
/// `localhost` and its subdomains, also written with the root's dot.
pub(super) fn is_localhost(name: &str) -> bool {
    let name = name.strip_suffix('.').unwrap_or(name);
    name == "localhost" || name.ends_with(".localhost")
}

/// Whether `ip` is link-local, or a cloud provider's metadata address, also
/// when carried inside an IPv6 address. Unlike the rest of private space,
/// these are never a developer's own server: 169.254.169.254 answers with
/// the machine's cloud credentials on AWS, GCP, Azure and Oracle Cloud.
pub(super) fn is_link_local_or_metadata(ip: IpAddr) -> bool {
    let v4 = match ssrf::normalize_ip(ip) {
        IpAddr::V4(v4) => Some(v4),
        IpAddr::V6(v6) => ssrf::embedded_ipv4(v6),
    };
    if let Some(v4) = v4 {
        // Alibaba Cloud's metadata service.
        return v4.is_link_local() || v4 == Ipv4Addr::new(100, 100, 100, 200);
    }
    let IpAddr::V6(v6) = ip else {
        return false;
    };
    (v6.segments()[0] & 0xffc0) == 0xfe80 // fe80::/10 link-local
        // AWS's metadata service over IPv6.
        || v6 == Ipv6Addr::new(0xfd00, 0x0ec2, 0, 0, 0, 0, 0, 0x0254)
}

fn refusal(url: &Url, address: IpAddr) -> String {
    let host = url.host_str().unwrap_or_default();
    let at = if host.trim_matches(['[', ']']) == address.to_string() {
        String::new()
    } else {
        format!(" ({address})")
    };
    format!("{host}{at} is a link-local or cloud-metadata address, which the browser does not load")
}

#[cfg(test)]
#[path = "navigation_tests.rs"]
mod tests;