Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
agent: Sandbox DNS rebinding and redirect cleanup (#60941)
Cameron Mcloughlin795c44f61dd8
diff --git a/crates/agent/src/tools/fetch_tool.rs b/crates/agent/src/tools/fetch_tool.rsindex cfe7792c9f1..e08d7dbc59e 100644--- a/crates/agent/src/tools/fetch_tool.rs+++ b/crates/agent/src/tools/fetch_tool.rs@@ -166,6 +166,31 @@ impl FetchTool {}}+/// Resolve the host of `url` and confirm it doesn't point into loopback /+/// private / link-local space, applying the same forbidden-IP policy the+/// terminal sandbox's proxy uses. Returns an error (including "resolves only to+/// forbidden addresses") that aborts the fetch.+///+/// DNS resolution blocks, so callers should run this off the foreground thread.+/// See the caller for why this is a gate rather than a full resolve-to-connect+/// pin.+fn verify_host_not_forbidden(url: &str) -> Result<()> {+ let normalized = normalize_url(url);+ let parsed =+ url::Url::parse(&normalized).with_context(|| format!("could not parse URL {url:?}"))?;+ let host = parsed+ .host_str()+ .with_context(|| format!("URL {url:?} has no host to reach"))?;+ // Default to the scheme's port when the URL omits one; resolution needs a+ // port but the value doesn't affect which IPs a host resolves to.+ let port = parsed+ .port_or_known_default()+ .unwrap_or(if parsed.scheme() == "http" { 80 } else { 443 });++ http_proxy::PinnedHost::resolve(host, port).map(|_pinned| ())?;+ Ok(())+}+/// Extracts the host from a fetch URL as a [`http_proxy::HostPattern`] so it can/// be matched against the shared network grants. Mirrors the scheme handling in/// [`normalize_url`] (defaulting to `https://` when none is given).@@ -276,6 +301,32 @@ impl AgentTool for FetchTool {return Err("Fetch cancelled by user".to_string());}};++ // Authorizing the *hostname* is not enough: a granted host+ // (or a redirect to one) whose DNS points into loopback /+ // private / link-local space would otherwise let the model+ // reach the local machine or LAN (SSRF / DNS rebinding).+ // Resolve and vet the host now, applying the same+ // forbidden-IP policy the terminal sandbox's proxy enforces.+ //+ // NOTE: this is a gate, not a full pin. `HttpClientWithUrl`+ // resolves the hostname again when it connects, so a DNS+ // answer that flips between this check and that connect could+ // still slip through. Closing that residual window would+ // require the HTTP client to connect to a pre-vetted IP+ // (`PinnedHost::socket_addrs`) rather than re-resolving;+ // until then this blocks the realistic case of a stably+ // resolving host that points at forbidden space.+ let verify_task = cx.background_spawn({+ let url = current_url.clone();+ async move { verify_host_not_forbidden(&url) }+ });+ futures::select! {+ result = verify_task.fuse() => result.map_err(|e| e.to_string())?,+ _ = event_stream.cancelled_by_user().fuse() => {+ return Err("Fetch cancelled by user".to_string());+ }+ };}let fetch_task = cx.background_spawn({@@ -312,3 +363,45 @@ impl AgentTool for FetchTool {})}}++#[cfg(test)]+mod tests {+ use super::*;++ // These use IP-literal URLs, which "resolve" to themselves, so the SSRF gate+ // is exercised without depending on real DNS. IP literals can't be *granted*+ // network access (that's a separate, earlier check), but they can be the+ // target a granted hostname redirects to or resolves into — which is exactly+ // the case this gate defends.++ #[test]+ fn verify_host_rejects_loopback_literal() {+ let error = verify_host_not_forbidden("http://127.0.0.1/internal")+ .expect_err("loopback must be refused");+ assert!(+ error.to_string().contains("loopback"),+ "error should explain the forbidden range, got: {error}"+ );+ }++ #[test]+ fn verify_host_rejects_private_and_metadata_literals() {+ for url in [+ "http://10.0.0.5/",+ "https://192.168.1.1/",+ "http://169.254.169.254/latest/meta-data/", // cloud metadata+ "http://[::1]/",+ ] {+ assert!(+ verify_host_not_forbidden(url).is_err(),+ "expected {url} to be refused as a forbidden destination"+ );+ }+ }++ #[test]+ fn verify_host_allows_public_literal() {+ verify_host_not_forbidden("https://93.184.215.14/")+ .expect("a public address must be allowed through the gate");+ }+}diff --git a/crates/http_proxy/src/http_proxy.rs b/crates/http_proxy/src/http_proxy.rsindex 1739d846c19..efed6140970 100644--- a/crates/http_proxy/src/http_proxy.rs+++ b/crates/http_proxy/src/http_proxy.rs@@ -47,9 +47,11 @@//! would see from a direct network failure, no proxy fingerprint.mod allowlist;+mod pinned_host;mod proxy;pub use allowlist::{Allowlist, HostPattern, HostPatternError};+pub use pinned_host::{PinnedHost, PinnedHostError, is_forbidden_ip};pub use proxy::{DenyReason, ProxyConfig, ProxyEvent, ProxyHandle, RequestMethod, RequestOutcome, UpstreamProxy,};diff --git a/crates/http_proxy/src/pinned_host.rs b/crates/http_proxy/src/pinned_host.rsnew file mode 100644index 00000000000..9443e9629cb--- /dev/null+++ b/crates/http_proxy/src/pinned_host.rs@@ -0,0 +1,287 @@+//! A resolved-and-vetted network destination, pinned to the exact IP addresses+//! that were checked.+//!+//! DNS rebinding / SSRF is a time-of-check-to-time-of-use hazard: code that+//! resolves a hostname, decides it's safe, and then hands the *hostname* back to+//! something that resolves it *again* has checked one answer and used another. A+//! hostname that the sandbox distrusts can exploit that window to point a+//! "granted" host at loopback, the LAN, or a cloud metadata endpoint.+//!+//! [`PinnedHost`] closes that window the way [`sandbox::HostFilesystemLocation`]+//! does for filesystem paths: construction resolves the name and vets every+//! address *once*, and the resulting value carries the checked addresses by+//! value. The type is deliberately opaque — it never hands back the hostname for+//! re-resolution — so the only way to "use" it is to connect to an address it+//! already vetted. Re-deriving a destination from the hostname requires reaching+//! for [`PinnedHost::untrusted_host_display`], whose name flags it as+//! display-only, so the type system nudges callers away from reintroducing the+//! TOCTOU.+//!+//! One `PinnedHost` describes **one** hostname's resolved addresses. A redirect+//! chain that visits several hosts produces one `PinnedHost` per hop, each+//! resolved and vetted independently at the moment its hop runs.++use crate::allowlist::Allowlist;+use std::collections::HashSet;+use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs as _};++/// Why pinning a host failed.+#[derive(Debug, thiserror::Error)]+pub enum PinnedHostError {+ /// DNS resolution itself failed (or the host is malformed).+ #[error("resolving {host}:{port}: {source}")]+ Resolve {+ host: String,+ port: u16,+ source: std::io::Error,+ },+ /// The host resolved, but to no addresses at all.+ #[error("{host}:{port} did not resolve to any address")]+ NoAddresses { host: String, port: u16 },+ /// Every resolved address was in loopback / private / link-local space, so+ /// there is nothing safe to connect to (DNS-rebinding protection).+ #[error(+ "{host} resolved only to loopback/private/link-local addresses, \+ which the sandbox never reaches by hostname"+ )]+ AllAddressesForbidden { host: String },+}++/// A hostname whose DNS has been resolved and whose addresses have been vetted+/// against the forbidden-IP policy, pinned to those exact addresses.+///+/// Construct with [`PinnedHost::resolve`] (applies the forbidden-IP filter) or+/// [`PinnedHost::resolve_allowing_any`] (skips it, for the "allow any host"+/// grant that means unrestricted egress). Use the pinned addresses via+/// [`PinnedHost::socket_addrs`]; the hostname is available only via+/// [`PinnedHost::untrusted_host_display`], which must never be fed back into a+/// resolver.+#[derive(Debug, Clone)]+pub struct PinnedHost {+ /// The vetted addresses, deduplicated. A set rather than a list because DNS+ /// order carries no meaning here and duplicate answers (or duplicates across+ /// A/AAAA records) should collapse — what matters is the set of endpoints we+ /// concluded are safe to reach.+ addrs: HashSet<SocketAddr>,+ /// The requested host, kept **only** for display in errors/UI. Never+ /// consulted to connect — treat it as untrusted, attacker-influenced text.+ untrusted_host_for_display: String,+}++impl PinnedHost {+ /// Resolve `host:port` and pin the subset of addresses that pass the+ /// forbidden-IP filter. Fails if resolution fails, yields no addresses, or+ /// yields only forbidden ones.+ pub fn resolve(host: &str, port: u16) -> Result<Self, PinnedHostError> {+ Self::resolve_inner(host, port, true)+ }++ /// Resolve `host:port` and pin **every** resolved address without applying+ /// the forbidden-IP filter.+ ///+ /// This is for the "allow any host" grant (`allow_all_hosts` /+ /// [`Allowlist::allows_any`]), which is unrestricted egress by definition —+ /// including the local network and metadata endpoints. Callers that don't+ /// hold such a grant must use [`PinnedHost::resolve`].+ pub fn resolve_allowing_any(host: &str, port: u16) -> Result<Self, PinnedHostError> {+ Self::resolve_inner(host, port, false)+ }++ /// Resolve against an [`Allowlist`], choosing the filtered or unfiltered path+ /// based on whether the allowlist grants arbitrary egress.+ pub fn resolve_for_allowlist(+ host: &str,+ port: u16,+ allowlist: &Allowlist,+ ) -> Result<Self, PinnedHostError> {+ Self::resolve_inner(host, port, !allowlist.allows_any())+ }++ fn resolve_inner(host: &str, port: u16, vet: bool) -> Result<Self, PinnedHostError> {+ let resolved =+ (host, port)+ .to_socket_addrs()+ .map_err(|source| PinnedHostError::Resolve {+ host: host.to_string(),+ port,+ source,+ })?;++ let mut addrs = HashSet::new();+ let mut saw_any = false;+ for addr in resolved {+ saw_any = true;+ if vet && is_forbidden_ip(addr.ip()) {+ continue;+ }+ addrs.insert(addr);+ }++ if !saw_any {+ return Err(PinnedHostError::NoAddresses {+ host: host.to_string(),+ port,+ });+ }+ if addrs.is_empty() {+ return Err(PinnedHostError::AllAddressesForbidden {+ host: host.to_string(),+ });+ }++ Ok(Self {+ addrs,+ untrusted_host_for_display: host.to_string(),+ })+ }++ /// The vetted addresses to connect to. Connecting to one of these — rather+ /// than re-resolving the hostname — is what keeps the check and the use+ /// pinned to the same answer.+ pub fn socket_addrs(&self) -> impl ExactSizeIterator<Item = SocketAddr> + '_ {+ self.addrs.iter().copied()+ }++ /// The requested host, for **display only** (errors, UI). This intentionally+ /// returns the untrusted, as-requested hostname — never a vetted address. Do+ /// not feed the result back into a resolver as if it identified this+ /// destination.+ pub fn untrusted_host_display(&self) -> &str {+ &self.untrusted_host_for_display+ }+}++/// Whether a resolved address is in loopback / private / link-local space —+/// destinations a hostname allowlist must never reach. The OS sandbox already+/// blocks them for direct connections from the sandbox; code running outside the+/// sandbox (the proxy, the fetch tool) must not reopen them.+pub fn is_forbidden_ip(ip: IpAddr) -> bool {+ // Escape hatch for the NixOS sandbox integration tests only: their echo+ // servers live on the VM's private network, which this filter would+ // otherwise reject. It is compiled in ONLY under the+ // `nixos-integration-tests` feature (enabled via `sandbox/nixos-test` when+ // building `bwrap_test_helper`), so in a real Zed build the env var has no+ // effect and cannot disable DNS-rebinding/SSRF protection.+ #[cfg(feature = "nixos-integration-tests")]+ if std::env::var_os("ZED_SANDBOX_PROXY_ALLOW_LOCAL_IPS").is_some() {+ return false;+ }+ match ip {+ IpAddr::V4(v4) => is_forbidden_ipv4(v4),+ IpAddr::V6(v6) => {+ if let Some(v4) = v6.to_ipv4_mapped() {+ return is_forbidden_ipv4(v4);+ }+ v6.is_loopback()+ || v6.is_unspecified()+ // Link-local (fe80::/10) and unique-local (fc00::/7); the+ // dedicated `is_unicast_link_local` / `is_unique_local`+ // methods are not yet stable.+ || (v6.segments()[0] & 0xffc0) == 0xfe80+ || (v6.segments()[0] & 0xfe00) == 0xfc00+ }+ }+}++fn is_forbidden_ipv4(ip: Ipv4Addr) -> bool {+ let octets = ip.octets();+ ip.is_loopback()+ || ip.is_private()+ || ip.is_link_local() // includes 169.254.169.254 cloud metadata+ || ip.is_unspecified()+ || ip.is_broadcast()+ // Shared address space (RFC 6598, 100.64.0.0/10): CGNAT, and notably+ // Tailscale-style overlay networks.+ || (octets[0] == 100 && (octets[1] & 0xc0) == 64)+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn forbidden_ip_covers_v4_ranges() {+ for ip in [+ "127.0.0.1",+ "10.0.0.1",+ "192.168.1.1",+ "172.16.0.1",+ "169.254.169.254", // cloud metadata+ "0.0.0.0",+ "255.255.255.255",+ "100.64.0.1", // CGNAT / Tailscale+ ] {+ assert!(+ is_forbidden_ip(ip.parse().unwrap()),+ "expected {ip} to be forbidden"+ );+ }+ }++ #[test]+ fn forbidden_ip_covers_v6_ranges() {+ for ip in [+ "::1", // loopback+ "::", // unspecified+ "fe80::1", // link-local+ "fc00::1", // unique-local+ "::ffff:127.0.0.1", // IPv4-mapped loopback+ "::ffff:10.0.0.1", // IPv4-mapped private+ ] {+ assert!(+ is_forbidden_ip(ip.parse().unwrap()),+ "expected {ip} to be forbidden"+ );+ }+ }++ #[test]+ fn public_ips_are_allowed() {+ for ip in [+ "93.184.215.14",+ "8.8.8.8",+ "2606:2800:220:1:248:1893:25c8:1946",+ ] {+ assert!(+ !is_forbidden_ip(ip.parse().unwrap()),+ "expected {ip} to be allowed"+ );+ }+ }++ #[test]+ fn resolve_pins_public_literal_addresses() {+ // An IP literal "resolves" to itself, so this exercises the vetting and+ // pinning without depending on real DNS.+ let pinned = PinnedHost::resolve("93.184.215.14", 443).expect("public IP should pin");+ let addrs: HashSet<SocketAddr> = pinned.socket_addrs().collect();+ assert_eq!(addrs, HashSet::from(["93.184.215.14:443".parse().unwrap()]));+ assert_eq!(pinned.untrusted_host_display(), "93.184.215.14");+ }++ #[test]+ fn resolve_rejects_forbidden_literal_address() {+ let error = PinnedHost::resolve("127.0.0.1", 80).expect_err("loopback must be rejected");+ assert!(matches!(+ error,+ PinnedHostError::AllAddressesForbidden { .. }+ ));+ }++ #[test]+ fn resolve_allowing_any_keeps_forbidden_addresses() {+ let pinned =+ PinnedHost::resolve_allowing_any("127.0.0.1", 80).expect("allow-any keeps loopback");+ let addrs: HashSet<SocketAddr> = pinned.socket_addrs().collect();+ assert_eq!(addrs, HashSet::from(["127.0.0.1:80".parse().unwrap()]));+ }++ #[test]+ fn resolve_deduplicates_addresses() {+ // `localhost` typically resolves to both 127.0.0.1 and ::1; under+ // allow-any both are kept and distinct. This mainly guards that the set+ // collapses exact duplicates rather than the specific addresses.+ let pinned = PinnedHost::resolve_allowing_any("127.0.0.1", 80).unwrap();+ assert_eq!(pinned.socket_addrs().count(), 1);+ }+}diff --git a/crates/http_proxy/src/proxy/connection.rs b/crates/http_proxy/src/proxy/connection.rsindex 06ae8ea73d0..98858357c30 100644--- a/crates/http_proxy/src/proxy/connection.rs+++ b/crates/http_proxy/src/proxy/connection.rs@@ -11,13 +11,14 @@//! client sends on it cannot escape the policy decision. Per-TCP-connection//! event granularity, by design.+use crate::pinned_host::{PinnedHost, PinnedHostError};use crate::proxy::{DenyReason, ProxyEvent, RequestMethod, RequestOutcome, RuntimeState, UpstreamProxy,};use anyhow::{Context, Result, anyhow, bail};use base64::Engine as _;use std::io::{Read, Write};-use std::net::{IpAddr, Ipv4Addr, Shutdown, SocketAddr, TcpStream, ToSocketAddrs as _};+use std::net::{IpAddr, Shutdown, SocketAddr, TcpStream, ToSocketAddrs as _};#[cfg(unix)]use std::os::unix::net::UnixStream;use std::sync::Arc;@@ -428,8 +429,9 @@ fn policy_denial(host: &str, port: u16, state: &RuntimeState) -> Option<DenyReas/// How an approved request will reach its destination.enum Route {- /// Connect directly to one of these resolved-and-vetted addresses.- Direct(Vec<SocketAddr>),+ /// Connect directly to one of the addresses pinned when the host was+ /// resolved and vetted.+ Direct(PinnedHost),/// Tunnel through the upstream proxy with a CONNECT handshake.ViaUpstream(UpstreamProxy),}@@ -460,76 +462,17 @@ fn plan_route(host: &str, port: u16, state: &RuntimeState) -> Result<Route, Routreturn Ok(Route::ViaUpstream(upstream.clone()));}- let resolved: Vec<SocketAddr> = (host, port)- .to_socket_addrs()- .map_err(|error| RouteFailure::Error(anyhow!("resolving {host}:{port}: {error}")))?- .collect();- if resolved.is_empty() {- return Err(RouteFailure::Error(anyhow!(- "{host}:{port} did not resolve to any address"- )));- }-- let vetted: Vec<SocketAddr> = if state.allowlist.allows_any() {- resolved- } else {- resolved- .into_iter()- .filter(|addr| !is_forbidden_ip(addr.ip()))- .collect()- };- if vetted.is_empty() {- return Err(RouteFailure::Denied(DenyReason::ResolvedToForbiddenIp {- host: host.to_string(),- }));- }- Ok(Route::Direct(vetted))-}--/// Whether a resolved address is in loopback / private / link-local space —-/// destinations a hostname allowlist must never reach. The Seatbelt rule-/// already blocks them for direct connections from the sandbox; the proxy-/// (which runs outside the sandbox) must not reopen them.-fn is_forbidden_ip(ip: IpAddr) -> bool {- // Escape hatch for the NixOS sandbox integration tests only: their echo- // servers live on the VM's private network, which this filter would- // otherwise reject. It is compiled in ONLY under the- // `nixos-integration-tests` feature (enabled via `sandbox/nixos-test` when- // building `bwrap_test_helper`), so in a real Zed build the env var has no- // effect and cannot disable DNS-rebinding/SSRF protection.- #[cfg(feature = "nixos-integration-tests")]- if std::env::var_os("ZED_SANDBOX_PROXY_ALLOW_LOCAL_IPS").is_some() {- return false;- }- match ip {- IpAddr::V4(v4) => is_forbidden_ipv4(v4),- IpAddr::V6(v6) => {- if let Some(v4) = v6.to_ipv4_mapped() {- return is_forbidden_ipv4(v4);- }- v6.is_loopback()- || v6.is_unspecified()- // Link-local (fe80::/10) and unique-local (fc00::/7); the- // dedicated `is_unicast_link_local` / `is_unique_local`- // methods are not yet stable.- || (v6.segments()[0] & 0xffc0) == 0xfe80- || (v6.segments()[0] & 0xfe00) == 0xfc00+ match PinnedHost::resolve_for_allowlist(host, port, &state.allowlist) {+ Ok(pinned) => Ok(Route::Direct(pinned)),+ Err(PinnedHostError::AllAddressesForbidden { host }) => {+ Err(RouteFailure::Denied(DenyReason::ResolvedToForbiddenIp {+ host,+ }))}+ Err(error) => Err(RouteFailure::Error(anyhow!(error))),}}-fn is_forbidden_ipv4(ip: Ipv4Addr) -> bool {- let octets = ip.octets();- ip.is_loopback()- || ip.is_private()- || ip.is_link_local() // includes 169.254.169.254 cloud metadata- || ip.is_unspecified()- || ip.is_broadcast()- // Shared address space (RFC 6598, 100.64.0.0/10): CGNAT, and notably- // Tailscale-style overlay networks.- || (octets[0] == 100 && (octets[1] & 0xc0) == 64)-}-fn handle_connect(mut client: ClientStream,host: String,@@ -726,7 +669,13 @@ fn handle_http_forward(/// handshake with the CONNECT path.fn open_route(route: &Route, host: &str, port: u16) -> Result<(TcpStream, Vec<u8>)> {match route {- Route::Direct(addrs) => Ok((connect_to_any(addrs, host, port)?, Vec::new())),+ Route::Direct(pinned) => {+ // Connect to the addresses pinned when the host was vetted, never+ // re-resolving `host` here — that re-resolution is exactly the+ // DNS-rebinding window `PinnedHost` exists to close.+ let addrs: Vec<SocketAddr> = pinned.socket_addrs().collect();+ Ok((connect_to_any(&addrs, host, port)?, Vec::new()))+ }Route::ViaUpstream(upstream) => connect_via_upstream(host, port, upstream),}}@@ -984,7 +933,8 @@ mod tests {fn plan_route_allows_loopback_when_allowlist_allows_any() {let state = runtime_state(Allowlist::any());match plan_route("localhost", 80, &state) {- Ok(Route::Direct(addrs)) => {+ Ok(Route::Direct(pinned)) => {+ let addrs: Vec<SocketAddr> = pinned.socket_addrs().collect();assert!(!addrs.is_empty());assert!(addrs.iter().all(|addr| addr.ip().is_loopback()));}@@ -1077,35 +1027,7 @@ mod tests {assert!(!is_ip_literal("localhost"));}- #[test]- fn forbidden_ips_cover_local_space() {- for forbidden in [- "127.0.0.1",- "10.1.2.3",- "172.16.0.1",- "192.168.1.1",- "169.254.169.254",- "100.100.1.1",- "0.0.0.0",- "::1",- "::",- "fe80::1",- "fd00::1",- "::ffff:127.0.0.1",- "::ffff:10.0.0.1",- ] {- assert!(- is_forbidden_ip(forbidden.parse().unwrap()),- "{forbidden} should be forbidden"- );- }- for public in ["140.82.112.3", "8.8.8.8", "2606:4700::6810:84e5"] {- assert!(- !is_forbidden_ip(public.parse().unwrap()),- "{public} should be allowed"- );- }- }+ // Forbidden-IP range coverage lives with the logic in `pinned_host.rs`.#[test]fn parsed_request_recognizes_connect() {