diff --git a/.crumb b/.crumb index a196cd3c..7839395a 100644 --- a/.crumb +++ b/.crumb @@ -77,14 +77,14 @@ } ], "children_root": "sha256:4ff1a56b7c8ac606ad5709f2ba1141f16055c34f5f242ac491233dca20d5153e", - "compiled_at": "2026-10-07T03:37:34Z", + "compiled_at": "2026-10-07T03:51:32Z", "compiler": "crumb-compile/1", - "digest": "sha256:9c94e6a0360e0904f07fd35e6dd1ae960c62f62b9d5043e6242d7cc08e3dca86", + "digest": "sha256:950da750f791d269aa8ab366556f3652cf49b9a06b1e74011dfc3d6d1962729a", "evidence_files": 155, "evidence_root": "sha256:ff6dddfecfcc02cea55f46acaccc7eaecd27557209e98c3d5616ec02bdde31db", "files": 1119, - "generated_at_commit": "a914490eb8ae8f9582ec31b498497eb4483ac400", - "source_tree": "sha256:116073775100d240cef5d339050b961d09e8538c2f141d89bf888ead349fdf69" + "generated_at_commit": "0b14ebab343e7cd911a8db1eded99d7a4718339e", + "source_tree": "sha256:8ff157daf713d6d9839d9d0c9720872d479e368575bdb78fe2143ffc14bb49ab" } }, "history": [], diff --git a/crates/aien-drive/src/http.rs b/crates/aien-drive/src/http.rs index 6c1f92df..170e9773 100644 --- a/crates/aien-drive/src/http.rs +++ b/crates/aien-drive/src/http.rs @@ -1,9 +1,11 @@ -//! Minimal plain-HTTP client over `std::net` (loopback endpoints only). +//! Minimal plain-HTTP client over `std::net`, loopback endpoints only: every +//! address the host resolves to must be loopback, and the connection goes to +//! those resolved addresses (no second lookup between the check and connect). //! Sends HTTP/1.0 with `Connection: close` and decodes chunked bodies if the //! server uses them anyway. use std::io::{BufRead, BufReader, Read, Write}; -use std::net::TcpStream; +use std::net::{SocketAddr, TcpStream, ToSocketAddrs}; use std::time::Duration; pub struct Response { @@ -19,7 +21,9 @@ fn split_url(url: &str) -> Result<(String, String), String> { Some(i) => (&rest[..i], &rest[i..]), None => (rest, "/"), }; - let host = if host.contains(':') { + let host = if host.starts_with('[') && host.ends_with(']') { + format!("{host}:80") + } else if host.contains(':') { host.to_string() } else { format!("{host}:80") @@ -27,6 +31,21 @@ fn split_url(url: &str) -> Result<(String, String), String> { Ok((host, path.to_string())) } +/// Resolve `host:port` and refuse it unless every address is loopback. +fn loopback_addrs(host: &str) -> Result, String> { + let addrs: Vec = host + .to_socket_addrs() + .map_err(|e| format!("resolve {host}: {e}"))? + .collect(); + if addrs.is_empty() { + return Err(format!("resolve {host}: no addresses")); + } + if let Some(a) = addrs.iter().find(|a| !a.ip().is_loopback()) { + return Err(format!("refused: {host} resolves to non-loopback {a}")); + } + Ok(addrs) +} + struct Chunked { inner: R, left: usize, @@ -63,7 +82,8 @@ impl Read for Chunked { /// POST a JSON body. `timeout` bounds each socket read and write. pub fn post_json(url: &str, body: &str, timeout: Duration) -> Result { let (host, path) = split_url(url)?; - let mut s = TcpStream::connect(&host).map_err(|e| format!("connect {host}: {e}"))?; + let addrs = loopback_addrs(&host)?; + let mut s = TcpStream::connect(&addrs[..]).map_err(|e| format!("connect {host}: {e}"))?; s.set_read_timeout(Some(timeout)) .map_err(|e| e.to_string())?; s.set_write_timeout(Some(timeout)) @@ -106,3 +126,33 @@ pub fn post_json(url: &str, body: &str, timeout: Duration) -> Result