From b412acddd71b7bf0cdb51b388eefd8f435611dfd Mon Sep 17 00:00:00 2001 From: Pablo Chiavarino Date: Fri, 13 Nov 2026 22:12:57 -0300 Subject: [PATCH] Look up the external IP over HTTPS when --external-ip is not set On mainnet and testnet an unset --external-ip is resolved from --ip-check-url (default https://checkip.amazonaws.com) with certificate and hostname verification, a single 5 s deadline and a 4 KiB response cap. Any failure falls back to 127.0.0.1 with a warning; "off" disables the lookup. Only public IPv4 answers are accepted, checked against the IANA special-purpose registry. The listeners are IPv4-only, so --external-ip and its config key now take an IPv4 address and reject IPv6. The lookup runs after the one-shot modes, which never start networking. --- Cargo.lock | 2 + README.md | 3 +- crates/alerts/Cargo.toml | 7 +- crates/cli/Cargo.toml | 7 +- crates/cli/src/config_file.rs | 38 +- crates/cli/src/main.rs | 30 +- crates/cli/src/public_ip.rs | 653 ++++++++++++++++++ crates/cli/testdata/tls/ca.der | Bin 0 -> 375 bytes crates/cli/testdata/tls/localhost.der | Bin 0 -> 382 bytes crates/cli/testdata/tls/localhost.key.der | Bin 0 -> 138 bytes crates/cli/testdata/tls/other.der | Bin 0 -> 382 bytes crates/cli/testdata/tls/other.key.der | Bin 0 -> 138 bytes crates/cli/testdata/tls/untrusted.der | Bin 0 -> 385 bytes crates/cli/testdata/tls/untrusted.key.der | Bin 0 -> 138 bytes .../tests/one_shot_modes_skip_ip_lookup.rs | 126 ++++ deny.toml | 8 +- node-light.example.toml | 4 +- node.example.toml | 4 +- 18 files changed, 862 insertions(+), 20 deletions(-) create mode 100644 crates/cli/src/public_ip.rs create mode 100644 crates/cli/testdata/tls/ca.der create mode 100644 crates/cli/testdata/tls/localhost.der create mode 100644 crates/cli/testdata/tls/localhost.key.der create mode 100644 crates/cli/testdata/tls/other.der create mode 100644 crates/cli/testdata/tls/other.key.der create mode 100644 crates/cli/testdata/tls/untrusted.der create mode 100644 crates/cli/testdata/tls/untrusted.key.der create mode 100644 crates/cli/tests/one_shot_modes_skip_ip_lookup.rs diff --git a/Cargo.lock b/Cargo.lock index 85f2380..8f313c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3314,10 +3314,12 @@ dependencies = [ "tempfile", "time", "tokio", + "tokio-rustls", "toml", "tracing", "tracing-appender", "tracing-subscriber 0.3.22", + "webpki-roots", ] [[package]] diff --git a/README.md b/README.md index 9004b6d..e10051a 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,8 @@ Logs are written to `/rustock.log` by default (with daily rotation). U | `--rpc-port` | `4444` | JSON-RPC HTTP port | | `--rpc-host` | `127.0.0.1` | JSON-RPC bind address | | `--no-rpc` | `false` | Disable the JSON-RPC server | -| `--external-ip` | none | External IP to advertise in discovery (e.g. `203.0.113.42`) | +| `--external-ip` | looked up | IPv4 address to advertise in discovery (e.g. `203.0.113.42`). Unset on mainnet or testnet, the node asks `--ip-check-url` for its public IPv4 address; if that fails it advertises `127.0.0.1`. Forward the P2P TCP port and the discovery UDP port (P2P + 1) yourself: the lookup finds the outbound address, which behind NAT is not reachable inbound | +| `--ip-check-url` | `https://checkip.amazonaws.com` | `https://` service that returns the caller's IPv4 address as text, requested once per start with certificate verification (5 s timeout). `off` disables the lookup | Commonly used beyond the basics: diff --git a/crates/alerts/Cargo.toml b/crates/alerts/Cargo.toml index cd14134..d66292c 100644 --- a/crates/alerts/Cargo.toml +++ b/crates/alerts/Cargo.toml @@ -25,9 +25,10 @@ lettre = { version = "0.11.10", default-features = false, features = ["builder", tracing = { workspace = true } [features] -# Off by default. Email pulls in rustls, ring, webpki-roots and the ICU/idna -# stack -- 55 crates, a 13% increase in the dependency tree of a consensus -# node -- which is not a reasonable default for an optional alerting feature. +# Off by default. Email pulls in lettre and the ICU/idna stack -- most of 55 +# crates, a 13% increase in the dependency tree of a consensus node -- which +# is not a reasonable default for an optional alerting feature. rustls, ring +# and webpki-roots are in every build, through rustock-cli's external-IP lookup. # Build with `--features smtp` on the host that needs to send mail. Without it # the watcher still runs and logs everything; only delivery is unavailable. default = [] diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 83c9d19..a1e8e14 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -35,9 +35,14 @@ rocksdb = { workspace = true } libc = { workspace = true } alloy-rlp = { workspace = true } flate2 = "1" +# HTTPS client for the external-IP lookup (src/public_ip.rs): rustls with the +# ring provider and Mozilla's root store, not the lettre/smtp stack. +tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] } +webpki-roots = "1" [features] -# Enable outgoing email for peg-out alerts (pulls in a TLS stack). +# Enable outgoing email for peg-out alerts (adds the SMTP transport to the +# TLS stack the external-IP lookup already carries). smtp = ["rustock-alerts/smtp"] [dev-dependencies] diff --git a/crates/cli/src/config_file.rs b/crates/cli/src/config_file.rs index 39138a7..ddb463e 100644 --- a/crates/cli/src/config_file.rs +++ b/crates/cli/src/config_file.rs @@ -108,9 +108,12 @@ pub struct NodeSection { pub data_dir: Option, pub network_id: Option, pub secret_key: Option, - /// Parsed as an IP address, matching the flag's type -- a malformed value - /// is rejected when the file is read rather than misused later. - pub external_ip: Option, + /// Parsed as an IPv4 address, matching the flag's type -- an IPv6 or + /// malformed value is rejected when the file is read rather than misused + /// later. + pub external_ip: Option, + /// `"off"` or an `https://` URL, as the flag takes. + pub ip_check_url: Option, pub supply_check: Option, /// `"on"` / `"off"`, as the flag takes. pub rskj_multisig_senders: Option, @@ -462,6 +465,7 @@ pub const CONFIGURABLE: &[&str] = &[ "network_id", "secret_key", "external_ip", + "ip_check_url", "supply_check", "execution_threads", "rskj_multisig_senders", @@ -684,6 +688,34 @@ mod tests { assert!(err.to_string().contains("port"), "{err}"); } + #[test] + fn an_ipv6_external_ip_is_rejected_on_the_command_line() { + let err = Args::command() + .try_get_matches_from(["rustock", "--external-ip", "::1"]) + .unwrap_err() + .to_string(); + assert!(err.contains("--external-ip") && err.contains("IPv4"), "{err}"); + } + + #[test] + fn an_ipv6_external_ip_is_rejected_in_the_file() { + let file = write("[node]\nexternal_ip = \"2606:4700::1111\"\n"); + let err = format!("{:#}", FileConfig::load(file.path().to_str().unwrap()).unwrap_err()); + assert!(err.contains("external_ip") && err.contains("IPv4"), "{err}"); + } + + #[test] + fn private_ipv4_external_addresses_are_accepted() { + for ip in ["10.1.2.3", "192.168.0.5", "127.0.0.1"] { + let (_, args) = parse(&["rustock", "--external-ip", ip]); + assert_eq!(args.external_ip, Some(ip.parse().unwrap())); + } + let file = write("[node]\nexternal_ip = \"10.1.2.3\"\n"); + let (m, mut a) = parse(&["rustock"]); + crate::apply_file_config(&m, &FileConfig::load(file.path().to_str().unwrap()).unwrap(), &mut a); + assert_eq!(a.external_ip, Some("10.1.2.3".parse().unwrap())); + } + /// A missing file is an error naming the path, not a silent fallback to /// defaults -- an operator who passes --config means it. #[test] diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index dd23ce6..008a7b0 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1,6 +1,7 @@ use clap::{CommandFactory, FromArgMatches, Parser}; mod config_file; +mod public_ip; use rustock_core::config::ChainConfig; use rustock_core::validation::HeaderVerifier; use rustock_storage::BlockStore; @@ -561,10 +562,19 @@ struct Args { #[arg(long)] peg_events_dir: Option, - /// External IP address to advertise to peers (e.g., 203.0.113.42). - /// If not set, 127.0.0.1 is used. - #[arg(long)] - external_ip: Option, + /// External IPv4 address to advertise to peers (e.g., 203.0.113.42). + /// If not set, the address is looked up with --ip-check-url on mainnet and + /// testnet; if that fails, or on any other network, 127.0.0.1 is used. + /// The listeners are IPv4-only, so an IPv6 address is rejected. + #[arg(long, value_name = "IPV4")] + external_ip: Option, + + /// https:// URL of a service that returns the caller's public IPv4 address + /// as plain text, asked for the address to advertise when --external-ip is + /// unset. The certificate is verified. "off" disables the lookup. The + /// request goes out on every start. + #[arg(long, default_value = public_ip::DEFAULT_URL, value_name = "URL")] + ip_check_url: String, /// Path to a synced rskj `blocks` LevelDB (e.g. ~/.rsk/mainnet/database/blocks). /// When set, block bodies are read from it before requesting them from @@ -1503,6 +1513,7 @@ fn apply_file_config( apply(matches, "network_id", f.node.network_id.as_ref(), &mut a.network_id); apply_opt(matches, "secret_key", f.node.secret_key.as_ref(), &mut a.secret_key); apply_opt(matches, "external_ip", f.node.external_ip.as_ref(), &mut a.external_ip); + apply(matches, "ip_check_url", f.node.ip_check_url.as_ref(), &mut a.ip_check_url); apply(matches, "supply_check", f.node.supply_check.as_ref(), &mut a.supply_check); apply(matches, "execution_threads", f.node.execution_threads.as_ref(), &mut a.execution_threads); @@ -2223,7 +2234,7 @@ async fn run(local_offset: Option) -> Result<()> { .map(|f| f.number) .unwrap_or(0); - let node_config = NodeConfig { + let mut node_config = NodeConfig { client_id: "Rustock/0.1.0".to_string(), listen_port: args.port, id: node_id, @@ -2253,7 +2264,7 @@ async fn run(local_offset: Option) -> Result<()> { secret_key: secret_key_bytes, discovery_port: args.port + 1, data_dir: args.data_dir.clone(), - external_ip: args.external_ip, + external_ip: args.external_ip.map(std::net::IpAddr::V4), max_outbound_peers: args.max_peers, max_inbound_peers: args.max_inbound_peers, max_inbound_per_ip: args.max_inbound_per_ip, @@ -3298,6 +3309,13 @@ async fn run(local_offset: Option) -> Result<()> { .with_events(events.clone()), ); + node_config.external_ip = public_ip::resolve( + args.external_ip.map(std::net::IpAddr::V4), + &args.ip_check_url, + args.network_id, + args.closed_network, + ) + .await; let mut node = Node::with_peer_store(node_config, peer_store.clone()).with_scoring(scoring.clone()); node.add_handler(sync_handler); diff --git a/crates/cli/src/public_ip.rs b/crates/cli/src/public_ip.rs new file mode 100644 index 0000000..1196524 --- /dev/null +++ b/crates/cli/src/public_ip.rs @@ -0,0 +1,653 @@ +//! Finds the address to advertise to peers when `--external-ip` is not given, +//! by asking an IP-echo service, as rskj's `public.ipCheckService` does. +//! +//! The service is reached over HTTPS with certificate and hostname +//! verification against Mozilla's roots, so an on-path attacker cannot choose +//! the address the node advertises. Plain HTTP is rejected. +//! +//! The lookup accepts only IPv4: the discovery and P2P listeners bind IPv4 +//! wildcard addresses, so an IPv6 endpoint could be advertised but never +//! reached. The address is also the outbound one; behind NAT it is not +//! inbound-reachable until the ports are forwarded. + +use std::net::{IpAddr, Ipv4Addr}; +use std::sync::Arc; +use std::time::Duration; + +use anyhow::{anyhow, bail, Context, Result}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::TcpStream; +use tokio_rustls::rustls::pki_types::ServerName; +use tokio_rustls::rustls::{self, ClientConfig, RootCertStore}; +use tokio_rustls::TlsConnector; + +pub const DEFAULT_URL: &str = "https://checkip.amazonaws.com"; +pub const TIMEOUT: Duration = Duration::from_secs(5); +const MAX_RESPONSE_BYTES: usize = 4096; +const MAX_URL_BYTES: usize = 2048; + +/// The value of `--ip-check-url` that turns the lookup off. +pub const DISABLED: &str = "off"; + +struct Target { + host: String, + port: u16, + path: String, +} + +fn parse_url(url: &str) -> Result { + if url.len() > MAX_URL_BYTES { + bail!("URL is longer than {MAX_URL_BYTES} bytes"); + } + if !url.bytes().all(|b| b.is_ascii_graphic()) { + bail!("URL contains whitespace, control or non-ASCII characters"); + } + let rest = url + .strip_prefix("https://") + .ok_or_else(|| anyhow!("only https:// URLs are supported"))?; + let (authority, path) = match rest.find('/') { + Some(i) => (&rest[..i], &rest[i..]), + None => (rest, "/"), + }; + let (host, port) = match authority.rsplit_once(':') { + Some((h, p)) => (h, p.parse::().context("invalid port")?), + None => (authority, 443), + }; + let host_ok = !host.is_empty() + && host + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'.'); + if !host_ok { + bail!("invalid host"); + } + Ok(Target { + host: host.to_string(), + port, + path: path.to_string(), + }) +} + +/// The host alone: the URL may carry credentials or a token in its path. +pub fn host_of(url: &str) -> String { + parse_url(url) + .map(|t| t.host) + .unwrap_or_else(|_| "".to_string()) +} + +/// Not in a block of the IANA IPv4 Special-Purpose Address Registry that is +/// marked as not globally reachable. +pub fn is_public(ip: Ipv4Addr) -> bool { + const SPECIAL_USE: &[([u8; 4], u32)] = &[ + ([0, 0, 0, 0], 8), + ([10, 0, 0, 0], 8), + ([100, 64, 0, 0], 10), + ([127, 0, 0, 0], 8), + ([169, 254, 0, 0], 16), + ([172, 16, 0, 0], 12), + ([192, 0, 0, 0], 24), + ([192, 0, 2, 0], 24), + ([192, 88, 99, 0], 24), + ([192, 168, 0, 0], 16), + ([198, 18, 0, 0], 15), + ([198, 51, 100, 0], 24), + ([203, 0, 113, 0], 24), + ([224, 0, 0, 0], 4), + ([240, 0, 0, 0], 4), + ]; + // PCP and TURN anycast: inside 192.0.0.0/24 but globally reachable. + const GLOBAL_EXCEPTIONS: [Ipv4Addr; 2] = + [Ipv4Addr::new(192, 0, 0, 9), Ipv4Addr::new(192, 0, 0, 10)]; + if GLOBAL_EXCEPTIONS.contains(&ip) { + return true; + } + let addr = u32::from(ip); + !SPECIAL_USE.iter().any(|&(base, prefix)| { + let mask = u32::MAX << (32 - prefix); + addr & mask == u32::from(Ipv4Addr::from(base)) & mask + }) +} + +/// The IP in an HTTP/1.0 response, or why the response is unusable. +fn parse_response(raw: &[u8]) -> Result { + let text = std::str::from_utf8(raw).context("response is not text")?; + let (head, body) = text + .split_once("\r\n\r\n") + .ok_or_else(|| anyhow!("incomplete response"))?; + let status = head.lines().next().unwrap_or_default(); + if status.split_whitespace().nth(1) != Some("200") { + bail!("service answered '{status}'"); + } + let line = body.lines().next().unwrap_or_default(); + // A forwarding proxy may answer with a comma-separated chain; the last + // entry is the one nearest to the service. + let candidate = line.rsplit(',').next().unwrap_or_default().trim(); + let ip: Ipv4Addr = candidate + .parse() + .map_err(|_| anyhow!("service did not return an IPv4 address"))?; + if !is_public(ip) { + bail!("service returned {ip}, which is not a public address"); + } + Ok(IpAddr::V4(ip)) +} + +fn connector(roots: RootCertStore) -> Result { + let provider = Arc::new(rustls::crypto::ring::default_provider()); + let config = ClientConfig::builder_with_provider(provider) + .with_safe_default_protocol_versions() + .context("no usable TLS protocol version")? + .with_root_certificates(roots) + .with_no_client_auth(); + Ok(TlsConnector::from(Arc::new(config))) +} + +fn default_connector() -> Result { + let mut roots = RootCertStore::empty(); + roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); + connector(roots) +} + +async fn lookup_with(tls: &TlsConnector, url: &str, timeout: Duration) -> Result { + let target = parse_url(url)?; + tokio::time::timeout(timeout, fetch(tls, &target)) + .await + .map_err(|_| anyhow!("timed out after {}s", timeout.as_secs()))? +} + +async fn fetch(tls: &TlsConnector, target: &Target) -> Result { + let tcp = TcpStream::connect((target.host.as_str(), target.port)) + .await + .context("connect failed")?; + let name = ServerName::try_from(target.host.clone()).context("invalid server name")?; + let mut stream = tls + .connect(name, tcp) + .await + .context("TLS handshake failed")?; + let request = format!( + "GET {} HTTP/1.0\r\nHost: {}\r\nUser-Agent: rustock\r\nAccept: text/plain\r\n\r\n", + target.path, target.host + ); + stream + .write_all(request.as_bytes()) + .await + .context("request failed")?; + + let mut raw = Vec::new(); + let mut chunk = [0u8; 1024]; + while raw.len() < MAX_RESPONSE_BYTES { + match stream.read(&mut chunk).await { + Ok(0) => break, + Ok(n) => raw.extend_from_slice(&chunk[..n.min(MAX_RESPONSE_BYTES - raw.len())]), + // A server that closes without close_notify has still sent its answer. + Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => break, + Err(e) => return Err(anyhow::Error::new(e).context("read failed")), + } + } + parse_response(&raw) +} + +/// The address to advertise: the operator's, else the looked-up one. Never +/// fails; `None` means the node advertises loopback. +pub async fn resolve( + explicit: Option, + url: &str, + network_id: u64, + closed_network: bool, +) -> Option { + resolve_with(default_connector, explicit, url, network_id, closed_network).await +} + +async fn resolve_with( + connector: impl FnOnce() -> Result, + explicit: Option, + url: &str, + network_id: u64, + closed_network: bool, +) -> Option { + if explicit.is_some() { + return explicit; + } + let public_network = network_id == 30 || network_id == 31; + if url == DISABLED || !public_network || closed_network { + return None; + } + let result = match connector() { + Ok(tls) => lookup_with(&tls, url, TIMEOUT).await, + Err(e) => Err(e), + }; + match result { + Ok(ip) => { + tracing::info!("External IP {ip} from {}", host_of(url)); + Some(ip) + } + Err(e) => { + tracing::warn!( + "Could not determine the external IP from {}: {e:#}; advertising 127.0.0.1, \ + so peers cannot dial this node. Set --external-ip.", + host_of(url) + ); + None + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::{AtomicUsize, Ordering}; + use tokio::net::TcpListener; + use tokio_rustls::rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer}; + use tokio_rustls::rustls::ServerConfig; + use tokio_rustls::TlsAcceptor; + + // The fixtures in crates/cli/testdata/tls are ECDSA P-256 chains valid for + // 100 years: `ca` signs `localhost` (SAN localhost) and `other` (SAN + // other.example); `untrusted` (SAN localhost) is signed by a root the + // tests never trust. + fn der(name: &str) -> Vec { + let bytes: &[u8] = match name { + "ca" => include_bytes!("../testdata/tls/ca.der"), + "localhost" => include_bytes!("../testdata/tls/localhost.der"), + "localhost.key" => include_bytes!("../testdata/tls/localhost.key.der"), + "other" => include_bytes!("../testdata/tls/other.der"), + "other.key" => include_bytes!("../testdata/tls/other.key.der"), + "untrusted" => include_bytes!("../testdata/tls/untrusted.der"), + "untrusted.key" => include_bytes!("../testdata/tls/untrusted.key.der"), + _ => panic!("no fixture {name}"), + }; + bytes.to_vec() + } + + fn trusting_test_ca() -> TlsConnector { + let mut roots = RootCertStore::empty(); + roots.add(CertificateDer::from(der("ca"))).unwrap(); + connector(roots).unwrap() + } + + #[derive(Clone, Copy, PartialEq)] + enum Close { + Notify, + Abrupt, + Never, + } + + struct Server { + url: String, + hits: Arc, + } + + impl Server { + fn hits(&self) -> usize { + self.hits.load(Ordering::SeqCst) + } + } + + async fn serve_tls(cert: &'static str, response: Vec, close: Close) -> Server { + let key = PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(der(&format!("{cert}.key")))); + let config = + ServerConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider())) + .with_safe_default_protocol_versions() + .unwrap() + .with_no_client_auth() + .with_single_cert(vec![CertificateDer::from(der(cert))], key) + .unwrap(); + let acceptor = TlsAcceptor::from(Arc::new(config)); + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = listener.local_addr().unwrap().port(); + let hits = Arc::new(AtomicUsize::new(0)); + let counter = hits.clone(); + tokio::spawn(async move { + loop { + let (tcp, _) = listener.accept().await.unwrap(); + counter.fetch_add(1, Ordering::SeqCst); + let acceptor = acceptor.clone(); + let response = response.clone(); + tokio::spawn(async move { + let Ok(mut tls) = acceptor.accept(tcp).await else { + return; + }; + let mut buf = [0u8; 512]; + let _ = tls.read(&mut buf).await; + if close == Close::Never { + tokio::time::sleep(Duration::from_secs(30)).await; + } + let _ = tls.write_all(&response).await; + let _ = tls.flush().await; + if close == Close::Notify { + let _ = tls.shutdown().await; + } + }); + } + }); + Server { + url: format!("https://localhost:{port}/"), + hits, + } + } + + async fn serve_ok(body: &str) -> Server { + let response = format!("HTTP/1.1 200 OK\r\n\r\n{body}\n").into_bytes(); + serve_tls("localhost", response, Close::Notify).await + } + + async fn raw_listener(reply: &'static [u8]) -> Server { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = listener.local_addr().unwrap().port(); + let hits = Arc::new(AtomicUsize::new(0)); + let counter = hits.clone(); + tokio::spawn(async move { + loop { + let (mut tcp, _) = listener.accept().await.unwrap(); + counter.fetch_add(1, Ordering::SeqCst); + tokio::spawn(async move { + let _ = tcp.write_all(reply).await; + tokio::time::sleep(Duration::from_secs(30)).await; + }); + } + }); + Server { + url: format!("https://localhost:{port}/"), + hits, + } + } + + const SHORT: Duration = Duration::from_millis(500); + + async fn lookup_err(tls: &TlsConnector, url: &str) -> String { + format!("{:#}", lookup_with(tls, url, SHORT).await.unwrap_err()) + } + + #[tokio::test] + async fn returns_the_address_when_the_certificate_is_trusted() { + let server = serve_ok("8.8.4.4").await; + let ip = lookup_with(&trusting_test_ca(), &server.url, SHORT) + .await + .unwrap(); + assert_eq!(ip, "8.8.4.4".parse::().unwrap()); + } + + #[tokio::test] + async fn takes_the_last_entry_of_a_comma_separated_chain() { + let server = serve_ok("10.0.0.1, 8.8.8.8").await; + let ip = lookup_with(&trusting_test_ca(), &server.url, SHORT) + .await + .unwrap(); + assert_eq!(ip, "8.8.8.8".parse::().unwrap()); + } + + #[tokio::test] + async fn still_reads_an_answer_from_a_server_that_closes_without_close_notify() { + let response = b"HTTP/1.1 200 OK\r\n\r\n8.8.4.4\n".to_vec(); + let server = serve_tls("localhost", response, Close::Abrupt).await; + let ip = lookup_with(&trusting_test_ca(), &server.url, SHORT) + .await + .unwrap(); + assert_eq!(ip, "8.8.4.4".parse::().unwrap()); + } + + #[tokio::test] + async fn rejects_a_certificate_for_another_host() { + let response = b"HTTP/1.1 200 OK\r\n\r\n8.8.4.4\n".to_vec(); + let server = serve_tls("other", response, Close::Notify).await; + let err = lookup_err(&trusting_test_ca(), &server.url).await; + assert!(err.contains("TLS handshake failed"), "{err}"); + } + + #[tokio::test] + async fn rejects_a_certificate_from_an_untrusted_root() { + let response = b"HTTP/1.1 200 OK\r\n\r\n8.8.4.4\n".to_vec(); + let server = serve_tls("untrusted", response, Close::Notify).await; + let err = lookup_err(&trusting_test_ca(), &server.url).await; + assert!(err.contains("TLS handshake failed"), "{err}"); + } + + #[tokio::test] + async fn the_default_roots_do_not_trust_a_private_ca() { + let server = serve_ok("8.8.4.4").await; + let err = lookup_err(&default_connector().unwrap(), &server.url).await; + assert!(err.contains("TLS handshake failed"), "{err}"); + } + + #[tokio::test] + async fn plain_http_is_rejected_without_connecting() { + let server = raw_listener(b"HTTP/1.1 200 OK\r\n\r\n8.8.8.8\n").await; + let http = server.url.replacen("https://", "http://", 1); + let err = lookup_err(&trusting_test_ca(), &http).await; + assert!(err.contains("only https://"), "{err}"); + assert_eq!(server.hits(), 0); + } + + #[tokio::test] + async fn a_server_that_answers_in_plaintext_on_an_https_url_fails_the_handshake() { + let server = raw_listener(b"HTTP/1.1 200 OK\r\n\r\n8.8.8.8\n").await; + let err = lookup_err(&trusting_test_ca(), &server.url).await; + assert!(err.contains("TLS handshake failed"), "{err}"); + } + + #[tokio::test] + async fn rejects_a_non_200_answer() { + let response = b"HTTP/1.1 503 Service Unavailable\r\n\r\n8.8.8.8\n".to_vec(); + let server = serve_tls("localhost", response, Close::Notify).await; + let err = lookup_err(&trusting_test_ca(), &server.url).await; + assert!(err.contains("503"), "{err}"); + } + + #[tokio::test] + async fn rejects_a_non_public_answer() { + for body in [ + "127.0.0.1", + "10.1.2.3", + "192.168.0.5", + "100.64.0.1", + "169.254.1.1", + "0.0.0.0", + ] { + let server = serve_ok(body).await; + let err = lookup_err(&trusting_test_ca(), &server.url).await; + assert!(err.contains("not a public address"), "{body}: {err}"); + } + } + + #[tokio::test] + async fn rejects_every_ipv6_answer() { + for body in [ + "2606:4700::1111", + "::1", + "::ffff:8.8.8.8", + "::ffff:10.0.0.1", + "2001:db8::1", + ] { + let server = serve_ok(body).await; + let err = lookup_err(&trusting_test_ca(), &server.url).await; + assert!(err.contains("IPv4"), "{body}: {err}"); + } + } + + #[tokio::test] + async fn rejects_a_body_that_is_not_an_address() { + let server = serve_ok("blocked").await; + assert!(lookup_with(&trusting_test_ca(), &server.url, SHORT) + .await + .is_err()); + } + + #[tokio::test] + async fn rejects_an_empty_body() { + let response = b"HTTP/1.1 200 OK\r\n\r\n".to_vec(); + let server = serve_tls("localhost", response, Close::Notify).await; + assert!(lookup_with(&trusting_test_ca(), &server.url, SHORT) + .await + .is_err()); + } + + #[tokio::test] + async fn an_answer_beyond_the_response_limit_is_not_read() { + let mut response = b"HTTP/1.1 200 OK\r\nX-Pad: ".to_vec(); + response.extend(std::iter::repeat_n(b'a', MAX_RESPONSE_BYTES + 1000)); + response.extend_from_slice(b"\r\n\r\n8.8.8.8\n"); + let server = serve_tls("localhost", response, Close::Notify).await; + let err = lookup_err(&trusting_test_ca(), &server.url).await; + assert!(err.contains("incomplete response"), "{err}"); + } + + #[tokio::test] + async fn the_timeout_covers_a_server_that_never_completes_the_handshake() { + let server = raw_listener(b"").await; + let err = lookup_err(&trusting_test_ca(), &server.url).await; + assert!(err.contains("timed out"), "{err}"); + } + + #[tokio::test] + async fn the_timeout_covers_a_server_that_never_sends_its_answer() { + let server = serve_tls("localhost", Vec::new(), Close::Never).await; + let err = lookup_err(&trusting_test_ca(), &server.url).await; + assert!(err.contains("timed out"), "{err}"); + } + + #[tokio::test] + async fn fails_when_nothing_is_listening() { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = listener.local_addr().unwrap().port(); + drop(listener); + let err = lookup_err(&trusting_test_ca(), &format!("https://localhost:{port}/")).await; + assert!(err.contains("connect failed"), "{err}"); + } + + #[test] + fn only_https_urls_are_accepted() { + assert!(parse_url("http://checkip.amazonaws.com").is_err()); + assert!(parse_url("checkip.amazonaws.com").is_err()); + assert!(parse_url("https://user:pw@host/").is_err()); + assert!(parse_url("https:///path").is_err()); + assert!(parse_url("https://host:notaport/").is_err()); + assert!(parse_url("https://[::1]/").is_err()); + } + + #[test] + fn urls_that_could_alter_the_request_are_rejected() { + for url in [ + "https://host/\r\nX-Injected: 1", + "https://host/a b", + "https://host/\n", + "https://host/\t", + "https://host/\u{e9}", + "https://ho st/", + "https://host\r\n/", + ] { + assert!(parse_url(url).is_err(), "{url:?}"); + } + let long = format!("https://host/{}", "a".repeat(MAX_URL_BYTES)); + assert!(parse_url(&long).is_err()); + } + + #[test] + fn url_parts_default_to_port_443_and_root_path() { + let t = parse_url("https://example.org").unwrap(); + assert_eq!( + (t.host.as_str(), t.port, t.path.as_str()), + ("example.org", 443, "/") + ); + let t = parse_url("https://example.org:8443/ip").unwrap(); + assert_eq!( + (t.host.as_str(), t.port, t.path.as_str()), + ("example.org", 8443, "/ip") + ); + } + + #[test] + fn the_logged_host_never_includes_the_path() { + assert_eq!( + host_of("https://example.org/ip?token=secret"), + "example.org" + ); + assert_eq!(host_of("http://example.org/"), ""); + } + + #[test] + fn public_address_classification() { + for ip in [ + "8.8.8.8", + "1.1.1.1", + "100.128.0.1", + "100.63.255.255", + "172.32.0.1", + "198.20.0.1", + "223.255.255.255", + "192.0.0.9", + "192.0.0.10", + ] { + assert!(is_public(ip.parse().unwrap()), "{ip}"); + } + for ip in [ + "0.0.0.0", + "0.255.255.255", + "10.0.0.1", + "10.255.255.255", + "100.64.0.1", + "100.127.255.255", + "127.0.0.1", + "127.255.255.255", + "169.254.0.1", + "172.16.0.1", + "172.31.255.255", + "192.0.0.0", + "192.0.0.1", + "192.0.0.8", + "192.0.0.11", + "192.0.0.255", + "192.0.2.1", + "192.88.99.1", + "192.168.1.1", + "198.18.0.1", + "198.19.255.255", + "198.51.100.1", + "203.0.113.5", + "224.0.0.1", + "239.255.255.255", + "240.0.0.1", + "255.255.255.255", + ] { + assert!(!is_public(ip.parse().unwrap()), "{ip}"); + } + } + + #[tokio::test] + async fn an_explicit_address_is_used_without_any_lookup() { + let server = serve_ok("8.8.8.8").await; + let ip: IpAddr = "203.0.113.42".parse().unwrap(); + let got = resolve_with(|| Ok(trusting_test_ca()), Some(ip), &server.url, 30, false).await; + assert_eq!(got, Some(ip)); + assert_eq!(server.hits(), 0); + } + + #[tokio::test] + async fn no_lookup_when_disabled_or_off_the_public_networks() { + let server = serve_ok("8.8.8.8").await; + let tls = || Ok(trusting_test_ca()); + assert_eq!(resolve_with(tls, None, DISABLED, 30, false).await, None); + assert_eq!(resolve_with(tls, None, &server.url, 33, false).await, None); + assert_eq!(resolve_with(tls, None, &server.url, 30, true).await, None); + assert_eq!(server.hits(), 0); + } + + #[tokio::test] + async fn one_request_is_made_when_a_lookup_is_due() { + let server = serve_ok("8.8.8.8").await; + let got = resolve_with(|| Ok(trusting_test_ca()), None, &server.url, 30, false).await; + assert_eq!(got, Some("8.8.8.8".parse().unwrap())); + assert_eq!(server.hits(), 1); + } + + #[tokio::test] + async fn a_failed_lookup_falls_back_to_none() { + let server = serve_ok("8.8.8.8").await; + let http = server.url.replacen("https://", "http://", 1); + assert_eq!( + resolve_with(|| Ok(trusting_test_ca()), None, &http, 30, false).await, + None + ); + assert_eq!( + resolve_with(|| Err(anyhow!("no roots")), None, &server.url, 30, false).await, + None + ); + } +} diff --git a/crates/cli/testdata/tls/ca.der b/crates/cli/testdata/tls/ca.der new file mode 100644 index 0000000000000000000000000000000000000000..d138beb272a1b099c72bbaa19b33bd5064502e6b GIT binary patch literal 375 zcmXqLVk|ajVw76I%*4pVB%;SXrzNAdc=qPPP|=GS46M47n>-D;*f_M>JkHs&Ff$p5 z8;TkTvoVLVF!S&vCn}Vr7MCa#<>!|eD2VeKnHd@yniv=w7#NvFN$?vP0tF0=450$J zwMGI>WCz>8#0a&DnUS5@iGihB=|lR{8DDcBy4tBc58LSeW8p3_@jdz?>x((E_>(Tr zy=h*(H_+=g@8OUWhNc^QoYqX0JRVS6xaxMsRlRxUJ@$*83>*w(fzFlXV-aH!VKH5C z?xpmqyX%gBn^t&j-y_#mGp-r%gQSHS8UM4e8ZZMX13r)dKS%)RVMaD&?=!nI7`QMg z*pw>#tY>KHop083eh&9hZjrrv^lKzTetg;T+}?LXNdS{VVbZTe=0_RfYtA;h1qt1D Wa4it6TXB8%oHhFlTgDbMliDb29RaOAI1`#<7EqXJUlf!_3Ie?8Lybzy3vN`McD>s&`@ke^~!L zaiJlmwzw+y%7n$6S3mD@-6yv`E;`YeN6GGZ*kV(WEw>aBI^sSR2b^`X4QsEzmLj*< z+rZO+2WX+JFeBrC7A6A*13nOsAH)Otg}njn6IoRj9*`Hc*%(<_*_jy)L_mrJS$GY& zn~>az6bQ^73ZwZ^B?9$uX$6B&XglYM; z{5M~cH`?;eE?snZs!46Z)rBt1P7Eyj>tBSHze^3QdKdQphxOkR7aC$}i>q?4Ojx{m q_46LreRAvLq7#jIlbk6(}b3U&=ec!Uq$MEqfPgfmLDfabSy)$h2XZaW{_BZea zTP7>a$oQXy$$-It4`{V4KZpnP2YUn97qY4>JRt9BvoW%=vNJOpNPrXxvIrXRH}Qhq zsFzxim|KvOiW~^c9t;MqOo|NKf7ZlI`rJ_cQ#Z2jnsI36-5zI_EL!(Xz*sLRior*q^;?^Q+6>Iettr zQic)+Vr4_CI*HE21aI468uJnKmh|I zL#Tj(h@p^y07N%uXmzvib14-FdIAA1|~+RRm_a+%uWm}4NK2VoG{t{^1X^% zM=m8ym#Qs)`SO<*$No=U-m}jx*;BY|E{FB=P4is-t>^x^)x=J*#3TlT2U yNPi`Ht7)aFCzB$>XXzbh9M$%_%&n-+zbJZSy}+d}NgPUCK1!OVe`JrH*#!XB%y(`8 literal 0 HcmV?d00001 diff --git a/crates/cli/testdata/tls/untrusted.key.der b/crates/cli/testdata/tls/untrusted.key.der new file mode 100644 index 0000000000000000000000000000000000000000..b271e9f3eb8232f3918b260b3e88e04874040e9d GIT binary patch literal 138 zcmV;50CoQ`frkPC05B5<2P%e0&OHJF1_&yKNX|V20S5$aFlzz<0R$kc+#&cc3UTgA zvO3c|6e;9P+`IOiOA&_3AZ2-jNs=|8L<2$q1c0T?k&u&5)82U6#L{4w8hv-^>G~=N szw(Mpo6V%Wai*OJJLj^VME Outcome { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + listener.set_nonblocking(true).unwrap(); + let hits = Arc::new(AtomicUsize::new(0)); + let stop = Arc::new(AtomicUsize::new(0)); + let (h, s) = (hits.clone(), stop.clone()); + let counter = std::thread::spawn(move || { + while s.load(Ordering::SeqCst) == 0 { + match listener.accept() { + Ok(_) => { + h.fetch_add(1, Ordering::SeqCst); + } + Err(_) => std::thread::sleep(Duration::from_millis(10)), + } + } + }); + + let mut child = Command::new(env!("CARGO_BIN_EXE_rustock-cli")) + .arg("--data-dir") + .arg(data_dir) + .args(["--trie-backend", "epoch", "--log-to-stdout"]) + .args(extra) + .arg("--ip-check-url") + .arg(format!("https://localhost:{port}/")) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + + let deadline = Instant::now() + Duration::from_secs(120); + let status = loop { + if let Some(status) = child.try_wait().unwrap() { + break status; + } + assert!(Instant::now() < deadline, "one-shot command did not exit"); + std::thread::sleep(Duration::from_millis(50)); + }; + + let mut output = String::new(); + child + .stdout + .take() + .unwrap() + .read_to_string(&mut output) + .unwrap(); + child + .stderr + .take() + .unwrap() + .read_to_string(&mut output) + .unwrap(); + stop.store(1, Ordering::SeqCst); + counter.join().unwrap(); + Outcome { + success: status.success(), + output, + lookups: hits.load(Ordering::SeqCst), + } +} + +#[test] +fn verify_state_reaches_its_own_error_without_a_lookup() { + let dir = tempfile::tempdir().unwrap(); + seed_genesis(dir.path()); + let out = run_one_shot(dir.path(), &["--verify-state", "head"]); + assert!(!out.success, "{}", out.output); + assert!( + out.output.contains("no executed head to verify"), + "the command must get past node setup and fail on its own check:\n{}", + out.output + ); + assert_eq!(out.lookups, 0, "{}", out.output); +} + +#[test] +fn measure_block_reads_makes_no_lookup() { + let dir = tempfile::tempdir().unwrap(); + seed_genesis(dir.path()); + let out = run_one_shot(dir.path(), &["--measure-block-reads", "1"]); + assert!( + out.output.contains("Measuring trie reads"), + "the command must get past node setup and start measuring:\n{}", + out.output + ); + assert_eq!(out.lookups, 0, "{}", out.output); +} diff --git a/deny.toml b/deny.toml index 9a67e0a..7bbd406 100644 --- a/deny.toml +++ b/deny.toml @@ -135,10 +135,10 @@ allow = [ "Unicode-3.0", "Unlicense", "Zlib", - # webpki-roots, which carries Mozilla's CA bundle, under the optional - # `smtp` feature only. CDLA-Permissive-2.0 is a permissive data licence - # with no copyleft or attribution burden; it covers the certificate data, - # not code. A default build does not pull it in. + # webpki-roots, which carries Mozilla's CA bundle: used by rustock-cli to + # verify the external-IP lookup, and by lettre under the optional `smtp` + # feature. CDLA-Permissive-2.0 is a permissive data licence with no + # copyleft or attribution burden; it covers the certificate data, not code. "CDLA-Permissive-2.0", # quoted_printable, via lettre, under the optional `smtp` feature only. # 0BSD is the BSD licence with the attribution clause removed -- OSI diff --git a/node-light.example.toml b/node-light.example.toml index 605f52e..be6d9db 100644 --- a/node-light.example.toml +++ b/node-light.example.toml @@ -50,7 +50,9 @@ # data_dir = "./data" # default — database and node key # network_id = 30 # default — 30 mainnet, 31 testnet # secret_key = "…" # default: unset — hex node key; generated and stored if unset -# external_ip = "203.0.113.1" # default: unset — advertised address; 127.0.0.1 if unset +# external_ip = "203.0.113.1" # default: unset — advertised address; looked up with ip_check_url, else 127.0.0.1 +# https:// only. "off" disables the lookup, which runs on mainnet and testnet when external_ip is unset. +# ip_check_url = "https://checkip.amazonaws.com" # default # off | per-block | per-transaction | both. Per-transaction is strictly more # sensitive: block-level netting hides a creation when one transaction mints # and another destroys the same amount. "both" keeps per-block as a cross-check diff --git a/node.example.toml b/node.example.toml index c6bdd6d..f3f27ba 100644 --- a/node.example.toml +++ b/node.example.toml @@ -34,7 +34,9 @@ # data_dir = "./data" # default — database and node key # network_id = 30 # default — 30 mainnet, 31 testnet # secret_key = "…" # default: unset — hex node key; generated and stored if unset -# external_ip = "203.0.113.1" # default: unset — advertised address; 127.0.0.1 if unset +# external_ip = "203.0.113.1" # default: unset — advertised address; looked up with ip_check_url, else 127.0.0.1 +# https:// only. "off" disables the lookup, which runs on mainnet and testnet when external_ip is unset. +# ip_check_url = "https://checkip.amazonaws.com" # default # off | per-block | per-transaction | both. Per-transaction is strictly more # sensitive: block-level netting hides a creation when one transaction mints # and another destroys the same amount. "both" keeps per-block as a cross-check